UpdateLanguageCodesTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Repair\Owncloud;
  24. use OC\Repair\Owncloud\UpdateLanguageCodes;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IConfig;
  27. use OCP\Migration\IOutput;
  28. use Test\TestCase;
  29. /**
  30. * Class UpdateLanguageCodesTest
  31. *
  32. * @group DB
  33. *
  34. * @package Test\Repair
  35. */
  36. class UpdateLanguageCodesTest extends TestCase {
  37. /** @var \OCP\IDBConnection */
  38. protected $connection;
  39. /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
  40. private $config;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->connection = \OC::$server->getDatabaseConnection();
  44. $this->config = $this->createMock(IConfig::class);
  45. }
  46. public function testRun() {
  47. $users = [
  48. ['userid' => 'user1', 'configvalue' => 'fi_FI'],
  49. ['userid' => 'user2', 'configvalue' => 'de'],
  50. ['userid' => 'user3', 'configvalue' => 'fi'],
  51. ['userid' => 'user4', 'configvalue' => 'ja'],
  52. ['userid' => 'user5', 'configvalue' => 'bg_BG'],
  53. ['userid' => 'user6', 'configvalue' => 'ja'],
  54. ['userid' => 'user7', 'configvalue' => 'th_TH'],
  55. ['userid' => 'user8', 'configvalue' => 'th_TH'],
  56. ];
  57. // insert test data
  58. $qb = $this->connection->getQueryBuilder();
  59. $qb->insert('preferences')
  60. ->values([
  61. 'userid' => $qb->createParameter('userid'),
  62. 'appid' => $qb->createParameter('appid'),
  63. 'configkey' => $qb->createParameter('configkey'),
  64. 'configvalue' => $qb->createParameter('configvalue'),
  65. ]);
  66. foreach ($users as $user) {
  67. $qb->setParameters([
  68. 'userid' => $user['userid'],
  69. 'appid' => 'core',
  70. 'configkey' => 'lang',
  71. 'configvalue' => $user['configvalue'],
  72. ])->execute();
  73. }
  74. // check if test data is written to DB
  75. $qb = $this->connection->getQueryBuilder();
  76. $result = $qb->select(['userid', 'configvalue'])
  77. ->from('preferences')
  78. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  79. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  80. ->orderBy('userid')
  81. ->execute();
  82. $rows = $result->fetchAll();
  83. $result->closeCursor();
  84. $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set');
  85. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  86. $outputMock = $this->createMock(IOutput::class);
  87. $outputMock->expects($this->exactly(7))
  88. ->method('info')
  89. ->withConsecutive(
  90. ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'],
  91. ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'],
  92. ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'],
  93. ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'],
  94. ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'],
  95. ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'],
  96. ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'],
  97. );
  98. $this->config->expects($this->once())
  99. ->method('getSystemValueString')
  100. ->with('version', '0.0.0')
  101. ->willReturn('12.0.0.13');
  102. // run repair step
  103. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  104. $repair->run($outputMock);
  105. // check if test data is correctly modified in DB
  106. $qb = $this->connection->getQueryBuilder();
  107. $result = $qb->select(['userid', 'configvalue'])
  108. ->from('preferences')
  109. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  110. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  111. ->orderBy('userid')
  112. ->execute();
  113. $rows = $result->fetchAll();
  114. $result->closeCursor();
  115. // value has changed for one user
  116. $users[0]['configvalue'] = 'fi';
  117. $users[4]['configvalue'] = 'bg';
  118. $users[6]['configvalue'] = 'th';
  119. $users[7]['configvalue'] = 'th';
  120. $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.');
  121. // remove test data
  122. foreach ($users as $user) {
  123. $qb = $this->connection->getQueryBuilder();
  124. $qb->delete('preferences')
  125. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid'])))
  126. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  127. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  128. ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR))
  129. ->execute();
  130. }
  131. }
  132. public function testSecondRun() {
  133. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  134. $outputMock = $this->createMock(IOutput::class);
  135. $outputMock->expects($this->never())
  136. ->method('info');
  137. $this->config->expects($this->once())
  138. ->method('getSystemValueString')
  139. ->with('version', '0.0.0')
  140. ->willReturn('12.0.0.14');
  141. // run repair step
  142. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  143. $repair->run($outputMock);
  144. }
  145. }