Skip to content

Commit 40e52e2

Browse files
Merge pull request #52434 from nextcloud/bugfix/52420/closure-as-default-app
fix(navigation): Fix default app entry registered as closure
2 parents 0d19a13 + cdfce26 commit 40e52e2

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

lib/private/NavigationManager.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function add($entry) {
7777
$this->closureEntries[] = $entry;
7878
return;
7979
}
80-
$this->init();
80+
$this->init(false);
8181

8282
$id = $entry['id'];
8383

@@ -123,10 +123,6 @@ private function updateDefaultEntries() {
123123
*/
124124
public function getAll(string $type = 'link'): array {
125125
$this->init();
126-
foreach ($this->closureEntries as $c) {
127-
$this->add($c());
128-
}
129-
$this->closureEntries = [];
130126

131127
$result = $this->entries;
132128
if ($type !== 'all') {
@@ -212,7 +208,13 @@ public function getActiveEntry() {
212208
return $this->activeEntry;
213209
}
214210

215-
private function init() {
211+
private function init(bool $resolveClosures = true): void {
212+
if ($resolveClosures) {
213+
while ($c = array_pop($this->closureEntries)) {
214+
$this->add($c());
215+
}
216+
}
217+
216218
if ($this->init) {
217219
return;
218220
}
@@ -420,11 +422,6 @@ public function setUnreadCounter(string $id, int $unreadCounter): void {
420422

421423
public function get(string $id): ?array {
422424
$this->init();
423-
foreach ($this->closureEntries as $c) {
424-
$this->add($c());
425-
}
426-
$this->closureEntries = [];
427-
428425
return $this->entries[$id];
429426
}
430427

lib/private/URLGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ public function linkToDefaultPageUrl(): string {
304304
if ($href === '') {
305305
throw new \InvalidArgumentException('Default navigation entry is missing href: ' . $entryId);
306306
}
307+
308+
if (str_starts_with($href, $this->getBaseUrl())) {
309+
return $href;
310+
}
311+
307312
if (str_starts_with($href, '/index.php/') && ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) || getenv('front_controller_active') === 'true')) {
308313
$href = substr($href, 10);
309314
}

tests/lib/NavigationManagerTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,30 +704,64 @@ public static function provideDefaultEntries(): array {
704704
true,
705705
'settings',
706706
],
707+
// closure navigation entries are also resolved
708+
[
709+
'closure2',
710+
'',
711+
'',
712+
true,
713+
'closure2',
714+
],
715+
[
716+
'',
717+
'closure2',
718+
'',
719+
true,
720+
'closure2',
721+
],
722+
[
723+
'',
724+
'',
725+
'{"closure2":{"order":1,"app":"closure2","href":"/closure2"}}',
726+
true,
727+
'closure2',
728+
],
707729
];
708730
}
709731

710732
/**
711733
* @dataProvider provideDefaultEntries
712734
*/
713-
public function testGetDefaultEntryIdForUser($defaultApps, $userDefaultApps, $userApporder, $withFallbacks, $expectedApp): void {
735+
public function testGetDefaultEntryIdForUser(string $defaultApps, string $userDefaultApps, string $userApporder, bool $withFallbacks, string $expectedApp): void {
714736
$this->navigationManager->add([
715737
'id' => 'files',
716738
]);
717739
$this->navigationManager->add([
718740
'id' => 'settings',
719741
]);
742+
$this->navigationManager->add(static function (): array {
743+
return [
744+
'id' => 'closure1',
745+
'href' => '/closure1',
746+
];
747+
});
748+
$this->navigationManager->add(static function (): array {
749+
return [
750+
'id' => 'closure2',
751+
'href' => '/closure2',
752+
];
753+
});
720754

721755
$this->appManager->method('getEnabledApps')->willReturn([]);
722756

723757
$user = $this->createMock(IUser::class);
724758
$user->method('getUID')->willReturn('user1');
725759

726-
$this->userSession->expects($this->once())
760+
$this->userSession->expects($this->atLeastOnce())
727761
->method('getUser')
728762
->willReturn($user);
729763

730-
$this->config->expects($this->once())
764+
$this->config->expects($this->atLeastOnce())
731765
->method('getSystemValueString')
732766
->with('defaultapp', $this->anything())
733767
->willReturn($defaultApps);

0 commit comments

Comments
 (0)