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
43 changes: 43 additions & 0 deletions lib/Listener/LoadNavigationEntryListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace OCA\Office\Listener;

use OCA\Office\AppInfo\Application;
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 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',
]);
}
}
46 changes: 46 additions & 0 deletions tests/unit/Listener/LoadNavigationEntryListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Listener;

use OCA\Office\Listener\LoadNavigationEntryListener;
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 INavigationManager&MockObject $navigationManager;
private LoadNavigationEntryListener $listener;

protected function setUp(): void {
parent::setUp();

$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');

$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());
}
}
Loading