diff options
Diffstat (limited to 'apps/files/tests')
-rw-r--r-- | apps/files/tests/Activity/Filter/GenericTest.php | 68 | ||||
-rw-r--r-- | apps/files/tests/Activity/ProviderTest.php | 54 | ||||
-rw-r--r-- | apps/files/tests/Activity/Setting/GenericTest.php | 77 | ||||
-rw-r--r-- | apps/files/tests/AdvancedCapabilitiesTest.php | 46 | ||||
-rw-r--r-- | apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php | 54 | ||||
-rw-r--r-- | apps/files/tests/BackgroundJob/ScanFilesTest.php | 21 | ||||
-rw-r--r-- | apps/files/tests/Command/DeleteOrphanedFilesTest.php | 32 | ||||
-rw-r--r-- | apps/files/tests/Controller/ApiControllerTest.php | 85 | ||||
-rw-r--r-- | apps/files/tests/Controller/ConversionApiControllerTest.php | 7 | ||||
-rw-r--r-- | apps/files/tests/Controller/ViewControllerTest.php | 176 | ||||
-rw-r--r-- | apps/files/tests/HelperTest.php | 52 | ||||
-rw-r--r-- | apps/files/tests/Service/TagServiceTest.php | 58 |
12 files changed, 378 insertions, 352 deletions
diff --git a/apps/files/tests/Activity/Filter/GenericTest.php b/apps/files/tests/Activity/Filter/GenericTest.php index 347bea0f643..40e2f9848b5 100644 --- a/apps/files/tests/Activity/Filter/GenericTest.php +++ b/apps/files/tests/Activity/Filter/GenericTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -8,6 +10,7 @@ namespace OCA\Files\Tests\Activity\Filter; use OCA\Files\Activity\Filter\Favorites; use OCA\Files\Activity\Filter\FileChanges; use OCP\Activity\IFilter; +use OCP\Server; use Test\TestCase; /** @@ -17,83 +20,62 @@ use Test\TestCase; * @group DB */ class GenericTest extends TestCase { - public function dataFilters() { + public static function dataFilters(): array { return [ [Favorites::class], [FileChanges::class], ]; } - /** - * @dataProvider dataFilters - * @param string $filterClass - */ - public function testImplementsInterface($filterClass): void { - $filter = \OC::$server->query($filterClass); + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilters')] + public function testImplementsInterface(string $filterClass): void { + $filter = Server::get($filterClass); $this->assertInstanceOf(IFilter::class, $filter); } - /** - * @dataProvider dataFilters - * @param string $filterClass - */ - public function testGetIdentifier($filterClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilters')] + public function testGetIdentifier(string $filterClass): void { /** @var IFilter $filter */ - $filter = \OC::$server->query($filterClass); + $filter = Server::get($filterClass); $this->assertIsString($filter->getIdentifier()); } - /** - * @dataProvider dataFilters - * @param string $filterClass - */ - public function testGetName($filterClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilters')] + public function testGetName(string $filterClass): void { /** @var IFilter $filter */ - $filter = \OC::$server->query($filterClass); + $filter = Server::get($filterClass); $this->assertIsString($filter->getName()); } - /** - * @dataProvider dataFilters - * @param string $filterClass - */ - public function testGetPriority($filterClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilters')] + public function testGetPriority(string $filterClass): void { /** @var IFilter $filter */ - $filter = \OC::$server->query($filterClass); + $filter = Server::get($filterClass); $priority = $filter->getPriority(); $this->assertIsInt($filter->getPriority()); $this->assertGreaterThanOrEqual(0, $priority); $this->assertLessThanOrEqual(100, $priority); } - /** - * @dataProvider dataFilters - * @param string $filterClass - */ - public function testGetIcon($filterClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilters')] + public function testGetIcon(string $filterClass): void { /** @var IFilter $filter */ - $filter = \OC::$server->query($filterClass); + $filter = Server::get($filterClass); $this->assertIsString($filter->getIcon()); $this->assertStringStartsWith('http', $filter->getIcon()); } - /** - * @dataProvider dataFilters - * @param string $filterClass - */ - public function testFilterTypes($filterClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilters')] + public function testFilterTypes(string $filterClass): void { /** @var IFilter $filter */ - $filter = \OC::$server->query($filterClass); + $filter = Server::get($filterClass); $this->assertIsArray($filter->filterTypes([])); } - /** - * @dataProvider dataFilters - * @param string $filterClass - */ - public function testAllowedApps($filterClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilters')] + public function testAllowedApps(string $filterClass): void { /** @var IFilter $filter */ - $filter = \OC::$server->query($filterClass); + $filter = Server::get($filterClass); $this->assertIsArray($filter->allowedApps()); } } diff --git a/apps/files/tests/Activity/ProviderTest.php b/apps/files/tests/Activity/ProviderTest.php index 7803563a11c..b6ba095ecfe 100644 --- a/apps/files/tests/Activity/ProviderTest.php +++ b/apps/files/tests/Activity/ProviderTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -26,23 +28,14 @@ use Test\TestCase; * @package OCA\Files\Tests\Activity */ class ProviderTest extends TestCase { - - /** @var IFactory|MockObject */ - protected $l10nFactory; - /** @var IURLGenerator|MockObject */ - protected $url; - /** @var IManager|MockObject */ - protected $activityManager; - /** @var IUserManager|MockObject */ - protected $userManager; - /** @var IRootFolder|MockObject */ - protected $rootFolder; - /** @var ICloudIdManager|MockObject */ - protected $cloudIdManager; - /** @var IContactsManager|MockObject */ - protected $contactsManager; - /** @var IEventMerger|MockObject */ - protected $eventMerger; + protected IFactory&MockObject $l10nFactory; + protected IURLGenerator&MockObject $url; + protected IManager&MockObject $activityManager; + protected IUserManager&MockObject $userManager; + protected IRootFolder&MockObject $rootFolder; + protected ICloudIdManager&MockObject $cloudIdManager; + protected IContactsManager&MockObject $contactsManager; + protected IEventMerger&MockObject $eventMerger; protected function setUp(): void { parent::setUp(); @@ -74,7 +67,7 @@ class ProviderTest extends TestCase { $this->contactsManager, $this->eventMerger, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } return new Provider( @@ -89,23 +82,16 @@ class ProviderTest extends TestCase { ); } - public function dataGetFile() { + public static function dataGetFile(): array { return [ [[42 => '/FortyTwo.txt'], null, '42', 'FortyTwo.txt', 'FortyTwo.txt'], [['23' => '/Twenty/Three.txt'], null, '23', 'Three.txt', 'Twenty/Three.txt'], - ['/Foo/Bar.txt', 128, 128, 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before + ['/Foo/Bar.txt', 128, '128', 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before ]; } - /** - * @dataProvider dataGetFile - * @param mixed $parameter - * @param mixed $eventId - * @param int $id - * @param string $name - * @param string $path - */ - public function testGetFile($parameter, $eventId, $id, $name, $path): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetFile')] + public function testGetFile(array|string $parameter, ?int $eventId, string $id, string $name, string $path): void { $provider = $this->getProvider(); if ($eventId !== null) { @@ -139,7 +125,7 @@ class ProviderTest extends TestCase { self::invokePrivate($provider, 'getFile', ['/Foo/Bar.txt', null]); } - public function dataGetUser() { + public static function dataGetUser(): array { return [ ['test', 'Test user', null, ['type' => 'user', 'id' => 'test', 'name' => 'Test user']], ['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => null], ['type' => 'user', 'id' => 'test', 'name' => 'test@localhost', 'server' => 'localhost']], @@ -148,13 +134,7 @@ class ProviderTest extends TestCase { ]; } - /** - * @dataProvider dataGetUser - * @param string $uid - * @param string|null $userDisplayName - * @param array|null $cloudIdData - * @param array $expected - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetUser')] public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloudIdData, array $expected): void { $provider = $this->getProvider(); diff --git a/apps/files/tests/Activity/Setting/GenericTest.php b/apps/files/tests/Activity/Setting/GenericTest.php index 1cce876cbcf..df6b1e0f6d4 100644 --- a/apps/files/tests/Activity/Setting/GenericTest.php +++ b/apps/files/tests/Activity/Setting/GenericTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -8,10 +10,11 @@ namespace OCA\Files\Tests\Activity\Setting; use OCA\Files\Activity\Settings\FavoriteAction; use OCA\Files\Activity\Settings\FileChanged; use OCP\Activity\ISetting; +use OCP\Server; use Test\TestCase; class GenericTest extends TestCase { - public function dataSettings() { + public static function dataSettings(): array { return [ [FavoriteAction::class], [FileChanged::class], @@ -19,85 +22,61 @@ class GenericTest extends TestCase { ]; } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testImplementsInterface($settingClass): void { - $setting = \OC::$server->query($settingClass); + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testImplementsInterface(string $settingClass): void { + $setting = Server::get($settingClass); $this->assertInstanceOf(ISetting::class, $setting); } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testGetIdentifier($settingClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testGetIdentifier(string $settingClass): void { /** @var ISetting $setting */ - $setting = \OC::$server->query($settingClass); + $setting = Server::get($settingClass); $this->assertIsString($setting->getIdentifier()); } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testGetName($settingClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testGetName(string $settingClass): void { /** @var ISetting $setting */ - $setting = \OC::$server->query($settingClass); + $setting = Server::get($settingClass); $this->assertIsString($setting->getName()); } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testGetPriority($settingClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testGetPriority(string $settingClass): void { /** @var ISetting $setting */ - $setting = \OC::$server->query($settingClass); + $setting = Server::get($settingClass); $priority = $setting->getPriority(); $this->assertIsInt($setting->getPriority()); $this->assertGreaterThanOrEqual(0, $priority); $this->assertLessThanOrEqual(100, $priority); } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testCanChangeStream($settingClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testCanChangeStream(string $settingClass): void { /** @var ISetting $setting */ - $setting = \OC::$server->query($settingClass); + $setting = Server::get($settingClass); $this->assertIsBool($setting->canChangeStream()); } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testIsDefaultEnabledStream($settingClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testIsDefaultEnabledStream(string $settingClass): void { /** @var ISetting $setting */ - $setting = \OC::$server->query($settingClass); + $setting = Server::get($settingClass); $this->assertIsBool($setting->isDefaultEnabledStream()); } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testCanChangeMail($settingClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testCanChangeMail(string $settingClass): void { /** @var ISetting $setting */ - $setting = \OC::$server->query($settingClass); + $setting = Server::get($settingClass); $this->assertIsBool($setting->canChangeMail()); } - /** - * @dataProvider dataSettings - * @param string $settingClass - */ - public function testIsDefaultEnabledMail($settingClass): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSettings')] + public function testIsDefaultEnabledMail(string $settingClass): void { /** @var ISetting $setting */ - $setting = \OC::$server->query($settingClass); + $setting = Server::get($settingClass); $this->assertIsBool($setting->isDefaultEnabledMail()); } } diff --git a/apps/files/tests/AdvancedCapabilitiesTest.php b/apps/files/tests/AdvancedCapabilitiesTest.php new file mode 100644 index 00000000000..f39ac1c873f --- /dev/null +++ b/apps/files/tests/AdvancedCapabilitiesTest.php @@ -0,0 +1,46 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\Files; + +use OCA\Files\Service\SettingsService; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class AdvancedCapabilitiesTest extends TestCase { + + protected SettingsService&MockObject $service; + protected AdvancedCapabilities $capabilities; + + protected function setUp(): void { + parent::setUp(); + $this->service = $this->createMock(SettingsService::class); + $this->capabilities = new AdvancedCapabilities($this->service); + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetCapabilities')] + public function testGetCapabilities(bool $wcf): void { + $this->service + ->expects(self::once()) + ->method('hasFilesWindowsSupport') + ->willReturn($wcf); + + self::assertEqualsCanonicalizing(['files' => [ 'windows_compatible_filenames' => $wcf ]], $this->capabilities->getCapabilities()); + } + + public static function dataGetCapabilities(): array { + return [ + 'WCF enabled' => [ + true, + ], + 'WCF disabled' => [ + false, + ], + ]; + } +} diff --git a/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php b/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php index 1ce2d7bd8f7..3f811fca407 100644 --- a/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php +++ b/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -11,6 +12,7 @@ use OCA\Files\BackgroundJob\DeleteOrphanedItems; use OCP\AppFramework\Utility\ITimeFactory; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\Server; use Psr\Log\LoggerInterface; /** @@ -27,21 +29,21 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->get(IDBConnection::class); + $this->connection = Server::get(IDBConnection::class); $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->logger = \OC::$server->get(LoggerInterface::class); + $this->logger = Server::get(LoggerInterface::class); } - protected function cleanMapping($table) { + protected function cleanMapping(string $table): void { $query = $this->connection->getQueryBuilder(); - $query->delete($table)->execute(); + $query->delete($table)->executeStatement(); } - protected function getMappings($table) { + protected function getMappings(string $table): array { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from($table); - $result = $query->execute(); + $result = $query->executeQuery(); $mapping = $result->fetchAll(); $result->closeCursor(); @@ -60,7 +62,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); + ])->executeStatement(); $fileId = $query->getLastInsertId(); // Existing file @@ -70,7 +72,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'objectid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), 'objecttype' => $query->createNamedParameter('files'), 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); + ])->executeStatement(); // Non-existing file $query = $this->connection->getQueryBuilder(); @@ -79,13 +81,13 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'objectid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), 'objecttype' => $query->createNamedParameter('files'), 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); + ])->executeStatement(); $mapping = $this->getMappings('systemtag_object_mapping'); $this->assertCount(2, $mapping); $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger); - $this->invokePrivate($job, 'cleanSystemTags'); + self::invokePrivate($job, 'cleanSystemTags'); $mapping = $this->getMappings('systemtag_object_mapping'); $this->assertCount(1, $mapping); @@ -93,7 +95,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { $query = $this->connection->getQueryBuilder(); $query->delete('filecache') ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); + ->executeStatement(); $this->cleanMapping('systemtag_object_mapping'); } @@ -109,7 +111,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); + ])->executeStatement(); $fileId = $query->getLastInsertId(); // Existing file @@ -119,7 +121,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'objid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), 'type' => $query->createNamedParameter('files'), 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); + ])->executeStatement(); // Non-existing file $query = $this->connection->getQueryBuilder(); @@ -128,13 +130,13 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'objid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), 'type' => $query->createNamedParameter('files'), 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); + ])->executeStatement(); $mapping = $this->getMappings('vcategory_to_object'); $this->assertCount(2, $mapping); $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger); - $this->invokePrivate($job, 'cleanUserTags'); + self::invokePrivate($job, 'cleanUserTags'); $mapping = $this->getMappings('vcategory_to_object'); $this->assertCount(1, $mapping); @@ -142,7 +144,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { $query = $this->connection->getQueryBuilder(); $query->delete('filecache') ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); + ->executeStatement(); $this->cleanMapping('vcategory_to_object'); } @@ -158,7 +160,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); + ])->executeStatement(); $fileId = $query->getLastInsertId(); // Existing file @@ -169,7 +171,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'object_type' => $query->createNamedParameter('files'), 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), 'actor_type' => $query->createNamedParameter('users'), - ])->execute(); + ])->executeStatement(); // Non-existing file $query = $this->connection->getQueryBuilder(); @@ -179,13 +181,13 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'object_type' => $query->createNamedParameter('files'), 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), 'actor_type' => $query->createNamedParameter('users'), - ])->execute(); + ])->executeStatement(); $mapping = $this->getMappings('comments'); $this->assertCount(2, $mapping); $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger); - $this->invokePrivate($job, 'cleanComments'); + self::invokePrivate($job, 'cleanComments'); $mapping = $this->getMappings('comments'); $this->assertCount(1, $mapping); @@ -193,7 +195,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { $query = $this->connection->getQueryBuilder(); $query->delete('filecache') ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); + ->executeStatement(); $this->cleanMapping('comments'); } @@ -209,7 +211,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); + ])->executeStatement(); $fileId = $query->getLastInsertId(); // Existing file @@ -219,7 +221,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), 'object_type' => $query->createNamedParameter('files'), 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), - ])->execute(); + ])->executeStatement(); // Non-existing file $query = $this->connection->getQueryBuilder(); @@ -228,13 +230,13 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), 'object_type' => $query->createNamedParameter('files'), 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), - ])->execute(); + ])->executeStatement(); $mapping = $this->getMappings('comments_read_markers'); $this->assertCount(2, $mapping); $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger); - $this->invokePrivate($job, 'cleanCommentMarkers'); + self::invokePrivate($job, 'cleanCommentMarkers'); $mapping = $this->getMappings('comments_read_markers'); $this->assertCount(1, $mapping); @@ -242,7 +244,7 @@ class DeleteOrphanedItemsJobTest extends \Test\TestCase { $query = $this->connection->getQueryBuilder(); $query->delete('filecache') ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); + ->executeStatement(); $this->cleanMapping('comments_read_markers'); } } diff --git a/apps/files/tests/BackgroundJob/ScanFilesTest.php b/apps/files/tests/BackgroundJob/ScanFilesTest.php index c535ddb808c..00d9ed823f9 100644 --- a/apps/files/tests/BackgroundJob/ScanFilesTest.php +++ b/apps/files/tests/BackgroundJob/ScanFilesTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -14,7 +15,9 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Config\IUserMountCache; use OCP\IConfig; +use OCP\IDBConnection; use OCP\IUser; +use OCP\Server; use Psr\Log\LoggerInterface; use Test\TestCase; use Test\Traits\MountProviderTrait; @@ -30,10 +33,8 @@ class ScanFilesTest extends TestCase { use UserTrait; use MountProviderTrait; - /** @var ScanFiles */ - private $scanFiles; - /** @var IUserMountCache */ - private $mountCache; + private ScanFiles $scanFiles; + private IUserMountCache $mountCache; protected function setUp(): void { parent::setUp(); @@ -41,10 +42,10 @@ class ScanFilesTest extends TestCase { $config = $this->createMock(IConfig::class); $dispatcher = $this->createMock(IEventDispatcher::class); $logger = $this->createMock(LoggerInterface::class); - $connection = \OC::$server->getDatabaseConnection(); - $this->mountCache = \OC::$server->getUserMountCache(); + $connection = Server::get(IDBConnection::class); + $this->mountCache = Server::get(IUserMountCache::class); - $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles') + $this->scanFiles = $this->getMockBuilder(ScanFiles::class) ->setConstructorArgs([ $config, $dispatcher, @@ -52,12 +53,12 @@ class ScanFilesTest extends TestCase { $connection, $this->createMock(ITimeFactory::class) ]) - ->setMethods(['runScanner']) + ->onlyMethods(['runScanner']) ->getMock(); } - private function runJob() { - $this->invokePrivate($this->scanFiles, 'run', [[]]); + private function runJob(): void { + self::invokePrivate($this->scanFiles, 'run', [[]]); } private function getUser(string $userId): IUser { diff --git a/apps/files/tests/Command/DeleteOrphanedFilesTest.php b/apps/files/tests/Command/DeleteOrphanedFilesTest.php index e480f7b632e..a488915e0cb 100644 --- a/apps/files/tests/Command/DeleteOrphanedFilesTest.php +++ b/apps/files/tests/Command/DeleteOrphanedFilesTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -12,6 +13,7 @@ use OCA\Files\Command\DeleteOrphanedFiles; use OCP\Files\IRootFolder; use OCP\Files\StorageNotAvailableException; use OCP\IDBConnection; +use OCP\IUserManager; use OCP\Server; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -37,14 +39,14 @@ class DeleteOrphanedFilesTest extends TestCase { $this->user1 = $this->getUniqueID('user1_'); - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); $userManager->createUser($this->user1, 'pass'); $this->command = new DeleteOrphanedFiles($this->connection); } protected function tearDown(): void { - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); $user1 = $userManager->get($this->user1); if ($user1) { $user1->delete(); @@ -55,7 +57,7 @@ class DeleteOrphanedFilesTest extends TestCase { parent::tearDown(); } - protected function getFile($fileId) { + protected function getFile(int $fileId): array { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from('filecache') @@ -63,7 +65,7 @@ class DeleteOrphanedFilesTest extends TestCase { return $query->executeQuery()->fetchAll(); } - protected function getMounts($storageId) { + protected function getMounts(int $storageId): array { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from('mounts') @@ -75,12 +77,8 @@ class DeleteOrphanedFilesTest extends TestCase { * Test clearing orphaned files */ public function testClearFiles(): void { - $input = $this->getMockBuilder(InputInterface::class) - ->disableOriginalConstructor() - ->getMock(); - $output = $this->getMockBuilder(OutputInterface::class) - ->disableOriginalConstructor() - ->getMock(); + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); $rootFolder = Server::get(IRootFolder::class); @@ -111,14 +109,18 @@ class DeleteOrphanedFilesTest extends TestCase { $this->assertSame(1, $deletedRows, 'Asserts that storage got deleted'); // parent folder, `files`, ´test` and `welcome.txt` => 4 elements + $calls = [ + '3 orphaned file cache entries deleted', + '0 orphaned file cache extended entries deleted', + '1 orphaned mount entries deleted', + ]; $output ->expects($this->exactly(3)) ->method('writeln') - ->withConsecutive( - ['3 orphaned file cache entries deleted'], - ['0 orphaned file cache extended entries deleted'], - ['1 orphaned mount entries deleted'], - ); + ->willReturnCallback(function (string $message) use (&$calls): void { + $expected = array_shift($calls); + $this->assertSame($expected, $message); + }); $this->command->execute($input, $output); diff --git a/apps/files/tests/Controller/ApiControllerTest.php b/apps/files/tests/Controller/ApiControllerTest.php index 429d3c06f66..e74989eb2f5 100644 --- a/apps/files/tests/Controller/ApiControllerTest.php +++ b/apps/files/tests/Controller/ApiControllerTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -28,9 +29,9 @@ use OCP\IPreview; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; -use OCP\Share\IAttributes; use OCP\Share\IManager; use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -40,41 +41,25 @@ use Test\TestCase; * @package OCA\Files\Controller */ class ApiControllerTest extends TestCase { - /** @var string */ - private $appName = 'files'; - /** @var IUser */ - private $user; - /** @var IRequest */ - private $request; - /** @var TagService */ - private $tagService; - /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */ - private $preview; - /** @var ApiController */ - private $apiController; - /** @var \OCP\Share\IManager */ - private $shareManager; - /** @var IConfig */ - private $config; - /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */ - private $userFolder; - /** @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $userConfig; - /** @var ViewConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $viewConfig; - /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ - private $l10n; - /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */ - private $rootFolder; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $logger; + private string $appName = 'files'; + private IUser $user; + private IRequest $request; + private TagService $tagService; + private IPreview&MockObject $preview; + private ApiController $apiController; + private IManager $shareManager; + private IConfig $config; + private Folder&MockObject $userFolder; + private UserConfig&MockObject $userConfig; + private ViewConfig&MockObject $viewConfig; + private IL10N&MockObject $l10n; + private IRootFolder&MockObject $rootFolder; + private LoggerInterface&MockObject $logger; protected function setUp(): void { parent::setUp(); - $this->request = $this->getMockBuilder(IRequest::class) - ->disableOriginalConstructor() - ->getMock(); + $this->request = $this->createMock(IRequest::class); $this->user = $this->createMock(IUser::class); $this->user->expects($this->any()) ->method('getUID') @@ -83,19 +68,11 @@ class ApiControllerTest extends TestCase { $userSession->expects($this->any()) ->method('getUser') ->willReturn($this->user); - $this->tagService = $this->getMockBuilder(TagService::class) - ->disableOriginalConstructor() - ->getMock(); - $this->shareManager = $this->getMockBuilder(IManager::class) - ->disableOriginalConstructor() - ->getMock(); - $this->preview = $this->getMockBuilder(IPreview::class) - ->disableOriginalConstructor() - ->getMock(); + $this->tagService = $this->createMock(TagService::class); + $this->shareManager = $this->createMock(IManager::class); + $this->preview = $this->createMock(IPreview::class); $this->config = $this->createMock(IConfig::class); - $this->userFolder = $this->getMockBuilder(Folder::class) - ->disableOriginalConstructor() - ->getMock(); + $this->userFolder = $this->createMock(Folder::class); $this->userConfig = $this->createMock(UserConfig::class); $this->viewConfig = $this->createMock(ViewConfig::class); $this->l10n = $this->createMock(IL10N::class); @@ -142,7 +119,7 @@ class ApiControllerTest extends TestCase { $this->tagService->expects($this->once()) ->method('updateFileTags') ->with('/path.txt', ['Tag1', 'Tag2']) - ->will($this->throwException(new NotFoundException('My error message'))); + ->willThrowException(new NotFoundException('My error message')); $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND); $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); @@ -152,7 +129,7 @@ class ApiControllerTest extends TestCase { $this->tagService->expects($this->once()) ->method('updateFileTags') ->with('/path.txt', ['Tag1', 'Tag2']) - ->will($this->throwException(new StorageNotAvailableException('My error message'))); + ->willThrowException(new StorageNotAvailableException('My error message')); $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE); $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); @@ -162,7 +139,7 @@ class ApiControllerTest extends TestCase { $this->tagService->expects($this->once()) ->method('updateFileTags') ->with('/path.txt', ['Tag1', 'Tag2']) - ->will($this->throwException(new \Exception('My error message'))); + ->willThrowException(new \Exception('My error message')); $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND); $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); @@ -205,16 +182,10 @@ class ApiControllerTest extends TestCase { } public function testGetThumbnailSharedNoDownload(): void { - $attributes = $this->createMock(IAttributes::class); - $attributes->expects(self::once()) - ->method('getAttribute') - ->with('permissions', 'download') - ->willReturn(false); - $share = $this->createMock(IShare::class); $share->expects(self::once()) - ->method('getAttributes') - ->willReturn($attributes); + ->method('canSeeContent') + ->willReturn(false); $storage = $this->createMock(ISharedStorage::class); $storage->expects(self::once()) @@ -243,8 +214,8 @@ class ApiControllerTest extends TestCase { public function testGetThumbnailShared(): void { $share = $this->createMock(IShare::class); $share->expects(self::once()) - ->method('getAttributes') - ->willReturn(null); + ->method('canSeeContent') + ->willReturn(true); $storage = $this->createMock(ISharedStorage::class); $storage->expects(self::once()) diff --git a/apps/files/tests/Controller/ConversionApiControllerTest.php b/apps/files/tests/Controller/ConversionApiControllerTest.php index a2f1fccd978..659fbe1a956 100644 --- a/apps/files/tests/Controller/ConversionApiControllerTest.php +++ b/apps/files/tests/Controller/ConversionApiControllerTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -60,12 +61,12 @@ class ConversionApiControllerTest extends TestCase { ); } - public function testThrowsNotFoundException() { + public function testThrowsNotFoundException(): void { $this->expectException(OCSNotFoundException::class); $this->conversionApiController->convert(42, 'image/png'); } - public function testThrowsOcsException() { + public function testThrowsOcsException(): void { $this->userFolder->method('getFirstNodeById')->with(42)->willReturn($this->file); $this->fileConversionManager->method('convert')->willThrowException(new \Exception()); @@ -73,7 +74,7 @@ class ConversionApiControllerTest extends TestCase { $this->conversionApiController->convert(42, 'image/png'); } - public function testConvert() { + public function testConvert(): void { $convertedFileAbsolutePath = $this->user . '/files/test.png'; $this->userFolder->method('getFirstNodeById')->with(42)->willReturn($this->file); diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php index 0c0647cc415..01aa955a13e 100644 --- a/apps/files/tests/Controller/ViewControllerTest.php +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,6 +9,8 @@ namespace OCA\Files\Tests\Controller; use OC\Files\FilenameValidator; +use OC\Route\Router; +use OC\URLGenerator; use OCA\Files\Controller\ViewController; use OCA\Files\Service\UserConfig; use OCA\Files\Service\ViewConfig; @@ -16,11 +19,14 @@ use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; +use OCP\Authentication\TwoFactorAuth\IRegistry; +use OCP\Diagnostics\IEventLogger; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\Template\ITemplateManager; +use OCP\ICacheFactory; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -28,39 +34,55 @@ use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; use Test\TestCase; /** * Class ViewControllerTest * + * @group RoutingWeirdness + * * @package OCA\Files\Tests\Controller */ class ViewControllerTest extends TestCase { - private IRequest&MockObject $request; - private IURLGenerator&MockObject $urlGenerator; - private IL10N&MockObject $l10n; + private ContainerInterface&MockObject $container; + private IAppManager&MockObject $appManager; + private ICacheFactory&MockObject $cacheFactory; private IConfig&MockObject $config; private IEventDispatcher $eventDispatcher; - private IUser&MockObject $user; - private IUserSession&MockObject $userSession; - private IAppManager&MockObject $appManager; - private IRootFolder&MockObject $rootFolder; + private IEventLogger&MockObject $eventLogger; private IInitialState&MockObject $initialState; + private IL10N&MockObject $l10n; + private IRequest&MockObject $request; + private IRootFolder&MockObject $rootFolder; private ITemplateManager&MockObject $templateManager; + private IURLGenerator $urlGenerator; + private IUser&MockObject $user; + private IUserSession&MockObject $userSession; + private LoggerInterface&MockObject $logger; private UserConfig&MockObject $userConfig; private ViewConfig&MockObject $viewConfig; + private Router $router; + private IRegistry&MockObject $twoFactorRegistry; private ViewController&MockObject $viewController; protected function setUp(): void { parent::setUp(); - $this->request = $this->getMockBuilder(IRequest::class)->getMock(); - $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); - $this->l10n = $this->getMockBuilder(IL10N::class)->getMock(); - $this->config = $this->getMockBuilder(IConfig::class)->getMock(); + $this->appManager = $this->createMock(IAppManager::class); + $this->config = $this->createMock(IConfig::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); - $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock(); - $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock(); + $this->initialState = $this->createMock(IInitialState::class); + $this->l10n = $this->createMock(IL10N::class); + $this->request = $this->createMock(IRequest::class); + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->templateManager = $this->createMock(ITemplateManager::class); + $this->userConfig = $this->createMock(UserConfig::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->viewConfig = $this->createMock(ViewConfig::class); + $this->twoFactorRegistry = $this->createMock(IRegistry::class); + $this->user = $this->getMockBuilder(IUser::class)->getMock(); $this->user->expects($this->any()) ->method('getUID') @@ -68,14 +90,41 @@ class ViewControllerTest extends TestCase { $this->userSession->expects($this->any()) ->method('getUser') ->willReturn($this->user); - $this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock(); - $this->initialState = $this->createMock(IInitialState::class); - $this->templateManager = $this->createMock(ITemplateManager::class); - $this->userConfig = $this->createMock(UserConfig::class); - $this->viewConfig = $this->createMock(ViewConfig::class); - $filenameValidator = $this->createMock(FilenameValidator::class); + // Make sure we know the app is enabled + $this->appManager->expects($this->any()) + ->method('cleanAppId') + ->willReturnArgument(0); + $this->appManager->expects($this->any()) + ->method('getAppPath') + ->willReturnCallback(fn (string $appid): string => \OC::$SERVERROOT . '/apps/' . $appid); + $this->appManager->expects($this->any()) + ->method('isAppLoaded') + ->willReturn(true); + + $this->cacheFactory = $this->createMock(ICacheFactory::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->eventLogger = $this->createMock(IEventLogger::class); + $this->container = $this->createMock(ContainerInterface::class); + $this->router = new Router( + $this->logger, + $this->request, + $this->config, + $this->eventLogger, + $this->container, + $this->appManager, + ); + + // Create a real URLGenerator instance to generate URLs + $this->urlGenerator = new URLGenerator( + $this->config, + $this->userSession, + $this->cacheFactory, + $this->request, + $this->router + ); + $filenameValidator = $this->createMock(FilenameValidator::class); $this->viewController = $this->getMockBuilder(ViewController::class) ->setConstructorArgs([ 'files', @@ -92,6 +141,7 @@ class ViewControllerTest extends TestCase { $this->userConfig, $this->viewConfig, $filenameValidator, + $this->twoFactorRegistry, ]) ->onlyMethods([ 'getStorageInfo', @@ -147,19 +197,67 @@ class ViewControllerTest extends TestCase { $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); } - public function testShowFileRouteWithTrashedFile(): void { - $this->appManager->expects($this->once()) + public static function dataTestShortRedirect(): array { + // openfile is true by default + // opendetails is undefined by default + // both will be evaluated as truthy + return [ + [null, null, '/index.php/apps/files/files/123456?openfile=true'], + ['', null, '/index.php/apps/files/files/123456?openfile=true'], + [null, '', '/index.php/apps/files/files/123456?openfile=true&opendetails=true'], + ['', '', '/index.php/apps/files/files/123456?openfile=true&opendetails=true'], + ['false', '', '/index.php/apps/files/files/123456?openfile=false'], + [null, 'false', '/index.php/apps/files/files/123456?openfile=true&opendetails=false'], + ['true', 'false', '/index.php/apps/files/files/123456?openfile=true&opendetails=false'], + ['false', 'true', '/index.php/apps/files/files/123456?openfile=false&opendetails=true'], + ['false', 'false', '/index.php/apps/files/files/123456?openfile=false&opendetails=false'], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShortRedirect')] + public function testShortRedirect(?string $openfile, ?string $opendetails, string $result): void { + $this->appManager->expects($this->any()) ->method('isEnabledForUser') - ->with('files_trashbin') + ->with('files') ->willReturn(true); + $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock(); + $this->rootFolder->expects($this->any()) + ->method('getUserFolder') + ->with('testuser1') + ->willReturn($baseFolderFiles); + $parentNode = $this->getMockBuilder(Folder::class)->getMock(); $parentNode->expects($this->once()) ->method('getPath') + ->willReturn('testuser1/files/Folder'); + + $node = $this->getMockBuilder(File::class)->getMock(); + $node->expects($this->once()) + ->method('getParent') + ->willReturn($parentNode); + + $baseFolderFiles->expects($this->any()) + ->method('getFirstNodeById') + ->with(123456) + ->willReturn($node); + + $response = $this->viewController->showFile('123456', $opendetails, $openfile); + $this->assertStringContainsString($result, $response->getHeaders()['Location']); + } + + public function testShowFileRouteWithTrashedFile(): void { + $this->appManager->expects($this->exactly(2)) + ->method('isEnabledForUser') + ->willReturn(true); + + $parentNode = $this->createMock(Folder::class); + $parentNode->expects($this->once()) + ->method('getPath') ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub'); - $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock(); - $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock(); + $baseFolderFiles = $this->createMock(Folder::class); + $baseFolderTrash = $this->createMock(Folder::class); $this->rootFolder->expects($this->any()) ->method('getUserFolder') @@ -175,7 +273,7 @@ class ViewControllerTest extends TestCase { ->with(123) ->willReturn(null); - $node = $this->getMockBuilder(File::class)->getMock(); + $node = $this->createMock(File::class); $node->expects($this->once()) ->method('getParent') ->willReturn($parentNode); @@ -189,13 +287,27 @@ class ViewControllerTest extends TestCase { ->with('testuser1/files_trashbin/files/test.d1462861890/sub') ->willReturn('/test.d1462861890/sub'); - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123']) - ->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub'); - - $expected = new RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub'); + $expected = new RedirectResponse('/index.php/apps/files/trashbin/123?dir=/test.d1462861890/sub'); $this->assertEquals($expected, $this->viewController->index('', '', '123')); } + + public function testTwoFactorAuthEnabled(): void { + $this->twoFactorRegistry->method('getProviderStates') + ->willReturn([ + 'totp' => true, + 'backup_codes' => true, + ]); + + $invokedCountProvideInitialState = $this->exactly(9); + $this->initialState->expects($invokedCountProvideInitialState) + ->method('provideInitialState') + ->willReturnCallback(function ($key, $data) use ($invokedCountProvideInitialState) { + if ($invokedCountProvideInitialState->numberOfInvocations() === 9) { + $this->assertEquals('isTwoFactorEnabled', $key); + $this->assertTrue($data); + } + }); + + $this->viewController->index('', '', null); + } } diff --git a/apps/files/tests/HelperTest.php b/apps/files/tests/HelperTest.php index 0cf2812f816..ba93fa0efdf 100644 --- a/apps/files/tests/HelperTest.php +++ b/apps/files/tests/HelperTest.php @@ -1,17 +1,17 @@ <?php -use OC\Files\FileInfo; -use OCA\Files\Helper; -use OCP\ITagManager; -use OCP\ITags; - +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-License-Identifier: AGPL-3.0-only */ + +use OC\Files\FileInfo; +use OCA\Files\Helper; + class HelperTest extends \Test\TestCase { - private function makeFileInfo($name, $size, $mtime, $isDir = false) { + private static function makeFileInfo($name, $size, $mtime, $isDir = false): FileInfo { return new FileInfo( '/' . $name, null, @@ -30,7 +30,7 @@ class HelperTest extends \Test\TestCase { /** * Returns a file list for testing */ - private function getTestFileList() { + private static function getTestFileList(): array { return [ self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)), self::makeFileInfo('q.txt', 5, 150), @@ -41,7 +41,7 @@ class HelperTest extends \Test\TestCase { ]; } - public function sortDataProvider() { + public static function sortDataProvider(): array { return [ [ 'name', @@ -76,9 +76,7 @@ class HelperTest extends \Test\TestCase { ]; } - /** - * @dataProvider sortDataProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('sortDataProvider')] public function testSortByName(string $sort, bool $sortDescending, array $expectedOrder): void { if (($sort === 'mtime') && (PHP_INT_SIZE < 8)) { $this->markTestSkipped('Skip mtime sorting on 32bit'); @@ -94,36 +92,4 @@ class HelperTest extends \Test\TestCase { $fileNames ); } - - public function testPopulateTags(): void { - $tagManager = $this->createMock(ITagManager::class); - $tagger = $this->createMock(ITags::class); - - $tagManager->method('load') - ->with('files') - ->willReturn($tagger); - - $data = [ - ['file_source' => 10], - ['file_source' => 22, 'foo' => 'bar'], - ['file_source' => 42, 'x' => 'y'], - ]; - - $tags = [ - 10 => ['tag3'], - 42 => ['tag1', 'tag2'], - ]; - - $tagger->method('getTagsForObjects') - ->with([10, 22, 42]) - ->willReturn($tags); - - $result = Helper::populateTags($data, $tagManager); - - $this->assertSame([ - ['file_source' => 10, 'tags' => ['tag3']], - ['file_source' => 22, 'foo' => 'bar', 'tags' => []], - ['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']], - ], $result); - } } diff --git a/apps/files/tests/Service/TagServiceTest.php b/apps/files/tests/Service/TagServiceTest.php index 0e0149cc71b..424e483102c 100644 --- a/apps/files/tests/Service/TagServiceTest.php +++ b/apps/files/tests/Service/TagServiceTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -10,10 +11,15 @@ namespace OCA\Files\Tests\Service; use OCA\Files\Service\TagService; use OCP\Activity\IManager; use OCP\Files\Folder; +use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; +use OCP\ITagManager; use OCP\ITags; use OCP\IUser; +use OCP\IUserManager; use OCP\IUserSession; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; /** * Class TagServiceTest @@ -23,38 +29,18 @@ use OCP\IUserSession; * @package OCA\Files */ class TagServiceTest extends \Test\TestCase { - - /** - * @var string - */ - private $user; - - /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ - private $userSession; - - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - private $activityManager; - - /** - * @var Folder - */ - private $root; - - /** - * @var TagService|\PHPUnit\Framework\MockObject\MockObject - */ - private $tagService; - - /** - * @var ITags - */ - private $tagger; + private string $user; + private IUserSession&MockObject $userSession; + private IManager&MockObject $activityManager; + private Folder $root; + private TagService&MockObject $tagService; + private ITags $tagger; protected function setUp(): void { parent::setUp(); $this->user = static::getUniqueID('user'); $this->activityManager = $this->createMock(IManager::class); - \OC::$server->getUserManager()->createUser($this->user, 'test'); + Server::get(IUserManager::class)->createUser($this->user, 'test'); \OC_User::setUserId($this->user); \OC_Util::setupFS($this->user); $user = $this->createMock(IUser::class); @@ -67,17 +53,13 @@ class TagServiceTest extends \Test\TestCase { ->withAnyParameters() ->willReturn($user); - $this->root = \OC::$server->getUserFolder(); + $this->root = Server::get(IRootFolder::class)->getUserFolder($this->user); - $this->tagger = \OC::$server->getTagManager()->load('files'); - $this->tagService = $this->getTagService(['addActivity']); + $this->tagger = Server::get(ITagManager::class)->load('files'); + $this->tagService = $this->getTagService(); } - /** - * @param array $methods - * @return TagService|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getTagService(array $methods = []) { + protected function getTagService(array $methods = []): TagService&MockObject { return $this->getMockBuilder(TagService::class) ->setConstructorArgs([ $this->userSession, @@ -85,16 +67,18 @@ class TagServiceTest extends \Test\TestCase { $this->tagger, $this->root, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } protected function tearDown(): void { \OC_User::setUserId(''); - $user = \OC::$server->getUserManager()->get($this->user); + $user = Server::get(IUserManager::class)->get($this->user); if ($user !== null) { $user->delete(); } + + parent::tearDown(); } public function testUpdateFileTags(): void { |