1
0

UpdateLanguageCodesTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\IDBConnection;
  11. use OCP\Migration\IOutput;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Test\TestCase;
  14. /**
  15. * Class UpdateLanguageCodesTest
  16. *
  17. * @group DB
  18. *
  19. * @package Test\Repair
  20. */
  21. class UpdateLanguageCodesTest extends TestCase {
  22. protected IDBConnection $connection;
  23. private IConfig&MockObject $config;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->connection = \OCP\Server::get(IDBConnection::class);
  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. ])->executeStatement();
  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. ->executeQuery();
  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. $expectedOutput = [
  69. ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'],
  70. ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'],
  71. ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'],
  72. ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'],
  73. ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'],
  74. ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'],
  75. ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'],
  76. ];
  77. $outputMessages = [];
  78. /** @var IOutput&MockObject $outputMock */
  79. $outputMock = $this->createMock(IOutput::class);
  80. $outputMock->expects($this->exactly(7))
  81. ->method('info')
  82. ->willReturnCallback(function () use (&$outputMessages) {
  83. $outputMessages[] = func_get_args();
  84. });
  85. $this->config->expects($this->once())
  86. ->method('getSystemValueString')
  87. ->with('version', '0.0.0')
  88. ->willReturn('12.0.0.13');
  89. // run repair step
  90. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  91. $repair->run($outputMock);
  92. // check if test data is correctly modified in DB
  93. $qb = $this->connection->getQueryBuilder();
  94. $result = $qb->select(['userid', 'configvalue'])
  95. ->from('preferences')
  96. ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  97. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  98. ->orderBy('userid')
  99. ->executeQuery();
  100. $rows = $result->fetchAll();
  101. $result->closeCursor();
  102. // value has changed for one user
  103. $users[0]['configvalue'] = 'fi';
  104. $users[4]['configvalue'] = 'bg';
  105. $users[6]['configvalue'] = 'th';
  106. $users[7]['configvalue'] = 'th';
  107. $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.');
  108. // remove test data
  109. foreach ($users as $user) {
  110. $qb = $this->connection->getQueryBuilder();
  111. $qb->delete('preferences')
  112. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid'])))
  113. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
  114. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
  115. ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR))
  116. ->executeStatement();
  117. }
  118. self::assertEquals($expectedOutput, $outputMessages);
  119. }
  120. public function testSecondRun(): void {
  121. /** @var IOutput&MockObject $outputMock */
  122. $outputMock = $this->createMock(IOutput::class);
  123. $outputMock->expects($this->never())
  124. ->method('info');
  125. $this->config->expects($this->once())
  126. ->method('getSystemValueString')
  127. ->with('version', '0.0.0')
  128. ->willReturn('12.0.0.14');
  129. // run repair step
  130. $repair = new UpdateLanguageCodes($this->connection, $this->config);
  131. $repair->run($outputMock);
  132. }
  133. }