diff options
Diffstat (limited to 'tests/lib/App/AppManagerTest.php')
-rw-r--r-- | tests/lib/App/AppManagerTest.php | 407 |
1 files changed, 204 insertions, 203 deletions
diff --git a/tests/lib/App/AppManagerTest.php b/tests/lib/App/AppManagerTest.php index c38cadf8e38..6637c529a1e 100644 --- a/tests/lib/App/AppManagerTest.php +++ b/tests/lib/App/AppManagerTest.php @@ -12,6 +12,7 @@ namespace Test\App; use OC\App\AppManager; use OC\AppConfig; +use OC\Config\ConfigManager; use OCP\App\AppPathNotFoundException; use OCP\App\Events\AppDisableEvent; use OCP\App\Events\AppEnableEvent; @@ -25,6 +26,7 @@ use OCP\IGroupManager; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; +use OCP\ServerVersion; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -35,10 +37,7 @@ use Test\TestCase; * @package Test\App */ class AppManagerTest extends TestCase { - /** - * @return AppConfig|MockObject - */ - protected function getAppConfig() { + protected function getAppConfig(): AppConfig&MockObject { $appConfig = []; $config = $this->createMock(AppConfig::class); @@ -49,7 +48,7 @@ class AppManagerTest extends TestCase { }); $config->expects($this->any()) ->method('setValue') - ->willReturnCallback(function ($app, $key, $value) use (&$appConfig) { + ->willReturnCallback(function ($app, $key, $value) use (&$appConfig): void { if (!isset($appConfig[$app])) { $appConfig[$app] = []; } @@ -70,35 +69,32 @@ class AppManagerTest extends TestCase { return $values; } }); + $config->expects($this->any()) + ->method('searchValues') + ->willReturnCallback(function ($key, $lazy, $type) use (&$appConfig) { + $values = []; + foreach ($appConfig as $appid => $appData) { + if (isset($appData[$key])) { + $values[$appid] = $appData[$key]; + } + } + return $values; + }); return $config; } - /** @var IUserSession|MockObject */ - protected $userSession; - - /** @var IConfig|MockObject */ - private $config; - - /** @var IGroupManager|MockObject */ - protected $groupManager; - - /** @var AppConfig|MockObject */ - protected $appConfig; - - /** @var ICache|MockObject */ - protected $cache; - - /** @var ICacheFactory|MockObject */ - protected $cacheFactory; - - /** @var IEventDispatcher|MockObject */ - protected $eventDispatcher; - - /** @var LoggerInterface|MockObject */ - protected $logger; - + protected IUserSession&MockObject $userSession; + private IConfig&MockObject $config; + protected IGroupManager&MockObject $groupManager; + protected AppConfig&MockObject $appConfig; + protected ICache&MockObject $cache; + protected ICacheFactory&MockObject $cacheFactory; + protected IEventDispatcher&MockObject $eventDispatcher; + protected LoggerInterface&MockObject $logger; protected IURLGenerator&MockObject $urlGenerator; + protected ServerVersion&MockObject $serverVersion; + protected ConfigManager&MockObject $configManager; /** @var IAppManager */ protected $manager; @@ -115,6 +111,8 @@ class AppManagerTest extends TestCase { $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->logger = $this->createMock(LoggerInterface::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->serverVersion = $this->createMock(ServerVersion::class); + $this->configManager = $this->createMock(ConfigManager::class); $this->overwriteService(AppConfig::class, $this->appConfig); $this->overwriteService(IURLGenerator::class, $this->urlGenerator); @@ -136,13 +134,13 @@ class AppManagerTest extends TestCase { $this->cacheFactory, $this->eventDispatcher, $this->logger, + $this->serverVersion, + $this->configManager, ); } - /** - * @dataProvider dataGetAppIcon - */ - public function testGetAppIcon($callback, ?bool $dark, string|null $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetAppIcon')] + public function testGetAppIcon($callback, ?bool $dark, ?string $expected): void { $this->urlGenerator->expects($this->atLeastOnce()) ->method('imagePath') ->willReturnCallback($callback); @@ -154,15 +152,15 @@ class AppManagerTest extends TestCase { } } - public function dataGetAppIcon(): array { - $nothing = function ($appId) { - $this->assertEquals('test', $appId); + public static function dataGetAppIcon(): array { + $nothing = function ($appId): void { + self::assertEquals('test', $appId); throw new \RuntimeException(); }; $createCallback = function ($workingIcons) { return function ($appId, $icon) use ($workingIcons) { - $this->assertEquals('test', $appId); + self::assertEquals('test', $appId); if (in_array($icon, $workingIcons)) { return '/path/' . $icon; } @@ -229,7 +227,7 @@ class AppManagerTest extends TestCase { ]; } - public function testEnableApp() { + public function testEnableApp(): void { // making sure "files_trashbin" is disabled if ($this->manager->isEnabledForUser('files_trashbin')) { $this->manager->disableApp('files_trashbin'); @@ -239,13 +237,13 @@ class AppManagerTest extends TestCase { $this->assertEquals('yes', $this->appConfig->getValue('files_trashbin', 'enabled', 'no')); } - public function testDisableApp() { + public function testDisableApp(): void { $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new AppDisableEvent('files_trashbin')); $this->manager->disableApp('files_trashbin'); $this->assertEquals('no', $this->appConfig->getValue('files_trashbin', 'enabled', 'no')); } - public function testNotEnableIfNotInstalled() { + public function testNotEnableIfNotInstalled(): void { try { $this->manager->enableApp('some_random_name_which_i_hope_is_not_an_app'); $this->assertFalse(true, 'If this line is reached the expected exception is not thrown.'); @@ -259,7 +257,7 @@ class AppManagerTest extends TestCase { )); } - public function testEnableAppForGroups() { + public function testEnableAppForGroups(): void { $group1 = $this->createMock(IGroup::class); $group1->method('getGID') ->willReturn('group1'); @@ -278,6 +276,8 @@ class AppManagerTest extends TestCase { $this->cacheFactory, $this->eventDispatcher, $this->logger, + $this->serverVersion, + $this->configManager, ]) ->onlyMethods([ 'getAppPath', @@ -295,7 +295,7 @@ class AppManagerTest extends TestCase { $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no')); } - public function dataEnableAppForGroupsAllowedTypes() { + public static function dataEnableAppForGroupsAllowedTypes(): array { return [ [[]], [[ @@ -308,11 +308,10 @@ class AppManagerTest extends TestCase { } /** - * @dataProvider dataEnableAppForGroupsAllowedTypes - * * @param array $appInfo */ - public function testEnableAppForGroupsAllowedTypes(array $appInfo) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataEnableAppForGroupsAllowedTypes')] + public function testEnableAppForGroupsAllowedTypes(array $appInfo): void { $group1 = $this->createMock(IGroup::class); $group1->method('getGID') ->willReturn('group1'); @@ -331,6 +330,8 @@ class AppManagerTest extends TestCase { $this->cacheFactory, $this->eventDispatcher, $this->logger, + $this->serverVersion, + $this->configManager, ]) ->onlyMethods([ 'getAppPath', @@ -341,7 +342,7 @@ class AppManagerTest extends TestCase { $manager->expects($this->once()) ->method('getAppPath') ->with('test') - ->willReturn(null); + ->willReturn(''); $manager->expects($this->once()) ->method('getAppInfo') @@ -354,7 +355,7 @@ class AppManagerTest extends TestCase { $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no')); } - public function dataEnableAppForGroupsForbiddenTypes() { + public static function dataEnableAppForGroupsForbiddenTypes(): array { return [ ['filesystem'], ['prelogin'], @@ -365,12 +366,12 @@ class AppManagerTest extends TestCase { } /** - * @dataProvider dataEnableAppForGroupsForbiddenTypes * * @param string $type * */ - public function testEnableAppForGroupsForbiddenTypes($type) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataEnableAppForGroupsForbiddenTypes')] + public function testEnableAppForGroupsForbiddenTypes($type): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('test can\'t be enabled for groups.'); @@ -392,6 +393,8 @@ class AppManagerTest extends TestCase { $this->cacheFactory, $this->eventDispatcher, $this->logger, + $this->serverVersion, + $this->configManager, ]) ->onlyMethods([ 'getAppPath', @@ -402,7 +405,7 @@ class AppManagerTest extends TestCase { $manager->expects($this->once()) ->method('getAppPath') ->with('test') - ->willReturn(null); + ->willReturn(''); $manager->expects($this->once()) ->method('getAppInfo') @@ -416,19 +419,19 @@ class AppManagerTest extends TestCase { $manager->enableAppForGroups('test', $groups); } - public function testIsInstalledEnabled() { + public function testIsInstalledEnabled(): void { $this->appConfig->setValue('test', 'enabled', 'yes'); - $this->assertTrue($this->manager->isInstalled('test')); + $this->assertTrue($this->manager->isEnabledForAnyone('test')); } - public function testIsInstalledDisabled() { + public function testIsInstalledDisabled(): void { $this->appConfig->setValue('test', 'enabled', 'no'); - $this->assertFalse($this->manager->isInstalled('test')); + $this->assertFalse($this->manager->isEnabledForAnyone('test')); } - public function testIsInstalledEnabledForGroups() { + public function testIsInstalledEnabledForGroups(): void { $this->appConfig->setValue('test', 'enabled', '["foo"]'); - $this->assertTrue($this->manager->isInstalled('test')); + $this->assertTrue($this->manager->isEnabledForAnyone('test')); } private function newUser($uid) { @@ -439,23 +442,23 @@ class AppManagerTest extends TestCase { return $user; } - public function testIsEnabledForUserEnabled() { + public function testIsEnabledForUserEnabled(): void { $this->appConfig->setValue('test', 'enabled', 'yes'); $user = $this->newUser('user1'); $this->assertTrue($this->manager->isEnabledForUser('test', $user)); } - public function testIsEnabledForUserDisabled() { + public function testIsEnabledForUserDisabled(): void { $this->appConfig->setValue('test', 'enabled', 'no'); $user = $this->newUser('user1'); $this->assertFalse($this->manager->isEnabledForUser('test', $user)); } - public function testGetAppPath() { + public function testGetAppPath(): void { $this->assertEquals(\OC::$SERVERROOT . '/apps/files', $this->manager->getAppPath('files')); } - public function testGetAppPathSymlink() { + public function testGetAppPathSymlink(): void { $fakeAppDirname = sha1(uniqid('test', true)); $fakeAppPath = sys_get_temp_dir() . '/' . $fakeAppDirname; $fakeAppLink = \OC::$SERVERROOT . '/' . $fakeAppDirname; @@ -484,12 +487,12 @@ class AppManagerTest extends TestCase { $this->assertEquals($fakeAppLink . '/test-test-app', $generatedAppPath); } - public function testGetAppPathFail() { + public function testGetAppPathFail(): void { $this->expectException(AppPathNotFoundException::class); $this->manager->getAppPath('testnotexisting'); } - public function testIsEnabledForUserEnabledForGroup() { + public function testIsEnabledForUserEnabledForGroup(): void { $user = $this->newUser('user1'); $this->groupManager->expects($this->once()) ->method('getUserGroupIds') @@ -500,7 +503,7 @@ class AppManagerTest extends TestCase { $this->assertTrue($this->manager->isEnabledForUser('test', $user)); } - public function testIsEnabledForUserDisabledForGroup() { + public function testIsEnabledForUserDisabledForGroup(): void { $user = $this->newUser('user1'); $this->groupManager->expects($this->once()) ->method('getUserGroupIds') @@ -511,12 +514,12 @@ class AppManagerTest extends TestCase { $this->assertFalse($this->manager->isEnabledForUser('test', $user)); } - public function testIsEnabledForUserLoggedOut() { + public function testIsEnabledForUserLoggedOut(): void { $this->appConfig->setValue('test', 'enabled', '["foo"]'); $this->assertFalse($this->manager->isEnabledForUser('test')); } - public function testIsEnabledForUserLoggedIn() { + public function testIsEnabledForUserLoggedIn(): void { $user = $this->newUser('user1'); $this->userSession->expects($this->once()) @@ -531,7 +534,7 @@ class AppManagerTest extends TestCase { $this->assertTrue($this->manager->isEnabledForUser('test')); } - public function testGetInstalledApps() { + public function testGetEnabledApps(): void { $this->appConfig->setValue('test1', 'enabled', 'yes'); $this->appConfig->setValue('test2', 'enabled', 'no'); $this->appConfig->setValue('test3', 'enabled', '["foo"]'); @@ -542,6 +545,7 @@ class AppManagerTest extends TestCase { 'files', 'lookup_server_connector', 'oauth2', + 'profile', 'provisioning_api', 'settings', 'test1', @@ -551,10 +555,10 @@ class AppManagerTest extends TestCase { 'viewer', 'workflowengine', ]; - $this->assertEquals($apps, $this->manager->getInstalledApps()); + $this->assertEquals($apps, $this->manager->getEnabledApps()); } - public function testGetAppsForUser() { + public function testGetAppsForUser(): void { $user = $this->newUser('user1'); $this->groupManager->expects($this->any()) ->method('getUserGroupIds') @@ -572,6 +576,7 @@ class AppManagerTest extends TestCase { 'files', 'lookup_server_connector', 'oauth2', + 'profile', 'provisioning_api', 'settings', 'test1', @@ -584,7 +589,7 @@ class AppManagerTest extends TestCase { $this->assertEquals($enabled, $this->manager->getEnabledAppsForUser($user)); } - public function testGetAppsNeedingUpgrade() { + public function testGetAppsNeedingUpgrade(): void { /** @var AppManager|MockObject $manager */ $manager = $this->getMockBuilder(AppManager::class) ->setConstructorArgs([ @@ -594,6 +599,8 @@ class AppManagerTest extends TestCase { $this->cacheFactory, $this->eventDispatcher, $this->logger, + $this->serverVersion, + $this->configManager, ]) ->onlyMethods(['getAppInfo']) ->getMock(); @@ -603,6 +610,7 @@ class AppManagerTest extends TestCase { 'dav' => ['id' => 'dav'], 'files' => ['id' => 'files'], 'federatedfilesharing' => ['id' => 'federatedfilesharing'], + 'profile' => ['id' => 'profile'], 'provisioning_api' => ['id' => 'provisioning_api'], 'lookup_server_connector' => ['id' => 'lookup_server_connector'], 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '9.0.0'], @@ -642,7 +650,7 @@ class AppManagerTest extends TestCase { $this->assertEquals('test4', $apps[1]['id']); } - public function testGetIncompatibleApps() { + public function testGetIncompatibleApps(): void { /** @var AppManager|MockObject $manager */ $manager = $this->getMockBuilder(AppManager::class) ->setConstructorArgs([ @@ -652,6 +660,8 @@ class AppManagerTest extends TestCase { $this->cacheFactory, $this->eventDispatcher, $this->logger, + $this->serverVersion, + $this->configManager, ]) ->onlyMethods(['getAppInfo']) ->getMock(); @@ -661,6 +671,7 @@ class AppManagerTest extends TestCase { 'dav' => ['id' => 'dav'], 'files' => ['id' => 'files'], 'federatedfilesharing' => ['id' => 'federatedfilesharing'], + 'profile' => ['id' => 'profile'], 'provisioning_api' => ['id' => 'provisioning_api'], 'lookup_server_connector' => ['id' => 'lookup_server_connector'], 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '8.0.0'], @@ -694,7 +705,7 @@ class AppManagerTest extends TestCase { $this->assertEquals('test3', $apps[1]['id']); } - public function testGetEnabledAppsForGroup() { + public function testGetEnabledAppsForGroup(): void { $group = $this->createMock(IGroup::class); $group->expects($this->any()) ->method('getGID') @@ -711,6 +722,7 @@ class AppManagerTest extends TestCase { 'files', 'lookup_server_connector', 'oauth2', + 'profile', 'provisioning_api', 'settings', 'test1', @@ -723,7 +735,7 @@ class AppManagerTest extends TestCase { $this->assertEquals($enabled, $this->manager->getEnabledAppsForGroup($group)); } - public function testGetAppRestriction() { + public function testGetAppRestriction(): void { $this->appConfig->setValue('test1', 'enabled', 'yes'); $this->appConfig->setValue('test2', 'enabled', 'no'); $this->appConfig->setValue('test3', 'enabled', '["foo"]'); @@ -733,156 +745,145 @@ class AppManagerTest extends TestCase { $this->assertEquals(['foo'], $this->manager->getAppRestriction('test3')); } - public function provideDefaultApps(): array { + public static function isBackendRequiredDataProvider(): array { return [ - // none specified, default to files + // backend available [ - '', - '', - '{}', + 'caldav', + ['app1' => ['caldav']], true, - 'files', ], - // none specified, without fallback [ - '', - '', - '{}', - false, - '', - ], - // unexisting or inaccessible app specified, default to files - [ - 'unexist', - '', - '{}', + 'caldav', + ['app1' => [], 'app2' => ['foo'], 'app3' => ['caldav']], true, - 'files', ], - // unexisting or inaccessible app specified, without fallbacks + // backend not available [ - 'unexist', - '', - '{}', + 'caldav', + ['app3' => [], 'app1' => ['foo'], 'app2' => ['bar', 'baz']], false, - '', - ], - // non-standard app - [ - 'settings', - '', - '{}', - true, - 'settings', - ], - // non-standard app, without fallback - [ - 'settings', - '', - '{}', - false, - 'settings', - ], - // non-standard app with fallback - [ - 'unexist,settings', - '', - '{}', - true, - 'settings', - ], - // system default app and user apporder - [ - // system default is settings - 'unexist,settings', - '', - // apporder says default app is files (order is lower) - '{"files_id":{"app":"files","order":1},"settings_id":{"app":"settings","order":2}}', - true, - // system default should override apporder - 'settings' - ], - // user-customized defaultapp - [ - '', - 'files', - '', - true, - 'files', - ], - // user-customized defaultapp with systemwide - [ - 'unexist,settings', - 'files', - '', - true, - 'files', - ], - // user-customized defaultapp with system wide and apporder - [ - 'unexist,settings', - 'files', - '{"settings_id":{"app":"settings","order":1},"files_id":{"app":"files","order":2}}', - true, - 'files', - ], - // user-customized apporder fallback - [ - '', - '', - '{"settings_id":{"app":"settings","order":1},"files":{"app":"files","order":2}}', - true, - 'settings', - ], - // user-customized apporder fallback with missing app key (entries added by closures does not always have an app key set (Nextcloud 27 spreed app for example)) - [ - '', - '', - '{"spreed":{"order":1},"files":{"app":"files","order":2}}', - true, - 'files', ], - // user-customized apporder, but called without fallback + // no app available [ - '', - '', - '{"settings":{"app":"settings","order":1},"files":{"app":"files","order":2}}', + 'caldav', + [], false, - '', - ], - // user-customized apporder with an app that has multiple routes - [ - '', - '', - '{"settings_id":{"app":"settings","order":1},"settings_id_2":{"app":"settings","order":3},"id_files":{"app":"files","order":2}}', - true, - 'settings', ], ]; } - /** - * @dataProvider provideDefaultApps - */ - public function testGetDefaultAppForUser($defaultApps, $userDefaultApps, $userApporder, $withFallbacks, $expectedApp) { - $user = $this->newUser('user1'); + #[\PHPUnit\Framework\Attributes\DataProvider('isBackendRequiredDataProvider')] + public function testIsBackendRequired( + string $backend, + array $appBackends, + bool $expected, + ): void { + $appInfoData = array_map( + static fn (array $backends) => ['dependencies' => ['backend' => $backends]], + $appBackends, + ); - $this->userSession->expects($this->once()) - ->method('getUser') - ->willReturn($user); + $reflection = new \ReflectionClass($this->manager); + $property = $reflection->getProperty('appInfos'); + $property->setValue($this->manager, $appInfoData); - $this->config->expects($this->once()) - ->method('getSystemValueString') - ->with('defaultapp', $this->anything()) - ->willReturn($defaultApps); + $this->assertEquals($expected, $this->manager->isBackendRequired($backend)); + } - $this->config->expects($this->atLeastOnce()) - ->method('getUserValue') - ->willReturnMap([ - ['user1', 'core', 'defaultapp', '', $userDefaultApps], - ['user1', 'core', 'apporder', '[]', $userApporder], - ]); + public function testGetAppVersion() { + $manager = $this->getMockBuilder(AppManager::class) + ->setConstructorArgs([ + $this->userSession, + $this->config, + $this->groupManager, + $this->cacheFactory, + $this->eventDispatcher, + $this->logger, + $this->serverVersion, + $this->configManager, + ]) + ->onlyMethods([ + 'getAppInfo', + ]) + ->getMock(); + + $manager->expects(self::once()) + ->method('getAppInfo') + ->with('myapp') + ->willReturn(['version' => '99.99.99-rc.99']); + + $this->serverVersion + ->expects(self::never()) + ->method('getVersionString'); - $this->assertEquals($expectedApp, $this->manager->getDefaultAppForUser(null, $withFallbacks)); + $this->assertEquals( + '99.99.99-rc.99', + $manager->getAppVersion('myapp'), + ); } + + public function testGetAppVersionCore() { + $manager = $this->getMockBuilder(AppManager::class) + ->setConstructorArgs([ + $this->userSession, + $this->config, + $this->groupManager, + $this->cacheFactory, + $this->eventDispatcher, + $this->logger, + $this->serverVersion, + $this->configManager, + ]) + ->onlyMethods([ + 'getAppInfo', + ]) + ->getMock(); + + $manager->expects(self::never()) + ->method('getAppInfo'); + + $this->serverVersion + ->expects(self::once()) + ->method('getVersionString') + ->willReturn('1.2.3-beta.4'); + + $this->assertEquals( + '1.2.3-beta.4', + $manager->getAppVersion('core'), + ); + } + + public function testGetAppVersionUnknown() { + $manager = $this->getMockBuilder(AppManager::class) + ->setConstructorArgs([ + $this->userSession, + $this->config, + $this->groupManager, + $this->cacheFactory, + $this->eventDispatcher, + $this->logger, + $this->serverVersion, + $this->configManager, + ]) + ->onlyMethods([ + 'getAppInfo', + ]) + ->getMock(); + + $manager->expects(self::once()) + ->method('getAppInfo') + ->with('unknown') + ->willReturn(null); + + $this->serverVersion + ->expects(self::never()) + ->method('getVersionString'); + + $this->assertEquals( + '0', + $manager->getAppVersion('unknown'), + ); + } + } |