Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/Http/Controllers/CP/Navigation/NavigationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function edit($nav)
$values = [
'title' => $nav->title(),
'handle' => $nav->handle(),
'collections' => $nav->collections()->map->handle()->all(),
'collections' => Arr::wrap($nav->collections()->map->handle()->all()),
'collections_query_scopes' => $nav->collectionsQueryScopes(),
'root' => $nav->expectsRoot(),
'sites' => $nav->trees()->keys()->all(),
Expand Down Expand Up @@ -138,7 +138,7 @@ public function show(Request $request, $nav)
'url' => $tree->showUrl(),
];
})->values()->all(),
'collections' => $nav->collections()->map->handle()->all(),
'collections' => Arr::wrap($nav->collections()->map->handle()->all()),
'entryQueryScopes' => $nav->collectionsQueryScopes(),
'initialMaxDepth' => $nav->maxDepth(),
'expectsRoot' => $nav->expectsRoot(),
Expand Down
103 changes: 103 additions & 0 deletions tests/Feature/Navigation/LinkToEntryWithMultipleCollectionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Tests\Feature\Navigation;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Collection;
use Statamic\Facades\Nav;
use Statamic\Facades\User;
use Tests\FakesRoles;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class LinkToEntryWithMultipleCollectionsTest extends TestCase
{
use FakesRoles;
use PreventSavingStacheItemsToDisk;

#[Test]
public function it_can_view_navigation_with_collections_as_array()
{
Collection::make('pages')->save();
Collection::make('articles')->save();

$nav = tap(Nav::make('test')->collections(['pages', 'articles']))->save();
$nav->makeTree('default')->save();

$this->setTestRoles(['test' => ['access cp', 'view test nav']]);
$user = tap(User::make()->assignRole('test'))->save();

$response = $this->actingAs($user)->get(cp_route('navigation.show', 'test'));

$response->assertOk();
$props = $response->viewData('page')['props'];
$this->assertIsArray($props['collections']);
$this->assertEquals(['pages', 'articles'], $props['collections']);
}

#[Test]
public function it_can_view_navigation_with_collections_as_string()
{
Collection::make('pages')->save();

// Simulate what happens when YAML has `collections: pages` instead of `collections: [pages]`
// The NavigationStore passes the raw YAML value (string) to the collections() setter
$nav = Nav::make('test');
$nav->collections('pages'); // Pass string directly
$nav->save();
$nav->makeTree('default')->save();

$this->setTestRoles(['test' => ['access cp', 'view test nav']]);
$user = tap(User::make()->assignRole('test'))->save();

$response = $this->actingAs($user)->get(cp_route('navigation.show', 'test'));

$response->assertOk();
$props = $response->viewData('page')['props'];
$this->assertIsArray($props['collections']);
$this->assertEquals(['pages'], $props['collections']);
}

#[Test]
public function it_can_edit_navigation_with_collections_as_string()
{
Collection::make('pages')->save();

// When a Nav is loaded from YAML with `collections: pages` (string),
// the NavigationStore passes that string to the collections() setter
$nav = Nav::make('test');
$nav->collections('pages'); // Pass string directly
$nav->save();

$this->setTestRoles(['test' => ['access cp', 'configure navs']]);
$user = tap(User::make()->assignRole('test'))->save();

$response = $this->actingAs($user)->get(cp_route('navigation.edit', 'test'));

$response->assertOk();
$this->assertIsArray($response->json('data.values.collections'));
$this->assertEquals(['pages'], $response->json('data.values.collections'));
}

#[Test]
public function it_can_get_page_selector_filters_when_navigation_has_multiple_collections()
{
Collection::make('pages')->save();
Collection::make('articles')->save();

// Test the full flow: navigation with multiple collections -> PageSelector -> filters endpoint
$this->setTestRoles(['test' => ['access cp', 'view pages entries', 'view articles entries']]);
$user = tap(User::make()->assignRole('test'))->save();

$config = base64_encode(json_encode([
'type' => 'entries',
'collections' => ['pages', 'articles'], // Array of two
]));

$response = $this->actingAs($user)->getJson("/cp/fieldtypes/relationship/filters?config={$config}");

$response->assertOk();
$handles = collect($response->json())->pluck('handle');
$this->assertTrue($handles->contains('collection'));
}
}
Loading