Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,4 @@
<dependencies>
<nextcloud min-version="36" max-version="36"/>
</dependencies>
<navigations>
<navigation>
<id>office</id>
<name>Office</name>
<route>office.page.index</route>
<icon>app.svg</icon>
<type>link</type>
</navigation>
</navigations>
</info>
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand Down
7 changes: 5 additions & 2 deletions lib/Listener/AppMenuActionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
}

Expand Down
53 changes: 53 additions & 0 deletions lib/Listener/LoadNavigationEntryListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace OCA\Office\Listener;

use OCA\Office\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\Navigation\Events\LoadAdditionalEntriesEvent;

/**
* Registers the app's main navigation entry. It is added here rather than in
* info.xml so that the name stays the untranslated product name "Office".
*
* @template-implements IEventListener<LoadAdditionalEntriesEvent>
*/
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,
) {
}

#[\Override]
public function handle(Event $event): void {
if (!$event instanceof LoadAdditionalEntriesEvent) {
return;
}

/** @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',
];
});
}
}
14 changes: 14 additions & 0 deletions tests/unit/Listener/AppMenuActionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -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');

Expand Down
69 changes: 69 additions & 0 deletions tests/unit/Listener/LoadNavigationEntryListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Listener;

use OCA\Office\Listener\LoadNavigationEntryListener;
use OCP\App\IAppManager;
use OCP\EventDispatcher\Event;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\Navigation\Events\LoadAdditionalEntriesEvent;
use PHPUnit\Framework\MockObject\MockObject;
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')->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->appManager, $this->navigationManager, $urlGenerator);
}

private function registeredEntry(): ?array {
$entry = null;
$this->navigationManager->expects($this->once())
->method('add')
->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 {
$this->navigationManager->expects($this->never())->method('add');

$this->listener->handle(new Event());
}
}
Loading