Skip to content

Commit 7d0ae64

Browse files
committed
Handle nav tag without handle param
1 parent 6ac81a8 commit 7d0ae64

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/Http/Middleware/CacheTracker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private function setupTagHooks()
181181
});
182182

183183
Tags\Nav::hook('init', function ($value, $next) use ($self) {
184-
$handle = $this->params->get('handle') ? 'nav:'.$this->params->get('handle') : $this->tag;
184+
$handle = 'nav:'.$this->params->get('handle', 'collection::pages');
185185
$self->addContentTag($handle);
186186

187187
return $next($value);

tests/Unit/NavTagTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Thoughtco\StatamicCacheTracker\Tests\Unit;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Statamic\Facades;
7+
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
8+
use Thoughtco\StatamicCacheTracker\Tests\TestCase;
9+
10+
class NavTagTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_tracks_nav_tag_with_explicit_handle()
14+
{
15+
// Create a navigation structure
16+
Facades\Nav::make('footer')
17+
->title('Footer')
18+
->expectsRoot(true)
19+
->collections(['pages'])
20+
->save();
21+
22+
$view = <<<'BLADE'
23+
{{ nav handle="footer" }}
24+
{{ title }}
25+
{{ /nav }}
26+
BLADE;
27+
28+
file_put_contents($this->viewPath('nav-test.antlers.html'), $view);
29+
30+
Facades\Entry::make()
31+
->id('nav-test-page')
32+
->slug('nav-test')
33+
->collection('pages')
34+
->data(['template' => 'nav-test'])
35+
->save();
36+
37+
$this->get('/nav-test');
38+
39+
$tags = collect(Tracker::all())->first()['tags'] ?? [];
40+
41+
$this->assertContains('nav:footer', $tags);
42+
}
43+
44+
#[Test]
45+
public function it_tracks_nav_tag_without_handle_using_default()
46+
{
47+
$view = <<<'BLADE'
48+
{{ nav }}
49+
{{ title }}
50+
{{ /nav }}
51+
BLADE;
52+
53+
file_put_contents($this->viewPath('nav-default.antlers.html'), $view);
54+
55+
Facades\Entry::make()
56+
->id('nav-default-page')
57+
->slug('nav-default')
58+
->collection('pages')
59+
->data(['template' => 'nav-default'])
60+
->save();
61+
62+
$this->get('/nav-default');
63+
64+
$tags = collect(Tracker::all())->first()['tags'] ?? [];
65+
66+
$this->assertContains('nav:collection::pages', $tags);
67+
}
68+
69+
protected function viewPath($name)
70+
{
71+
return __DIR__.'/../__fixtures__/resources/views/'.$name;
72+
}
73+
}

0 commit comments

Comments
 (0)