1
0

UpdateLanguageCodesTest.php 5.8 KB

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