Add: clear form frontend cache when form updated
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/tag Build is failing Details

This commit is contained in:
philipp lang 2024-06-10 00:17:14 +02:00
parent ccea9ab094
commit edfd5dcbe7
12 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,17 @@
<?php
namespace App\Form\Actions;
use App\Form\FormSettings;
use Illuminate\Support\Facades\Http;
use Lorisleiva\Actions\Concerns\AsAction;
class ClearFrontendCacheAction
{
use AsAction;
public function handle()
{
Http::get(app(FormSettings::class)->clearCacheUrl);
}
}

View File

@ -14,6 +14,8 @@ class FormDestroyAction
{
$form->delete();
ClearFrontendCacheAction::run();
Succeeded::message('Veranstaltung gelöscht.')->dispatch();
}
}

View File

@ -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();
});
}

View File

@ -42,6 +42,9 @@ class FormUpdateAction
public function handle(Form $form, array $attributes): Form
{
$form->update($attributes);
ClearFrontendCacheAction::run();
return $form;
}

View File

@ -19,6 +19,7 @@ class SettingIndexAction
return [
'data' => [
'register_url' => $settings->registerUrl,
'clear_cache_url' => $settings->clearCacheUrl,
],
];
}

View File

@ -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',
];
}

View File

@ -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
{

View File

@ -0,0 +1,11 @@
<?php
use Spatie\LaravelSettings\Migrations\SettingsMigration;
return new class extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('form.clearCacheUrl', '');
}
};

View File

@ -10,6 +10,7 @@
</div>
<div class="grid grid-cols-2 gap-4">
<f-text id="register_url" v-model="inner.register_url" label="Formular-Link" name="register_url"></f-text>
<f-text id="clear_cache_url" v-model="inner.clear_cache_url" label="Frontend-Cache-Url" name="clear_cache_url"></f-text>
</div>
</form>
</setting-layout>

View File

@ -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

View File

@ -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');
}
}

View File

@ -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();