From edfd5dcbe76e10469094db99a023f7c85b920def Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 10 Jun 2024 00:17:14 +0200 Subject: [PATCH] Add: clear form frontend cache when form updated --- app/Form/Actions/ClearFrontendCacheAction.php | 17 +++++++++++++++++ app/Form/Actions/FormDestroyAction.php | 2 ++ app/Form/Actions/FormStoreAction.php | 1 + app/Form/Actions/FormUpdateAction.php | 3 +++ app/Form/Actions/SettingIndexAction.php | 1 + app/Form/Actions/SettingStoreAction.php | 2 ++ app/Form/FormSettings.php | 1 + ...06_09_235516_add_clear_cache_url_setting.php | 11 +++++++++++ resources/js/views/setting/Form.vue | 1 + tests/Feature/Form/FormStoreActionTest.php | 1 + tests/Feature/Form/FormTestCase.php | 17 +++++++++++++++++ tests/Feature/Form/FormUpdateActionTest.php | 11 +++++++++++ 12 files changed, 68 insertions(+) create mode 100644 app/Form/Actions/ClearFrontendCacheAction.php create mode 100644 database/settings/2024_06_09_235516_add_clear_cache_url_setting.php diff --git a/app/Form/Actions/ClearFrontendCacheAction.php b/app/Form/Actions/ClearFrontendCacheAction.php new file mode 100644 index 00000000..d62da43d --- /dev/null +++ b/app/Form/Actions/ClearFrontendCacheAction.php @@ -0,0 +1,17 @@ +clearCacheUrl); + } +} diff --git a/app/Form/Actions/FormDestroyAction.php b/app/Form/Actions/FormDestroyAction.php index 31cb1d8d..ed215382 100644 --- a/app/Form/Actions/FormDestroyAction.php +++ b/app/Form/Actions/FormDestroyAction.php @@ -14,6 +14,8 @@ class FormDestroyAction { $form->delete(); + ClearFrontendCacheAction::run(); + Succeeded::message('Veranstaltung gelöscht.')->dispatch(); } } diff --git a/app/Form/Actions/FormStoreAction.php b/app/Form/Actions/FormStoreAction.php index 2d32e64b..14bd1241 100644 --- a/app/Form/Actions/FormStoreAction.php +++ b/app/Form/Actions/FormStoreAction.php @@ -45,6 +45,7 @@ class FormStoreAction return tap(Form::create($attributes), function ($form) { $form->setDeferredUploads(request()->input('header_image')); $form->setDeferredUploads(request()->input('mailattachments')); + ClearFrontendCacheAction::run(); }); } diff --git a/app/Form/Actions/FormUpdateAction.php b/app/Form/Actions/FormUpdateAction.php index d682048b..1069e449 100644 --- a/app/Form/Actions/FormUpdateAction.php +++ b/app/Form/Actions/FormUpdateAction.php @@ -42,6 +42,9 @@ class FormUpdateAction public function handle(Form $form, array $attributes): Form { $form->update($attributes); + + ClearFrontendCacheAction::run(); + return $form; } diff --git a/app/Form/Actions/SettingIndexAction.php b/app/Form/Actions/SettingIndexAction.php index 3170afdd..009c48f7 100644 --- a/app/Form/Actions/SettingIndexAction.php +++ b/app/Form/Actions/SettingIndexAction.php @@ -19,6 +19,7 @@ class SettingIndexAction return [ 'data' => [ 'register_url' => $settings->registerUrl, + 'clear_cache_url' => $settings->clearCacheUrl, ], ]; } diff --git a/app/Form/Actions/SettingStoreAction.php b/app/Form/Actions/SettingStoreAction.php index 509f3128..25f9897a 100644 --- a/app/Form/Actions/SettingStoreAction.php +++ b/app/Form/Actions/SettingStoreAction.php @@ -20,6 +20,7 @@ class SettingStoreAction $settings->fill([ 'registerUrl' => $input['register_url'], + 'clearCacheUrl' => $input['clear_cache_url'], ]); $settings->save(); @@ -32,6 +33,7 @@ class SettingStoreAction { return [ 'register_url' => 'present|string', + 'clear_cache_url' => 'present|string', ]; } diff --git a/app/Form/FormSettings.php b/app/Form/FormSettings.php index 5e596d03..7ca1636b 100644 --- a/app/Form/FormSettings.php +++ b/app/Form/FormSettings.php @@ -11,6 +11,7 @@ use App\Setting\LocalSettings; class FormSettings extends LocalSettings implements Indexable, Storeable { public string $registerUrl; + public string $clearCacheUrl; public static function group(): string { diff --git a/database/settings/2024_06_09_235516_add_clear_cache_url_setting.php b/database/settings/2024_06_09_235516_add_clear_cache_url_setting.php new file mode 100644 index 00000000..68849291 --- /dev/null +++ b/database/settings/2024_06_09_235516_add_clear_cache_url_setting.php @@ -0,0 +1,11 @@ +migrator->add('form.clearCacheUrl', ''); + } +}; diff --git a/resources/js/views/setting/Form.vue b/resources/js/views/setting/Form.vue index 4d374621..510040b8 100644 --- a/resources/js/views/setting/Form.vue +++ b/resources/js/views/setting/Form.vue @@ -10,6 +10,7 @@
+
diff --git a/tests/Feature/Form/FormStoreActionTest.php b/tests/Feature/Form/FormStoreActionTest.php index cb286b4f..5e11ac6c 100644 --- a/tests/Feature/Form/FormStoreActionTest.php +++ b/tests/Feature/Form/FormStoreActionTest.php @@ -51,6 +51,7 @@ class FormStoreActionTest extends FormTestCase $this->assertCount(1, $form->getMedia('headerImage')); $this->assertEquals('formname.jpg', $form->getMedia('headerImage')->first()->file_name); Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Veranstaltung gespeichert.'); + $this->assertFrontendCacheCleared(); } public function testRegistrationDatesCanBeNull(): void diff --git a/tests/Feature/Form/FormTestCase.php b/tests/Feature/Form/FormTestCase.php index 888699b6..43875b9f 100644 --- a/tests/Feature/Form/FormTestCase.php +++ b/tests/Feature/Form/FormTestCase.php @@ -2,6 +2,8 @@ namespace Tests\Feature\Form; +use App\Form\FormSettings; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; use Tests\TestCase; use Tests\Lib\CreatesFormFields; @@ -10,10 +12,25 @@ class FormTestCase extends TestCase { use CreatesFormFields; + private string $clearCacheUrl = 'http://event.com/clear-cache'; + public function setUp(): void { parent::setUp(); + app(FormSettings::class)->fill(['clearCacheUrl' => 'http://event.com/clear-cache'])->save(); + + Http::fake(function ($request) { + if ($request->url() === $this->clearCacheUrl) { + return Http::response('', 200); + } + }); + Storage::fake('temp'); } + + protected function assertFrontendCacheCleared(): void + { + Http::assertSent(fn ($request) => $request->url() === $this->clearCacheUrl && $request->method() === 'GET'); + } } diff --git a/tests/Feature/Form/FormUpdateActionTest.php b/tests/Feature/Form/FormUpdateActionTest.php index d6e1020f..6cd1ed32 100644 --- a/tests/Feature/Form/FormUpdateActionTest.php +++ b/tests/Feature/Form/FormUpdateActionTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature\Form; use App\Form\Models\Form; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Support\Facades\Http; class FormUpdateActionTest extends FormTestCase { @@ -28,6 +29,16 @@ class FormUpdateActionTest extends FormTestCase $this->assertTrue($form->config->sections->get(0)->fields->get(0)->maxToday); } + public function testItClearsFrontendCacheWhenFormUpdated(): void + { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); + + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->create()); + + $this->assertFrontendCacheCleared(); + } + public function testItUpdatesActiveColumnsWhenFieldRemoved(): void { $this->login()->loginNami()->withoutExceptionHandling();