From 9ccce57d88d9037ac7629f585aed647158ed7dba Mon Sep 17 00:00:00 2001 From: Julius Knorr Date: Thu, 15 Oct 2026 08:37:16 +0200 Subject: [PATCH 1/3] fix(navigation): register app menu entry in PHP to keep name untranslated Assisted-by: GitHubCopilot:copilot-swe-agent Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Julius Knorr --- appinfo/info.xml | 9 ---- lib/AppInfo/Application.php | 2 + lib/Listener/LoadNavigationEntryListener.php | 43 +++++++++++++++++ .../LoadNavigationEntryListenerTest.php | 46 +++++++++++++++++++ 4 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 lib/Listener/LoadNavigationEntryListener.php create mode 100644 tests/unit/Listener/LoadNavigationEntryListenerTest.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 6e5846a1..a872b36c 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -14,13 +14,4 @@ - - - office - Office - office.page.index - app.svg - link - - diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 68a60099..501f131b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -5,6 +5,7 @@ namespace OCA\Office\AppInfo; use OCA\Office\Listener\AppMenuActionListener; +use OCA\Office\Listener\LoadNavigationEntryListener; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; @@ -22,6 +23,7 @@ public function __construct() { #[\Override] public function register(IRegistrationContext $context): void { $context->registerEventListener(LoadAdditionalEntriesEvent::class, AppMenuActionListener::class); + $context->registerEventListener(LoadAdditionalEntriesEvent::class, LoadNavigationEntryListener::class); } #[\Override] diff --git a/lib/Listener/LoadNavigationEntryListener.php b/lib/Listener/LoadNavigationEntryListener.php new file mode 100644 index 00000000..6fee3d1e --- /dev/null +++ b/lib/Listener/LoadNavigationEntryListener.php @@ -0,0 +1,43 @@ + + */ +final class LoadNavigationEntryListener implements IEventListener { + /** @psalm-suppress PossiblyUnusedMethod Constructed by the DI container */ + public function __construct( + private INavigationManager $navigationManager, + private IURLGenerator $urlGenerator, + ) { + } + + #[\Override] + public function handle(Event $event): void { + if (!$event instanceof LoadAdditionalEntriesEvent) { + return; + } + + $this->navigationManager->add([ + 'id' => Application::APP_ID, + 'type' => 'link', + 'order' => 10, + 'href' => $this->urlGenerator->linkToRoute(Application::APP_ID . '.page.index'), + 'icon' => $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'), + 'name' => 'Office', + ]); + } +} diff --git a/tests/unit/Listener/LoadNavigationEntryListenerTest.php b/tests/unit/Listener/LoadNavigationEntryListenerTest.php new file mode 100644 index 00000000..ecceec0f --- /dev/null +++ b/tests/unit/Listener/LoadNavigationEntryListenerTest.php @@ -0,0 +1,46 @@ +navigationManager = $this->createMock(INavigationManager::class); + $urlGenerator = $this->createMock(IURLGenerator::class); + $urlGenerator->method('linkToRoute')->willReturn('/apps/office/'); + $urlGenerator->method('imagePath')->willReturn('/apps/office/img/app.svg'); + + $this->listener = new LoadNavigationEntryListener($this->navigationManager, $urlGenerator); + } + + public function testRegistersTheUntranslatedAppEntry(): void { + $this->navigationManager->expects($this->once()) + ->method('add') + ->with($this->callback(static fn (array $entry): bool => $entry['id'] === 'office' + && $entry['name'] === 'Office' + && $entry['href'] === '/apps/office/' + && $entry['icon'] === '/apps/office/img/app.svg')); + + $this->listener->handle(new LoadAdditionalEntriesEvent()); + } + + public function testIgnoresOtherEvents(): void { + $this->navigationManager->expects($this->never())->method('add'); + + $this->listener->handle(new Event()); + } +} From 89c8d44addb130fd971b074dc1daa9286e64d243 Mon Sep 17 00:00:00 2001 From: Julius Knorr Date: Fri, 6 Nov 2026 12:14:57 +0100 Subject: [PATCH 2/3] fix(navigation): only show the app entry to users it is enabled for Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Julius Knorr --- lib/Listener/LoadNavigationEntryListener.php | 26 +++++++++---- .../LoadNavigationEntryListenerTest.php | 39 +++++++++++++++---- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/lib/Listener/LoadNavigationEntryListener.php b/lib/Listener/LoadNavigationEntryListener.php index 6fee3d1e..5cef6b4f 100644 --- a/lib/Listener/LoadNavigationEntryListener.php +++ b/lib/Listener/LoadNavigationEntryListener.php @@ -5,6 +5,7 @@ namespace OCA\Office\Listener; use OCA\Office\AppInfo\Application; +use OCP\App\IAppManager; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\INavigationManager; @@ -20,6 +21,7 @@ final class LoadNavigationEntryListener implements IEventListener { /** @psalm-suppress PossiblyUnusedMethod Constructed by the DI container */ public function __construct( + private IAppManager $appManager, private INavigationManager $navigationManager, private IURLGenerator $urlGenerator, ) { @@ -31,13 +33,21 @@ public function handle(Event $event): void { return; } - $this->navigationManager->add([ - 'id' => Application::APP_ID, - 'type' => 'link', - 'order' => 10, - 'href' => $this->urlGenerator->linkToRoute(Application::APP_ID . '.page.index'), - 'icon' => $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'), - 'name' => 'Office', - ]); + /** @psalm-suppress InvalidArgument OCP types the closure as non-nullable, but NavigationManager skips null entries */ + $this->navigationManager->add(function (): ?array { + if (!$this->appManager->isEnabledForUser(Application::APP_ID)) { + return null; + } + + return [ + 'id' => Application::APP_ID, + 'app' => Application::APP_ID, + 'type' => INavigationManager::TYPE_APPS, + 'order' => 10, + 'href' => $this->urlGenerator->linkToRoute(Application::APP_ID . '.page.index'), + 'icon' => $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'), + 'name' => 'Office', + ]; + }); } } diff --git a/tests/unit/Listener/LoadNavigationEntryListenerTest.php b/tests/unit/Listener/LoadNavigationEntryListenerTest.php index ecceec0f..6b664004 100644 --- a/tests/unit/Listener/LoadNavigationEntryListenerTest.php +++ b/tests/unit/Listener/LoadNavigationEntryListenerTest.php @@ -5,6 +5,7 @@ namespace Listener; use OCA\Office\Listener\LoadNavigationEntryListener; +use OCP\App\IAppManager; use OCP\EventDispatcher\Event; use OCP\INavigationManager; use OCP\IURLGenerator; @@ -13,29 +14,51 @@ use PHPUnit\Framework\TestCase; final class LoadNavigationEntryListenerTest extends TestCase { + private IAppManager&MockObject $appManager; private INavigationManager&MockObject $navigationManager; private LoadNavigationEntryListener $listener; protected function setUp(): void { parent::setUp(); + $this->appManager = $this->createMock(IAppManager::class); $this->navigationManager = $this->createMock(INavigationManager::class); $urlGenerator = $this->createMock(IURLGenerator::class); - $urlGenerator->method('linkToRoute')->willReturn('/apps/office/'); - $urlGenerator->method('imagePath')->willReturn('/apps/office/img/app.svg'); + $urlGenerator->method('linkToRoute')->with('office.page.index')->willReturn('/apps/office/'); + $urlGenerator->method('imagePath')->with('office', 'app.svg')->willReturn('/apps/office/img/app.svg'); - $this->listener = new LoadNavigationEntryListener($this->navigationManager, $urlGenerator); + $this->listener = new LoadNavigationEntryListener($this->appManager, $this->navigationManager, $urlGenerator); } - public function testRegistersTheUntranslatedAppEntry(): void { + private function registeredEntry(): ?array { + $entry = null; $this->navigationManager->expects($this->once()) ->method('add') - ->with($this->callback(static fn (array $entry): bool => $entry['id'] === 'office' - && $entry['name'] === 'Office' - && $entry['href'] === '/apps/office/' - && $entry['icon'] === '/apps/office/img/app.svg')); + ->willReturnCallback(static function (\Closure $closure) use (&$entry): void { + $entry = $closure(); + }); $this->listener->handle(new LoadAdditionalEntriesEvent()); + + return $entry; + } + + public function testRegistersTheUntranslatedAppEntry(): void { + $this->appManager->method('isEnabledForUser')->with('office')->willReturn(true); + + $entry = $this->registeredEntry(); + + $this->assertSame('office', $entry['id']); + $this->assertSame('office', $entry['app']); + $this->assertSame('Office', $entry['name']); + $this->assertSame('/apps/office/', $entry['href']); + $this->assertSame('/apps/office/img/app.svg', $entry['icon']); + } + + public function testSkipsEntryForUsersWithoutAccess(): void { + $this->appManager->method('isEnabledForUser')->with('office')->willReturn(false); + + $this->assertNull($this->registeredEntry()); } public function testIgnoresOtherEvents(): void { From 2cf8ffd948dd92a51a2c36df821f01a271a0f9ec Mon Sep 17 00:00:00 2001 From: Julius Knorr Date: Fri, 6 Nov 2026 12:27:01 +0100 Subject: [PATCH 3/3] fix(navigation): only add app menu actions for users the app is enabled for Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Julius Knorr --- lib/Listener/AppMenuActionListener.php | 7 +++++-- tests/unit/Listener/AppMenuActionListenerTest.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/Listener/AppMenuActionListener.php b/lib/Listener/AppMenuActionListener.php index d4eeb32d..2a8b1598 100644 --- a/lib/Listener/AppMenuActionListener.php +++ b/lib/Listener/AppMenuActionListener.php @@ -6,6 +6,7 @@ use OCA\Office\AppInfo\Application; use OCA\Office\Service\CreatorCategoryService; +use OCP\App\IAppManager; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\INavigationManager; @@ -22,6 +23,7 @@ final class AppMenuActionListener implements IEventListener { /** @psalm-suppress PossiblyUnusedMethod Constructed by the DI container */ public function __construct( + private IAppManager $appManager, private CreatorCategoryService $categoryService, private INavigationManager $navigationManager, private IURLGenerator $urlGenerator, @@ -35,8 +37,9 @@ public function handle(Event $event): void { return; } - // Creators are registered per user, so there is nothing to offer a guest. - if (!$this->userSession->isLoggedIn()) { + // Creators are registered per user, so there is nothing to offer a guest, + // nor to a user outside the groups the app is limited to. + if (!$this->userSession->isLoggedIn() || !$this->appManager->isEnabledForUser(Application::APP_ID)) { return; } diff --git a/tests/unit/Listener/AppMenuActionListenerTest.php b/tests/unit/Listener/AppMenuActionListenerTest.php index 94ee20da..7ed84640 100644 --- a/tests/unit/Listener/AppMenuActionListenerTest.php +++ b/tests/unit/Listener/AppMenuActionListenerTest.php @@ -6,6 +6,7 @@ use OCA\Office\Listener\AppMenuActionListener; use OCA\Office\Service\CreatorCategoryService; +use OCP\App\IAppManager; use OCP\EventDispatcher\Event; use OCP\Files\Template\ITemplateManager; use OCP\Files\Template\TemplateFileCreator; @@ -20,6 +21,7 @@ use PHPUnit\Framework\TestCase; final class AppMenuActionListenerTest extends TestCase { + private bool $appEnabled = true; private ITemplateManager&MockObject $templateManager; private INavigationManager&MockObject $navigationManager; private IUserSession&MockObject $userSession; @@ -30,6 +32,8 @@ protected function setUp(): void { $l10n = $this->createMock(IL10N::class); $l10n->method('t')->willReturnArgument(0); + $appManager = $this->createMock(IAppManager::class); + $appManager->method('isEnabledForUser')->willReturnCallback(fn (string $app): bool => $app === 'office' && $this->appEnabled); $this->templateManager = $this->createMock(ITemplateManager::class); $this->navigationManager = $this->createMock(INavigationManager::class); $this->userSession = $this->createMock(IUserSession::class); @@ -43,6 +47,7 @@ protected function setUp(): void { ->willReturnCallback(static fn (string $app, string $image): string => "/apps/$app/img/$image"); $this->listener = new AppMenuActionListener( + $appManager, $this->categoryService($l10n, $this->userSession), $this->navigationManager, $urlGenerator, @@ -177,6 +182,7 @@ public function testIgnoresGuests(): void { $userSession->method('getUser')->willReturn(null); $l10n = $this->createMock(IL10N::class); $listener = new AppMenuActionListener( + $this->createMock(IAppManager::class), $this->categoryService($l10n, $userSession), $this->navigationManager, $this->createMock(IURLGenerator::class), @@ -188,6 +194,14 @@ public function testIgnoresGuests(): void { $listener->handle(new LoadAdditionalEntriesEvent()); } + public function testIgnoresUsersTheAppIsNotEnabledFor(): void { + $this->appEnabled = false; + $this->templateManager->expects($this->never())->method('listCreators'); + $this->navigationManager->expects($this->never())->method('add'); + + $this->listener->handle(new LoadAdditionalEntriesEvent()); + } + public function testIgnoresOtherEvents(): void { $this->navigationManager->expects($this->never())->method('add');