diff options
Diffstat (limited to 'tests/lib/NavigationManagerTest.php')
-rw-r--r-- | tests/lib/NavigationManagerTest.php | 337 |
1 files changed, 305 insertions, 32 deletions
diff --git a/tests/lib/NavigationManagerTest.php b/tests/lib/NavigationManagerTest.php index 1899fdd9443..53d7e81302b 100644 --- a/tests/lib/NavigationManagerTest.php +++ b/tests/lib/NavigationManagerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -11,6 +12,7 @@ use OC\App\AppManager; use OC\Group\Manager; use OC\NavigationManager; use OC\SubAdmin; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; @@ -18,6 +20,10 @@ use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; use OCP\L10N\IFactory; +use OCP\Navigation\Events\LoadAdditionalEntriesEvent; +use OCP\Util; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; class NavigationManagerTest extends TestCase { /** @var AppManager|\PHPUnit\Framework\MockObject\MockObject */ @@ -33,8 +39,11 @@ class NavigationManagerTest extends TestCase { /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ protected $config; - /** @var \OC\NavigationManager */ + protected IEVentDispatcher|MockObject $dispatcher; + + /** @var NavigationManager */ protected $navigationManager; + protected LoggerInterface $logger; protected function setUp(): void { parent::setUp(); @@ -45,22 +54,26 @@ class NavigationManagerTest extends TestCase { $this->userSession = $this->createMock(IUserSession::class); $this->groupManager = $this->createMock(Manager::class); $this->config = $this->createMock(IConfig::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->navigationManager = new NavigationManager( $this->appManager, $this->urlGenerator, $this->l10nFac, $this->userSession, $this->groupManager, - $this->config + $this->config, + $this->logger, + $this->dispatcher, ); $this->navigationManager->clear(false); } - public function addArrayData() { + public static function addArrayData(): array { return [ [ - 'entry id' => [ + 'entry' => [ 'id' => 'entry id', 'name' => 'link text', 'order' => 1, @@ -70,7 +83,7 @@ class NavigationManagerTest extends TestCase { 'classes' => '', 'unread' => 0 ], - 'entry id2' => [ + 'expectedEntry' => [ 'id' => 'entry id', 'name' => 'link text', 'order' => 1, @@ -83,7 +96,7 @@ class NavigationManagerTest extends TestCase { ] ], [ - 'entry id' => [ + 'entry' => [ 'id' => 'entry id', 'name' => 'link text', 'order' => 1, @@ -92,7 +105,7 @@ class NavigationManagerTest extends TestCase { 'active' => true, 'unread' => 0, ], - 'entry id2' => [ + 'expectedEntry' => [ 'id' => 'entry id', 'name' => 'link text', 'order' => 1, @@ -109,12 +122,12 @@ class NavigationManagerTest extends TestCase { } /** - * @dataProvider addArrayData * * @param array $entry * @param array $expectedEntry */ - public function testAddArray(array $entry, array $expectedEntry) { + #[\PHPUnit\Framework\Attributes\DataProvider('addArrayData')] + public function testAddArray(array $entry, array $expectedEntry): void { $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists'); $this->navigationManager->add($entry); @@ -127,12 +140,12 @@ class NavigationManagerTest extends TestCase { } /** - * @dataProvider addArrayData * * @param array $entry * @param array $expectedEntry */ - public function testAddClosure(array $entry, array $expectedEntry) { + #[\PHPUnit\Framework\Attributes\DataProvider('addArrayData')] + public function testAddClosure(array $entry, array $expectedEntry): void { global $testAddClosureNumberOfCalls; $testAddClosureNumberOfCalls = 0; @@ -159,7 +172,7 @@ class NavigationManagerTest extends TestCase { $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()'); } - public function testAddArrayClearGetAll() { + public function testAddArrayClearGetAll(): void { $entry = [ 'id' => 'entry id', 'name' => 'link text', @@ -174,7 +187,7 @@ class NavigationManagerTest extends TestCase { $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()'); } - public function testAddClosureClearGetAll() { + public function testAddClosureClearGetAll(): void { $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists'); $entry = [ @@ -202,10 +215,8 @@ class NavigationManagerTest extends TestCase { $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()'); } - /** - * @dataProvider providesNavigationConfig - */ - public function testWithAppManager($expected, $navigation, $isAdmin = false) { + #[\PHPUnit\Framework\Attributes\DataProvider('providesNavigationConfig')] + public function testWithAppManager($expected, $navigation, $isAdmin = false): void { $l = $this->createMock(IL10N::class); $l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); @@ -216,9 +227,9 @@ class NavigationManagerTest extends TestCase { ->willReturnArgument(3); $this->appManager->expects($this->any()) - ->method('isEnabledForUser') - ->with('theming') - ->willReturn(true); + ->method('isEnabledForUser') + ->with('theming') + ->willReturn(true); $this->appManager->expects($this->once()) ->method('getAppInfo') ->with('test') @@ -243,20 +254,25 @@ class NavigationManagerTest extends TestCase { $this->userSession->expects($this->any())->method('getUser')->willReturn($user); $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true); $this->appManager->expects($this->any()) - ->method('getEnabledAppsForUser') - ->with($user) - ->willReturn(['test']); + ->method('getEnabledAppsForUser') + ->with($user) + ->willReturn(['test']); $this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin); $subadmin = $this->createMock(SubAdmin::class); $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false); $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin); $this->navigationManager->clear(); + $this->dispatcher->expects($this->once()) + ->method('dispatchTyped') + ->willReturnCallback(function ($event): void { + $this->assertInstanceOf(LoadAdditionalEntriesEvent::class, $event); + }); $entries = $this->navigationManager->getAll('all'); $this->assertEquals($expected, $entries); } - public function providesNavigationConfig() { + public static function providesNavigationConfig(): array { $apps = [ 'core_apps' => [ 'id' => 'core_apps', @@ -307,7 +323,7 @@ class NavigationManagerTest extends TestCase { 'logout' => [ 'id' => 'logout', 'order' => 99999, - 'href' => 'https://example.com/logout?requesttoken='. urlencode(\OCP\Util::callRegister()), + 'href' => 'https://example.com/logout?requesttoken=' . urlencode(Util::callRegister()), 'icon' => '/apps/core/img/actions/logout.svg', 'name' => 'Log out', 'active' => false, @@ -485,7 +501,7 @@ class NavigationManagerTest extends TestCase { ]; } - public function testWithAppManagerAndApporder() { + public function testWithAppManagerAndApporder(): void { $l = $this->createMock(IL10N::class); $l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); @@ -525,9 +541,9 @@ class NavigationManagerTest extends TestCase { ); $this->appManager->expects($this->any()) - ->method('isEnabledForUser') - ->with('theming') - ->willReturn(true); + ->method('isEnabledForUser') + ->with('theming') + ->willReturn(true); $this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation); $this->appManager->expects($this->once())->method('getAppIcon')->with('test')->willReturn('/apps/test/img/app.svg'); $this->l10nFac->expects($this->any())->method('get')->willReturn($l); @@ -545,16 +561,273 @@ class NavigationManagerTest extends TestCase { $this->userSession->expects($this->any())->method('getUser')->willReturn($user); $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true); $this->appManager->expects($this->any()) - ->method('getEnabledAppsForUser') - ->with($user) - ->willReturn(['test']); + ->method('getEnabledAppsForUser') + ->with($user) + ->willReturn(['test']); $this->groupManager->expects($this->any())->method('isAdmin')->willReturn(false); $subadmin = $this->createMock(SubAdmin::class); $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false); $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin); $this->navigationManager->clear(); + $this->dispatcher->expects($this->once()) + ->method('dispatchTyped') + ->willReturnCallback(function ($event): void { + $this->assertInstanceOf(LoadAdditionalEntriesEvent::class, $event); + }); $entries = $this->navigationManager->getAll(); $this->assertEquals($expected, $entries); } + + public static function provideDefaultEntries(): array { + return [ + // none specified, default to files + [ + '', + '', + '{}', + true, + 'files', + ], + // none specified, without fallback + [ + '', + '', + '{}', + false, + '', + ], + // unexisting or inaccessible app specified, default to files + [ + 'unexist', + '', + '{}', + true, + 'files', + ], + // unexisting or inaccessible app specified, without fallbacks + [ + 'unexist', + '', + '{}', + 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 + [ + '', + '', + '{"settings":{"app":"settings","order":1},"files":{"app":"files","order":2}}', + 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', + ], + // closure navigation entries are also resolved + [ + 'closure2', + '', + '', + true, + 'closure2', + ], + [ + '', + 'closure2', + '', + true, + 'closure2', + ], + [ + '', + '', + '{"closure2":{"order":1,"app":"closure2","href":"/closure2"}}', + true, + 'closure2', + ], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('provideDefaultEntries')] + 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->atLeastOnce()) + ->method('getUser') + ->willReturn($user); + + $this->config->expects($this->atLeastOnce()) + ->method('getSystemValueString') + ->with('defaultapp', $this->anything()) + ->willReturn($defaultApps); + + $this->config->expects($this->atLeastOnce()) + ->method('getUserValue') + ->willReturnMap([ + ['user1', 'core', 'defaultapp', '', $userDefaultApps], + ['user1', 'core', 'apporder', '[]', $userApporder], + ]); + + $this->assertEquals($expectedApp, $this->navigationManager->getDefaultEntryIdForUser(null, $withFallbacks)); + } + + public function testDefaultEntryUpdated(): void { + $this->appManager->method('getEnabledApps')->willReturn([]); + + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn('user1'); + + $this->userSession + ->method('getUser') + ->willReturn($user); + + $this->config + ->method('getSystemValueString') + ->with('defaultapp', $this->anything()) + ->willReturn('app4,app3,app2,app1'); + + $this->config + ->method('getUserValue') + ->willReturnMap([ + ['user1', 'core', 'defaultapp', '', ''], + ['user1', 'core', 'apporder', '[]', ''], + ]); + + $this->navigationManager->add([ + 'id' => 'app1', + ]); + + $this->assertEquals('app1', $this->navigationManager->getDefaultEntryIdForUser(null, false)); + $this->assertEquals(true, $this->navigationManager->get('app1')['default']); + + $this->navigationManager->add([ + 'id' => 'app3', + ]); + + $this->assertEquals('app3', $this->navigationManager->getDefaultEntryIdForUser(null, false)); + $this->assertEquals(false, $this->navigationManager->get('app1')['default']); + $this->assertEquals(true, $this->navigationManager->get('app3')['default']); + + $this->navigationManager->add([ + 'id' => 'app2', + ]); + + $this->assertEquals('app3', $this->navigationManager->getDefaultEntryIdForUser(null, false)); + $this->assertEquals(false, $this->navigationManager->get('app1')['default']); + $this->assertEquals(false, $this->navigationManager->get('app2')['default']); + $this->assertEquals(true, $this->navigationManager->get('app3')['default']); + + $this->navigationManager->add([ + 'id' => 'app4', + ]); + + $this->assertEquals('app4', $this->navigationManager->getDefaultEntryIdForUser(null, false)); + $this->assertEquals(false, $this->navigationManager->get('app1')['default']); + $this->assertEquals(false, $this->navigationManager->get('app2')['default']); + $this->assertEquals(false, $this->navigationManager->get('app3')['default']); + $this->assertEquals(true, $this->navigationManager->get('app4')['default']); + } } |