|
| 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_with_shorthand_handle() |
| 46 | + { |
| 47 | + // Create a navigation structure |
| 48 | + Facades\Nav::make('footer') |
| 49 | + ->title('Footer') |
| 50 | + ->expectsRoot(true) |
| 51 | + ->collections(['pages']) |
| 52 | + ->save(); |
| 53 | + |
| 54 | + $view = <<<'BLADE' |
| 55 | +{{ nav:footer }} |
| 56 | + {{ title }} |
| 57 | +{{ /nav:footer }} |
| 58 | +BLADE; |
| 59 | + |
| 60 | + file_put_contents($this->viewPath('nav-test.antlers.html'), $view); |
| 61 | + |
| 62 | + Facades\Entry::make() |
| 63 | + ->id('nav-test-page') |
| 64 | + ->slug('nav-test') |
| 65 | + ->collection('pages') |
| 66 | + ->data(['template' => 'nav-test']) |
| 67 | + ->save(); |
| 68 | + |
| 69 | + $this->get('/nav-test'); |
| 70 | + |
| 71 | + $tags = collect(Tracker::all())->first()['tags'] ?? []; |
| 72 | + |
| 73 | + $this->assertContains('nav:footer', $tags); |
| 74 | + } |
| 75 | + |
| 76 | + #[Test] |
| 77 | + public function it_tracks_nav_tag_without_handle_using_default() |
| 78 | + { |
| 79 | + $view = <<<'BLADE' |
| 80 | +{{ nav }} |
| 81 | + {{ title }} |
| 82 | +{{ /nav }} |
| 83 | +BLADE; |
| 84 | + |
| 85 | + file_put_contents($this->viewPath('nav-default.antlers.html'), $view); |
| 86 | + |
| 87 | + Facades\Entry::make() |
| 88 | + ->id('nav-default-page') |
| 89 | + ->slug('nav-default') |
| 90 | + ->collection('pages') |
| 91 | + ->data(['template' => 'nav-default']) |
| 92 | + ->save(); |
| 93 | + |
| 94 | + $this->get('/nav-default'); |
| 95 | + |
| 96 | + $tags = collect(Tracker::all())->first()['tags'] ?? []; |
| 97 | + |
| 98 | + $this->assertContains('nav:collection::pages', $tags); |
| 99 | + } |
| 100 | + |
| 101 | + #[Test] |
| 102 | + public function it_tracks_nav_tag_when_handle_is_nav_instance() |
| 103 | + { |
| 104 | + // Create a navigation structure |
| 105 | + $nav = tap(Facades\Nav::make('sidebar') |
| 106 | + ->title('Sidebar') |
| 107 | + ->expectsRoot(true) |
| 108 | + ->collections(['pages'])) |
| 109 | + ->save(); |
| 110 | + |
| 111 | + // The template uses a blueprint field that returns a Nav instance |
| 112 | + $view = <<<'BLADE' |
| 113 | +{{ nav :handle="my_nav" }} |
| 114 | + {{ title }} |
| 115 | +{{ /nav }} |
| 116 | +BLADE; |
| 117 | + |
| 118 | + file_put_contents($this->viewPath('nav-test.antlers.html'), $view); |
| 119 | + |
| 120 | + // Create entry with nav field that returns Nav instance |
| 121 | + Facades\Entry::make() |
| 122 | + ->id('nav-test-page') |
| 123 | + ->slug('nav-test') |
| 124 | + ->collection('pages') |
| 125 | + ->data([ |
| 126 | + 'template' => 'nav-test', |
| 127 | + 'my_nav' => $nav, |
| 128 | + ]) |
| 129 | + ->save(); |
| 130 | + |
| 131 | + $this->get('/nav-test'); |
| 132 | + |
| 133 | + $tags = collect(Tracker::all())->first()['tags'] ?? []; |
| 134 | + |
| 135 | + $this->assertContains('nav:sidebar', $tags); |
| 136 | + } |
| 137 | + |
| 138 | + protected function viewPath($name) |
| 139 | + { |
| 140 | + return __DIR__.'/../__fixtures__/resources/views/'.$name; |
| 141 | + } |
| 142 | +} |
0 commit comments