UpdateLanguageCodesTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Repair\Owncloud;
  7. use OC\Repair\Owncloud\UpdateLanguageCodes;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\IConfig;
  10. use OCP\Migration\IOutput;
  11. use Test\TestCase;
  12. /**
  13. * Class UpdateLanguageCodesTest
  14. *
  15. * @group DB
  16. *
  17. * @package Test\Repair
  18. */
  19. class UpdateLanguageCodesTest extends TestCase {
  20. /** @var \OCP\IDBConnection */
  21. protected $connection;
  22. /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
  23. private $config;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->connection = \OC::$server->getDatabaseConnection();
  27. $this->config = $this->createMock(IConfig::class);
  28. }
  29. public function testRun(): void {
  30. $users = [
  31. ['userid' => 'user1', 'configvalue' => 'fi_FI'],
  32. ['userid' => 'user2', 'configvalue' => 'de'],
  33. ['userid' => 'user3', 'configvalue' => 'fi'],
  34. ['userid' => 'user4', 'configvalue' => 'ja'],
  35. ['userid' => 'user5', 'configvalue' => 'bg_BG'],
  36. ['userid' => 'user6', 'configvalue' => 'ja'],
  37. ['userid' => 'user7', 'configvalue' => 'th_TH'],
  38. ['userid' => 'user8', 'configvalue' => 'th_TH'],
  39. ];
  40. // insert test data
  41. $qb = $this->connection->getQueryBuilder();
  42. $qb->insert('preferences')
  43. ->values([
  44. 'userid' => $qb->createParameter('userid'),
  45. 'appid' => $qb->createParameter('appid'),
  46. 'configkey' => $qb->createParameter('configkey'),
  47. 'configvalue' => $qb->createParameter('configvalue'),
  48. ]);
  49. foreach ($users as $user) {
  50. $qb->setParameters([
  51. 'userid' => $user['userid'],
  52. 'appid' => 'core',
  53. 'configkey' => 'lang',
  54. 'configvalue' => $user['configvalue'],
  55. ])->execute();
  56. }
  57. // check if test data is written to DB
  58. $qb = $this->connection->getQueryBuilder();
  59. $result = $qb->select(['userid', 'configvalue'])
  60. ->from('preferences')
  61. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  62. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  63. ->orderBy('userid')
  64. ->execute();
  65. $rows = $result->fetchAll();
  66. $result->closeCursor();
  67. $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set');
  68. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  69. $outputMock = $this->createMock(IOutput::class);
  70. $outputMock->expects($this->exactly(7))
  71. ->method('info')
  72. ->withConsecutive(
  73. ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'],
  74. ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'],
  75. ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'],
  76. ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'],
  77. ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'],
  78. ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'],
  79. ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'],
  80. );
  81. $this->config->expects($this->once())
  82. ->method('getSystemValueString')
  83. ->with('version', '0.0.0')
  84. ->willReturn('12.0.0.13');
  85. // run repair step
  86. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  87. $repair->run($outputMock);
  88. // check if test data is correctly modified in DB
  89. $qb = $this->connection->getQueryBuilder();
  90. $result = $qb->select(['userid', 'configvalue'])
  91. ->from('preferences')
  92. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  93. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  94. ->orderBy('userid')
  95. ->execute();
  96. $rows = $result->fetchAll();
  97. $result->closeCursor();
  98. // value has changed for one user
  99. $users[0]['configvalue'] = 'fi';
  100. $users[4]['configvalue'] = 'bg';
  101. $users[6]['configvalue'] = 'th';
  102. $users[7]['configvalue'] = 'th';
  103. $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.');
  104. // remove test data
  105. foreach ($users as $user) {
  106. $qb = $this->connection->getQueryBuilder();
  107. $qb->delete('preferences')
  108. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid'])))
  109. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  110. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  111. ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR))
  112. ->execute();
  113. }
  114. }
  115. public function testSecondRun(): void {
  116. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  117. $outputMock = $this->createMock(IOutput::class);
  118. $outputMock->expects($this->never())
  119. ->method('info');
  120. $this->config->expects($this->once())
  121. ->method('getSystemValueString')
  122. ->with('version', '0.0.0')
  123. ->willReturn('12.0.0.14');
  124. // run repair step
  125. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  126. $repair->run($outputMock);
  127. }
  128. }