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