Skip to content

Commit

Permalink
[5.x] If nav:from references an invalid entry, ensure nothing is retu…
Browse files Browse the repository at this point in the history
…rned (#11464)
  • Loading branch information
ryanmitchell authored Feb 18, 2025
1 parent 69fb5f5 commit bb6da47
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Structures/TreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public function build($params)

$tree->withEntries();

$entry = ($from && $from !== '/') ? Entry::findByUri(Str::start($from, '/'), $params['site']) : null;
$entry = null;

if ($from && $from !== '/') {
if (! $entry = Entry::findByUri(Str::start($from, '/'), $params['site'])) {
return [];
}
}

if ($entry) {
$page = $tree->find($entry->id());
Expand Down
35 changes: 35 additions & 0 deletions tests/Tags/StructureTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,41 @@ public function it_sets_is_current_and_is_parent_for_a_collection()
$this->assertEquals('[1=parent][1-1=parent][1-1-1=parent][1-1-1-1=current][2]', $result);
}

#[Test]
public function it_doesnt_render_anything_when_nav_from_is_invalid()
{
$this->createCollectionAndNav();
Entry::shouldReceive('findByUri')->andReturn(null);

// The html uses <i> tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion.
$template = <<<'EOT'
<ul>
{{ nav from="something-invalid" }}
<li>
<i>{{ title }}</i>
{{ if children }}
<ul>
{{ *recursive children* }}
</ul>
{{ /if }}
</li>
{{ /nav }}
</ul>
EOT;

$expected = <<<'EOT'
<ul>
<li>
<i>outer title</i>
</li>
</ul>
EOT;

$this->assertXmlStringEqualsXmlString($expected, (string) Antlers::parse($template, [
'title' => 'outer title', // to test that cascade the page's data takes precedence over the cascading data.
]));
}

private function makeNav($tree)
{
$nav = Nav::make('test');
Expand Down

0 comments on commit bb6da47

Please sign in to comment.