diff --git a/app/Form/Actions/FormStoreAction.php b/app/Form/Actions/FormStoreAction.php index a5fd4f39..7181ff59 100644 --- a/app/Form/Actions/FormStoreAction.php +++ b/app/Form/Actions/FormStoreAction.php @@ -36,6 +36,7 @@ class FormStoreAction 'is_private' => 'boolean', 'export' => 'nullable|array', 'needs_prevention' => 'present|boolean', + 'prevention_text' => 'array', ]; } diff --git a/app/Form/Actions/FormUpdateAction.php b/app/Form/Actions/FormUpdateAction.php index eebd7379..49defea4 100644 --- a/app/Form/Actions/FormUpdateAction.php +++ b/app/Form/Actions/FormUpdateAction.php @@ -35,6 +35,7 @@ class FormUpdateAction 'is_private' => 'boolean', 'export' => 'nullable|array', 'needs_prevention' => 'present|boolean', + 'prevention_text' => 'array', ]; } diff --git a/app/Form/Actions/PreventionRememberAction.php b/app/Form/Actions/PreventionRememberAction.php index 76b3c1ba..c6ff78cd 100644 --- a/app/Form/Actions/PreventionRememberAction.php +++ b/app/Form/Actions/PreventionRememberAction.php @@ -28,7 +28,8 @@ class PreventionRememberAction } $body = app(PreventionSettings::class)->formmail - ->placeholder('formname', $participant->form->name); + ->placeholder('formname', $participant->form->name) + ->append($participant->form->prevention_text); if ($participant->getFields()->getMailRecipient() === null) { continue; diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index 554eb063..bb581c38 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -5,6 +5,7 @@ namespace App\Form\Models; use App\Form\Data\ExportData; use App\Form\Data\FieldCollection; use App\Form\Data\FormConfigData; +use App\Lib\Editor\EditorData; use Cviebrock\EloquentSluggable\Sluggable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -37,6 +38,7 @@ class Form extends Model implements HasMedia 'is_private' => 'boolean', 'export' => ExportData::class, 'needs_prevention' => 'boolean', + 'prevention_text' => EditorData::class, ]; /** @var array */ diff --git a/app/Form/Resources/FormResource.php b/app/Form/Resources/FormResource.php index 493462d1..818d36b2 100644 --- a/app/Form/Resources/FormResource.php +++ b/app/Form/Resources/FormResource.php @@ -11,6 +11,7 @@ use App\Form\Scopes\FormFilterScope; use App\Form\Models\Form; use App\Form\Models\Formtemplate; use App\Group; +use App\Lib\Editor\EditorData; use App\Lib\HasMeta; use Illuminate\Http\Resources\Json\JsonResource; @@ -94,6 +95,7 @@ class FormResource extends JsonResource 'config' => null, 'header_image' => null, 'mailattachments' => [], + 'prevention_text' => EditorData::default(), 'id' => null, 'export' => ExportData::from([]), ], diff --git a/app/Lib/Editor/EditorData.php b/app/Lib/Editor/EditorData.php index ba2c4378..7794eb31 100644 --- a/app/Lib/Editor/EditorData.php +++ b/app/Lib/Editor/EditorData.php @@ -5,7 +5,7 @@ namespace App\Lib\Editor; use Spatie\LaravelData\Data; /** @todo replace blocks with actual block data classes */ -class EditorData extends Data +class EditorData extends Data implements Editorable { public function __construct( @@ -31,6 +31,22 @@ class EditorData extends Data return collect($wanted)->first(fn ($search) => !str(json_encode($this->blocks))->contains($search)) === null; } + public static function default(): self + { + return static::from([ + 'version' => '1.0', + 'blocks' => [], + 'time' => 0, + ]); + } + + public function append(Editorable $editorable): self + { + $this->blocks = array_merge($this->blocks, $editorable->toEditorData()->blocks); + + return $this; + } + public function replaceWithList(string $blockContent, array $replacements): self { $this->blocks = collect($this->blocks)->map(function ($block) use ($blockContent, $replacements) { @@ -56,6 +72,11 @@ class EditorData extends Data })->toArray(); + return $this; + } + + public function toEditorData(): EditorData + { return $this; } } diff --git a/app/Lib/Editor/Editorable.php b/app/Lib/Editor/Editorable.php new file mode 100644 index 00000000..3954bf6c --- /dev/null +++ b/app/Lib/Editor/Editorable.php @@ -0,0 +1,8 @@ +json('prevention_text')->after('description')->default(json_encode(EditorRequestFactory::new()->empty()->create())); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('forms', function (Blueprint $table) { + $table->dropColumn('prevention_text'); + }); + } +}; diff --git a/tests/EndToEnd/Form/FormIndexActionTest.php b/tests/EndToEnd/Form/FormIndexActionTest.php index ee8e4f48..0323304e 100644 --- a/tests/EndToEnd/Form/FormIndexActionTest.php +++ b/tests/EndToEnd/Form/FormIndexActionTest.php @@ -61,6 +61,7 @@ class FormIndexActionTest extends FormTestCase ->assertInertiaPath('data.meta.templates.0.name', 'tname') ->assertInertiaPath('data.meta.templates.0.config.sections.0.name', 'sname') ->assertInertiaPath('data.meta.default.name', '') + ->assertInertiaPath('data.meta.default.prevention_text.version', '1.0') ->assertInertiaPath('data.meta.default.description', []) ->assertInertiaPath('data.meta.default.excerpt', '') ->assertInertiaPath('data.meta.default.is_active', true) diff --git a/tests/Feature/Form/FormRequest.php b/tests/Feature/Form/FormRequest.php index f5cb9db6..4a949965 100644 --- a/tests/Feature/Form/FormRequest.php +++ b/tests/Feature/Form/FormRequest.php @@ -50,6 +50,7 @@ class FormRequest extends RequestFactory 'mailattachments' => [], 'export' => ExportData::from([])->toArray(), 'needs_prevention' => $this->faker->boolean(), + 'prevention_text' => EditorRequestFactory::new()->create(), ]; } diff --git a/tests/Feature/Form/FormStoreActionTest.php b/tests/Feature/Form/FormStoreActionTest.php index 6e0ac957..4c098468 100644 --- a/tests/Feature/Form/FormStoreActionTest.php +++ b/tests/Feature/Form/FormStoreActionTest.php @@ -26,6 +26,7 @@ class FormStoreActionTest extends FormTestCase ->name('formname') ->description($description) ->excerpt('avff') + ->preventionText(EditorRequestFactory::new()->paragraphs(['lorem ipsum'])) ->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->from('2023-07-07')->to('2023-07-08') ->mailTop(EditorRequestFactory::new()->text(11, 'lala')) ->mailBottom(EditorRequestFactory::new()->text(12, 'lalab')) @@ -38,6 +39,7 @@ class FormStoreActionTest extends FormTestCase $form = Form::latest()->first(); $this->assertEquals('sname', $form->config->sections->get(0)->name); $this->assertEquals('formname', $form->name); + $this->assertEquals('lorem ipsum', $form->prevention_text->blocks[0]['data']['text']); $this->assertEquals('avff', $form->excerpt); $this->assertEquals($description->paragraphBlock(10, 'Lorem'), $form->description); $this->assertEquals(json_decode('{"time":1,"blocks":[{"id":11,"type":"paragraph","data":{"text":"lala"},"tunes":{"condition":{"mode":"all","ifs":[]}}}],"version":"1.0"}', true), $form->mail_top); @@ -88,6 +90,7 @@ class FormStoreActionTest extends FormTestCase yield [FormRequest::new()->state(['header_image' => null]), ['header_image' => 'Bild ist erforderlich']]; } + /** * @dataProvider validationDataProvider * @param array $messages diff --git a/tests/Feature/Form/FormUpdateActionTest.php b/tests/Feature/Form/FormUpdateActionTest.php index 6efe1cef..417b588c 100644 --- a/tests/Feature/Form/FormUpdateActionTest.php +++ b/tests/Feature/Form/FormUpdateActionTest.php @@ -5,7 +5,9 @@ namespace Tests\Feature\Form; use App\Fileshare\Data\FileshareResourceData; use App\Form\Data\ExportData; use App\Form\Models\Form; +use App\Lib\Editor\EditorData; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\EditorRequestFactory; class FormUpdateActionTest extends FormTestCase { @@ -128,9 +130,13 @@ class FormUpdateActionTest extends FormTestCase { $this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create(); - $payload = FormRequest::new()->state(['needs_prevention' => true])->create(); + $payload = FormRequest::new() + ->preventionText(EditorRequestFactory::new()->text(10, 'lorem ipsum')->create()) + ->state(['needs_prevention' => true]) + ->create(); $this->patchJson(route('form.update', ['form' => $form]), $payload); $this->assertTrue($form->fresh()->needs_prevention); + $this->assertEquals('lorem ipsum', $form->fresh()->prevention_text->blocks[0]['data']['text']); } } diff --git a/tests/Feature/Member/PreventionTest.php b/tests/Feature/Member/PreventionTest.php index 93f351ff..9f64cc31 100644 --- a/tests/Feature/Member/PreventionTest.php +++ b/tests/Feature/Member/PreventionTest.php @@ -200,6 +200,23 @@ class PreventionTest extends TestCase ])); } + public function testItAppendsTextOfForm(): void + { + Mail::fake(); + app(PreventionSettings::class)->fake([ + 'formmail' => EditorRequestFactory::new()->paragraphs(["::first::"])->toData() + ])->save(); + $form = $this->createForm(); + $form->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['event'])->toData()]); + $this->createParticipant($form); + + PreventionRememberAction::run(); + + Mail::assertSent(PreventionRememberMail::class, fn ($mail) => $mail->bodyText->hasAll([ + 'event' + ])); + } + public function testItDisplaysBodyTextInMail(): void { $form = $this->createForm(); diff --git a/tests/RequestFactories/EditorRequestFactory.php b/tests/RequestFactories/EditorRequestFactory.php index 7a9baeb9..f340507b 100644 --- a/tests/RequestFactories/EditorRequestFactory.php +++ b/tests/RequestFactories/EditorRequestFactory.php @@ -21,6 +21,11 @@ class EditorRequestFactory extends RequestFactory ]; } + public function empty(): self + { + return $this->state(['blocks' => []]); + } + /** * @param array $conditions */