diff --git a/src/Tags/Nav.php b/src/Tags/Nav.php index fc1ff19ccea..ee09c0b7761 100644 --- a/src/Tags/Nav.php +++ b/src/Tags/Nav.php @@ -23,6 +23,7 @@ public function breadcrumbs() if (! $this->params->bool('include_home', true)) { array_shift($segments); + $segments = array_values(array_filter($segments, fn ($segment) => $segment !== '')); } $crumbs = collect($segments)->map(function () use (&$segments) { diff --git a/tests/Tags/NavBreadcrumbsTest.php b/tests/Tags/NavBreadcrumbsTest.php new file mode 100644 index 00000000000..d91c0877274 --- /dev/null +++ b/tests/Tags/NavBreadcrumbsTest.php @@ -0,0 +1,78 @@ +routes('{parent_uri}/{slug}')->structureContents(['root' => true]))->save(); + + EntryFactory::collection('pages')->id('home')->slug('home')->data(['title' => 'Home'])->create(); + EntryFactory::collection('pages')->id('about')->slug('about')->data(['title' => 'About'])->create(); + EntryFactory::collection('pages')->id('team')->slug('team')->data(['title' => 'Team'])->create(); + + $collection->structure()->in('en')->tree([ + ['entry' => 'home'], + ['entry' => 'about', 'children' => [ + ['entry' => 'team'], + ]], + ])->save(); + } + + private function tag($tag) + { + return (string) Parse::template($tag, [], trusted: true); + } + + #[Test] + public function it_includes_home_by_default() + { + $this->get('/about/team'); + + $titles = $this->tag('{{ nav:breadcrumbs }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('Home|About|Team|', $titles); + } + + #[Test] + public function it_excludes_home_when_include_home_is_false() + { + $this->get('/about/team'); + + $titles = $this->tag('{{ nav:breadcrumbs include_home="false" }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('About|Team|', $titles); + } + + #[Test] + public function it_excludes_home_when_include_home_is_false_on_a_top_level_page() + { + $this->get('/about'); + + $titles = $this->tag('{{ nav:breadcrumbs include_home="false" }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('About|', $titles); + } + + #[Test] + public function it_returns_no_breadcrumbs_on_the_home_page_when_include_home_is_false() + { + $this->get('/'); + + $titles = $this->tag('{{ nav:breadcrumbs include_home="false" }}{{ title }}|{{ /nav:breadcrumbs }}'); + + $this->assertSame('', $titles); + } +}