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\IConfig;
  26. use OCP\Migration\IOutput;
  27. use Test\TestCase;
  28. /**
  29. * Class UpdateLanguageCodesTest
  30. *
  31. * @group DB
  32. *
  33. * @package Test\Repair
  34. */
  35. class UpdateLanguageCodesTest extends TestCase {
  36. /** @var \OCP\IDBConnection */
  37. protected $connection;
  38. /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
  39. private $config;
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->connection = \OC::$server->getDatabaseConnection();
  43. $this->config = $this->createMock(IConfig::class);
  44. }
  45. public function testRun() {
  46. $users = [
  47. ['userid' => 'user1', 'configvalue' => 'fi_FI'],
  48. ['userid' => 'user2', 'configvalue' => 'de'],
  49. ['userid' => 'user3', 'configvalue' => 'fi'],
  50. ['userid' => 'user4', 'configvalue' => 'ja'],
  51. ['userid' => 'user5', 'configvalue' => 'bg_BG'],
  52. ['userid' => 'user6', 'configvalue' => 'ja'],
  53. ['userid' => 'user7', 'configvalue' => 'th_TH'],
  54. ['userid' => 'user8', 'configvalue' => 'th_TH'],
  55. ];
  56. // insert test data
  57. $qb = $this->connection->getQueryBuilder();
  58. $qb->insert('preferences')
  59. ->values([
  60. 'userid' => $qb->createParameter('userid'),
  61. 'appid' => $qb->createParameter('appid'),
  62. 'configkey' => $qb->createParameter('configkey'),
  63. 'configvalue' => $qb->createParameter('configvalue'),
  64. ]);
  65. foreach ($users as $user) {
  66. $qb->setParameters([
  67. 'userid' => $user['userid'],
  68. 'appid' => 'core',
  69. 'configkey' => 'lang',
  70. 'configvalue' => $user['configvalue'],
  71. ])->execute();
  72. }
  73. // check if test data is written to DB
  74. $qb = $this->connection->getQueryBuilder();
  75. $result = $qb->select(['userid', 'configvalue'])
  76. ->from('preferences')
  77. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  78. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  79. ->execute();
  80. $rows = $result->fetchAll();
  81. $result->closeCursor();
  82. $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set');
  83. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  84. $outputMock = $this->createMock(IOutput::class);
  85. $outputMock->expects($this->at(0))
  86. ->method('info')
  87. ->with('Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.');
  88. $outputMock->expects($this->at(1))
  89. ->method('info')
  90. ->with('Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.');
  91. $outputMock->expects($this->at(2))
  92. ->method('info')
  93. ->with('Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.');
  94. $outputMock->expects($this->at(3))
  95. ->method('info')
  96. ->with('Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.');
  97. $outputMock->expects($this->at(4))
  98. ->method('info')
  99. ->with('Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.');
  100. $outputMock->expects($this->at(5))
  101. ->method('info')
  102. ->with('Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.');
  103. $outputMock->expects($this->at(6))
  104. ->method('info')
  105. ->with('Changed 2 setting(s) from "th_TH" to "th" in preferences table.');
  106. $this->config->expects($this->once())
  107. ->method('getSystemValue')
  108. ->with('version', '0.0.0')
  109. ->willReturn('12.0.0.13');
  110. // run repair step
  111. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  112. $repair->run($outputMock);
  113. // check if test data is correctly modified in DB
  114. $qb = $this->connection->getQueryBuilder();
  115. $result = $qb->select(['userid', 'configvalue'])
  116. ->from('preferences')
  117. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  118. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  119. ->orderBy('userid')
  120. ->execute();
  121. $rows = $result->fetchAll();
  122. $result->closeCursor();
  123. // value has changed for one user
  124. $users[0]['configvalue'] = 'fi';
  125. $users[4]['configvalue'] = 'bg';
  126. $users[6]['configvalue'] = 'th';
  127. $users[7]['configvalue'] = 'th';
  128. $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.');
  129. // remove test data
  130. foreach ($users as $user) {
  131. $qb = $this->connection->getQueryBuilder();
  132. $qb->delete('preferences')
  133. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid'])))
  134. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  135. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  136. ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue'])))
  137. ->execute();
  138. }
  139. }
  140. public function testSecondRun() {
  141. /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */
  142. $outputMock = $this->createMock(IOutput::class);
  143. $outputMock->expects($this->never())
  144. ->method('info');
  145. $this->config->expects($this->once())
  146. ->method('getSystemValue')
  147. ->with('version', '0.0.0')
  148. ->willReturn('12.0.0.14');
  149. // run repair step
  150. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  151. $repair->run($outputMock);
  152. }
  153. }