From 4f8e916585889b99247d967d51325ac0c840ec59 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Wed, 29 Mar 2023 13:36:45 -0700 Subject: [PATCH] Add label for logo link Signed-off-by: Christopher Ng --- core/templates/layout.user.php | 7 +-- lib/private/App/AppManager.php | 24 ++++++++++ lib/private/TemplateLayout.php | 8 +++- lib/private/URLGenerator.php | 18 +------ lib/public/App/IAppManager.php | 9 ++++ tests/lib/App/AppManagerTest.php | 43 +++++++++++++++++ tests/lib/UrlGeneratorTest.php | 80 ++++---------------------------- 7 files changed, 96 insertions(+), 93 deletions(-) diff --git a/core/templates/layout.user.php b/core/templates/layout.user.php index 7c2625bbaaf0b..1a55c0ab64d9e 100644 --- a/core/templates/layout.user.php +++ b/core/templates/layout.user.php @@ -62,12 +62,9 @@
- + diff --git a/lib/private/App/AppManager.php b/lib/private/App/AppManager.php index 5f243a1250ed3..9b038b7382689 100644 --- a/lib/private/App/AppManager.php +++ b/lib/private/App/AppManager.php @@ -827,4 +827,28 @@ public function getDefaultEnabledApps():array { return $this->defaultEnabled; } + + public function getDefaultAppForUser(?IUser $user = null): string { + // Set fallback to always-enabled files app + $appId = 'files'; + $defaultApps = explode(',', $this->config->getSystemValueString('defaultapp', 'dashboard,files')); + + $user ??= $this->userSession->getUser(); + + if ($user !== null) { + $userDefaultApps = explode(',', $this->config->getUserValue($user->getUID(), 'core', 'defaultapp')); + $defaultApps = array_filter(array_merge($userDefaultApps, $defaultApps)); + } + + // Find the first app that is enabled for the current user + foreach ($defaultApps as $defaultApp) { + $defaultApp = \OC_App::cleanAppId(strip_tags($defaultApp)); + if ($this->isEnabledForUser($defaultApp, $user)) { + $appId = $defaultApp; + break; + } + } + + return $appId; + } } diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index d127443944f55..123fd6debb56d 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -119,10 +119,16 @@ public function __construct($renderAs, $appId = '') { $this->assign('enabledThemes', $themesService->getEnabledThemes()); } - // set logo link target + // Set logo link target $logoUrl = $this->config->getSystemValueString('logo_url', ''); $this->assign('logoUrl', $logoUrl); + // Set default app name + $defaultApp = \OC::$server->getAppManager()->getDefaultAppForUser(); + $defaultAppInfo = \OC::$server->getAppManager()->getAppInfo($defaultApp); + $l10n = \OC::$server->getL10NFactory()->get($defaultApp); + $this->assign('defaultAppName', $l10n->t($defaultAppInfo['name'])); + // Add navigation entry $this->assign('application', ''); $this->assign('appid', $appId); diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php index 7be2895a1ef21..a5a3609703b09 100644 --- a/lib/private/URLGenerator.php +++ b/lib/private/URLGenerator.php @@ -311,23 +311,7 @@ public function linkToDefaultPageUrl(): string { return $this->getAbsoluteURL($defaultPage); } - $appId = 'files'; - $defaultApps = explode(',', $this->config->getSystemValue('defaultapp', 'dashboard,files')); - - $userId = $this->userSession->isLoggedIn() ? $this->userSession->getUser()->getUID() : null; - if ($userId !== null) { - $userDefaultApps = explode(',', $this->config->getUserValue($userId, 'core', 'defaultapp')); - $defaultApps = array_filter(array_merge($userDefaultApps, $defaultApps)); - } - - // find the first app that is enabled for the current user - foreach ($defaultApps as $defaultApp) { - $defaultApp = \OC_App::cleanAppId(strip_tags($defaultApp)); - if (\OC::$server->getAppManager()->isEnabledForUser($defaultApp)) { - $appId = $defaultApp; - break; - } - } + $appId = $this->getAppManager()->getDefaultAppForUser(); if ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true') { diff --git a/lib/public/App/IAppManager.php b/lib/public/App/IAppManager.php index faaf871a74cc9..6eeeed2da6970 100644 --- a/lib/public/App/IAppManager.php +++ b/lib/public/App/IAppManager.php @@ -242,4 +242,13 @@ public function getEnabledAppsForGroup(IGroup $group): array; * @since 17.0.0 */ public function getAppRestriction(string $appId): array; + + /** + * Returns the id of the user's default app + * + * If `user` is not passed, the currently logged in user will be used + * + * @since 25.0.6 + */ + public function getDefaultAppForUser(?IUser $user = null): string; } diff --git a/tests/lib/App/AppManagerTest.php b/tests/lib/App/AppManagerTest.php index bf9592ac6a685..3518ada331436 100644 --- a/tests/lib/App/AppManagerTest.php +++ b/tests/lib/App/AppManagerTest.php @@ -617,4 +617,47 @@ public function testGetAppRestriction() { $this->assertEquals([], $this->manager->getAppRestriction('test2')); $this->assertEquals(['foo'], $this->manager->getAppRestriction('test3')); } + + public function provideDefaultApps(): array { + return [ + // none specified, default to files + [ + '', + 'files', + ], + // unexisting or inaccessible app specified, default to files + [ + 'unexist', + 'files', + ], + // non-standard app + [ + 'settings', + 'settings', + ], + // non-standard app with fallback + [ + 'unexist,settings', + 'settings', + ], + ]; + } + + /** + * @dataProvider provideDefaultApps + */ + public function testGetDefaultAppForUser($defaultApps, $expectedApp) { + $user = $this->newUser('user1'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + + $this->config->expects($this->once()) + ->method('getSystemValueString') + ->with('defaultapp', $this->anything()) + ->willReturn($defaultApps); + + $this->assertEquals($expectedApp, $this->manager->getDefaultAppForUser()); + } } diff --git a/tests/lib/UrlGeneratorTest.php b/tests/lib/UrlGeneratorTest.php index 420b2fe4eb9dc..523616b45328a 100644 --- a/tests/lib/UrlGeneratorTest.php +++ b/tests/lib/UrlGeneratorTest.php @@ -13,7 +13,6 @@ use OCP\IConfig; use OCP\IRequest; use OCP\IURLGenerator; -use OCP\IUser; use OCP\IUserSession; /** @@ -216,21 +215,16 @@ public function provideOCSRoutes() { ]; } - private function mockLinkToDefaultPageUrl(string $defaultAppConfig = '', bool $ignoreFrontControllerConfig = false) { - $this->config->expects($this->exactly(2)) - ->method('getSystemValue') - ->withConsecutive( - ['defaultapp', $this->anything()], - ['htaccess.IgnoreFrontController', $this->anything()], - ) - ->will($this->onConsecutiveCalls( - $defaultAppConfig, - $ignoreFrontControllerConfig - )); + private function mockLinkToDefaultPageUrl(bool $ignoreFrontControllerConfig = false) { $this->config->expects($this->once()) ->method('getAppValue') ->with('core', 'defaultpage') ->willReturn(''); + + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with('htaccess.IgnoreFrontController', $this->anything()) + ->willReturn($ignoreFrontControllerConfig); } public function testLinkToDefaultPageUrlWithRedirectUrlWithoutFrontController() { @@ -246,7 +240,7 @@ public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFron putenv('front_controller_active=false'); $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl()); } public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() { @@ -255,70 +249,16 @@ public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontCo putenv('front_controller_active=true'); $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl()); } public function testLinkToDefaultPageUrlWithRedirectUrlWithIgnoreFrontController() { $this->mockBaseUrl(); - $this->mockLinkToDefaultPageUrl('', true); + $this->mockLinkToDefaultPageUrl(true); putenv('front_controller_active=false'); $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a'; - $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/files/', $this->urlGenerator->linkToDefaultPageUrl()); - } - - /** - * @dataProvider provideDefaultApps - */ - public function testLinkToDefaultPageUrlWithDefaultApps($defaultAppConfig, $expectedPath) { - $userId = $this->getUniqueID(); - - /** @var \PHPUnit\Framework\MockObject\MockObject|IUser $userMock */ - $userMock = $this->createMock(IUser::class); - $userMock->expects($this->once()) - ->method('getUID') - ->willReturn($userId); - - $this->mockBaseUrl(); - $this->mockLinkToDefaultPageUrl($defaultAppConfig); - - $this->config->expects($this->once()) - ->method('getUserValue') - ->with($userId, 'core', 'defaultapp') - ->willReturn(''); - $this->userSession->expects($this->once()) - ->method('isLoggedIn') - ->willReturn(true); - $this->userSession->expects($this->once()) - ->method('getUser') - ->willReturn($userMock); - - $this->assertEquals('http://localhost' . \OC::$WEBROOT . $expectedPath, $this->urlGenerator->linkToDefaultPageUrl()); - } - - public function provideDefaultApps(): array { - return [ - // none specified, default to files - [ - '', - '/index.php/apps/files/', - ], - // unexisting or inaccessible app specified, default to files - [ - 'unexist', - '/index.php/apps/files/', - ], - // non-standard app - [ - 'settings', - '/index.php/apps/settings/', - ], - // non-standard app with fallback - [ - 'unexist,settings', - '/index.php/apps/settings/', - ], - ]; + $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl()); } public function imagePathProvider(): array {