diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-19 20:50:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-19 20:50:35 +0200 |
commit | 9813cbf6164a81e1054d3225d135261bca260daa (patch) | |
tree | e5eba7524fb525136a164d083459fa408147e63f /tests/lib | |
parent | 75f5cb76300ca754f141573e76e029efb9369e51 (diff) | |
parent | 349a9fc5f6f65ad4b0a8278af54757903bd1a170 (diff) | |
download | nextcloud-server-9813cbf6164a81e1054d3225d135261bca260daa.tar.gz nextcloud-server-9813cbf6164a81e1054d3225d135261bca260daa.zip |
Merge pull request #48217 from nextcloud/chore/prepare-oc_repair-unit10
chore: Cleanup and prepare `\OC\Repair\RepairMimeTypes` tests for PHPUnit 10
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/Repair/CleanTagsTest.php | 41 | ||||
-rw-r--r-- | tests/lib/Repair/ClearFrontendCachesTest.php | 14 | ||||
-rw-r--r-- | tests/lib/Repair/ClearGeneratedAvatarCacheTest.php | 16 | ||||
-rw-r--r-- | tests/lib/Repair/OldGroupMembershipSharesTest.php | 27 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php | 239 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/CleanPreviewsTest.php | 109 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php | 154 | ||||
-rw-r--r-- | tests/lib/Repair/RepairCollationTest.php | 38 | ||||
-rw-r--r-- | tests/lib/Repair/RepairDavSharesTest.php | 38 | ||||
-rw-r--r-- | tests/lib/Repair/RepairInvalidSharesTest.php | 42 | ||||
-rw-r--r-- | tests/lib/Repair/RepairMimeTypesTest.php | 24 |
11 files changed, 598 insertions, 144 deletions
diff --git a/tests/lib/Repair/CleanTagsTest.php b/tests/lib/Repair/CleanTagsTest.php index 13aca706a5a..adb14b16fc4 100644 --- a/tests/lib/Repair/CleanTagsTest.php +++ b/tests/lib/Repair/CleanTagsTest.php @@ -8,8 +8,10 @@ namespace Test\Repair; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; use OCP\IUserManager; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; /** * Tests for the cleaning the tags tables @@ -19,25 +21,18 @@ use OCP\Migration\IOutput; * @see \OC\Repair\CleanTags */ class CleanTagsTest extends \Test\TestCase { - /** @var \OC\Repair\CleanTags */ - protected $repair; - /** @var \OCP\IDBConnection */ - protected $connection; + private ?int $createdFile = null; + private \OC\Repair\CleanTags $repair; + private IDBConnection $connection; - /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; - - /** @var int */ - protected $createdFile; - - /** @var IOutput */ - private $outputMock; + private IUserManager&MockObject $userManager; + private IOutput&MockObject $outputMock; protected function setUp(): void { parent::setUp(); - $this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') + $this->outputMock = $this->getMockBuilder(IOutput::class) ->disableOriginalConstructor() ->getMock(); @@ -45,7 +40,7 @@ class CleanTagsTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->repair = new \OC\Repair\CleanTags($this->connection, $this->userManager); $this->cleanUpTables(); } @@ -59,14 +54,14 @@ class CleanTagsTest extends \Test\TestCase { protected function cleanUpTables() { $qb = $this->connection->getQueryBuilder(); $qb->delete('vcategory') - ->execute(); + ->executeStatement(); $qb->delete('vcategory_to_object') - ->execute(); + ->executeStatement(); $qb->delete('filecache') ->runAcrossAllShards() - ->execute(); + ->executeStatement(); } public function testRun(): void { @@ -119,7 +114,7 @@ class CleanTagsTest extends \Test\TestCase { $qb = $this->connection->getQueryBuilder(); $result = $qb->select($qb->func()->count('*')) ->from($tableName) - ->execute(); + ->executeQuery(); $this->assertEquals($expected, $result->fetchOne(), $message); } @@ -140,7 +135,7 @@ class CleanTagsTest extends \Test\TestCase { 'category' => $qb->createNamedParameter($category), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); return $qb->getLastInsertId(); } @@ -159,7 +154,7 @@ class CleanTagsTest extends \Test\TestCase { 'categoryid' => $qb->createNamedParameter($category, IQueryBuilder::PARAM_INT), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); } /** @@ -167,7 +162,7 @@ class CleanTagsTest extends \Test\TestCase { * @return int */ protected function getFileID() { - if ($this->createdFile) { + if ($this->createdFile !== null) { return $this->createdFile; } @@ -181,7 +176,7 @@ class CleanTagsTest extends \Test\TestCase { 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) - ->execute(); + ->executeStatement(); $fileName = $this->getUniqueID('TestRepairCleanTags', 12); $qb->insert('filecache') ->values([ @@ -189,7 +184,7 @@ class CleanTagsTest extends \Test\TestCase { 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) - ->execute(); + ->executeStatement(); $this->createdFile = $qb->getLastInsertId(); return $this->createdFile; diff --git a/tests/lib/Repair/ClearFrontendCachesTest.php b/tests/lib/Repair/ClearFrontendCachesTest.php index e48db295d45..2e4681c6e5d 100644 --- a/tests/lib/Repair/ClearFrontendCachesTest.php +++ b/tests/lib/Repair/ClearFrontendCachesTest.php @@ -10,19 +10,15 @@ use OC\Template\JSCombiner; use OCP\ICache; use OCP\ICacheFactory; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; class ClearFrontendCachesTest extends \Test\TestCase { - /** @var ICacheFactory */ - private $cacheFactory; - /** @var JSCombiner */ - private $jsCombiner; + private ICacheFactory&MockObject $cacheFactory; + private JSCombiner&MockObject $jsCombiner; + private IOutput&MockObject $outputMock; - /** @var \OC\Repair\ClearFrontendCaches */ - protected $repair; - - /** @var IOutput */ - private $outputMock; + protected \OC\Repair\ClearFrontendCaches $repair; protected function setUp(): void { parent::setUp(); diff --git a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php index c03b9082446..810fa1fe4e8 100644 --- a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php +++ b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php @@ -10,27 +10,19 @@ use OC\Avatar\AvatarManager; use OC\Repair\ClearGeneratedAvatarCache; use OCP\BackgroundJob\IJobList; use OCP\IConfig; -use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; class ClearGeneratedAvatarCacheTest extends \Test\TestCase { - /** @var AvatarManager */ - private $avatarManager; - /** @var IOutput */ - private $outputMock; - - /** @var IConfig */ - private $config; - - /** @var IJobList */ - private $jobList; + private AvatarManager&MockObject $avatarManager; + private IConfig&MockObject $config; + private IJobList&MockObject $jobList; protected ClearGeneratedAvatarCache $repair; protected function setUp(): void { parent::setUp(); - $this->outputMock = $this->createMock(IOutput::class); $this->avatarManager = $this->createMock(AvatarManager::class); $this->config = $this->createMock(IConfig::class); $this->jobList = $this->createMock(IJobList::class); diff --git a/tests/lib/Repair/OldGroupMembershipSharesTest.php b/tests/lib/Repair/OldGroupMembershipSharesTest.php index 5d3590499e6..594a8bf0b66 100644 --- a/tests/lib/Repair/OldGroupMembershipSharesTest.php +++ b/tests/lib/Repair/OldGroupMembershipSharesTest.php @@ -8,8 +8,11 @@ namespace Test\Repair; use OC\Repair\OldGroupMembershipShares; +use OCP\IDBConnection; +use OCP\IGroupManager; use OCP\Migration\IOutput; use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; /** * Class OldGroupMembershipSharesTest @@ -19,23 +22,17 @@ use OCP\Share\IShare; * @package Test\Repair */ class OldGroupMembershipSharesTest extends \Test\TestCase { - /** @var OldGroupMembershipShares */ - protected $repair; - /** @var \OCP\IDBConnection */ - protected $connection; - - /** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; + private IDBConnection $connection; + private IGroupManager&MockObject $groupManager; protected function setUp(): void { parent::setUp(); - /** \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - $this->groupManager = $this->getMockBuilder('OCP\IGroupManager') + $this->groupManager = $this->getMockBuilder(IGroupManager::class) ->disableOriginalConstructor() ->getMock(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->deleteAllShares(); } @@ -48,7 +45,7 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } public function testRun(): void { @@ -76,7 +73,7 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member], ['id' => $notAMember]], $rows); $result->closeCursor(); @@ -92,7 +89,7 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member]], $rows); $result->closeCursor(); @@ -127,8 +124,8 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values($shareValues) - ->execute(); + ->executeStatement(); - return $this->connection->lastInsertId('*PREFIX*share'); + return $qb->getLastInsertId(); } } diff --git a/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php new file mode 100644 index 00000000000..3c5583c49da --- /dev/null +++ b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php @@ -0,0 +1,239 @@ +<?php +/** + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace Test\Repair\Owncloud; + +use OC\Repair\Owncloud\CleanPreviewsBackgroundJob; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\IJobList; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use Test\TestCase; + +class CleanPreviewsBackgroundJobTest extends TestCase { + + private IRootFolder&MockObject $rootFolder; + private LoggerInterface&MockObject $logger; + private IJobList&MockObject $jobList; + private ITimeFactory&MockObject $timeFactory; + private IUserManager&MockObject $userManager; + private CleanPreviewsBackgroundJob $job; + + public function setUp(): void { + parent::setUp(); + + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->jobList = $this->createMock(IJobList::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->userManager = $this->createMock(IUserManager::class); + + $this->userManager->expects($this->any())->method('userExists')->willReturn(true); + + $this->job = new CleanPreviewsBackgroundJob( + $this->rootFolder, + $this->logger, + $this->jobList, + $this->timeFactory, + $this->userManager + ); + } + + public function testCleanupPreviewsUnfinished(): void { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $thumbnailFolder = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo('myuid')) + ->willReturn($userFolder); + + $userFolder->method('getParent')->willReturn($userRoot); + + $userRoot->method('get') + ->with($this->equalTo('thumbnails')) + ->willReturn($thumbnailFolder); + + $previewFolder1 = $this->createMock(Folder::class); + + $previewFolder1->expects($this->once()) + ->method('delete'); + + $thumbnailFolder->method('getDirectoryListing') + ->willReturn([$previewFolder1]); + $thumbnailFolder->expects($this->never()) + ->method('delete'); + + $this->timeFactory->method('getTime') + ->will($this->onConsecutiveCalls(100, 200)); + + $this->jobList->expects($this->once()) + ->method('add') + ->with( + $this->equalTo(CleanPreviewsBackgroundJob::class), + $this->equalTo(['uid' => 'myuid']) + ); + + $loggerCalls = []; + $this->logger->expects($this->exactly(2)) + ->method('info') + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); + + $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['New preview cleanup scheduled for myuid', []], + ], $loggerCalls); + } + + public function testCleanupPreviewsFinished(): void { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $thumbnailFolder = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo('myuid')) + ->willReturn($userFolder); + + $userFolder->method('getParent')->willReturn($userRoot); + + $userRoot->method('get') + ->with($this->equalTo('thumbnails')) + ->willReturn($thumbnailFolder); + + $previewFolder1 = $this->createMock(Folder::class); + + $previewFolder1->expects($this->once()) + ->method('delete'); + + $thumbnailFolder->method('getDirectoryListing') + ->willReturn([$previewFolder1]); + + $this->timeFactory->method('getTime') + ->will($this->onConsecutiveCalls(100, 101)); + + $this->jobList->expects($this->never()) + ->method('add'); + + $loggerCalls = []; + $this->logger->expects($this->exactly(2)) + ->method('info') + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); + + $thumbnailFolder->expects($this->once()) + ->method('delete'); + + $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); + } + + + public function testNoUserFolder(): void { + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo('myuid')) + ->willThrowException(new NotFoundException()); + + $loggerCalls = []; + $this->logger->expects($this->exactly(2)) + ->method('info') + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); + + $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); + } + + public function testNoThumbnailFolder(): void { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo('myuid')) + ->willReturn($userFolder); + + $userFolder->method('getParent')->willReturn($userRoot); + + $userRoot->method('get') + ->with($this->equalTo('thumbnails')) + ->willThrowException(new NotFoundException()); + + $loggerCalls = []; + $this->logger->expects($this->exactly(2)) + ->method('info') + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); + + $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); + } + + public function testNotPermittedToDelete(): void { + $userFolder = $this->createMock(Folder::class); + $userRoot = $this->createMock(Folder::class); + $thumbnailFolder = $this->createMock(Folder::class); + + $this->rootFolder->method('getUserFolder') + ->with($this->equalTo('myuid')) + ->willReturn($userFolder); + + $userFolder->method('getParent')->willReturn($userRoot); + + $userRoot->method('get') + ->with($this->equalTo('thumbnails')) + ->willReturn($thumbnailFolder); + + $previewFolder1 = $this->createMock(Folder::class); + + $previewFolder1->expects($this->once()) + ->method('delete') + ->willThrowException(new NotPermittedException()); + + $thumbnailFolder->method('getDirectoryListing') + ->willReturn([$previewFolder1]); + + $this->timeFactory->method('getTime') + ->will($this->onConsecutiveCalls(100, 101)); + + $this->jobList->expects($this->never()) + ->method('add'); + + $thumbnailFolder->expects($this->once()) + ->method('delete') + ->willThrowException(new NotPermittedException()); + + $loggerCalls = []; + $this->logger->expects($this->exactly(2)) + ->method('info') + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); + + $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); + } +} diff --git a/tests/lib/Repair/Owncloud/CleanPreviewsTest.php b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php new file mode 100644 index 00000000000..3a1936076a0 --- /dev/null +++ b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php @@ -0,0 +1,109 @@ +<?php +/** + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace Test\Repair\Owncloud; + +use OC\Repair\Owncloud\CleanPreviews; +use OC\Repair\Owncloud\CleanPreviewsBackgroundJob; +use OCP\BackgroundJob\IJobList; +use OCP\IConfig; +use OCP\IUser; +use OCP\IUserManager; +use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class CleanPreviewsTest extends TestCase { + + private IJobList&MockObject $jobList; + private IUserManager&MockObject $userManager; + private IConfig&MockObject $config; + + /** @var CleanPreviews */ + private $repair; + + public function setUp(): void { + parent::setUp(); + + $this->jobList = $this->createMock(IJobList::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->config = $this->createMock(IConfig::class); + + $this->repair = new CleanPreviews( + $this->jobList, + $this->userManager, + $this->config + ); + } + + public function testGetName(): void { + $this->assertSame('Add preview cleanup background jobs', $this->repair->getName()); + } + + public function testRun(): void { + $user1 = $this->createMock(IUser::class); + $user1->method('getUID') + ->willReturn('user1'); + $user2 = $this->createMock(IUser::class); + $user2->method('getUID') + ->willReturn('user2'); + + $this->userManager->expects($this->once()) + ->method('callForSeenUsers') + ->will($this->returnCallback(function (\Closure $function) use (&$user1, $user2) { + $function($user1); + $function($user2); + })); + + $jobListCalls = []; + $this->jobList->expects($this->exactly(2)) + ->method('add') + ->willReturnCallback(function () use (&$jobListCalls) { + $jobListCalls[] = func_get_args(); + }); + + $this->config->expects($this->once()) + ->method('getAppValue') + ->with( + $this->equalTo('core'), + $this->equalTo('previewsCleanedUp'), + $this->equalTo(false) + )->willReturn(false); + $this->config->expects($this->once()) + ->method('setAppValue') + ->with( + $this->equalTo('core'), + $this->equalTo('previewsCleanedUp'), + $this->equalTo(1) + ); + + $this->repair->run($this->createMock(IOutput::class)); + $this->assertEqualsCanonicalizing([ + [CleanPreviewsBackgroundJob::class, ['uid' => 'user1']], + [CleanPreviewsBackgroundJob::class, ['uid' => 'user2']], + ], $jobListCalls); + } + + + public function testRunAlreadyDone(): void { + $this->userManager->expects($this->never()) + ->method($this->anything()); + + $this->jobList->expects($this->never()) + ->method($this->anything()); + + $this->config->expects($this->once()) + ->method('getAppValue') + ->with( + $this->equalTo('core'), + $this->equalTo('previewsCleanedUp'), + $this->equalTo(false) + )->willReturn('1'); + $this->config->expects($this->never()) + ->method('setAppValue'); + + $this->repair->run($this->createMock(IOutput::class)); + } +} diff --git a/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php new file mode 100644 index 00000000000..a7907308d93 --- /dev/null +++ b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php @@ -0,0 +1,154 @@ +<?php +/** + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Repair\Owncloud; + +use OC\Repair\Owncloud\UpdateLanguageCodes; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +/** + * Class UpdateLanguageCodesTest + * + * @group DB + * + * @package Test\Repair + */ +class UpdateLanguageCodesTest extends TestCase { + + protected IDBConnection $connection; + private IConfig&MockObject $config; + + protected function setUp(): void { + parent::setUp(); + + $this->connection = \OCP\Server::get(IDBConnection::class); + $this->config = $this->createMock(IConfig::class); + } + + public function testRun(): void { + $users = [ + ['userid' => 'user1', 'configvalue' => 'fi_FI'], + ['userid' => 'user2', 'configvalue' => 'de'], + ['userid' => 'user3', 'configvalue' => 'fi'], + ['userid' => 'user4', 'configvalue' => 'ja'], + ['userid' => 'user5', 'configvalue' => 'bg_BG'], + ['userid' => 'user6', 'configvalue' => 'ja'], + ['userid' => 'user7', 'configvalue' => 'th_TH'], + ['userid' => 'user8', 'configvalue' => 'th_TH'], + ]; + + // insert test data + $qb = $this->connection->getQueryBuilder(); + $qb->insert('preferences') + ->values([ + 'userid' => $qb->createParameter('userid'), + 'appid' => $qb->createParameter('appid'), + 'configkey' => $qb->createParameter('configkey'), + 'configvalue' => $qb->createParameter('configvalue'), + ]); + foreach ($users as $user) { + $qb->setParameters([ + 'userid' => $user['userid'], + 'appid' => 'core', + 'configkey' => 'lang', + 'configvalue' => $user['configvalue'], + ])->executeStatement(); + } + + // check if test data is written to DB + $qb = $this->connection->getQueryBuilder(); + $result = $qb->select(['userid', 'configvalue']) + ->from('preferences') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) + ->orderBy('userid') + ->executeQuery(); + + $rows = $result->fetchAll(); + $result->closeCursor(); + + $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set'); + + $expectedOutput = [ + ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'], + ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'], + ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'], + ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'], + ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'], + ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'], + ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'], + ]; + $outputMessages = []; + /** @var IOutput&MockObject $outputMock */ + $outputMock = $this->createMock(IOutput::class); + $outputMock->expects($this->exactly(7)) + ->method('info') + ->willReturnCallback(function () use (&$outputMessages) { + $outputMessages[] = func_get_args(); + }); + + $this->config->expects($this->once()) + ->method('getSystemValueString') + ->with('version', '0.0.0') + ->willReturn('12.0.0.13'); + + // run repair step + $repair = new UpdateLanguageCodes($this->connection, $this->config); + $repair->run($outputMock); + + // check if test data is correctly modified in DB + $qb = $this->connection->getQueryBuilder(); + $result = $qb->select(['userid', 'configvalue']) + ->from('preferences') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) + ->orderBy('userid') + ->executeQuery(); + + $rows = $result->fetchAll(); + $result->closeCursor(); + + // value has changed for one user + $users[0]['configvalue'] = 'fi'; + $users[4]['configvalue'] = 'bg'; + $users[6]['configvalue'] = 'th'; + $users[7]['configvalue'] = 'th'; + $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.'); + + // remove test data + foreach ($users as $user) { + $qb = $this->connection->getQueryBuilder(); + $qb->delete('preferences') + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid']))) + ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) + ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR)) + ->executeStatement(); + } + self::assertEquals($expectedOutput, $outputMessages); + } + + public function testSecondRun(): void { + /** @var IOutput&MockObject $outputMock */ + $outputMock = $this->createMock(IOutput::class); + $outputMock->expects($this->never()) + ->method('info'); + + $this->config->expects($this->once()) + ->method('getSystemValueString') + ->with('version', '0.0.0') + ->willReturn('12.0.0.14'); + + // run repair step + $repair = new UpdateLanguageCodes($this->connection, $this->config); + $repair->run($outputMock); + } +} diff --git a/tests/lib/Repair/RepairCollationTest.php b/tests/lib/Repair/RepairCollationTest.php index 2b5091ad927..3f007fa6310 100644 --- a/tests/lib/Repair/RepairCollationTest.php +++ b/tests/lib/Repair/RepairCollationTest.php @@ -7,9 +7,12 @@ namespace Test\Repair; +use OC\DB\ConnectionAdapter; use OC\Repair\Collation; +use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -31,39 +34,25 @@ class TestCollationRepair extends Collation { * @see \OC\Repair\RepairMimeTypes */ class RepairCollationTest extends TestCase { - /** - * @var TestCollationRepair - */ - private $repair; - /** - * @var IDBConnection - */ - private $connection; + private TestCollationRepair $repair; + private ConnectionAdapter $connection; + private string $tableName; + private IConfig $config; - /** - * @var string - */ - private $tableName; - - /** - * @var \OCP\IConfig - */ - private $config; - - /** @var LoggerInterface */ - private $logger; + private LoggerInterface&MockObject $logger; protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->get(IDBConnection::class); - $this->logger = $this->createMock(LoggerInterface::class); - $this->config = \OC::$server->getConfig(); + $this->connection = \OCP\Server::get(ConnectionAdapter::class); + $this->config = \OCP\Server::get(IConfig::class); if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) { $this->markTestSkipped('Test only relevant on MySql'); } + $this->logger = $this->createMock(LoggerInterface::class); + $dbPrefix = $this->config->getSystemValueString('dbtableprefix'); $this->tableName = $this->getUniqueID($dbPrefix . '_collation_test'); $this->connection->prepare("CREATE TABLE $this->tableName(text VARCHAR(16)) COLLATE utf8_unicode_ci")->execute(); @@ -80,8 +69,7 @@ class RepairCollationTest extends TestCase { $tables = $this->repair->getAllNonUTF8BinTables($this->connection); $this->assertGreaterThanOrEqual(1, count($tables)); - /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ - $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') + $outputMock = $this->getMockBuilder(IOutput::class) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/lib/Repair/RepairDavSharesTest.php b/tests/lib/Repair/RepairDavSharesTest.php index 3a97de4d25d..73e71d75055 100644 --- a/tests/lib/Repair/RepairDavSharesTest.php +++ b/tests/lib/Repair/RepairDavSharesTest.php @@ -16,23 +16,19 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroupManager; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; use function in_array; class RepairDavSharesTest extends TestCase { - /** @var IOutput|\PHPUnit\Framework\MockObject\MockObject */ - protected $output; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ - protected $dbc; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; - /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */ - protected $logger; - /** @var RepairDavSharesTest */ - protected $repair; + + private IOutput&MockObject $output; + private IConfig&MockObject $config; + private IDBConnection&MockObject $dbc; + private LoggerInterface&MockObject $logger; + private IGroupManager&MockObject $groupManager; + private RepairDavShares $repair; public function setUp(): void { parent::setUp(); @@ -138,6 +134,7 @@ class RepairDavSharesTest extends TestCase { ->method('execute') ->willReturn($shareResults); + $updateCalls = []; $updateMock = $this->createMock(IQueryBuilder::class); $updateMock->expects($this->any()) ->method('expr') @@ -153,13 +150,10 @@ class RepairDavSharesTest extends TestCase { ->willReturnSelf(); $updateMock->expects($this->exactly(4)) ->method('setParameter') - ->withConsecutive( - ['updatedPrincipalUri', 'principals/groups/' . urlencode('family friends')], - ['shareId', 7], - ['updatedPrincipalUri', 'principals/groups/' . urlencode('Wants Repair')], - ['shareId', 1], - ) - ->willReturnSelf(); + ->willReturnCallback(function () use (&$updateCalls, &$updateMock) { + $updateCalls[] = func_get_args(); + return $updateMock; + }); $updateMock->expects($this->exactly(2)) ->method('execute'); @@ -174,5 +168,11 @@ class RepairDavSharesTest extends TestCase { }); $this->repair->run($this->output); + self::assertEquals([ + ['updatedPrincipalUri', 'principals/groups/' . urlencode('family friends'), null], + ['shareId', 7, null], + ['updatedPrincipalUri', 'principals/groups/' . urlencode('Wants Repair'), null], + ['shareId', 1, null] + ], $updateCalls); } } diff --git a/tests/lib/Repair/RepairInvalidSharesTest.php b/tests/lib/Repair/RepairInvalidSharesTest.php index d728e0c7ecd..dfcdce3801a 100644 --- a/tests/lib/Repair/RepairInvalidSharesTest.php +++ b/tests/lib/Repair/RepairInvalidSharesTest.php @@ -9,8 +9,8 @@ namespace Test\Repair; use OC\Repair\RepairInvalidShares; use OCP\IConfig; +use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; use OCP\Share\IShare; use Test\TestCase; @@ -22,11 +22,9 @@ use Test\TestCase; * @see \OC\Repair\RepairInvalidShares */ class RepairInvalidSharesTest extends TestCase { - /** @var IRepairStep */ - private $repair; - /** @var \OCP\IDBConnection */ - private $connection; + private RepairInvalidShares $repair; + private IDBConnection $connection; protected function setUp(): void { parent::setUp(); @@ -39,10 +37,9 @@ class RepairInvalidSharesTest extends TestCase { ->with('version') ->willReturn('12.0.0.0'); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->deleteAllShares(); - /** @var \OCP\IConfig $config */ $this->repair = new RepairInvalidShares($config, $this->connection); } @@ -54,7 +51,7 @@ class RepairInvalidSharesTest extends TestCase { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } /** @@ -80,30 +77,30 @@ class RepairInvalidSharesTest extends TestCase { $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values($shareValues) - ->execute(); - $parent = $this->getLastShareId(); + ->executeStatement(); + $parent = $qb->getLastInsertId(); // share with existing parent $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values(array_merge($shareValues, [ 'parent' => $qb->expr()->literal($parent), - ]))->execute(); - $validChild = $this->getLastShareId(); + ]))->executeStatement(); + $validChild = $qb->getLastInsertId(); // share with non-existing parent $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values(array_merge($shareValues, [ 'parent' => $qb->expr()->literal($parent + 100), - ]))->execute(); - $invalidChild = $this->getLastShareId(); + ]))->executeStatement(); + $invalidChild = $qb->getLastInsertId(); $query = $this->connection->getQueryBuilder(); $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $validChild], ['id' => $invalidChild]], $rows); $result->closeCursor(); @@ -119,7 +116,7 @@ class RepairInvalidSharesTest extends TestCase { $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $validChild]], $rows); $result->closeCursor(); @@ -167,9 +164,7 @@ class RepairInvalidSharesTest extends TestCase { 'permissions' => $qb->expr()->literal($testPerms), 'stime' => $qb->expr()->literal(time()), ]) - ->execute(); - - $shareId = $this->getLastShareId(); + ->executeStatement(); /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') @@ -182,7 +177,7 @@ class RepairInvalidSharesTest extends TestCase { ->select('*') ->from('share') ->orderBy('permissions', 'ASC') - ->execute() + ->executeQuery() ->fetchAll(); $this->assertCount(1, $results); @@ -191,11 +186,4 @@ class RepairInvalidSharesTest extends TestCase { $this->assertEquals($expectedPerms, $updatedShare['permissions']); } - - /** - * @return int - */ - protected function getLastShareId() { - return $this->connection->lastInsertId('*PREFIX*share'); - } } diff --git a/tests/lib/Repair/RepairMimeTypesTest.php b/tests/lib/Repair/RepairMimeTypesTest.php index 6bf59b9c102..4f044c054d3 100644 --- a/tests/lib/Repair/RepairMimeTypesTest.php +++ b/tests/lib/Repair/RepairMimeTypesTest.php @@ -14,7 +14,6 @@ use OCP\IAppConfig; use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; /** * Tests for the converting of legacy storages to home storages. @@ -24,21 +23,18 @@ use OCP\Migration\IRepairStep; * @see \OC\Repair\RepairMimeTypes */ class RepairMimeTypesTest extends \Test\TestCase { - /** @var IRepairStep */ - private $repair; - /** @var Temporary */ - private $storage; - - /** @var IMimeTypeLoader */ - private $mimetypeLoader; + private RepairMimeTypes $repair; + private Temporary $storage; + private IMimeTypeLoader $mimetypeLoader; + private IDBConnection $db; protected function setUp(): void { parent::setUp(); - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); + $this->mimetypeLoader = \OCP\Server::get(IMimeTypeLoader::class); + $this->db = \OCP\Server::get(IDBConnection::class); - /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject $config */ $config = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); @@ -66,10 +62,10 @@ class RepairMimeTypesTest extends \Test\TestCase { protected function tearDown(): void { $this->storage->getCache()->clear(); - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = $this->db->getQueryBuilder(); $qb->delete('storages') ->where($qb->expr()->eq('id', $qb->createNamedParameter($this->storage->getId()))); - $qb->execute(); + $qb->executeStatement(); $this->clearMimeTypes(); @@ -77,9 +73,9 @@ class RepairMimeTypesTest extends \Test\TestCase { } private function clearMimeTypes() { - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = $this->db->getQueryBuilder(); $qb->delete('mimetypes'); - $qb->execute(); + $qb->executeStatement(); $this->mimetypeLoader->reset(); } |