|
4 | 4 |
|
5 | 5 | use Mockery; |
6 | 6 | use PHPUnit\Framework\Attributes\Test; |
| 7 | +use Statamic\Facades\Collection; |
7 | 8 | use Statamic\Facades\Site; |
| 9 | +use Statamic\Facades\Taxonomy; |
| 10 | +use Statamic\SeoPro\Events\CollectionDefaultsSaved; |
| 11 | +use Statamic\SeoPro\Events\TaxonomyDefaultsSaved; |
8 | 12 | use Statamic\SeoPro\SiteDefaults\SiteDefaults; |
9 | 13 | use Statamic\StaticCaching\Cacher; |
| 14 | +use Statamic\StaticCaching\Invalidator; |
10 | 15 |
|
11 | 16 | class StaticCachingInvalidationTest extends TestCase |
12 | 17 | { |
@@ -96,4 +101,91 @@ public function invalidates_urls_when_site_defaults_are_saved_in_a_multisite() |
96 | 101 |
|
97 | 102 | SiteDefaults::in('fr')->save(); |
98 | 103 | } |
| 104 | + |
| 105 | + #[Test] |
| 106 | + public function does_nothing_for_collection_defaults_when_static_caching_is_disabled() |
| 107 | + { |
| 108 | + $invalidator = Mockery::mock(Invalidator::class); |
| 109 | + $invalidator->shouldReceive('invalidate')->never(); |
| 110 | + |
| 111 | + $this->app->bind(Invalidator::class, fn () => $invalidator); |
| 112 | + |
| 113 | + config()->set('statamic.static_caching.strategy', null); |
| 114 | + |
| 115 | + $collection = Collection::find('pages'); |
| 116 | + |
| 117 | + CollectionDefaultsSaved::dispatch($collection); |
| 118 | + } |
| 119 | + |
| 120 | + #[Test] |
| 121 | + public function flushes_static_cache_when_collection_defaults_are_saved_with_all_rules() |
| 122 | + { |
| 123 | + $invalidator = Mockery::mock(Invalidator::class); |
| 124 | + $invalidator->shouldReceive('invalidate')->never(); |
| 125 | + |
| 126 | + $this->app->bind(Invalidator::class, fn () => $invalidator); |
| 127 | + |
| 128 | + config()->set('statamic.static_caching.invalidation.rules', 'all'); |
| 129 | + |
| 130 | + $collection = Collection::find('pages'); |
| 131 | + |
| 132 | + CollectionDefaultsSaved::dispatch($collection); |
| 133 | + } |
| 134 | + |
| 135 | + #[Test] |
| 136 | + public function invalidates_collection_when_collection_defaults_are_saved() |
| 137 | + { |
| 138 | + $collection = Collection::find('pages'); |
| 139 | + |
| 140 | + $invalidator = Mockery::mock(Invalidator::class); |
| 141 | + $invalidator->shouldReceive('invalidate')->withArgs(function ($item) use ($collection) { |
| 142 | + return $item->handle() === $collection->handle(); |
| 143 | + })->once(); |
| 144 | + |
| 145 | + $this->app->bind(Invalidator::class, fn () => $invalidator); |
| 146 | + |
| 147 | + CollectionDefaultsSaved::dispatch($collection); |
| 148 | + } |
| 149 | + |
| 150 | + #[Test] |
| 151 | + public function does_nothing_for_taxonomy_defaults_when_static_caching_is_disabled() |
| 152 | + { |
| 153 | + $invalidator = Mockery::mock(Invalidator::class); |
| 154 | + $invalidator->shouldReceive('invalidate')->never(); |
| 155 | + |
| 156 | + $this->app->bind(Invalidator::class, fn () => $invalidator); |
| 157 | + |
| 158 | + config()->set('statamic.static_caching.strategy', null); |
| 159 | + |
| 160 | + $taxonomy = Taxonomy::find('topics'); |
| 161 | + |
| 162 | + TaxonomyDefaultsSaved::dispatch($taxonomy); |
| 163 | + } |
| 164 | + |
| 165 | + #[Test] |
| 166 | + public function flushes_static_cache_when_taxonomy_defaults_are_saved_with_all_rules() |
| 167 | + { |
| 168 | + $cacher = Mockery::mock(Cacher::class)->shouldReceive('flush')->once()->getMock(); |
| 169 | + |
| 170 | + $this->app->bind(Cacher::class, fn () => $cacher); |
| 171 | + |
| 172 | + config()->set('statamic.static_caching.invalidation.rules', 'all'); |
| 173 | + |
| 174 | + $taxonomy = Taxonomy::find('topics'); |
| 175 | + |
| 176 | + TaxonomyDefaultsSaved::dispatch($taxonomy); |
| 177 | + } |
| 178 | + |
| 179 | + #[Test] |
| 180 | + public function invalidates_terms_when_taxonomy_defaults_are_saved() |
| 181 | + { |
| 182 | + $taxonomy = Taxonomy::find('topics'); |
| 183 | + |
| 184 | + $invalidator = Mockery::mock(Invalidator::class); |
| 185 | + $invalidator->shouldReceive('invalidate')->times($taxonomy->queryTerms()->count()); |
| 186 | + |
| 187 | + $this->app->bind(Invalidator::class, fn () => $invalidator); |
| 188 | + |
| 189 | + TaxonomyDefaultsSaved::dispatch($taxonomy); |
| 190 | + } |
99 | 191 | } |
0 commit comments