diff options
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/Collaboration/Resources/ProviderManagerTest.php | 15 | ||||
-rw-r--r-- | tests/lib/DB/MigrationsTest.php | 39 | ||||
-rw-r--r-- | tests/lib/Federation/CloudIdManagerTest.php | 4 | ||||
-rw-r--r-- | tests/lib/L10N/FactoryTest.php | 68 | ||||
-rw-r--r-- | tests/lib/Log/LogFactoryTest.php | 18 | ||||
-rw-r--r-- | tests/lib/Mail/EMailTemplateTest.php | 24 | ||||
-rw-r--r-- | tests/lib/NavigationManagerTest.php | 40 | ||||
-rw-r--r-- | tests/lib/OCS/ProviderTest.php | 49 | ||||
-rw-r--r-- | tests/lib/Preview/BackgroundCleanupJobTest.php | 43 | ||||
-rw-r--r-- | tests/lib/Preview/GeneratorTest.php | 7 | ||||
-rw-r--r-- | tests/lib/Preview/Provider.php | 33 | ||||
-rw-r--r-- | tests/lib/Security/IdentityProof/ManagerTest.php | 28 | ||||
-rw-r--r-- | tests/lib/Security/Normalizer/IpAddressTest.php | 8 | ||||
-rw-r--r-- | tests/lib/Settings/ManagerTest.php | 4 | ||||
-rw-r--r-- | tests/lib/Updater/ChangesCheckTest.php | 22 | ||||
-rw-r--r-- | tests/lib/User/AvailabilityCoordinatorTest.php | 11 | ||||
-rw-r--r-- | tests/lib/User/ManagerTest.php | 29 |
17 files changed, 247 insertions, 195 deletions
diff --git a/tests/lib/Collaboration/Resources/ProviderManagerTest.php b/tests/lib/Collaboration/Resources/ProviderManagerTest.php index 01c201e0a4e..b063d89f06e 100644 --- a/tests/lib/Collaboration/Resources/ProviderManagerTest.php +++ b/tests/lib/Collaboration/Resources/ProviderManagerTest.php @@ -77,13 +77,14 @@ class ProviderManagerTest extends TestCase { public function testGetResourceProvidersValidAndInvalidProvider(): void { $this->serverContainer->expects($this->exactly(2)) ->method('query') - ->withConsecutive( - [$this->equalTo('InvalidResourceProvider')], - [$this->equalTo(ResourceProvider::class)], - )->willReturnOnConsecutiveCalls( - $this->throwException(new QueryException('A meaningful error message')), - $this->createMock(ResourceProvider::class), - ); + ->willReturnCallback(function (string $service) { + if ($service === 'InvalidResourceProvider') { + throw new QueryException('A meaningful error message'); + } + if ($service === ResourceProvider::class) { + return $this->createMock(ResourceProvider::class); + } + }); $this->logger->expects($this->once()) ->method('error'); diff --git a/tests/lib/DB/MigrationsTest.php b/tests/lib/DB/MigrationsTest.php index 531e0a3805a..57ffb91e37e 100644 --- a/tests/lib/DB/MigrationsTest.php +++ b/tests/lib/DB/MigrationsTest.php @@ -93,7 +93,7 @@ class MigrationsTest extends \Test\TestCase { $this->expectExceptionMessage('Migration step \'X\' is unknown'); $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['findMigrations']) + ->onlyMethods(['findMigrations']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); $this->migrationService->expects($this->any())->method('findMigrations')->willReturn( @@ -134,7 +134,7 @@ class MigrationsTest extends \Test\TestCase { ->method('postSchemaChange'); $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['createInstance']) + ->onlyMethods(['createInstance']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); @@ -164,7 +164,7 @@ class MigrationsTest extends \Test\TestCase { ->method('postSchemaChange'); $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['createInstance']) + ->onlyMethods(['createInstance']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); @@ -191,7 +191,7 @@ class MigrationsTest extends \Test\TestCase { */ public function testGetMigration($alias, $expected): void { $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['getMigratedVersions', 'findMigrations']) + ->onlyMethods(['getMigratedVersions', 'findMigrations']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( @@ -211,22 +211,33 @@ class MigrationsTest extends \Test\TestCase { public function testMigrate(): void { $this->migrationService = $this->getMockBuilder(MigrationService::class) - ->setMethods(['getMigratedVersions', 'findMigrations', 'executeStep']) + ->onlyMethods(['getMigratedVersions', 'findMigrations', 'executeStep']) ->setConstructorArgs(['testing', $this->db]) ->getMock(); - $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( - ['20170130180000', '20170130180001'] - ); - $this->migrationService->expects($this->any())->method('findMigrations')->willReturn( - ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] - ); + $this->migrationService->method('getMigratedVersions') + ->willReturn( + ['20170130180000', '20170130180001'] + ); + $this->migrationService->method('findMigrations') + ->willReturn( + ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] + ); $this->assertEquals( ['20170130180000', '20170130180001', '20170130180002', '20170130180003'], - $this->migrationService->getAvailableVersions()); + $this->migrationService->getAvailableVersions() + ); - $this->migrationService->expects($this->exactly(2))->method('executeStep') - ->withConsecutive(['20170130180002'], ['20170130180003']); + $calls = [ + ['20170130180002', false], + ['20170130180003', false], + ]; + $this->migrationService->expects($this->exactly(2)) + ->method('executeStep') + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->migrationService->migrate(); } diff --git a/tests/lib/Federation/CloudIdManagerTest.php b/tests/lib/Federation/CloudIdManagerTest.php index 1775c18a5e9..7019cd202db 100644 --- a/tests/lib/Federation/CloudIdManagerTest.php +++ b/tests/lib/Federation/CloudIdManagerTest.php @@ -91,6 +91,10 @@ class CloudIdManagerTest extends TestCase { ['test@example.com/cloud/', 'test', 'example.com/cloud', 'test@example.com/cloud'], ['test@example.com/cloud/index.php', 'test', 'example.com/cloud', 'test@example.com/cloud'], ['test@example.com@example.com', 'test@example.com', 'example.com', 'test@example.com@example.com'], + + // Equal signs are not valid on Nextcloud side, but can be used by other federated OCM compatible servers + ['test==@example.com', 'test==', 'example.com', 'test==@example.com'], + ['==@example.com', '==', 'example.com', '==@example.com'], ]; } diff --git a/tests/lib/L10N/FactoryTest.php b/tests/lib/L10N/FactoryTest.php index c29c31bf650..cd66217c49d 100644 --- a/tests/lib/L10N/FactoryTest.php +++ b/tests/lib/L10N/FactoryTest.php @@ -83,14 +83,14 @@ class FactoryTest extends TestCase { $this->serverRoot, $this->appManager, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } return new Factory($this->config, $this->request, $this->userSession, $this->cacheFactory, $this->serverRoot, $this->appManager); } - public function dataFindAvailableLanguages(): array { + public static function dataFindAvailableLanguages(): array { return [ [null], ['files'], @@ -124,24 +124,17 @@ class FactoryTest extends TestCase { $this->invokePrivate($factory, 'requestLanguage', ['de']); $factory->expects($this->exactly(2)) ->method('languageExists') - ->withConsecutive( - ['MyApp', 'de'], - ['MyApp', 'jp'], - ) - ->willReturnOnConsecutiveCalls( - false, - true, - ); + ->willReturnMap([ + ['MyApp', 'de', false], + ['MyApp', 'jp', true], + ]); $this->config ->expects($this->exactly(1)) ->method('getSystemValue') - ->withConsecutive( - ['force_language', false], - )->willReturnOnConsecutiveCalls( - false, - ); - $user = $this->getMockBuilder(IUser::class) - ->getMock(); + ->willReturnMap([ + ['force_language', false, false], + ]); + $user = $this->createMock(IUser::class); $user->expects(self::once()) ->method('getUID') ->willReturn('MyUserUid'); @@ -175,8 +168,7 @@ class FactoryTest extends TestCase { ['force_language', false, false], ['default_language', false, 'es'] ]); - $user = $this->getMockBuilder(IUser::class) - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects(self::once()) ->method('getUID') ->willReturn('MyUserUid'); @@ -210,8 +202,7 @@ class FactoryTest extends TestCase { ['force_language', false, false], ['default_language', false, 'es'] ]); - $user = $this->getMockBuilder(IUser::class) - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects(self::once()) ->method('getUID') ->willReturn('MyUserUid'); @@ -248,8 +239,7 @@ class FactoryTest extends TestCase { ['force_language', false, false], ['default_language', false, 'es'] ]); - $user = $this->getMockBuilder(IUser::class) - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects(self::once()) ->method('getUID') ->willReturn('MyUserUid'); @@ -302,7 +292,7 @@ class FactoryTest extends TestCase { self::assertEqualsCanonicalizing(['cs', 'de', 'en', 'ru'], $factory->findAvailableLanguages($app)); } - public function dataLanguageExists(): array { + public static function dataLanguageExists(): array { return [ [null, 'en', [], true], [null, 'de', [], false], @@ -351,7 +341,7 @@ class FactoryTest extends TestCase { self::assertSame($expected, $factory->languageExists($app, $lang)); } - public function dataSetLanguageFromRequest(): array { + public static function dataSetLanguageFromRequest(): array { return [ // Language is available [null, 'de', ['de'], 'de'], @@ -406,7 +396,7 @@ class FactoryTest extends TestCase { } } - public function dataGetL10nFilesForApp(): array { + public static function dataGetL10nFilesForApp(): array { return [ ['', 'de', [\OC::$SERVERROOT . '/core/l10n/de.json']], ['core', 'ru', [\OC::$SERVERROOT . '/core/l10n/ru.json']], @@ -440,7 +430,7 @@ class FactoryTest extends TestCase { self::assertSame($expected, $this->invokePrivate($factory, 'getL10nFilesForApp', [$app, $lang])); } - public function dataFindL10NDir(): array { + public static function dataFindL10NDir(): array { return [ ['', \OC::$SERVERROOT . '/core/l10n/'], ['core', \OC::$SERVERROOT . '/core/l10n/'], @@ -473,7 +463,7 @@ class FactoryTest extends TestCase { self::assertSame($expected, $this->invokePrivate($factory, 'findL10nDir', [$app])); } - public function dataFindLanguage(): array { + public static function dataFindLanguage(): array { return [ // Not logged in [false, [], 'en'], @@ -511,8 +501,7 @@ class FactoryTest extends TestCase { }); if ($loggedIn) { - $user = $this->getMockBuilder(IUser::class) - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects(self::any()) ->method('getUID') ->willReturn('MyUserUid'); @@ -670,7 +659,7 @@ class FactoryTest extends TestCase { self::assertSame('en', $lang); } - public function dataTestRespectDefaultLanguage(): array { + public static function dataTestRespectDefaultLanguage(): array { return [ ['de', 'de_DE', true, 'de_DE'], ['de', 'de', true, 'de'], @@ -747,21 +736,22 @@ class FactoryTest extends TestCase { self::assertEqualsCanonicalizing($expected, $commonLanguagesCodes); } - public function languageIteratorRequestProvider():array { + public static function languageIteratorRequestProvider(): array { return [ - [ true, $this->createMock(IUser::class)], - [ false, $this->createMock(IUser::class)], - [ false, null] + [ true, true], + [ false, true], + [ false, false], ]; } /** * @dataProvider languageIteratorRequestProvider */ - public function testGetLanguageIterator(bool $hasSession, ?IUser $iUserMock = null): void { + public function testGetLanguageIterator(bool $hasSession, bool $mockUser): void { $factory = $this->getFactory(); + $user = null; - if ($iUserMock === null) { + if (!$mockUser) { $matcher = $this->userSession->expects(self::once()) ->method('getUser'); @@ -770,9 +760,11 @@ class FactoryTest extends TestCase { } else { $this->expectException(\RuntimeException::class); } + } else { + $user = $this->createMock(IUser::class); } - $iterator = $factory->getLanguageIterator($iUserMock); + $iterator = $factory->getLanguageIterator($user); self::assertInstanceOf(ILanguageIterator::class, $iterator); } diff --git a/tests/lib/Log/LogFactoryTest.php b/tests/lib/Log/LogFactoryTest.php index 6219fd438f7..1d87c856061 100644 --- a/tests/lib/Log/LogFactoryTest.php +++ b/tests/lib/Log/LogFactoryTest.php @@ -39,7 +39,7 @@ class LogFactoryTest extends TestCase { $this->factory = new LogFactory($this->c, $this->systemConfig); } - public function fileTypeProvider(): array { + public static function fileTypeProvider(): array { return [ [ 'file' @@ -67,14 +67,17 @@ class LogFactoryTest extends TestCase { $this->systemConfig->expects($this->exactly(3)) ->method('getValue') - ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640]) - ->willReturnOnConsecutiveCalls($datadir, $defaultLog, 0640); + ->willReturnMap([ + ['datadirectory', $datadir, $datadir], + ['logfile', $defaultLog, $defaultLog], + ['logfilemode', 0640, 0640], + ]); $log = $this->factory->get($type); $this->assertInstanceOf(File::class, $log); } - public function logFilePathProvider():array { + public static function logFilePathProvider():array { return [ [ '/dev/null', @@ -97,8 +100,11 @@ class LogFactoryTest extends TestCase { $this->systemConfig->expects($this->exactly(3)) ->method('getValue') - ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640]) - ->willReturnOnConsecutiveCalls($datadir, $path, 0640); + ->willReturnMap([ + ['datadirectory', $datadir, $datadir], + ['logfile', $defaultLog, $path], + ['logfilemode', 0640, 0640], + ]); $log = $this->factory->get('file'); $this->assertInstanceOf(File::class, $log); diff --git a/tests/lib/Mail/EMailTemplateTest.php b/tests/lib/Mail/EMailTemplateTest.php index 76b37d48ff3..4943024043c 100644 --- a/tests/lib/Mail/EMailTemplateTest.php +++ b/tests/lib/Mail/EMailTemplateTest.php @@ -47,7 +47,7 @@ class EMailTemplateTest extends TestCase { public function testEMailTemplateCustomFooter(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -59,8 +59,8 @@ class EMailTemplateTest extends TestCase { ->method('getName') ->willReturn('TestCloud'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) @@ -88,7 +88,7 @@ class EMailTemplateTest extends TestCase { public function testEMailTemplateDefaultFooter(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -104,8 +104,8 @@ class EMailTemplateTest extends TestCase { ->method('getLogo') ->willReturn('/img/logo-mail-header.png'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) @@ -131,7 +131,7 @@ class EMailTemplateTest extends TestCase { public function testEMailTemplateSingleButton(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -147,8 +147,8 @@ class EMailTemplateTest extends TestCase { ->method('getLogo') ->willReturn('/img/logo-mail-header.png'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) @@ -176,7 +176,7 @@ class EMailTemplateTest extends TestCase { public function testEMailTemplateAlternativePlainTexts(): void { $this->defaults - ->expects($this->any()) + ->expects($this->atLeastOnce()) ->method('getDefaultColorPrimary') ->willReturn('#0082c9'); $this->defaults @@ -192,8 +192,8 @@ class EMailTemplateTest extends TestCase { ->method('getLogo') ->willReturn('/img/logo-mail-header.png'); $this->defaults - ->expects($this->any()) - ->method('getTextColorPrimary') + ->expects($this->atLeastOnce()) + ->method('getDefaultTextColorPrimary') ->willReturn('#ffffff'); $this->urlGenerator ->expects($this->once()) diff --git a/tests/lib/NavigationManagerTest.php b/tests/lib/NavigationManagerTest.php index 91da970f3b9..48cfa972f2b 100644 --- a/tests/lib/NavigationManagerTest.php +++ b/tests/lib/NavigationManagerTest.php @@ -704,30 +704,64 @@ class NavigationManagerTest extends TestCase { true, 'settings', ], + // closure navigation entries are also resolved + [ + 'closure2', + '', + '', + true, + 'closure2', + ], + [ + '', + 'closure2', + '', + true, + 'closure2', + ], + [ + '', + '', + '{"closure2":{"order":1,"app":"closure2","href":"/closure2"}}', + true, + 'closure2', + ], ]; } /** * @dataProvider provideDefaultEntries */ - public function testGetDefaultEntryIdForUser($defaultApps, $userDefaultApps, $userApporder, $withFallbacks, $expectedApp): void { + public function testGetDefaultEntryIdForUser(string $defaultApps, string $userDefaultApps, string $userApporder, bool $withFallbacks, string $expectedApp): void { $this->navigationManager->add([ 'id' => 'files', ]); $this->navigationManager->add([ 'id' => 'settings', ]); + $this->navigationManager->add(static function (): array { + return [ + 'id' => 'closure1', + 'href' => '/closure1', + ]; + }); + $this->navigationManager->add(static function (): array { + return [ + 'id' => 'closure2', + 'href' => '/closure2', + ]; + }); $this->appManager->method('getEnabledApps')->willReturn([]); $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('user1'); - $this->userSession->expects($this->once()) + $this->userSession->expects($this->atLeastOnce()) ->method('getUser') ->willReturn($user); - $this->config->expects($this->once()) + $this->config->expects($this->atLeastOnce()) ->method('getSystemValueString') ->with('defaultapp', $this->anything()) ->willReturn($defaultApps); diff --git a/tests/lib/OCS/ProviderTest.php b/tests/lib/OCS/ProviderTest.php index 3834c1d613e..ce028ce764a 100644 --- a/tests/lib/OCS/ProviderTest.php +++ b/tests/lib/OCS/ProviderTest.php @@ -29,13 +29,12 @@ class ProviderTest extends \Test\TestCase { $this->appManager ->expects($this->exactly(4)) ->method('isEnabledForUser') - ->withConsecutive( - ['files_sharing'], - ['federation'], - ['activity'], - ['provisioning_api'] - ) - ->willReturn(false); + ->willReturnMap([ + ['files_sharing', null, false], + ['federation', null, false], + ['activity', null, false], + ['provisioning_api', null, false], + ]); $expected = new \OCP\AppFramework\Http\JSONResponse( [ @@ -60,18 +59,12 @@ class ProviderTest extends \Test\TestCase { $this->appManager ->expects($this->exactly(4)) ->method('isEnabledForUser') - ->withConsecutive( - ['files_sharing'], - ['federation'], - ['activity'], - ['provisioning_api'] - ) - ->willReturnOnConsecutiveCalls( - true, - false, - false, - false - ); + ->willReturnMap([ + ['files_sharing', null, true], + ['federation', null, false], + ['activity', null, false], + ['provisioning_api', null, false], + ]); $expected = new \OCP\AppFramework\Http\JSONResponse( [ @@ -109,18 +102,12 @@ class ProviderTest extends \Test\TestCase { $this->appManager ->expects($this->exactly(4)) ->method('isEnabledForUser') - ->withConsecutive( - ['files_sharing'], - ['federation'], - ['activity'], - ['provisioning_api'] - ) - ->willReturnOnConsecutiveCalls( - false, - true, - false, - false - ); + ->willReturnMap([ + ['files_sharing', null, false], + ['federation', null, true], + ['activity', null, false], + ['provisioning_api', null, false], + ]); $expected = new \OCP\AppFramework\Http\JSONResponse( [ diff --git a/tests/lib/Preview/BackgroundCleanupJobTest.php b/tests/lib/Preview/BackgroundCleanupJobTest.php index 945366cde9f..cecb4a7a212 100644 --- a/tests/lib/Preview/BackgroundCleanupJobTest.php +++ b/tests/lib/Preview/BackgroundCleanupJobTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only @@ -9,12 +10,15 @@ namespace Test\Preview; use OC\Preview\BackgroundCleanupJob; use OC\Preview\Storage\Root; use OC\PreviewManager; +use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\File; use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IDBConnection; +use OCP\IPreview; +use OCP\Server; use Test\Traits\MountProviderTrait; use Test\Traits\UserTrait; @@ -28,25 +32,12 @@ use Test\Traits\UserTrait; class BackgroundCleanupJobTest extends \Test\TestCase { use MountProviderTrait; use UserTrait; - - /** @var string */ - private $userId; - - /** @var bool */ - private $trashEnabled; - - /** @var IDBConnection */ - private $connection; - - /** @var PreviewManager */ - private $previewManager; - - /** @var IRootFolder */ - private $rootFolder; - - /** @var IMimeTypeLoader */ - private $mimeTypeLoader; - + private string $userId; + private bool $trashEnabled; + private IDBConnection $connection; + private PreviewManager $previewManager; + private IRootFolder $rootFolder; + private IMimeTypeLoader $mimeTypeLoader; private ITimeFactory $timeFactory; protected function setUp(): void { @@ -62,20 +53,20 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $this->logout(); $this->loginAsUser($this->userId); - $appManager = \OC::$server->getAppManager(); + $appManager = Server::get(IAppManager::class); $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $user); $appManager->disableApp('files_trashbin'); - $this->connection = \OC::$server->getDatabaseConnection(); - $this->previewManager = \OC::$server->getPreviewManager(); - $this->rootFolder = \OC::$server->get(IRootFolder::class); - $this->mimeTypeLoader = \OC::$server->getMimeTypeLoader(); - $this->timeFactory = \OCP\Server::get(ITimeFactory::class); + $this->connection = Server::get(IDBConnection::class); + $this->previewManager = Server::get(IPreview::class); + $this->rootFolder = Server::get(IRootFolder::class); + $this->mimeTypeLoader = Server::get(IMimeTypeLoader::class); + $this->timeFactory = Server::get(ITimeFactory::class); } protected function tearDown(): void { if ($this->trashEnabled) { - $appManager = \OC::$server->getAppManager(); + $appManager = Server::get(IAppManager::class); $appManager->enableApp('files_trashbin'); } diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php index 7bd121250fb..8a08d741909 100644 --- a/tests/lib/Preview/GeneratorTest.php +++ b/tests/lib/Preview/GeneratorTest.php @@ -19,6 +19,7 @@ use OCP\IImage; use OCP\IPreview; use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Preview\IProviderV2; +use Psr\Log\LoggerInterface; class GeneratorTest extends \Test\TestCase { /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ @@ -39,6 +40,8 @@ class GeneratorTest extends \Test\TestCase { /** @var Generator */ private $generator; + private LoggerInterface|\PHPUnit\Framework\MockObject\MockObject $logger; + protected function setUp(): void { parent::setUp(); @@ -47,13 +50,15 @@ class GeneratorTest extends \Test\TestCase { $this->appData = $this->createMock(IAppData::class); $this->helper = $this->createMock(GeneratorHelper::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->generator = new Generator( $this->config, $this->previewManager, $this->appData, $this->helper, - $this->eventDispatcher + $this->eventDispatcher, + $this->logger, ); } diff --git a/tests/lib/Preview/Provider.php b/tests/lib/Preview/Provider.php index 41a2a4ca3c4..a7f55151354 100644 --- a/tests/lib/Preview/Provider.php +++ b/tests/lib/Preview/Provider.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -9,33 +10,25 @@ namespace Test\Preview; use OC\Files\Node\File; use OCP\Files\IRootFolder; +use OCP\IUserManager; abstract class Provider extends \Test\TestCase { - /** @var string */ - protected $imgPath; - /** @var int */ - protected $width; - /** @var int */ - protected $height; - /** @var \OC\Preview\Provider */ + protected string $imgPath; + protected int $width; + protected int $height; + /** @var \OC\Preview\Provider|mixed $provider */ protected $provider; - /** @var int */ - protected $maxWidth = 1024; - /** @var int */ - protected $maxHeight = 1024; - /** @var bool */ - protected $scalingUp = false; - /** @var int */ - protected $userId; - /** @var \OC\Files\View */ - protected $rootView; - /** @var \OC\Files\Storage\Storage */ - protected $storage; + protected int $maxWidth = 1024; + protected int $maxHeight = 1024; + protected bool $scalingUp = false; + protected string $userId; + protected \OC\Files\View $rootView; + protected \OC\Files\Storage\Storage $storage; protected function setUp(): void { parent::setUp(); - $userManager = \OC::$server->getUserManager(); + $userManager = \OCP\Server::get(IUserManager::class); $userManager->clearBackends(); $backend = new \Test\Util\User\Dummy(); $userManager->registerBackend($backend); diff --git a/tests/lib/Security/IdentityProof/ManagerTest.php b/tests/lib/Security/IdentityProof/ManagerTest.php index 722555efe01..445158e8a23 100644 --- a/tests/lib/Security/IdentityProof/ManagerTest.php +++ b/tests/lib/Security/IdentityProof/ManagerTest.php @@ -75,7 +75,9 @@ class ManagerTest extends TestCase { $this->crypto, $this->config, $this->logger - ])->setMethods($setMethods)->getMock(); + ]) + ->onlyMethods($setMethods) + ->getMock(); } } @@ -104,14 +106,10 @@ class ManagerTest extends TestCase { $folder ->expects($this->exactly(2)) ->method('getFile') - ->withConsecutive( - ['private'], - ['public'] - ) - ->willReturnOnConsecutiveCalls( - $privateFile, - $publicFile - ); + ->willReturnMap([ + ['private', $privateFile], + ['public', $publicFile], + ]); $this->appData ->expects($this->once()) ->method('getFolder') @@ -155,14 +153,10 @@ class ManagerTest extends TestCase { $folder ->expects($this->exactly(2)) ->method('newFile') - ->withConsecutive( - ['private'], - ['public'] - ) - ->willReturnOnConsecutiveCalls( - $privateFile, - $publicFile - ); + ->willReturnMap([ + ['private', null, $privateFile], + ['public', null, $publicFile], + ]); $this->appData ->expects($this->exactly(2)) ->method('getFolder') diff --git a/tests/lib/Security/Normalizer/IpAddressTest.php b/tests/lib/Security/Normalizer/IpAddressTest.php index c2989c44ef2..33a8b4d28f1 100644 --- a/tests/lib/Security/Normalizer/IpAddressTest.php +++ b/tests/lib/Security/Normalizer/IpAddressTest.php @@ -37,19 +37,19 @@ class IpAddressTest extends TestCase { ], [ '2001:0db8:0000:0000:0000:8a2e:0370:7334', - '2001:db8::/48', + '2001:db8::/56', ], [ '2001:db8:3333:4444:5555:6666:7777:8888', - '2001:db8:3333::/48', + '2001:db8:3333:4400::/56', ], [ '::1234:5678', - '::/48', + '::/56', ], [ '[::1]', - '::/48', + '::/56', ], ]; } diff --git a/tests/lib/Settings/ManagerTest.php b/tests/lib/Settings/ManagerTest.php index 90c195e6bd0..38b0262bb55 100644 --- a/tests/lib/Settings/ManagerTest.php +++ b/tests/lib/Settings/ManagerTest.php @@ -178,10 +178,6 @@ class ManagerTest extends TestCase { $this->container->expects($this->exactly(2)) ->method('get') - ->withConsecutive( - ['section1'], - ['section2'] - ) ->willReturnMap([ ['section1', $section], ['section2', $section2], diff --git a/tests/lib/Updater/ChangesCheckTest.php b/tests/lib/Updater/ChangesCheckTest.php index 81eaa95d1fe..4b0c54b0881 100644 --- a/tests/lib/Updater/ChangesCheckTest.php +++ b/tests/lib/Updater/ChangesCheckTest.php @@ -42,7 +42,7 @@ class ChangesCheckTest extends TestCase { $this->checker = new ChangesCheck($this->clientService, $this->mapper, $this->logger); } - public function statusCodeProvider():array { + public static function statusCodeProvider(): array { return [ [200, ChangesCheck::RESPONSE_HAS_CONTENT], [304, ChangesCheck::RESPONSE_USE_CACHE], @@ -74,8 +74,10 @@ class ChangesCheckTest extends TestCase { $entry = $this->createMock(Changes::class); $entry->expects($this->exactly(2)) ->method('__call') - ->withConsecutive(['getVersion'], ['setVersion', [$version]]) - ->willReturnOnConsecutiveCalls('', null); + ->willReturnMap([ + ['getVersion', [], ''], + ['setVersion', [$version], null], + ]); $this->mapper->expects($this->once()) ->method('insert'); @@ -100,7 +102,7 @@ class ChangesCheckTest extends TestCase { $this->invokePrivate($this->checker, 'cacheResult', [$entry, $version]); } - public function changesXMLProvider(): array { + public static function changesXMLProvider(): array { return [ [ # 0 - full example '<?xml version="1.0" encoding="utf-8" ?> @@ -277,7 +279,7 @@ class ChangesCheckTest extends TestCase { $this->assertSame($expected, $actual); } - public function etagProvider() { + public static function etagProvider() { return [ [''], ['a27aab83d8205d73978435076e53d143'] @@ -310,7 +312,7 @@ class ChangesCheckTest extends TestCase { $this->assertInstanceOf(IResponse::class, $response); } - public function versionProvider(): array { + public static function versionProvider(): array { return [ ['13.0.7', '13.0.7'], ['13.0.7.3', '13.0.7'], @@ -329,12 +331,12 @@ class ChangesCheckTest extends TestCase { $this->assertSame($expected, $normalized); } - public function changeDataProvider():array { - $testDataFound = $testDataNotFound = $this->versionProvider(); - array_walk($testDataFound, function (&$params) { + public static function changeDataProvider():array { + $testDataFound = $testDataNotFound = self::versionProvider(); + array_walk($testDataFound, static function (&$params) { $params[] = true; }); - array_walk($testDataNotFound, function (&$params) { + array_walk($testDataNotFound, static function (&$params) { $params[] = false; }); return array_merge($testDataFound, $testDataNotFound); diff --git a/tests/lib/User/AvailabilityCoordinatorTest.php b/tests/lib/User/AvailabilityCoordinatorTest.php index 8b9446279a6..09c1528912b 100644 --- a/tests/lib/User/AvailabilityCoordinatorTest.php +++ b/tests/lib/User/AvailabilityCoordinatorTest.php @@ -88,10 +88,17 @@ class AvailabilityCoordinatorTest extends TestCase { ->method('getAbsence') ->with($user->getUID()) ->willReturn($absence); + + $calls = [ + [$user->getUID() . '_timezone', 'Europe/Berlin', 3600], + [$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300], + ]; $this->cache->expects(self::exactly(2)) ->method('set') - ->withConsecutive([$user->getUID() . '_timezone', 'Europe/Berlin', 3600], - [$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300]); + ->willReturnCallback(static function () use (&$calls): void { + $expected = array_shift($calls); + self::assertEquals($expected, func_get_args()); + }); $expected = new OutOfOfficeData( '420', diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php index 53d63ea6758..5d3966cf4d8 100644 --- a/tests/lib/User/ManagerTest.php +++ b/tests/lib/User/ManagerTest.php @@ -79,6 +79,20 @@ class ManagerTest extends TestCase { $this->assertTrue($manager->userExists('foo')); } + public function testUserExistsTooLong(): void { + /** @var \Test\Util\User\Dummy|MockObject $backend */ + $backend = $this->createMock(\Test\Util\User\Dummy::class); + $backend->expects($this->never()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->willReturn(true); + + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger); + $manager->registerBackend($backend); + + $this->assertFalse($manager->userExists('foo' . str_repeat('a', 62))); + } + public function testUserExistsSingleBackendNotExists(): void { /** * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend @@ -230,6 +244,20 @@ class ManagerTest extends TestCase { $this->assertEquals(null, $manager->get('foo')); } + public function testGetTooLong(): void { + /** @var \Test\Util\User\Dummy|MockObject $backend */ + $backend = $this->createMock(\Test\Util\User\Dummy::class); + $backend->expects($this->never()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->willReturn(false); + + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger); + $manager->registerBackend($backend); + + $this->assertEquals(null, $manager->get('foo' . str_repeat('a', 62))); + } + public function testGetOneBackendDoNotTranslateLoginNames(): void { /** * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend @@ -333,6 +361,7 @@ class ManagerTest extends TestCase { ['..', 'foo', 'Username must not consist of dots only'], ['.test', '', 'A valid password must be provided'], ['test', '', 'A valid password must be provided'], + ['test' . str_repeat('a', 61), '', 'Login is too long'], ]; } |