You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateLanguageCodesTest.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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->at(0))
  88. ->method('info')
  89. ->with('Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.');
  90. $outputMock->expects($this->at(1))
  91. ->method('info')
  92. ->with('Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.');
  93. $outputMock->expects($this->at(2))
  94. ->method('info')
  95. ->with('Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.');
  96. $outputMock->expects($this->at(3))
  97. ->method('info')
  98. ->with('Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.');
  99. $outputMock->expects($this->at(4))
  100. ->method('info')
  101. ->with('Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.');
  102. $outputMock->expects($this->at(5))
  103. ->method('info')
  104. ->with('Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.');
  105. $outputMock->expects($this->at(6))
  106. ->method('info')
  107. ->with('Changed 2 setting(s) from "th_TH" to "th" in preferences table.');
  108. $this->config->expects($this->once())
  109. ->method('getSystemValue')
  110. ->with('version', '0.0.0')
  111. ->willReturn('12.0.0.13');
  112. // run repair step
  113. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  114. $repair->run($outputMock);
  115. // check if test data is correctly modified in DB
  116. $qb = $this->connection->getQueryBuilder();
  117. $result = $qb->select(['userid', 'configvalue'])
  118. ->from('preferences')
  119. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  120. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  121. ->orderBy('userid')
  122. ->execute();
  123. $rows = $result->fetchAll();
  124. $result->closeCursor();
  125. // value has changed for one user
  126. $users[0]['configvalue'] = 'fi';
  127. $users[4]['configvalue'] = 'bg';
  128. $users[6]['configvalue'] = 'th';
  129. $users[7]['configvalue'] = 'th';
  130. $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.');
  131. // remove test data
  132. foreach ($users as $user) {
  133. $qb = $this->connection->getQueryBuilder();
  134. $qb->delete('preferences')
  135. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid'])))
  136. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  137. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  138. ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR))
  139. ->execute();
  140. }
  141. }
  142. public function testSecondRun() {
  143. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  144. $outputMock = $this->createMock(IOutput::class);
  145. $outputMock->expects($this->never())
  146. ->method('info');
  147. $this->config->expects($this->once())
  148. ->method('getSystemValue')
  149. ->with('version', '0.0.0')
  150. ->willReturn('12.0.0.14');
  151. // run repair step
  152. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  153. $repair->run($outputMock);
  154. }
  155. }