|
6 | 6 | use Eminos\StatamicCloudflareCache\Http\Client; |
7 | 7 | use Eminos\StatamicCloudflareCache\Listeners\PurgeCloudflareCache; |
8 | 8 | use Statamic\Events\EntrySaved; |
| 9 | +use Statamic\Events\GlobalSetSaved; |
| 10 | +use Statamic\Events\GlobalSetDeleted; |
9 | 11 | use Statamic\Contracts\Entries\Entry; |
10 | 12 | use Statamic\Contracts\Entries\Collection; |
11 | 13 | use Illuminate\Support\Facades\Http; |
@@ -49,6 +51,11 @@ protected function mockEntry($url = '/test-entry', $collectionUrl = '/test-colle |
49 | 51 | return $entry; |
50 | 52 | } |
51 | 53 |
|
| 54 | + protected function mockGlobals() |
| 55 | + { |
| 56 | + return new stdClass(); |
| 57 | + } |
| 58 | + |
52 | 59 | #[Test] |
53 | 60 | public function it_purges_cache_synchronously_when_entry_is_saved_and_queue_disabled() |
54 | 61 | { |
@@ -199,4 +206,107 @@ public function it_does_not_dispatch_job_when_queue_enabled_but_no_urls_and_no_f |
199 | 206 |
|
200 | 207 | Queue::assertNothingPushed(); |
201 | 208 | } |
| 209 | + |
| 210 | + #[Test] |
| 211 | + public function it_purges_everything_synchronously_when_global_set_is_saved_and_queue_disabled() |
| 212 | + { |
| 213 | + config([ |
| 214 | + 'cloudflare-cache.queue_purge' => false, |
| 215 | + 'cloudflare-cache.purge_everything_fallback' => true, |
| 216 | + 'cloudflare-cache.purge_urls' => true, |
| 217 | + 'cloudflare-cache.purge_on.global_set_saved' => true, |
| 218 | + ]); |
| 219 | + |
| 220 | + $globals = $this->mockGlobals(); |
| 221 | + $event = new GlobalSetSaved($globals); |
| 222 | + |
| 223 | + $clientMock = $this->mock(Client::class); |
| 224 | + $clientMock->shouldReceive('purgeEverything')->once(); |
| 225 | + $clientMock->shouldNotReceive('purgeUrls'); |
| 226 | + |
| 227 | + $listener = $this->app->make(PurgeCloudflareCache::class); |
| 228 | + $listener->handle($event); |
| 229 | + |
| 230 | + Queue::assertNothingPushed(); |
| 231 | + } |
| 232 | + |
| 233 | + #[Test] |
| 234 | + public function it_purges_everything_synchronously_when_global_set_is_deleted_and_queue_disabled() |
| 235 | + { |
| 236 | + config([ |
| 237 | + 'cloudflare-cache.queue_purge' => false, |
| 238 | + 'cloudflare-cache.purge_everything_fallback' => true, |
| 239 | + 'cloudflare-cache.purge_urls' => true, |
| 240 | + 'cloudflare-cache.purge_on.global_set_deleted' => true, |
| 241 | + ]); |
| 242 | + |
| 243 | + $globals = $this->mockGlobals(); |
| 244 | + $event = new GlobalSetDeleted($globals); |
| 245 | + |
| 246 | + $clientMock = $this->mock(Client::class); |
| 247 | + $clientMock->shouldReceive('purgeEverything')->once(); |
| 248 | + $clientMock->shouldNotReceive('purgeUrls'); |
| 249 | + |
| 250 | + $listener = $this->app->make(PurgeCloudflareCache::class); |
| 251 | + $listener->handle($event); |
| 252 | + |
| 253 | + Queue::assertNothingPushed(); |
| 254 | + } |
| 255 | + |
| 256 | + #[Test] |
| 257 | + public function it_dispatches_purge_everything_job_when_global_set_is_saved_and_queue_enabled() |
| 258 | + { |
| 259 | + config([ |
| 260 | + 'cloudflare-cache.queue_purge' => true, |
| 261 | + 'cloudflare-cache.purge_everything_fallback' => true, |
| 262 | + 'cloudflare-cache.purge_urls' => true, |
| 263 | + 'cloudflare-cache.purge_on.global_set_saved' => true, |
| 264 | + ]); |
| 265 | + |
| 266 | + $globals = $this->mockGlobals(); |
| 267 | + $event = new GlobalSetSaved($globals); |
| 268 | + |
| 269 | + $clientMock = $this->mock(Client::class); |
| 270 | + $clientMock->shouldNotReceive('purgeUrls'); |
| 271 | + $clientMock->shouldNotReceive('purgeEverything'); |
| 272 | + |
| 273 | + $listener = $this->app->make(PurgeCloudflareCache::class); |
| 274 | + $listener->handle($event); |
| 275 | + |
| 276 | + Queue::assertPushed(PurgeCloudflareCacheJob::class, function ($job) { |
| 277 | + $reflection = new \ReflectionClass($job); |
| 278 | + $urlsProp = $reflection->getProperty('urls'); |
| 279 | + $urlsProp->setAccessible(true); |
| 280 | + $urls = $urlsProp->getValue($job); |
| 281 | + |
| 282 | + $purgeEverythingProp = $reflection->getProperty('purgeEverything'); |
| 283 | + $purgeEverythingProp->setAccessible(true); |
| 284 | + $purgeEverything = $purgeEverythingProp->getValue($job); |
| 285 | + |
| 286 | + return is_null($urls) && $purgeEverything; |
| 287 | + }); |
| 288 | + } |
| 289 | + |
| 290 | + #[Test] |
| 291 | + public function it_does_not_purge_when_global_set_saved_event_is_disabled_in_config() |
| 292 | + { |
| 293 | + config([ |
| 294 | + 'cloudflare-cache.queue_purge' => false, |
| 295 | + 'cloudflare-cache.purge_everything_fallback' => true, |
| 296 | + 'cloudflare-cache.purge_on.global_set_saved' => false, |
| 297 | + ]); |
| 298 | + |
| 299 | + $globals = $this->mockGlobals(); |
| 300 | + $event = new GlobalSetSaved($globals); |
| 301 | + |
| 302 | + $clientMock = $this->mock(Client::class); |
| 303 | + $clientMock->shouldNotReceive('purgeUrls'); |
| 304 | + $clientMock->shouldNotReceive('purgeEverything'); |
| 305 | + |
| 306 | + $listener = $this->app->make(PurgeCloudflareCache::class); |
| 307 | + $listener->handle($event); |
| 308 | + |
| 309 | + Queue::assertNothingPushed(); |
| 310 | + Http::assertNothingSent(); |
| 311 | + } |
202 | 312 | } |
0 commit comments