Skip to content
Merged
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
9 changes: 8 additions & 1 deletion src/Http/Middleware/CacheTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Statamic\Facades\URL;
use Statamic\Forms;
use Statamic\StaticCaching\Cacher;
use Statamic\Structures\Nav;
use Statamic\Structures\Page;
use Statamic\Support\Str;
use Statamic\Tags;
Expand Down Expand Up @@ -181,7 +182,13 @@ private function setupTagHooks()
});

Tags\Nav::hook('init', function ($value, $next) use ($self) {
$handle = $this->params->get('handle') ? 'nav:'.$this->params->get('handle') : $this->tag;
$handle = $this->params->get('handle', $this->tag != 'nav:index' ? Str::after($this->tag, 'nav:') : 'collection::pages');

if ($handle instanceof Nav) {
$handle = $handle->handle();
}

$handle = 'nav:'.$handle;
$self->addContentTag($handle);

return $next($value);
Expand Down
142 changes: 142 additions & 0 deletions tests/Unit/NavTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Thoughtco\StatamicCacheTracker\Tests\Unit;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades;
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
use Thoughtco\StatamicCacheTracker\Tests\TestCase;

class NavTagTest extends TestCase
{
#[Test]
public function it_tracks_nav_tag_with_explicit_handle()
{
// Create a navigation structure
Facades\Nav::make('footer')
->title('Footer')
->expectsRoot(true)
->collections(['pages'])
->save();

$view = <<<'BLADE'
{{ nav handle="footer" }}
{{ title }}
{{ /nav }}
BLADE;

file_put_contents($this->viewPath('nav-test.antlers.html'), $view);

Facades\Entry::make()
->id('nav-test-page')
->slug('nav-test')
->collection('pages')
->data(['template' => 'nav-test'])
->save();

$this->get('/nav-test');

$tags = collect(Tracker::all())->first()['tags'] ?? [];

$this->assertContains('nav:footer', $tags);
}

#[Test]
public function it_tracks_nav_tag_with_shorthand_handle()
{
// Create a navigation structure
Facades\Nav::make('footer')
->title('Footer')
->expectsRoot(true)
->collections(['pages'])
->save();

$view = <<<'BLADE'
{{ nav:footer }}
{{ title }}
{{ /nav:footer }}
BLADE;

file_put_contents($this->viewPath('nav-test.antlers.html'), $view);

Facades\Entry::make()
->id('nav-test-page')
->slug('nav-test')
->collection('pages')
->data(['template' => 'nav-test'])
->save();

$this->get('/nav-test');

$tags = collect(Tracker::all())->first()['tags'] ?? [];

$this->assertContains('nav:footer', $tags);
}

#[Test]
public function it_tracks_nav_tag_without_handle_using_default()
{
$view = <<<'BLADE'
{{ nav }}
{{ title }}
{{ /nav }}
BLADE;

file_put_contents($this->viewPath('nav-default.antlers.html'), $view);

Facades\Entry::make()
->id('nav-default-page')
->slug('nav-default')
->collection('pages')
->data(['template' => 'nav-default'])
->save();

$this->get('/nav-default');

$tags = collect(Tracker::all())->first()['tags'] ?? [];

$this->assertContains('nav:collection::pages', $tags);
}

#[Test]
public function it_tracks_nav_tag_when_handle_is_nav_instance()
{
// Create a navigation structure
$nav = tap(Facades\Nav::make('sidebar')
->title('Sidebar')
->expectsRoot(true)
->collections(['pages']))
->save();

// The template uses a blueprint field that returns a Nav instance
$view = <<<'BLADE'
{{ nav :handle="my_nav" }}
{{ title }}
{{ /nav }}
BLADE;

file_put_contents($this->viewPath('nav-test.antlers.html'), $view);

// Create entry with nav field that returns Nav instance
Facades\Entry::make()
->id('nav-test-page')
->slug('nav-test')
->collection('pages')
->data([
'template' => 'nav-test',
'my_nav' => $nav,
])
->save();

$this->get('/nav-test');

$tags = collect(Tracker::all())->first()['tags'] ?? [];

$this->assertContains('nav:sidebar', $tags);
}

protected function viewPath($name)
{
return __DIR__.'/../__fixtures__/resources/views/'.$name;
}
}