From c3f6d3827bbae7d733672174a96a634898b78b11 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 14 Jul 2024 19:29:42 +0200 Subject: [PATCH] Add EditorData for forms texts --- app/Form/Models/Form.php | 6 +-- app/Lib/Editor/ConditionResolver.php | 4 +- .../factories/Form/Models/FormFactory.php | 12 ++--- ...4642_create_formtemplates_mail_columns.php | 35 ++++++++++++ tests/Feature/Form/FormRegisterMailTest.php | 18 ------- tests/Feature/Form/FormRequest.php | 8 +++ tests/Feature/Form/FormStoreActionTest.php | 6 +-- tests/Feature/Form/FormUpdateActionTest.php | 53 +++++++++++-------- 8 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 database/migrations/2024_07_14_174642_create_formtemplates_mail_columns.php diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index b227ebdb..1a58a986 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -32,9 +32,9 @@ class Form extends Model implements HasMedia public $casts = [ 'config' => FormConfigData::class, 'meta' => 'json', - 'description' => 'json', - 'mail_top' => 'json', - 'mail_bottom' => 'json', + 'description' => EditorData::class, + 'mail_top' => EditorData::class, + 'mail_bottom' => EditorData::class, 'is_active' => 'boolean', 'is_private' => 'boolean', 'export' => ExportData::class, diff --git a/app/Lib/Editor/ConditionResolver.php b/app/Lib/Editor/ConditionResolver.php index 776eb105..d5e706c3 100644 --- a/app/Lib/Editor/ConditionResolver.php +++ b/app/Lib/Editor/ConditionResolver.php @@ -11,9 +11,9 @@ abstract class ConditionResolver * @param array $content * @return array */ - public function makeBlocks(array $content): array + public function makeBlocks(EditorData $data): array { - return array_filter(data_get($content, 'blocks', []), fn ($block) => $this->filterBlock($block)); + return array_filter($data->blocks, fn ($block) => $this->filterBlock($block)); } /** diff --git a/database/factories/Form/Models/FormFactory.php b/database/factories/Form/Models/FormFactory.php index 26db7218..eda27a1f 100644 --- a/database/factories/Form/Models/FormFactory.php +++ b/database/factories/Form/Models/FormFactory.php @@ -43,15 +43,15 @@ class FormFactory extends Factory { return [ 'name' => $this->faker->words(4, true), - 'description' => EditorRequestFactory::new()->create(), + 'description' => EditorRequestFactory::new()->toData(), 'excerpt' => $this->faker->words(10, true), 'config' => ['sections' => []], 'from' => $this->faker->dateTimeBetween('+1 week', '+4 weeks')->format('Y-m-d H:i:s'), 'to' => $this->faker->dateTimeBetween('+1 week', '+4 weeks')->format('Y-m-d H:i:s'), 'registration_from' => $this->faker->dateTimeBetween('-2 weeks', 'now')->format('Y-m-d H:i:s'), 'registration_until' => $this->faker->dateTimeBetween('now', '+2 weeks')->format('Y-m-d H:i:s'), - 'mail_top' => EditorRequestFactory::new()->create(), - 'mail_bottom' => EditorRequestFactory::new()->create(), + 'mail_top' => EditorRequestFactory::new()->toData(), + 'mail_bottom' => EditorRequestFactory::new()->toData(), 'is_active' => true, 'is_private' => false, 'export' => ExportData::from([]), @@ -85,16 +85,16 @@ class FormFactory extends Factory public function mailTop(EditorRequestFactory $factory): self { - return $this->state(['mail_top' => $factory->create()]); + return $this->state(['mail_top' => $factory->toData()]); } public function mailBottom(EditorRequestFactory $factory): self { - return $this->state(['mail_bottom' => $factory->create()]); + return $this->state(['mail_bottom' => $factory->toData()]); } public function description(EditorRequestFactory $factory): self { - return $this->state(['description' => $factory->create()]); + return $this->state(['description' => $factory->toData()]); } } diff --git a/database/migrations/2024_07_14_174642_create_formtemplates_mail_columns.php b/database/migrations/2024_07_14_174642_create_formtemplates_mail_columns.php new file mode 100644 index 00000000..e700b0f6 --- /dev/null +++ b/database/migrations/2024_07_14_174642_create_formtemplates_mail_columns.php @@ -0,0 +1,35 @@ +json('description')->after('name')->default(json_encode(['time' => 4, 'blocks' => [], 'version' => '1.0']))->change(); + $table->json('mail_top')->after('name')->default(json_encode(['time' => 4, 'blocks' => [], 'version' => '1.0']))->change(); + $table->json('mail_bottom')->after('name')->default(json_encode(['time' => 4, 'blocks' => [], 'version' => '1.0']))->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('formtemplates', function (Blueprint $table) { + $table->dropColumn('mail_top'); + $table->dropColumn('mail_bottom'); + }); + } +}; diff --git a/tests/Feature/Form/FormRegisterMailTest.php b/tests/Feature/Form/FormRegisterMailTest.php index c655112b..50009263 100644 --- a/tests/Feature/Form/FormRegisterMailTest.php +++ b/tests/Feature/Form/FormRegisterMailTest.php @@ -241,24 +241,6 @@ class FormRegisterMailTest extends FormTestCase } } - public function testItSendsEmailWhenMailTopIsEmpty(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - - $participant = Participant::factory()->for( - Form::factory() - ->fields([ - $this->textField('firstname')->specialType(SpecialType::FIRSTNAME), - $this->textField('lastname')->specialType(SpecialType::LASTNAME), - ])->state(['mail_top' => []]) - ) - ->data(['firstname' => 'Max', 'lastname' => 'Muster']) - ->create(); - - $mail = new ConfirmRegistrationMail($participant); - $mail->assertSeeInText('Max'); - } - /** * @dataProvider blockDataProvider * @param array $conditions diff --git a/tests/Feature/Form/FormRequest.php b/tests/Feature/Form/FormRequest.php index 1fd69672..a255d4b4 100644 --- a/tests/Feature/Form/FormRequest.php +++ b/tests/Feature/Form/FormRequest.php @@ -73,6 +73,14 @@ class FormRequest extends RequestFactory return $this->state([str($method)->snake()->toString() => $args[0]]); } + /** + * @param array $fields + */ + public function fields(array $fields): self + { + return $this->sections([FormtemplateSectionRequest::new()->fields($fields)]); + } + public function headerImage(string $fileName): self { UploadedFile::fake()->image($fileName, 1000, 1000)->storeAs('media-library', $fileName, 'temp'); diff --git a/tests/Feature/Form/FormStoreActionTest.php b/tests/Feature/Form/FormStoreActionTest.php index 4c098468..cb402fbb 100644 --- a/tests/Feature/Form/FormStoreActionTest.php +++ b/tests/Feature/Form/FormStoreActionTest.php @@ -41,9 +41,9 @@ class FormStoreActionTest extends FormTestCase $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); - $this->assertEquals(json_decode('{"time":1,"blocks":[{"id":12,"type":"paragraph","data":{"text":"lalab"},"tunes":{"condition":{"mode":"all","ifs":[]}}}],"version":"1.0"}', true), $form->mail_bottom); + $this->assertEquals('Lorem', $form->description->blocks[0]['data']['text']); + $this->assertEquals('lala', $form->mail_top->blocks[0]['data']['text']); + $this->assertEquals('lalab', $form->mail_bottom->blocks[0]['data']['text']); $this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i')); $this->assertEquals(true, $form->is_active); $this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i')); diff --git a/tests/Feature/Form/FormUpdateActionTest.php b/tests/Feature/Form/FormUpdateActionTest.php index e5d367bd..4d1cd51d 100644 --- a/tests/Feature/Form/FormUpdateActionTest.php +++ b/tests/Feature/Form/FormUpdateActionTest.php @@ -19,10 +19,8 @@ class FormUpdateActionTest extends FormTestCase { $this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create(); - $payload = FormRequest::new()->sections([ - FormtemplateSectionRequest::new()->fields([ - $this->dateField()->state(['max_today' => true]), - ]) + $payload = FormRequest::new()->fields([ + $this->dateField()->state(['max_today' => true]), ])->create(); $this->patchJson(route('form.update', ['form' => $form]), $payload) @@ -33,6 +31,24 @@ class FormUpdateActionTest extends FormTestCase $this->assertTrue($form->config->sections->get(0)->fields->get(0)->maxToday); } + public function testItSetsTexts(): void + { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); + $payload = FormRequest::new()->fields([]) + ->mailTop(EditorRequestFactory::new()->text(11, 'lala')) + ->mailBottom(EditorRequestFactory::new()->text(12, 'lalab')) + ->description(EditorRequestFactory::new()->text(12, 'desc')) + ->create(); + + $this->patchJson(route('form.update', ['form' => $form]), $payload) + ->assertOk(); + + $this->assertEquals('lala', $form->fresh()->mail_top->blocks[0]['data']['text']); + $this->assertEquals('lalab', $form->fresh()->mail_bottom->blocks[0]['data']['text']); + $this->assertEquals('desc', $form->fresh()->description->blocks[0]['data']['text']); + } + public function testItClearsFrontendCacheWhenFormUpdated(): void { $this->login()->loginNami()->withoutExceptionHandling(); @@ -56,17 +72,14 @@ class FormUpdateActionTest extends FormTestCase public function testItUpdatesActiveColumnsWhenFieldRemoved(): void { $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory() - ->sections([FormtemplateSectionRequest::new()->fields([ - $this->textField('firstname'), - $this->textField('geb'), - $this->textField('lastname'), - ])]) + $form = Form::factory()->fields([ + $this->textField('firstname'), + $this->textField('geb'), + $this->textField('lastname'), + ]) ->create(); - $payload = FormRequest::new()->sections([ - FormtemplateSectionRequest::new()->fields([ - $this->textField('firstname'), - ]) + $payload = FormRequest::new()->fields([ + $this->textField('firstname'), ])->create(); $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); @@ -113,14 +126,12 @@ class FormUpdateActionTest extends FormTestCase { $this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory() - ->sections([FormtemplateSectionRequest::new()->fields([])]) + ->fields([]) ->create(); - $payload = FormRequest::new()->sections([ - FormtemplateSectionRequest::new()->fields([ - $this->textField('firstname'), - $this->textField('geb'), - $this->textField('lastname'), - ]) + $payload = FormRequest::new()->fields([ + $this->textField('firstname'), + $this->textField('geb'), + $this->textField('lastname'), ])->create(); $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk();