diff options
Diffstat (limited to 'tests/lib/Repair')
-rw-r--r-- | tests/lib/Repair/CleanTagsTest.php | 64 | ||||
-rw-r--r-- | tests/lib/Repair/ClearFrontendCachesTest.php | 20 | ||||
-rw-r--r-- | tests/lib/Repair/ClearGeneratedAvatarCacheTest.php | 29 | ||||
-rw-r--r-- | tests/lib/Repair/NC11/FixMountStoragesTest.php | 114 | ||||
-rw-r--r-- | tests/lib/Repair/NC29/SanitizeAccountPropertiesJobTest.php | 116 | ||||
-rw-r--r-- | tests/lib/Repair/NC29/SanitizeAccountPropertiesTest.php | 43 | ||||
-rw-r--r-- | tests/lib/Repair/OldGroupMembershipSharesTest.php | 31 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php | 237 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/CleanPreviewsTest.php | 110 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php | 156 | ||||
-rw-r--r-- | tests/lib/Repair/RepairCollationTest.php | 53 | ||||
-rw-r--r-- | tests/lib/Repair/RepairDavSharesTest.php | 40 | ||||
-rw-r--r-- | tests/lib/Repair/RepairInvalidSharesTest.php | 62 | ||||
-rw-r--r-- | tests/lib/Repair/RepairMimeTypesTest.php | 56 |
14 files changed, 826 insertions, 305 deletions
diff --git a/tests/lib/Repair/CleanTagsTest.php b/tests/lib/Repair/CleanTagsTest.php index 586ae24a0aa..04afabb87d9 100644 --- a/tests/lib/Repair/CleanTagsTest.php +++ b/tests/lib/Repair/CleanTagsTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,9 +8,13 @@ namespace Test\Repair; +use OC\Repair\CleanTags; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; use OCP\IUserManager; use OCP\Migration\IOutput; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; /** * Tests for the cleaning the tags tables @@ -19,25 +24,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; - /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; + private ?int $createdFile = null; + private CleanTags $repair; + private IDBConnection $connection; - /** @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,8 +43,8 @@ class CleanTagsTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); - $this->connection = \OC::$server->getDatabaseConnection(); - $this->repair = new \OC\Repair\CleanTags($this->connection, $this->userManager); + $this->connection = Server::get(IDBConnection::class); + $this->repair = new CleanTags($this->connection, $this->userManager); $this->cleanUpTables(); } @@ -59,16 +57,17 @@ 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') - ->execute(); + ->runAcrossAllShards() + ->executeStatement(); } - public function testRun() { + public function testRun(): void { $cat1 = $this->addTagCategory('TestRepairCleanTags', 'files'); // Retained $cat2 = $this->addTagCategory('TestRepairCleanTags2', 'files'); // Deleted: Category will be empty $this->addTagCategory('TestRepairCleanTags3', 'files'); // Deleted: Category is empty @@ -118,7 +117,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); } @@ -139,9 +138,9 @@ class CleanTagsTest extends \Test\TestCase { 'category' => $qb->createNamedParameter($category), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); - return (int) $this->getLastInsertID('vcategory', 'id'); + return $qb->getLastInsertId(); } /** @@ -158,7 +157,7 @@ class CleanTagsTest extends \Test\TestCase { 'categoryid' => $qb->createNamedParameter($category, IQueryBuilder::PARAM_INT), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); } /** @@ -166,7 +165,7 @@ class CleanTagsTest extends \Test\TestCase { * @return int */ protected function getFileID() { - if ($this->createdFile) { + if ($this->createdFile !== null) { return $this->createdFile; } @@ -176,28 +175,21 @@ class CleanTagsTest extends \Test\TestCase { $fileName = $this->getUniqueID('TestRepairCleanTags', 12); $qb->insert('filecache') ->values([ + 'storage' => $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT), 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) - ->execute(); + ->executeStatement(); $fileName = $this->getUniqueID('TestRepairCleanTags', 12); $qb->insert('filecache') ->values([ + 'storage' => $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT), 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) - ->execute(); + ->executeStatement(); - $this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid'); + $this->createdFile = $qb->getLastInsertId(); return $this->createdFile; } - - /** - * @param $tableName - * @param $idName - * @return int - */ - protected function getLastInsertID($tableName, $idName) { - return $this->connection->lastInsertId("*PREFIX*$tableName"); - } } diff --git a/tests/lib/Repair/ClearFrontendCachesTest.php b/tests/lib/Repair/ClearFrontendCachesTest.php index 640e272f3b7..3e5927565fe 100644 --- a/tests/lib/Repair/ClearFrontendCachesTest.php +++ b/tests/lib/Repair/ClearFrontendCachesTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -6,23 +7,20 @@ namespace Test\Repair; +use OC\Repair\ClearFrontendCaches; 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; - /** @var \OC\Repair\ClearFrontendCaches */ - protected $repair; + private ICacheFactory&MockObject $cacheFactory; + private JSCombiner&MockObject $jsCombiner; + private IOutput&MockObject $outputMock; - /** @var IOutput */ - private $outputMock; + protected ClearFrontendCaches $repair; protected function setUp(): void { parent::setUp(); @@ -32,11 +30,11 @@ class ClearFrontendCachesTest extends \Test\TestCase { $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->jsCombiner = $this->createMock(JSCombiner::class); - $this->repair = new \OC\Repair\ClearFrontendCaches($this->cacheFactory, $this->jsCombiner); + $this->repair = new ClearFrontendCaches($this->cacheFactory, $this->jsCombiner); } - public function testRun() { + public function testRun(): void { $imagePathCache = $this->createMock(ICache::class); $imagePathCache->expects($this->once()) ->method('clear') diff --git a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php index 1545085f977..43203d489e6 100644 --- a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php +++ b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -10,27 +11,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); @@ -38,7 +31,7 @@ class ClearGeneratedAvatarCacheTest extends \Test\TestCase { $this->repair = new ClearGeneratedAvatarCache($this->config, $this->avatarManager, $this->jobList); } - public function shouldRunDataProvider() { + public static function shouldRunDataProvider(): array { return [ ['11.0.0.0', true], ['15.0.0.3', true], @@ -52,16 +45,16 @@ class ClearGeneratedAvatarCacheTest extends \Test\TestCase { } /** - * @dataProvider shouldRunDataProvider * * @param string $from * @param boolean $expected */ - public function testShouldRun($from, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('shouldRunDataProvider')] + public function testShouldRun($from, $expected): void { $this->config->expects($this->any()) - ->method('getSystemValueString') - ->with('version', '0.0.0.0') - ->willReturn($from); + ->method('getSystemValueString') + ->with('version', '0.0.0.0') + ->willReturn($from); $this->assertEquals($expected, $this->invokePrivate($this->repair, 'shouldRun')); } diff --git a/tests/lib/Repair/NC11/FixMountStoragesTest.php b/tests/lib/Repair/NC11/FixMountStoragesTest.php deleted file mode 100644 index 6fdcc07d24e..00000000000 --- a/tests/lib/Repair/NC11/FixMountStoragesTest.php +++ /dev/null @@ -1,114 +0,0 @@ -<?php -/** - * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -namespace Test\Repair\NC11; - -use OC\Repair\NC11\FixMountStorages; -use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\IDBConnection; -use OCP\Migration\IOutput; -use Test\TestCase; - -/** - * Class FixMountStoragesTest - * - * @package Test\Repair\NC11 - * @group DB - */ -class FixMountStoragesTest extends TestCase { - /** @var IDBConnection */ - private $db; - - /** @var FixMountStorages */ - private $repair; - - protected function setUp(): void { - parent::setUp(); - - $this->db = \OC::$server->getDatabaseConnection(); - - $this->repair = new FixMountStorages( - $this->db - ); - } - - public function testGetName() { - $this->assertSame('Fix potential broken mount points', $this->repair->getName()); - } - - public function testRun() { - // Valid mount - $file1 = $this->createFile(42); - $mount1 = $this->createMount($file1, 42); - $this->assertStorage($mount1, 42); - - // Broken mount - $file2 = $this->createFile(23); - $mount2 = $this->createMount($file2, 1337); - $this->assertStorage($mount2, 1337); - - /** @var IOutput|\PHPUnit\Framework\MockObject\MockObject $output */ - $output = $this->createMock(IOutput::class); - $output->expects($this->exactly(2)) - ->method('info') - ->withConsecutive( - ['1 mounts updated'], - ['No mounts updated'] - ); - - $this->repair->run($output); - $this->assertStorage($mount1, 42); - $this->assertStorage($mount2, 23); - - $this->repair->run($output); - $this->assertStorage($mount1, 42); - $this->assertStorage($mount2, 23); - } - - - protected function createFile($storage) { - $query = $this->db->getQueryBuilder(); - - $query->insert('filecache') - ->values([ - 'storage' => $query->createNamedParameter($storage, IQueryBuilder::PARAM_INT), - 'path_hash' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR), - 'encrypted' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), - 'size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), - 'unencrypted_size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), - ]); - $query->execute(); - - return $query->getLastInsertId(); - } - - protected function createMount($fileId, $storage) { - $query = $this->db->getQueryBuilder(); - - $query->insert('mounts') - ->values([ - 'storage_id' => $query->createNamedParameter($storage, IQueryBuilder::PARAM_INT), - 'root_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), - 'user_id' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR), - 'mount_point' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR), - ]); - $query->execute(); - - return $query->getLastInsertId(); - } - - protected function assertStorage($mount, $storage) { - $query = $this->db->getQueryBuilder(); - $query->select('storage_id') - ->from('mounts') - ->where($query->expr()->eq('id', $query->createNamedParameter($mount, IQueryBuilder::PARAM_INT))); - $result = $query->execute(); - $row = $result->fetch(); - $result->closeCursor(); - - $this->assertEquals($storage, $row['storage_id']); - } -} diff --git a/tests/lib/Repair/NC29/SanitizeAccountPropertiesJobTest.php b/tests/lib/Repair/NC29/SanitizeAccountPropertiesJobTest.php new file mode 100644 index 00000000000..2a4f6e9ecf1 --- /dev/null +++ b/tests/lib/Repair/NC29/SanitizeAccountPropertiesJobTest.php @@ -0,0 +1,116 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Repair\NC29; + +use InvalidArgumentException; +use OCP\Accounts\IAccount; +use OCP\Accounts\IAccountManager; +use OCP\Accounts\IAccountProperty; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IUser; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use Test\TestCase; + +class SanitizeAccountPropertiesJobTest extends TestCase { + + private IUserManager&MockObject $userManager; + private IAccountManager&MockObject $accountManager; + private LoggerInterface&MockObject $logger; + + private SanitizeAccountPropertiesJob $job; + + protected function setUp(): void { + $this->userManager = $this->createMock(IUserManager::class); + $this->accountManager = $this->createMock(IAccountManager::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->job = new SanitizeAccountPropertiesJob( + $this->createMock(ITimeFactory::class), + $this->userManager, + $this->accountManager, + $this->logger, + ); + } + + public function testParallel() { + self::assertFalse($this->job->getAllowParallelRuns()); + } + + public function testRun(): void { + $users = [ + $this->createMock(IUser::class), + $this->createMock(IUser::class), + $this->createMock(IUser::class), + ]; + $this->userManager + ->expects(self::once()) + ->method('callForSeenUsers') + ->willReturnCallback(fn ($fn) => array_map($fn, $users)); + + $property = $this->createMock(IAccountProperty::class); + $property->expects(self::once())->method('getName')->willReturn(IAccountManager::PROPERTY_PHONE); + $property->expects(self::once())->method('getScope')->willReturn(IAccountManager::SCOPE_LOCAL); + + $account1 = $this->createMock(IAccount::class); + $account1->expects(self::once()) + ->method('getProperty') + ->with(IAccountManager::PROPERTY_PHONE) + ->willReturn($property); + $account1->expects(self::once()) + ->method('setProperty') + ->with(IAccountManager::PROPERTY_PHONE, '', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED); + $account1->expects(self::once()) + ->method('jsonSerialize') + ->willReturn([ + IAccountManager::PROPERTY_DISPLAYNAME => [], + IAccountManager::PROPERTY_PHONE => [], + ]); + + $account2 = $this->createMock(IAccount::class); + $account2->expects(self::never()) + ->method('getProperty'); + $account2->expects(self::once()) + ->method('jsonSerialize') + ->willReturn([ + IAccountManager::PROPERTY_DISPLAYNAME => [], + IAccountManager::PROPERTY_PHONE => [], + ]); + + $account3 = $this->createMock(IAccount::class); + $account3->expects(self::never()) + ->method('getProperty'); + $account3->expects(self::once()) + ->method('jsonSerialize') + ->willReturn([ + IAccountManager::PROPERTY_DISPLAYNAME => [], + ]); + + $this->accountManager + ->expects(self::exactly(3)) + ->method('getAccount') + ->willReturnMap([ + [$users[0], $account1], + [$users[1], $account2], + [$users[2], $account3], + ]); + $valid = false; + $this->accountManager->expects(self::exactly(3)) + ->method('updateAccount') + ->willReturnCallback(function (IAccount $account) use (&$account1, &$valid): void { + if (!$valid && $account === $account1) { + $valid = true; + throw new InvalidArgumentException(IAccountManager::PROPERTY_PHONE); + } + }); + + self::invokePrivate($this->job, 'run', [null]); + } +} diff --git a/tests/lib/Repair/NC29/SanitizeAccountPropertiesTest.php b/tests/lib/Repair/NC29/SanitizeAccountPropertiesTest.php new file mode 100644 index 00000000000..d0d33eb2817 --- /dev/null +++ b/tests/lib/Repair/NC29/SanitizeAccountPropertiesTest.php @@ -0,0 +1,43 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Repair\NC29; + +use OCP\BackgroundJob\IJobList; +use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class SanitizeAccountPropertiesTest extends TestCase { + + private IJobList&MockObject $jobList; + private SanitizeAccountProperties $repairStep; + + protected function setUp(): void { + $this->jobList = $this->createMock(IJobList::class); + + $this->repairStep = new SanitizeAccountProperties($this->jobList); + } + + public function testGetName(): void { + self::assertStringContainsString('Validate account properties', $this->repairStep->getName()); + } + + public function testRun(): void { + $this->jobList->expects(self::once()) + ->method('add') + ->with(SanitizeAccountPropertiesJob::class, null); + + $output = $this->createMock(IOutput::class); + $output->expects(self::once()) + ->method('info') + ->with(self::matchesRegularExpression('/queued background/i')); + + $this->repairStep->run($output); + } +} diff --git a/tests/lib/Repair/OldGroupMembershipSharesTest.php b/tests/lib/Repair/OldGroupMembershipSharesTest.php index 4678bd174eb..099290b18ed 100644 --- a/tests/lib/Repair/OldGroupMembershipSharesTest.php +++ b/tests/lib/Repair/OldGroupMembershipSharesTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,8 +9,12 @@ namespace Test\Repair; use OC\Repair\OldGroupMembershipShares; +use OCP\IDBConnection; +use OCP\IGroupManager; use OCP\Migration\IOutput; +use OCP\Server; use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; /** * Class OldGroupMembershipSharesTest @@ -19,23 +24,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 = Server::get(IDBConnection::class); $this->deleteAllShares(); } @@ -48,10 +47,10 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } - public function testRun() { + public function testRun(): void { $repair = new OldGroupMembershipShares( $this->connection, $this->groupManager @@ -76,7 +75,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 +91,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 +126,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..fc88ee5d226 --- /dev/null +++ b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php @@ -0,0 +1,237 @@ +<?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')->willReturnOnConsecutiveCalls(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): void { + $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')->willReturnOnConsecutiveCalls(100, 101); + + $this->jobList->expects($this->never()) + ->method('add'); + + $loggerCalls = []; + $this->logger->expects($this->exactly(2)) + ->method('info') + ->willReturnCallback(function () use (&$loggerCalls): void { + $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): void { + $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): void { + $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')->willReturnOnConsecutiveCalls(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): void { + $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..e5a4441a4fa --- /dev/null +++ b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php @@ -0,0 +1,110 @@ +<?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') + ->willReturnCallback(function (\Closure $function) use (&$user1, $user2): void { + $function($user1); + $function($user2); + }); + + $jobListCalls = []; + $this->jobList->expects($this->exactly(2)) + ->method('add') + ->willReturnCallback(function () use (&$jobListCalls): void { + $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..a3eb163b0d6 --- /dev/null +++ b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php @@ -0,0 +1,156 @@ +<?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 OCP\Server; +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 = 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): void { + $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 55bda8337c9..3c51325562d 100644 --- a/tests/lib/Repair/RepairCollationTest.php +++ b/tests/lib/Repair/RepairCollationTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,10 +8,13 @@ namespace Test\Repair; -use Doctrine\DBAL\Platforms\MySqlPlatform; +use OC\DB\ConnectionAdapter; use OC\Repair\Collation; +use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -32,57 +36,42 @@ class TestCollationRepair extends Collation { * @see \OC\Repair\RepairMimeTypes */ class RepairCollationTest extends TestCase { - /** - * @var TestCollationRepair - */ - private $repair; - - /** - * @var IDBConnection - */ - private $connection; - - /** - * @var string - */ - private $tableName; - /** - * @var \OCP\IConfig - */ - private $config; + private TestCollationRepair $repair; + private ConnectionAdapter $connection; + private string $tableName; + private IConfig $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(); - if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { - $this->markTestSkipped("Test only relevant on MySql"); + $this->connection = Server::get(ConnectionAdapter::class); + $this->config = Server::get(IConfig::class); + if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) { + $this->markTestSkipped('Test only relevant on MySql'); } - $dbPrefix = $this->config->getSystemValueString("dbtableprefix"); - $this->tableName = $this->getUniqueID($dbPrefix . "_collation_test"); + $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(); $this->repair = new TestCollationRepair($this->config, $this->logger, $this->connection, false); } protected function tearDown(): void { - $this->connection->getInner()->getSchemaManager()->dropTable($this->tableName); + $this->connection->getInner()->createSchemaManager()->dropTable($this->tableName); parent::tearDown(); } - public function testCollationConvert() { + public function testCollationConvert(): void { $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 9831365e974..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(); @@ -52,7 +48,7 @@ class RepairDavSharesTest extends TestCase { ); } - public function testRun() { + public function testRun(): void { $this->config->expects($this->any()) ->method('getSystemValueString') ->with('version', '0.0.0') @@ -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 eb0e46c61db..72103976da5 100644 --- a/tests/lib/Repair/RepairInvalidSharesTest.php +++ b/tests/lib/Repair/RepairInvalidSharesTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,9 +9,11 @@ namespace Test\Repair; use OC\Repair\RepairInvalidShares; +use OCP\Constants; use OCP\IConfig; +use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; +use OCP\Server; use OCP\Share\IShare; use Test\TestCase; @@ -22,11 +25,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 +40,9 @@ class RepairInvalidSharesTest extends TestCase { ->with('version') ->willReturn('12.0.0.0'); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); $this->deleteAllShares(); - /** @var \OCP\IConfig $config */ $this->repair = new RepairInvalidShares($config, $this->connection); } @@ -54,13 +54,13 @@ class RepairInvalidSharesTest extends TestCase { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } /** * Test remove shares where the parent share does not exist anymore */ - public function testSharesNonExistingParent() { + public function testSharesNonExistingParent(): void { $qb = $this->connection->getQueryBuilder(); $shareValues = [ 'share_type' => $qb->expr()->literal(IShare::TYPE_USER), @@ -80,30 +80,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,13 +119,13 @@ 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(); } - public function fileSharePermissionsProvider() { + public static function fileSharePermissionsProvider(): array { return [ // unchanged for folder [ @@ -136,24 +136,23 @@ class RepairInvalidSharesTest extends TestCase { // unchanged for read-write + share [ 'file', - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE, + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, ], // fixed for all perms [ 'file', - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_SHARE, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE, + Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE | Constants::PERMISSION_SHARE, + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, ], ]; } /** * Test adjusting file share permissions - * - * @dataProvider fileSharePermissionsProvider */ - public function testFileSharePermissions($itemType, $testPerms, $expectedPerms) { + #[\PHPUnit\Framework\Attributes\DataProvider('fileSharePermissionsProvider')] + public function testFileSharePermissions($itemType, $testPerms, $expectedPerms): void { $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values([ @@ -167,9 +166,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 +179,7 @@ class RepairInvalidSharesTest extends TestCase { ->select('*') ->from('share') ->orderBy('permissions', 'ASC') - ->execute() + ->executeQuery() ->fetchAll(); $this->assertCount(1, $results); @@ -191,11 +188,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 b7076b32e77..0261b56ebe9 100644 --- a/tests/lib/Repair/RepairMimeTypesTest.php +++ b/tests/lib/Repair/RepairMimeTypesTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,10 +9,13 @@ namespace Test\Repair; use OC\Files\Storage\Temporary; +use OC\Repair\RepairMimeTypes; use OCP\Files\IMimeTypeLoader; +use OCP\IAppConfig; use OCP\IConfig; +use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; +use OCP\Server; /** * Tests for the converting of legacy storages to home storages. @@ -21,41 +25,49 @@ 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 = Server::get(IMimeTypeLoader::class); + $this->db = Server::get(IDBConnection::class); - /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject $config */ $config = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); - $config->expects($this->any()) - ->method('getSystemValueString') + $config->method('getSystemValueString') ->with('version') ->willReturn('11.0.0.0'); - $this->storage = new \OC\Files\Storage\Temporary([]); + $appConfig = $this->getMockBuilder(IAppConfig::class) + ->disableOriginalConstructor() + ->getMock(); + $appConfig->method('getValueString') + ->with('files', 'mimetype_version') + ->willReturn('11.0.0.0'); + + $this->storage = new Temporary([]); + $this->storage->getScanner()->scan(''); - $this->repair = new \OC\Repair\RepairMimeTypes($config, \OC::$server->getDatabaseConnection()); + $this->repair = new RepairMimeTypes( + $config, + $appConfig, + Server::get(IDBConnection::class), + ); } 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(); @@ -63,9 +75,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(); } @@ -111,7 +123,7 @@ class RepairMimeTypesTest extends \Test\TestCase { /** * Test renaming the additional image mime types */ - public function testRenameImageTypes() { + public function testRenameImageTypes(): void { $currentMimeTypes = [ ['test.jp2', 'application/octet-stream'], ['test.webp', 'application/octet-stream'], @@ -128,7 +140,7 @@ class RepairMimeTypesTest extends \Test\TestCase { /** * Test renaming the richdocuments additional office mime types */ - public function testRenameWindowsProgramTypes() { + public function testRenameWindowsProgramTypes(): void { $currentMimeTypes = [ ['test.htaccess', 'application/octet-stream'], ['.htaccess', 'application/octet-stream'], @@ -150,7 +162,7 @@ class RepairMimeTypesTest extends \Test\TestCase { * Test that nothing happens and no error happens when all mimetypes are * already correct and no old ones exist.. */ - public function testDoNothingWhenOnlyNewFiles() { + public function testDoNothingWhenOnlyNewFiles(): void { $currentMimeTypes = [ ['test.doc', 'application/msword'], ['test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'], @@ -265,7 +277,7 @@ class RepairMimeTypesTest extends \Test\TestCase { /** * Test that mime type renaming does not affect folders */ - public function testDoNotChangeFolderMimeType() { + public function testDoNotChangeFolderMimeType(): void { $currentMimeTypes = [ ['test.conf', 'httpd/unix-directory'], ['test.cnf', 'httpd/unix-directory'], |