From cff62ebc1db3102b7decb5ceb955264bc64ca0c1 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 15 Jun 2025 23:29:26 +0200 Subject: [PATCH] Move FormTestCase in Trait --- tests/Feature/Form/FormRegisterActionTest.php | 1460 ++++++++--------- tests/Feature/Form/FormRegisterMailTest.php | 489 +++--- tests/Feature/Form/FormStoreActionTest.php | 172 +- tests/Feature/Form/FormTestCase.php | 36 - tests/Feature/Form/FormUpdateActionTest.php | 245 ++- .../Feature/Form/FormUpdateMetaActionTest.php | 68 +- .../Form/FormtemplateStoreActionTest.php | 136 +- tests/Feature/Form/IsDirtyActionTest.php | 32 +- .../Form/ParticipantDestroyActionTest.php | 31 +- .../Form/ParticipantExportActionTest.php | 55 +- .../Form/ParticipantFieldsActionTest.php | 49 +- .../Form/ParticipantStoreActionTest.php | 56 +- .../Form/ParticipantUpdateActionTest.php | 58 +- tests/Lib/CreatesFormFields.php | 45 +- 14 files changed, 1425 insertions(+), 1507 deletions(-) delete mode 100644 tests/Feature/Form/FormTestCase.php diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index 6ff43c62..884ed0d4 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -2,788 +2,728 @@ namespace Tests\Feature\Form; -use App\Form\Actions\ExportAction; -use App\Form\Actions\ExportSyncAction; use App\Form\Enums\NamiType; use App\Form\Enums\SpecialType; use App\Form\Mails\ConfirmRegistrationMail; use App\Form\Models\Form; use App\Group; use App\Group\Enums\Level; -use App\Member\Member; use Carbon\Carbon; use Database\Factories\Member\MemberFactory; -use Generator; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Mail; -use Illuminate\Testing\TestResponse; -use PHPUnit\Framework\Attributes\DataProvider; +use Tests\Lib\CreatesFormFields; -class FormRegisterActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); + Mail::fake(); +}); - public function setUp(): void - { - parent::setUp(); - Mail::fake(); - } +dataset('validation', fn() => [ + fn () => [ + test()->dateField('birthday')->name('Geburtsdatum')->maxToday(false), + ['birthday' => 'aa'], + ['birthday' => 'Geburtsdatum muss ein gültiges Datum sein.'] + ], - public function testItSavesParticipantAsModel(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory() - ->sections([ - FormtemplateSectionRequest::new()->fields([ - $this->textField('vorname'), - $this->textField('nachname'), - ]), - FormtemplateSectionRequest::new()->fields([ - $this->textField('spitzname'), - ]), - ]) - ->create(); + fn () => [ + test()->dateField('birthday')->name('Geburtsdatum')->maxToday(false), + ['birthday' => '2021-05-06'], + null, + ], - $this->register($form, ['vorname' => 'Max', 'nachname' => 'Muster', 'spitzname' => 'Abraham']) - ->assertOk(); + fn () => [ + test()->dateField('birthday')->name('Geburtsdatum')->maxToday(true), + ['birthday' => '2024-02-16'], + ['birthday' => 'Geburtsdatum muss ein Datum vor oder gleich dem 15.02.2024 sein.'], + ], - $participants = $form->fresh()->participants; - $this->assertCount(1, $participants); - $this->assertEquals('Max', $participants->first()->data['vorname']); - $this->assertEquals('Muster', $participants->first()->data['nachname']); - $this->assertEquals('Abraham', $participants->first()->data['spitzname']); - } + fn () => [ + test()->dateField('birthday')->name('Geburtsdatum')->maxToday(true), + ['birthday' => '2024-02-15'], + null, + ], - public function testItCannotRegisterWhenRegistrationFromReached(): void - { - $this->login()->loginNami(); - $form = Form::factory()->registrationFrom(now()->addDay())->create(); + fn () => [ + test()->textField('vorname')->name('Vorname der Mutter')->required(true), + ['vorname' => ''], + ['vorname' => 'Vorname der Mutter ist erforderlich.'] + ], - $this->register($form, [])->assertJsonValidationErrors(['event' => 'Anmeldung zzt nicht möglich.']); - } + fn () => [ + test()->textField('vorname')->name('Vorname der Mutter')->required(true), + ['vorname' => 5], + ['vorname' => 'Vorname der Mutter muss ein String sein.'] + ], - public function testItCannotRegisterWhenRegistrationUntilReached(): void - { - $this->login()->loginNami(); - $form = Form::factory()->registrationUntil(now()->subDay())->create(); + fn () => [ + test()->radioField('yes_or_no')->name('Ja oder Nein')->required(true), + ['yes_or_no' => null], + ['yes_or_no' => 'Ja oder Nein ist erforderlich.'] + ], - $this->register($form, [])->assertJsonValidationErrors(['event' => 'Anmeldung zzt nicht möglich.']); - } + fn () => [ + test()->radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false)->allowcustom(false), + ['letter' => 'Z'], + ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] + ], - public function testItSendsEmailToParticipant(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->name('Ver2')->fields([ - $this->textField('vorname')->specialType(SpecialType::FIRSTNAME), - $this->textField('nachname')->specialType(SpecialType::LASTNAME), - $this->textField('email')->specialType(SpecialType::EMAIL), + fn () => [ + test()->radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), + ['letter' => 'Z'], + ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] + ], + + fn () => [ + test()->radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(true), + ['letter' => 'lalalaa'], + null, + ], + + fn () => [ + test()->radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), + ['letter' => 'A'], + null + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), + ['letter' => ['Z']], + ['letter.0' => 'Der gewählte Wert für Buchstabe ist ungültig.'], + ], + + fn () => [ + test()->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->allowcustom(true), + ['letter' => 'Z'], + null, + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), + ['letter' => 77], + ['letter' => 'Buchstabe muss ein Array sein.'], + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), + ['letter' => ['A']], + null, + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), + ['letter' => []], + null, + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(2), + ['letter' => ['A', 'B', 'C']], + ['letter' => 'Buchstabe darf maximal 2 Elemente haben.'], + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(2)->max(0), + ['letter' => ['A']], + ['letter' => 'Buchstabe muss mindestens 2 Elemente haben.'], + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(1)->max(0), + ['letter' => []], + ['letter' => 'Buchstabe muss mindestens 1 Elemente haben.'], + ], + + fn () => [ + test()->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(1), + ['letter' => ['A', 'B']], + ['letter' => 'Buchstabe darf maximal 1 Elemente haben.'], + ], + + fn () => [ + test()->checkboxField('data')->name('Datenschutz')->required(false), + ['data' => 5], + ['data' => 'Datenschutz muss ein Wahrheitswert sein.'], + ], + + fn () => [ + test()->checkboxField('data')->name('Datenschutz')->required(false), + ['data' => false], + null + ], + + fn () => [ + test()->checkboxField('data')->name('Datenschutz')->required(true), + ['data' => false], + ['data' => 'Datenschutz muss akzeptiert werden.'], + ], + + fn () => [ + test()->checkboxField('data')->name('Datenschutz')->required(true), + ['data' => true], + null, + ], + + fn () => [ + test()->dropdownField('yes_or_no')->name('Ja oder Nein')->required(true), + ['yes_or_no' => null], + ['yes_or_no' => 'Ja oder Nein ist erforderlich.'] + ], + + fn () => [ + test()->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false)->allowcustom(false), + ['letter' => 'Z'], + ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] + ], + + fn () => [ + test()->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), + ['letter' => 'Z'], + ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] + ], + + fn () => [ + test()->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), + ['letter' => 'A'], + null + ], + + fn () => [ + test()->textareaField('vorname')->name('Vorname der Mutter')->required(true), + ['vorname' => ''], + ['vorname' => 'Vorname der Mutter ist erforderlich.'] + ], + + fn () => [ + test()->textareaField('vorname')->name('Vorname der Mutter')->required(true), + ['vorname' => 5], + ['vorname' => 'Vorname der Mutter muss ein String sein.'] + ], + + fn () => [ + test()->textareaField('vorname')->name('Vorname der Mutter')->required(true), + ['vorname' => 5], + ['vorname' => 'Vorname der Mutter muss ein String sein.'] + ], + + fn () => [ + test()->emailField('email')->name('Mail')->required(true), + ['email' => 'alaaa'], + ['email' => 'Mail muss eine gültige E-Mail-Adresse sein.'] + ], + + fn () => [ + test()->emailField('email')->name('Mail')->required(false), + ['email' => 'alaaa'], + ['email' => 'Mail muss eine gültige E-Mail-Adresse sein.'] + ], + + fn () => [ + test()->numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), + ['numb' => 21], + ['numb' => 'Nummer muss kleiner oder gleich 20 sein.'] + ], + + fn () => [ + test()->numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), + ['numb' => 9], + ['numb' => 'Nummer muss größer oder gleich 10 sein.'] + ], + + fn () => [ + test()->numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), + ['numb' => 'asss'], + ['numb' => 'Nummer muss eine ganze Zahl sein.'] + ], + + fn () => [ + test()->numberField('numb')->name('Nummer')->required(true), + ['numb' => ''], + ['numb' => 'Nummer ist erforderlich.'] + ] +]); + +it('testItSavesParticipantAsModel', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory() + ->sections([ + FormtemplateSectionRequest::new()->fields([ + $this->textField('vorname'), + $this->textField('nachname'), + ]), + FormtemplateSectionRequest::new()->fields([ + $this->textField('spitzname'), + ]), ]) - ->create(); + ->create(); - $this->register($form, ['vorname' => 'Lala', 'nachname' => 'GG', 'email' => 'example@test.test']) - ->assertOk(); + $this->register($form, ['vorname' => 'Max', 'nachname' => 'Muster', 'spitzname' => 'Abraham']) + ->assertOk(); - Mail::assertQueued(ConfirmRegistrationMail::class, fn ($message) => $message->hasTo('example@test.test', 'Lala GG') && $message->hasSubject('Deine Anmeldung zu Ver2')); + $participants = $form->fresh()->participants; + $this->assertCount(1, $participants); + $this->assertEquals('Max', $participants->first()->data['vorname']); + $this->assertEquals('Muster', $participants->first()->data['nachname']); + $this->assertEquals('Abraham', $participants->first()->data['spitzname']); +}); + +it('testItCannotRegisterWhenRegistrationFromReached', function () { + $this->login()->loginNami(); + $form = Form::factory()->registrationFrom(now()->addDay())->create(); + + $this->register($form, [])->assertJsonValidationErrors(['event' => 'Anmeldung zzt nicht möglich.']); +}); + +it('testItCannotRegisterWhenRegistrationUntilReached', function () { + $this->login()->loginNami(); + $form = Form::factory()->registrationUntil(now()->subDay())->create(); + + $this->register($form, [])->assertJsonValidationErrors(['event' => 'Anmeldung zzt nicht möglich.']); +}); + +it('testItSendsEmailToParticipant', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->name('Ver2')->fields([ + $this->textField('vorname')->specialType(SpecialType::FIRSTNAME), + $this->textField('nachname')->specialType(SpecialType::LASTNAME), + $this->textField('email')->specialType(SpecialType::EMAIL), + ]) + ->create(); + + $this->register($form, ['vorname' => 'Lala', 'nachname' => 'GG', 'email' => 'example@test.test']) + ->assertOk(); + + Mail::assertQueued(ConfirmRegistrationMail::class, fn($message) => $message->hasTo('example@test.test', 'Lala GG') && $message->hasSubject('Deine Anmeldung zu Ver2')); +}); + +it('testItDoesntSendEmailWhenNoMailFieldGiven', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->fields([ + $this->textField('vorname')->specialType(SpecialType::FIRSTNAME), + $this->textField('nachname')->specialType(SpecialType::LASTNAME), + ]) + ->create(); + + $this->register($form, ['vorname' => 'Lala', 'nachname' => 'GG']) + ->assertOk(); + + Mail::assertNotQueued(ConfirmRegistrationMail::class); +}); + +/** + * @param array $payload + * @param ?array $messages + */ +it('testItValidatesInput', function (FormtemplateFieldRequest $fieldGenerator, array $payload, ?array $messages) { + Carbon::setTestNow(Carbon::parse('2024-02-15 06:00:00')); + $this->login()->loginNami(); + $form = Form::factory()->fields([$fieldGenerator])->create(); + + $response = $this->postJson(route('form.register', ['form' => $form]), $payload); + + if ($messages) { + $response->assertJsonValidationErrors($messages); + } else { + $response->assertOk(); } - - public function testItDoesntSendEmailWhenNoMailFieldGiven(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->fields([ - $this->textField('vorname')->specialType(SpecialType::FIRSTNAME), - $this->textField('nachname')->specialType(SpecialType::LASTNAME), - ]) - ->create(); - - $this->register($form, ['vorname' => 'Lala', 'nachname' => 'GG']) - ->assertOk(); - - Mail::assertNotQueued(ConfirmRegistrationMail::class); - } - - /** - * @param array $payload - * @param ?array $messages - */ - #[DataProvider('validationDataProvider')] - public function testItValidatesInput(FormtemplateFieldRequest $fieldGenerator, array $payload, ?array $messages): void - { - Carbon::setTestNow(Carbon::parse('2024-02-15 06:00:00')); - $this->login()->loginNami(); - $form = Form::factory()->fields([$fieldGenerator])->create(); - - $response = $this->postJson(route('form.register', ['form' => $form]), $payload); - - if ($messages) { - $response->assertJsonValidationErrors($messages); - } else { - $response->assertOk(); - } - } - - public static function validationDataProvider(): Generator - { - yield [ - static::dateField('birthday')->name('Geburtsdatum')->maxToday(false), - ['birthday' => 'aa'], - ['birthday' => 'Geburtsdatum muss ein gültiges Datum sein.'] - ]; - - yield [ - static::dateField('birthday')->name('Geburtsdatum')->maxToday(false), - ['birthday' => '2021-05-06'], - null, - ]; - - yield [ - static::dateField('birthday')->name('Geburtsdatum')->maxToday(true), - ['birthday' => '2024-02-16'], - ['birthday' => 'Geburtsdatum muss ein Datum vor oder gleich dem 15.02.2024 sein.'], - ]; - - yield [ - static::dateField('birthday')->name('Geburtsdatum')->maxToday(true), - ['birthday' => '2024-02-15'], - null, - ]; - - yield [ - static::textField('vorname')->name('Vorname der Mutter')->required(true), - ['vorname' => ''], - ['vorname' => 'Vorname der Mutter ist erforderlich.'] - ]; - - yield [ - static::textField('vorname')->name('Vorname der Mutter')->required(true), - ['vorname' => 5], - ['vorname' => 'Vorname der Mutter muss ein String sein.'] - ]; - - yield [ - static::radioField('yes_or_no')->name('Ja oder Nein')->required(true), - ['yes_or_no' => null], - ['yes_or_no' => 'Ja oder Nein ist erforderlich.'] - ]; - - yield [ - static::radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false)->allowcustom(false), - ['letter' => 'Z'], - ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] - ]; - - yield [ - static::radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), - ['letter' => 'Z'], - ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] - ]; - - yield [ - static::radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(true), - ['letter' => 'lalalaa'], - null, - ]; - - yield [ - static::radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), - ['letter' => 'A'], - null - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), - ['letter' => ['Z']], - ['letter.0' => 'Der gewählte Wert für Buchstabe ist ungültig.'], - ]; - - yield [ - static::dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->allowcustom(true), - ['letter' => 'Z'], - null, - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), - ['letter' => 77], - ['letter' => 'Buchstabe muss ein Array sein.'], - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), - ['letter' => ['A']], - null, - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']), - ['letter' => []], - null, - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(2), - ['letter' => ['A', 'B', 'C']], - ['letter' => 'Buchstabe darf maximal 2 Elemente haben.'], - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(2)->max(0), - ['letter' => ['A']], - ['letter' => 'Buchstabe muss mindestens 2 Elemente haben.'], - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(1)->max(0), - ['letter' => []], - ['letter' => 'Buchstabe muss mindestens 1 Elemente haben.'], - ]; - - yield [ - static::checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(1), - ['letter' => ['A', 'B']], - ['letter' => 'Buchstabe darf maximal 1 Elemente haben.'], - ]; - - yield [ - static::checkboxField('data')->name('Datenschutz')->required(false), - ['data' => 5], - ['data' => 'Datenschutz muss ein Wahrheitswert sein.'], - ]; - - yield [ - static::checkboxField('data')->name('Datenschutz')->required(false), - ['data' => false], - null - ]; - - yield [ - static::checkboxField('data')->name('Datenschutz')->required(true), - ['data' => false], - ['data' => 'Datenschutz muss akzeptiert werden.'], - ]; - - yield [ - static::checkboxField('data')->name('Datenschutz')->required(true), - ['data' => true], - null, - ]; - - yield [ - static::dropdownField('yes_or_no')->name('Ja oder Nein')->required(true), - ['yes_or_no' => null], - ['yes_or_no' => 'Ja oder Nein ist erforderlich.'] - ]; - - yield [ - static::dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false)->allowcustom(false), - ['letter' => 'Z'], - ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] - ]; - - yield [ - static::dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), - ['letter' => 'Z'], - ['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.'] - ]; - - yield [ - static::dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true)->allowcustom(false), - ['letter' => 'A'], - null - ]; - - yield [ - static::textareaField('vorname')->name('Vorname der Mutter')->required(true), - ['vorname' => ''], - ['vorname' => 'Vorname der Mutter ist erforderlich.'] - ]; - - yield [ - static::textareaField('vorname')->name('Vorname der Mutter')->required(true), - ['vorname' => 5], - ['vorname' => 'Vorname der Mutter muss ein String sein.'] - ]; - - yield [ - static::textareaField('vorname')->name('Vorname der Mutter')->required(true), - ['vorname' => 5], - ['vorname' => 'Vorname der Mutter muss ein String sein.'] - ]; - - yield [ - static::emailField('email')->name('Mail')->required(true), - ['email' => 'alaaa'], - ['email' => 'Mail muss eine gültige E-Mail-Adresse sein.'] - ]; - - yield [ - static::emailField('email')->name('Mail')->required(false), - ['email' => 'alaaa'], - ['email' => 'Mail muss eine gültige E-Mail-Adresse sein.'] - ]; - - yield [ - static::numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), - ['numb' => 21], - ['numb' => 'Nummer muss kleiner oder gleich 20 sein.'] - ]; - - yield [ - static::numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), - ['numb' => 9], - ['numb' => 'Nummer muss größer oder gleich 10 sein.'] - ]; - - yield [ - static::numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), - ['numb' => 'asss'], - ['numb' => 'Nummer muss eine ganze Zahl sein.'] - ]; - - yield [ - static::numberField('numb')->name('Nummer')->required(true), - ['numb' => ''], - ['numb' => 'Nummer ist erforderlich.'] - ]; - } - - public function testItValidatesGroupFieldWithParentGroupField(): void - { - $this->login()->loginNami(); - $group = Group::factory()->has(Group::factory()->count(3), 'children')->create(); - $foreignGroup = Group::factory()->create(); - $form = Form::factory()->fields([ - $this->groupField('group')->name('Gruppe')->parentGroup($group->id)->required(true) - ]) - ->create(); - - $this->register($form, ['group' => null]) - ->assertJsonValidationErrors(['group' => 'Gruppe ist erforderlich.']); - $this->register($form, ['group' => $foreignGroup->id]) - ->assertJsonValidationErrors(['group' => 'Der gewählte Wert für Gruppe ist ungültig.']); - } - - public function testGroupFieldCanBeUnsetWhenGiven(): void - { - $this->login()->loginNami(); - $group = Group::factory()->has(Group::factory(), 'children')->create(); - $form = Form::factory()->fields([ - $this->groupField('region')->emptyOptionValue('kein Bezirk')->parentGroup($group->id)->required(false), - $this->groupField('stamm')->name('Gruppe')->emptyOptionValue('kein Stamm')->parentField('region')->required(true) - ]) - ->create(); - - $this->register($form, ['region' => -1, 'stamm' => -1])->assertOk(); - $participants = $form->fresh()->participants; - $this->assertEquals(-1, $participants->first()->data['region']); - $this->assertEquals(-1, $participants->first()->data['stamm']); - } - - public function testGroupFieldCanBeNullWhenNotRequired(): void - { - $this->login()->loginNami(); - $form = Form::factory()->fields([ - $this->groupField('group')->parentGroup(Group::factory()->create()->id)->required(false) - ]) - ->create(); - - $this->register($form, ['group' => null]) - ->assertOk(); - } - - public function testItValidatesGroupWithParentFieldField(): void - { - $this->login()->loginNami(); - $group = Group::factory()->has(Group::factory()->has(Group::factory()->count(3), 'children'), 'children')->create(); - $foreignGroup = Group::factory()->create(); - $form = Form::factory()->fields([ - $this->groupField('parentgroup')->name('Übergeordnete Gruppe')->parentGroup($group->id)->required(true), - $this->groupField('group')->name('Gruppe')->parentField('parentgroup')->required(true), - ]) - ->create(); - - $this->register($form, ['parentgroup' => $group->children->first()->id, 'group' => $foreignGroup->id]) - ->assertJsonValidationErrors(['group' => 'Der gewählte Wert für Gruppe ist ungültig.']); - $this->register($form, ['parentgroup' => $group->children->first()->id, 'group' => $group->children->first()->children->first()->id]) - ->assertOk(); - } - - public function testItSetsMitgliedsnrForMainMember(): void - { - $this->login()->loginNami(); - $member = $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster']); - $form = Form::factory()->fields([ - $this->textField('email')->namiType(NamiType::EMAIL), - $this->textField('firstname')->namiType(NamiType::FIRSTNAME), - $this->textField('lastname')->namiType(NamiType::LASTNAME), - ]) - ->create(); - - $this->register($form, ['email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster'])->assertOk(); - $this->assertEquals($member->id, $form->participants->first()->member_id); - } - - public function testItDoesntSetMitgliedsnrWhenFieldDoesntHaveType(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de']); - $form = Form::factory()->fields([ - $this->textField('email'), - ]) - ->create(); - - $this->register($form, ['email' => 'max@muster.de'])->assertOk(); - $this->assertNull($form->participants->first()->member_id); - } - - public function testItDoesntSyncMembersWhenTwoMembersMatch(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de']); - $this->createMember(['mitgliedsnr' => '9967', 'email' => 'max@muster.de']); - $form = Form::factory()->fields([ - $this->textField('email')->namiType(NamiType::EMAIL), - ]) - ->create(); - - $this->register($form, ['email' => 'max@muster.de'])->assertOk(); - $this->assertNull($form->participants->first()->member_id); - $this->assertNull($form->participants->first()->parent_id); - } - - // --------------------------- NamiField Tests --------------------------- - // *********************************************************************** - public function testItAddsMitgliedsnrFromMembers(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505']); - $this->createMember(['mitgliedsnr' => '5506']); - $form = Form::factory()->fields([ - $this->namiField('members'), - ]) - ->create(); - - $this->register($form, ['members' => [['id' => '5505'], ['id' => '5506']]]) - ->assertOk(); - $this->assertCount(3, $form->participants()->get()); - $this->assertEquals([['id' => '5505'], ['id' => '5506']], $form->participants->get(0)->data['members']); - $this->assertEquals([], $form->participants->get(1)->data['members']); - $this->assertEquals([], $form->participants->get(2)->data['members']); - $this->assertEquals($form->participants->get(0)->id, $form->participants->get(2)->parent_id); - $this->assertEquals($form->participants->get(0)->id, $form->participants->get(1)->parent_id); - } - - public static function memberMatchingDataProvider(): Generator - { - yield [ - ['email' => 'max@muster.de'], - NamiType::EMAIL, - 'max@muster.de', - ]; - - yield [ - ['firstname' => 'Philipp'], - NamiType::FIRSTNAME, - 'Philipp' - ]; - - yield [ - ['lastname' => 'Muster'], - NamiType::LASTNAME, - 'Muster' - ]; - - yield [ - ['address' => 'Maxstr 5'], - NamiType::ADDRESS, - 'Maxstr 5' - ]; - - yield [ - ['zip' => 44444], - NamiType::ZIP, - '44444' - ]; - - yield [ - ['location' => 'Hilden'], - NamiType::LOCATION, - 'Hilden' - ]; - - yield [ - ['birthday' => '2023-06-06'], - NamiType::BIRTHDAY, - '2023-06-06' - ]; - - yield [ - [], - NamiType::GENDER, - 'Männlich', - fn (MemberFactory $factory) => $factory->male(), - ]; - - yield [ - ['gender_id' => null], - NamiType::GENDER, - '', - ]; - - yield [ - ['birthday' => '1991-10-02'], - NamiType::AGE, - '31' - ]; - - yield [ - ['birthday' => '1991-05-04'], - NamiType::AGE, - '32' - ]; - - yield [ - ['birthday' => '1991-08-15'], - NamiType::AGEEVENT, - '32' - ]; - - yield [ - ['mobile_phone' => '+49 7776666'], - NamiType::MOBILEPHONE, - '+49 7776666' - ]; - } - - /** - * @param array $memberAttributes - * @param mixed $participantValue - */ - #[DataProvider('memberMatchingDataProvider')] - public function testItSynchsMemberAttributes(array $memberAttributes, NamiType $type, mixed $participantValue, ?callable $factory = null): void - { - Carbon::setTestNow(Carbon::parse('2023-05-04')); - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505', ...$memberAttributes], $factory); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->textField('other')->required(true)->namiType($type), - ]) - ->from('2023-08-15') - ->create(); - - $this->register($form, ['other' => '::other::', 'members' => [['id' => '5505']]])->assertOk(); - $this->assertEquals($participantValue, $form->participants->get(1)->data['other']); - } - - public function testItAddsOtherFieldsOfMember(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505']); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->textField('other')->required(false), - ]) - ->create(); - - $this->register($form, ['other' => '::string::', 'members' => [['id' => '5505', 'other' => 'othervalue']]]) - ->assertOk(); - $this->assertEquals('othervalue', $form->participants->get(1)->data['other']); - } - - public function testItAddsMemberForNonNami(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => null]); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->textField('gender')->namiType(NamiType::GENDER)->required(false), - $this->textField('vorname')->namiType(NamiType::FIRSTNAME)->required(false), - $this->textField('other')->required(false), - ]) - ->create(); - - $this->register($form, ['other' => '::string::', 'vorname' => 'LA', 'members' => [['id' => null, 'vorname' => 'BBB', 'gender' => 'Herr', 'other' => 'othervalue']]]) - ->assertOk(); - $this->assertEquals('othervalue', $form->participants->get(1)->data['other']); - $this->assertEquals('Herr', $form->participants->get(1)->data['gender']); - $this->assertEquals('BBB', $form->participants->get(1)->data['vorname']); - } - - public function testItValidatesNamiTypeFieldsForNonMembers(): void - { - $this->login()->loginNami(); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->textField('gender')->name('Geschlecht')->namiType(NamiType::GENDER)->required(true), - ]) - ->create(); - - $this->register($form, ['gender' => 'Herr', 'members' => [['id' => null, 'gender' => null]]]) - ->assertJsonValidationErrors(['members.0.gender' => 'Geschlecht für ein Mitglied ist erforderlich.']); - } - - public function testItValidatesMembersFields(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505']); - $this->createMember(['mitgliedsnr' => '5506']); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->textField('other')->name('Andere')->required(true), - ]) - ->create(); - - $this->register($form, ['other' => 'ooo', 'members' => [['id' => '5505', 'other' => ''], ['id' => '5506', 'other' => '']]]) - ->assertJsonValidationErrors(['members.0.other' => 'Andere für ein Mitglied ist erforderlich.']) - ->assertJsonValidationErrors(['members.1.other' => 'Andere für ein Mitglied ist erforderlich.']); - } - - public function testItValidatesIfMemberExists(): void - { - $this->login()->loginNami(); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->textField('other')->required(true), - ]) - ->create(); - - $this->register($form, ['other' => '::string::', 'members' => [['id' => '9999', 'other' => '::string::']]]) - ->assertJsonValidationErrors(['members.0.id' => 'Mitglied Nr 9999 ist nicht vorhanden.']); - } - - public function testItValidatesMembersCheckboxesOptions(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505']); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->checkboxesField('other')->name('Andere')->options(['A', 'B']), - ]) - ->create(); - - $this->register($form, ['other' => [], 'members' => [ - ['id' => '5505', 'other' => ['A', 'missing']] - ]]) - ->assertJsonValidationErrors(['members.0.other.1' => 'Der gewählte Wert für Andere für ein Mitglied ist ungültig.']); - } - - public function testItValidatesMembersCheckboxesAsArray(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505']); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->checkboxesField('other')->name('Andere')->options(['A', 'B']), - ]) - ->create(); - - $this->register($form, ['other' => [], 'members' => [ - ['id' => '5505', 'other' => 'lala'] - ]]) - ->assertJsonValidationErrors(['members.0.other' => 'Andere für ein Mitglied muss ein Array sein.']); - } - - public function testItSetsDefaultValueForFieldsThatAreNotNamiFillable(): void - { - $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505', 'firstname' => 'Paula']); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->textField('other')->required(true)->forMembers(false)->options(['A', 'B']), - $this->textField('firstname')->required(true)->namiType(NamiType::FIRSTNAME), - ]) - ->create(); - - $this->register($form, ['firstname' => 'A', 'other' => 'B', 'members' => [['id' => '5505']]]) - ->assertOk(); - $this->assertEquals('Paula', $form->participants->get(1)->data['firstname']); - $this->assertEquals('', $form->participants->get(1)->data['other']); - } - - public function testNamiFieldCanBeEmptyArray(): void - { - $this->login()->loginNami(); - $form = Form::factory()->fields([ - $this->namiField('members'), - ]) - ->create(); - - $this->register($form, ['members' => []])->assertOk(); - $this->assertDatabaseCount('participants', 1); - } - - public function testNamiFieldMustBeArray(): void - { - $this->login()->loginNami(); - $form = Form::factory()->fields([ - $this->namiField('members'), - ]) - ->create(); - - $this->register($form, ['members' => null])->assertJsonValidationErrors(['members']); - } - - public function testParticipantsHaveRelationToActualMember(): void - { - $this->login()->loginNami(); - $member = $this->createMember(['mitgliedsnr' => '5505']); - $form = Form::factory()->fields([ - $this->namiField('members'), - ]) - ->create(); - - $this->register($form, ['members' => [['id' => '5505']]])->assertOk(); - $this->assertEquals($member->id, $form->participants->get(1)->member_id); - } - - public function testItSetsRegionIdAndGroupIdOfParentGroup(): void - { - $this->login()->loginNami(); - $bezirk = Group::factory()->level(Level::REGION)->create(); - $stamm = Group::factory()->for($bezirk, 'parent')->level(Level::GROUP)->create(); - $this->createMember(['mitgliedsnr' => '5505', 'group_id' => $stamm->id]); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->groupField('bezirk')->forMembers(false)->namiType(NamiType::REGION), - $this->groupField('stamm')->forMembers(false)->namiType(NamiType::STAMM), - ]) - ->create(); - - $this->register($form, ['bezirk' => $bezirk->id, 'stamm' => $stamm->id, 'members' => [['id' => '5505']]])->assertOk(); - $this->assertEquals($bezirk->id, $form->participants->get(1)->data['bezirk']); - $this->assertEquals($stamm->id, $form->participants->get(1)->data['stamm']); - } - - public function testItSetsRegionIfMemberIsDirectRegionMember(): void - { - $this->login()->loginNami(); - $bezirk = Group::factory()->level(Level::REGION)->create(); - $this->createMember(['mitgliedsnr' => '5505', 'group_id' => $bezirk->id]); - $form = Form::factory()->fields([ - $this->namiField('members'), - $this->groupField('bezirk')->forMembers(false)->namiType(NamiType::REGION), - ]) - ->create(); - - $this->register($form, ['bezirk' => $bezirk->id, 'members' => [['id' => '5505']]])->assertOk(); - $this->assertEquals($bezirk->id, $form->participants->get(1)->data['bezirk']); - } - - /** - * @param array $attributes - */ - protected function createMember(array $attributes, ?callable $factoryCallback = null): Member - { - return call_user_func($factoryCallback ?: fn ($factory) => $factory, Member::factory()->defaults()) - ->create($attributes); - } - - /** - * @param array $payload - */ - protected function register(Form $form, array $payload): TestResponse - { - return $this->postJson(route('form.register', ['form' => $form]), $payload); - } -} +})->with('validation'); + +it('testItValidatesGroupFieldWithParentGroupField', function () { + $this->login()->loginNami(); + $group = Group::factory()->has(Group::factory()->count(3), 'children')->create(); + $foreignGroup = Group::factory()->create(); + $form = Form::factory()->fields([ + $this->groupField('group')->name('Gruppe')->parentGroup($group->id)->required(true) + ]) + ->create(); + + $this->register($form, ['group' => null]) + ->assertJsonValidationErrors(['group' => 'Gruppe ist erforderlich.']); + $this->register($form, ['group' => $foreignGroup->id]) + ->assertJsonValidationErrors(['group' => 'Der gewählte Wert für Gruppe ist ungültig.']); +}); + +it('testGroupFieldCanBeUnsetWhenGiven', function () { + $this->login()->loginNami(); + $group = Group::factory()->has(Group::factory(), 'children')->create(); + $form = Form::factory()->fields([ + $this->groupField('region')->emptyOptionValue('kein Bezirk')->parentGroup($group->id)->required(false), + $this->groupField('stamm')->name('Gruppe')->emptyOptionValue('kein Stamm')->parentField('region')->required(true) + ]) + ->create(); + + $this->register($form, ['region' => -1, 'stamm' => -1])->assertOk(); + $participants = $form->fresh()->participants; + $this->assertEquals(-1, $participants->first()->data['region']); + $this->assertEquals(-1, $participants->first()->data['stamm']); +}); + +it('testGroupFieldCanBeNullWhenNotRequired', function () { + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->groupField('group')->parentGroup(Group::factory()->create()->id)->required(false) + ]) + ->create(); + + $this->register($form, ['group' => null]) + ->assertOk(); +}); + +it('testItValidatesGroupWithParentFieldField', function () { + $this->login()->loginNami(); + $group = Group::factory()->has(Group::factory()->has(Group::factory()->count(3), 'children'), 'children')->create(); + $foreignGroup = Group::factory()->create(); + $form = Form::factory()->fields([ + $this->groupField('parentgroup')->name('Übergeordnete Gruppe')->parentGroup($group->id)->required(true), + $this->groupField('group')->name('Gruppe')->parentField('parentgroup')->required(true), + ]) + ->create(); + + $this->register($form, ['parentgroup' => $group->children->first()->id, 'group' => $foreignGroup->id]) + ->assertJsonValidationErrors(['group' => 'Der gewählte Wert für Gruppe ist ungültig.']); + $this->register($form, ['parentgroup' => $group->children->first()->id, 'group' => $group->children->first()->children->first()->id]) + ->assertOk(); +}); + +it('testItSetsMitgliedsnrForMainMember', function () { + $this->login()->loginNami(); + $member = $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster']); + $form = Form::factory()->fields([ + $this->textField('email')->namiType(NamiType::EMAIL), + $this->textField('firstname')->namiType(NamiType::FIRSTNAME), + $this->textField('lastname')->namiType(NamiType::LASTNAME), + ]) + ->create(); + + $this->register($form, ['email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster'])->assertOk(); + $this->assertEquals($member->id, $form->participants->first()->member_id); +}); + +it('testItDoesntSetMitgliedsnrWhenFieldDoesntHaveType', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de']); + $form = Form::factory()->fields([ + $this->textField('email'), + ]) + ->create(); + + $this->register($form, ['email' => 'max@muster.de'])->assertOk(); + $this->assertNull($form->participants->first()->member_id); +}); + +it('testItDoesntSyncMembersWhenTwoMembersMatch', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de']); + $this->createMember(['mitgliedsnr' => '9967', 'email' => 'max@muster.de']); + $form = Form::factory()->fields([ + $this->textField('email')->namiType(NamiType::EMAIL), + ]) + ->create(); + + $this->register($form, ['email' => 'max@muster.de'])->assertOk(); + $this->assertNull($form->participants->first()->member_id); + $this->assertNull($form->participants->first()->parent_id); +}); + +// --------------------------- NamiField Tests --------------------------- +// *********************************************************************** +it('testItAddsMitgliedsnrFromMembers', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '5505']); + $this->createMember(['mitgliedsnr' => '5506']); + $form = Form::factory()->fields([ + $this->namiField('members'), + ]) + ->create(); + + $this->register($form, ['members' => [['id' => '5505'], ['id' => '5506']]]) + ->assertOk(); + $this->assertCount(3, $form->participants()->get()); + $this->assertEquals([['id' => '5505'], ['id' => '5506']], $form->participants->get(0)->data['members']); + $this->assertEquals([], $form->participants->get(1)->data['members']); + $this->assertEquals([], $form->participants->get(2)->data['members']); + $this->assertEquals($form->participants->get(0)->id, $form->participants->get(2)->parent_id); + $this->assertEquals($form->participants->get(0)->id, $form->participants->get(1)->parent_id); +}); + +/** + * @param array $memberAttributes + * @param mixed $participantValue + */ +it('testItSynchsMemberAttributes', function (array $memberAttributes, NamiType $type, mixed $participantValue, ?callable $factory = null) { + Carbon::setTestNow(Carbon::parse('2023-05-04')); + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '5505', ...$memberAttributes], $factory); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('other')->required(true)->namiType($type), + ]) + ->from('2023-08-15') + ->create(); + + $this->register($form, ['other' => '::other::', 'members' => [['id' => '5505']]])->assertOk(); + $this->assertEquals($participantValue, $form->participants->get(1)->data['other']); +})->with([ + [ + ['email' => 'max@muster.de'], + NamiType::EMAIL, + 'max@muster.de', + ], + + [ + ['firstname' => 'Philipp'], + NamiType::FIRSTNAME, + 'Philipp' + ], + + [ + ['lastname' => 'Muster'], + NamiType::LASTNAME, + 'Muster' + ], + + [ + ['address' => 'Maxstr 5'], + NamiType::ADDRESS, + 'Maxstr 5' + ], + + [ + ['zip' => 44444], + NamiType::ZIP, + '44444' + ], + + [ + ['location' => 'Hilden'], + NamiType::LOCATION, + 'Hilden' + ], + + [ + ['birthday' => '2023-06-06'], + NamiType::BIRTHDAY, + '2023-06-06' + ], + + [ + [], + NamiType::GENDER, + 'Männlich', + fn(MemberFactory $factory) => $factory->male(), + ], + + [ + ['gender_id' => null], + NamiType::GENDER, + '', + ], + + [ + ['birthday' => '1991-10-02'], + NamiType::AGE, + '31' + ], + + [ + ['birthday' => '1991-05-04'], + NamiType::AGE, + '32' + ], + + [ + ['birthday' => '1991-08-15'], + NamiType::AGEEVENT, + '32' + ], + + [ + ['mobile_phone' => '+49 7776666'], + NamiType::MOBILEPHONE, + '+49 7776666' + ], +]); + +it('testItAddsOtherFieldsOfMember', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '5505']); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('other')->required(false), + ]) + ->create(); + + $this->register($form, ['other' => '::string::', 'members' => [['id' => '5505', 'other' => 'othervalue']]]) + ->assertOk(); + $this->assertEquals('othervalue', $form->participants->get(1)->data['other']); +}); + +it('testItAddsMemberForNonNami', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => null]); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('gender')->namiType(NamiType::GENDER)->required(false), + $this->textField('vorname')->namiType(NamiType::FIRSTNAME)->required(false), + $this->textField('other')->required(false), + ]) + ->create(); + + $this->register($form, ['other' => '::string::', 'vorname' => 'LA', 'members' => [['id' => null, 'vorname' => 'BBB', 'gender' => 'Herr', 'other' => 'othervalue']]]) + ->assertOk(); + $this->assertEquals('othervalue', $form->participants->get(1)->data['other']); + $this->assertEquals('Herr', $form->participants->get(1)->data['gender']); + $this->assertEquals('BBB', $form->participants->get(1)->data['vorname']); +}); + +it('testItValidatesNamiTypeFieldsForNonMembers', function () { + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('gender')->name('Geschlecht')->namiType(NamiType::GENDER)->required(true), + ]) + ->create(); + + $this->register($form, ['gender' => 'Herr', 'members' => [['id' => null, 'gender' => null]]]) + ->assertJsonValidationErrors(['members.0.gender' => 'Geschlecht für ein Mitglied ist erforderlich.']); +}); + +it('testItValidatesMembersFields', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '5505']); + $this->createMember(['mitgliedsnr' => '5506']); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('other')->name('Andere')->required(true), + ]) + ->create(); + + $this->register($form, ['other' => 'ooo', 'members' => [['id' => '5505', 'other' => ''], ['id' => '5506', 'other' => '']]]) + ->assertJsonValidationErrors(['members.0.other' => 'Andere für ein Mitglied ist erforderlich.']) + ->assertJsonValidationErrors(['members.1.other' => 'Andere für ein Mitglied ist erforderlich.']); +}); + +it('testItValidatesIfMemberExists', function () { + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('other')->required(true), + ]) + ->create(); + + $this->register($form, ['other' => '::string::', 'members' => [['id' => '9999', 'other' => '::string::']]]) + ->assertJsonValidationErrors(['members.0.id' => 'Mitglied Nr 9999 ist nicht vorhanden.']); +}); + +it('testItValidatesMembersCheckboxesOptions', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '5505']); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->checkboxesField('other')->name('Andere')->options(['A', 'B']), + ]) + ->create(); + + $this->register($form, ['other' => [], 'members' => [ + ['id' => '5505', 'other' => ['A', 'missing']] + ]]) + ->assertJsonValidationErrors(['members.0.other.1' => 'Der gewählte Wert für Andere für ein Mitglied ist ungültig.']); +}); + +it('testItValidatesMembersCheckboxesAsArray', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '5505']); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->checkboxesField('other')->name('Andere')->options(['A', 'B']), + ]) + ->create(); + + $this->register($form, ['other' => [], 'members' => [ + ['id' => '5505', 'other' => 'lala'] + ]]) + ->assertJsonValidationErrors(['members.0.other' => 'Andere für ein Mitglied muss ein Array sein.']); +}); + +it('testItSetsDefaultValueForFieldsThatAreNotNamiFillable', function () { + $this->login()->loginNami(); + $this->createMember(['mitgliedsnr' => '5505', 'firstname' => 'Paula']); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->textField('other')->required(true)->forMembers(false)->options(['A', 'B']), + $this->textField('firstname')->required(true)->namiType(NamiType::FIRSTNAME), + ]) + ->create(); + + $this->register($form, ['firstname' => 'A', 'other' => 'B', 'members' => [['id' => '5505']]]) + ->assertOk(); + $this->assertEquals('Paula', $form->participants->get(1)->data['firstname']); + $this->assertEquals('', $form->participants->get(1)->data['other']); +}); + +it('testNamiFieldCanBeEmptyArray', function () { + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->namiField('members'), + ]) + ->create(); + + $this->register($form, ['members' => []])->assertOk(); + $this->assertDatabaseCount('participants', 1); +}); + +it('testNamiFieldMustBeArray', function () { + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->namiField('members'), + ]) + ->create(); + + $this->register($form, ['members' => null])->assertJsonValidationErrors(['members']); +}); + +it('testParticipantsHaveRelationToActualMember', function () { + $this->login()->loginNami(); + $member = $this->createMember(['mitgliedsnr' => '5505']); + $form = Form::factory()->fields([ + $this->namiField('members'), + ]) + ->create(); + + $this->register($form, ['members' => [['id' => '5505']]])->assertOk(); + $this->assertEquals($member->id, $form->participants->get(1)->member_id); +}); + +it('testItSetsRegionIdAndGroupIdOfParentGroup', function () { + $this->login()->loginNami(); + $bezirk = Group::factory()->level(Level::REGION)->create(); + $stamm = Group::factory()->for($bezirk, 'parent')->level(Level::GROUP)->create(); + $this->createMember(['mitgliedsnr' => '5505', 'group_id' => $stamm->id]); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->groupField('bezirk')->forMembers(false)->namiType(NamiType::REGION), + $this->groupField('stamm')->forMembers(false)->namiType(NamiType::STAMM), + ]) + ->create(); + + $this->register($form, ['bezirk' => $bezirk->id, 'stamm' => $stamm->id, 'members' => [['id' => '5505']]])->assertOk(); + $this->assertEquals($bezirk->id, $form->participants->get(1)->data['bezirk']); + $this->assertEquals($stamm->id, $form->participants->get(1)->data['stamm']); +}); + +it('testItSetsRegionIfMemberIsDirectRegionMember', function () { + $this->login()->loginNami(); + $bezirk = Group::factory()->level(Level::REGION)->create(); + $this->createMember(['mitgliedsnr' => '5505', 'group_id' => $bezirk->id]); + $form = Form::factory()->fields([ + $this->namiField('members'), + $this->groupField('bezirk')->forMembers(false)->namiType(NamiType::REGION), + ]) + ->create(); + + $this->register($form, ['bezirk' => $bezirk->id, 'members' => [['id' => '5505']]])->assertOk(); + $this->assertEquals($bezirk->id, $form->participants->get(1)->data['bezirk']); +}); diff --git a/tests/Feature/Form/FormRegisterMailTest.php b/tests/Feature/Form/FormRegisterMailTest.php index 568edaff..9c41b159 100644 --- a/tests/Feature/Form/FormRegisterMailTest.php +++ b/tests/Feature/Form/FormRegisterMailTest.php @@ -6,269 +6,262 @@ use App\Form\Enums\SpecialType; use App\Form\Mails\ConfirmRegistrationMail; use App\Form\Models\Form; use App\Form\Models\Participant; -use Generator; use Illuminate\Foundation\Testing\DatabaseTransactions; -use PHPUnit\Framework\Attributes\DataProvider; use Tests\RequestFactories\EditorRequestFactory; +use Tests\Lib\CreatesFormFields; -class FormRegisterMailTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItShowsFormContent(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); +dataset('blocks', fn() => [ + fn () => [ + ['mode' => 'all', 'ifs' => []], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], - $participant = Participant::factory()->for( - Form::factory()->sections([ - FormtemplateSectionRequest::new()->name('Persönliches')->fields([ - $this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME), - $this->textField('nachname')->specialType(SpecialType::LASTNAME), - ]) - ]) + fn () => [ + ['mode' => 'any', 'ifs' => []], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], - ->mailTop(EditorRequestFactory::new()->text(10, 'mail top'))->mailBottom(EditorRequestFactory::new()->text(11, 'mail bottom')) - ) - ->data(['vorname' => 'Max', 'nachname' => 'Muster']) - ->create(); + fn () => [ + ['mode' => 'any', 'ifs' => []], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], - $mail = new ConfirmRegistrationMail($participant); - $mail->assertSeeInText('mail top'); - $mail->assertSeeInText('mail bottom'); - } + fn () => [ + ['mode' => 'any', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], - public function testItShowsParticipantGreeting(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $participant = Participant::factory()->for(Form::factory()->sections([ + fn () => [ + ['mode' => 'any', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + false, + ], + + fn () => [ + ['mode' => 'any', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'], + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], + + fn () => [ + ['mode' => 'any', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'], + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'B'], + true, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'], + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'B'], + false, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'B'], + true, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isNotEqual', 'value' => 'A'] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'B'], + true, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isIn', 'value' => ['A']] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isNotIn', 'value' => ['B']] + ]], + test()->dropdownField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isNotIn', 'value' => ['B']] + ]], + test()->radioField('fieldkey')->options(['A', 'B']), + ['fieldkey' => 'A'], + true, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => true] + ]], + test()->checkboxField('fieldkey'), + ['fieldkey' => true], + true, + ], + + fn () => [ + ['mode' => 'all', 'ifs' => [ + ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => false] + ]], + test()->checkboxField('fieldkey'), + ['fieldkey' => true], + false, + ], +]); + + +it('testItShowsFormContent', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + + $participant = Participant::factory()->for( + Form::factory()->sections([ FormtemplateSectionRequest::new()->name('Persönliches')->fields([ $this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME), $this->textField('nachname')->specialType(SpecialType::LASTNAME), - $this->checkboxField('fullyear')->name('Volljährig'), ]) - ])) - ->data(['vorname' => 'Max', 'nachname' => 'Muster', 'fullyear' => true]) - ->create(); + ]) - $mail = new ConfirmRegistrationMail($participant); - $mail->assertSeeInText('# Hallo Max Muster'); - $mail->assertSeeInText('## Persönliches'); - $mail->assertSeeInText('* Vorname: Max'); - $mail->assertSeeInText('* Volljährig: Ja'); + ->mailTop(EditorRequestFactory::new()->text(10, 'mail top'))->mailBottom(EditorRequestFactory::new()->text(11, 'mail bottom')) + ) + ->data(['vorname' => 'Max', 'nachname' => 'Muster']) + ->create(); + + $mail = new ConfirmRegistrationMail($participant); + $mail->assertSeeInText('mail top'); + $mail->assertSeeInText('mail bottom'); +}); + +it('testItShowsParticipantGreeting', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $participant = Participant::factory()->for(Form::factory()->sections([ + FormtemplateSectionRequest::new()->name('Persönliches')->fields([ + $this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME), + $this->textField('nachname')->specialType(SpecialType::LASTNAME), + $this->checkboxField('fullyear')->name('Volljährig'), + ]) + ])) + ->data(['vorname' => 'Max', 'nachname' => 'Muster', 'fullyear' => true]) + ->create(); + + $mail = new ConfirmRegistrationMail($participant); + $mail->assertSeeInText('# Hallo Max Muster'); + $mail->assertSeeInText('## Persönliches'); + $mail->assertSeeInText('* Vorname: Max'); + $mail->assertSeeInText('* Volljährig: Ja'); +}); + +it('testItAttachesMailAttachments', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $participant = Participant::factory()->for( + Form::factory() + ->fields([ + $this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME), + $this->textField('nachname')->specialType(SpecialType::LASTNAME), + ]) + ->withDocument('mailattachments', 'beispiel.pdf', 'content1') + ->withDocument('mailattachments', 'beispiel2.pdf', 'content2') + ) + ->data(['vorname' => 'Max', 'nachname' => 'Muster']) + ->create(); + + $mail = new ConfirmRegistrationMail($participant); + $mail->assertHasAttachedData('content1', 'beispiel.pdf', ['mime' => 'application/pdf']); + $mail->assertHasAttachedData('content2', 'beispiel2.pdf', ['mime' => 'application/pdf']); +}); + +/** + * @param array $conditions + * @param array $participantValues + */ +it('testItFiltersForBlockConditions', function (array $conditions, FormtemplateFieldRequest $field, array $participantValues, bool $result) { + $this->login()->loginNami()->withoutExceptionHandling(); + + $participant = Participant::factory()->for( + Form::factory() + ->fields([ + $field, + $this->textField('firstname')->specialType(SpecialType::FIRSTNAME), + $this->textField('lastname')->specialType(SpecialType::LASTNAME), + ]) + ->mailTop(EditorRequestFactory::new()->text(10, '::content::', $conditions)) + ) + ->data(['firstname' => 'Max', 'lastname' => 'Muster', ...$participantValues]) + ->create(); + + $mail = new ConfirmRegistrationMail($participant); + if ($result) { + $mail->assertSeeInText('::content::'); + } else { + $mail->assertDontSeeInText('::content::'); } +})->with('blocks'); - public function testItAttachesMailAttachments(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $participant = Participant::factory()->for( - Form::factory() - ->fields([ - $this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME), - $this->textField('nachname')->specialType(SpecialType::LASTNAME), - ]) - ->withDocument('mailattachments', 'beispiel.pdf', 'content1') - ->withDocument('mailattachments', 'beispiel2.pdf', 'content2') - ) - ->data(['vorname' => 'Max', 'nachname' => 'Muster']) - ->create(); +/** + * @param array $conditions + * @param array $participantValues + */ +it('testItFiltersForAttachments', function (array $conditions, FormtemplateFieldRequest $field, array $participantValues, bool $result) { + $this->login()->loginNami()->withoutExceptionHandling(); - $mail = new ConfirmRegistrationMail($participant); - $mail->assertHasAttachedData('content1', 'beispiel.pdf', ['mime' => 'application/pdf']); - $mail->assertHasAttachedData('content2', 'beispiel2.pdf', ['mime' => 'application/pdf']); + $participant = Participant::factory()->for( + Form::factory() + ->fields([ + $field, + $this->textField('firstname')->specialType(SpecialType::FIRSTNAME), + $this->textField('lastname')->specialType(SpecialType::LASTNAME), + ]) + ->withDocument('mailattachments', 'beispiel.pdf', 'content', ['conditions' => $conditions]) + ) + ->data(['firstname' => 'Max', 'lastname' => 'Muster', ...$participantValues]) + ->create(); + + $mail = new ConfirmRegistrationMail($participant); + $mail->assertSeeInHtml('Daten'); + if ($result) { + $this->assertTrue($mail->hasAttachedData('content', 'beispiel.pdf', ['mime' => 'application/pdf'])); + } else { + $this->assertFalse($mail->hasAttachedData('content', 'beispiel.pdf', ['mime' => 'application/pdf'])); } - - public static function blockDataProvider(): Generator - { - yield [ - ['mode' => 'all', 'ifs' => []], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'any', 'ifs' => []], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'any', 'ifs' => []], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'any', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'any', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - false, - ]; - - yield [ - ['mode' => 'any', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'], - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'any', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'], - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'B'], - true, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'], - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A'] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'B'], - false, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B'] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'B'], - true, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isNotEqual', 'value' => 'A'] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'B'], - true, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isIn', 'value' => ['A']] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isNotIn', 'value' => ['B']] - ]], - static::dropdownField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isNotIn', 'value' => ['B']] - ]], - static::radioField('fieldkey')->options(['A', 'B']), - ['fieldkey' => 'A'], - true, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => true] - ]], - static::checkboxField('fieldkey'), - ['fieldkey' => true], - true, - ]; - - yield [ - ['mode' => 'all', 'ifs' => [ - ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => false] - ]], - static::checkboxField('fieldkey'), - ['fieldkey' => true], - false, - ]; - } - - /** - * @param array $conditions - * @param array $participantValues - */ - #[DataProvider('blockDataProvider')] - public function testItFiltersForBlockConditions(array $conditions, FormtemplateFieldRequest $field, array $participantValues, bool $result): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - - $participant = Participant::factory()->for( - Form::factory() - ->fields([ - $field, - $this->textField('firstname')->specialType(SpecialType::FIRSTNAME), - $this->textField('lastname')->specialType(SpecialType::LASTNAME), - ]) - ->mailTop(EditorRequestFactory::new()->text(10, '::content::', $conditions)) - ) - ->data(['firstname' => 'Max', 'lastname' => 'Muster', ...$participantValues]) - ->create(); - - $mail = new ConfirmRegistrationMail($participant); - if ($result) { - $mail->assertSeeInText('::content::'); - } else { - $mail->assertDontSeeInText('::content::'); - } - } - - /** - * @param array $conditions - * @param array $participantValues - */ - #[DataProvider('blockDataProvider')] - public function testItFiltersForAttachments(array $conditions, FormtemplateFieldRequest $field, array $participantValues, bool $result): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - - $participant = Participant::factory()->for( - Form::factory() - ->fields([ - $field, - $this->textField('firstname')->specialType(SpecialType::FIRSTNAME), - $this->textField('lastname')->specialType(SpecialType::LASTNAME), - ]) - ->withDocument('mailattachments', 'beispiel.pdf', 'content', ['conditions' => $conditions]) - ) - ->data(['firstname' => 'Max', 'lastname' => 'Muster', ...$participantValues]) - ->create(); - - $mail = new ConfirmRegistrationMail($participant); - $mail->assertSeeInHtml('Daten'); - if ($result) { - $this->assertTrue($mail->hasAttachedData('content', 'beispiel.pdf', ['mime' => 'application/pdf'])); - } else { - $this->assertFalse($mail->hasAttachedData('content', 'beispiel.pdf', ['mime' => 'application/pdf'])); - } - } -} +})->with('blocks'); diff --git a/tests/Feature/Form/FormStoreActionTest.php b/tests/Feature/Form/FormStoreActionTest.php index 1c9fe294..fe889295 100644 --- a/tests/Feature/Form/FormStoreActionTest.php +++ b/tests/Feature/Form/FormStoreActionTest.php @@ -9,111 +9,103 @@ use App\Form\Models\Form; use App\Lib\Events\Succeeded; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Event; -use Generator; -use PHPUnit\Framework\Attributes\DataProvider; use Tests\RequestFactories\EditorRequestFactory; +use Tests\Lib\CreatesFormFields; -class FormStoreActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function() { + test()->setUpForm(); +}); - public function testItStoresForm(): void - { - Event::fake([Succeeded::class]); - $this->login()->loginNami()->withoutExceptionHandling(); - $description = EditorRequestFactory::new()->text(10, 'Lorem'); - FormRequest::new() - ->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')) - ->headerImage('htzz.jpg') - ->sections([FormtemplateSectionRequest::new()->name('sname')->fields([$this->textField()->namiType(NamiType::BIRTHDAY)->forMembers(false)->hint('hhh')])]) - ->fake(); +it('testItStoresForm', function () { + Event::fake([Succeeded::class]); + $this->login()->loginNami()->withoutExceptionHandling(); + $description = EditorRequestFactory::new()->text(10, 'Lorem'); + FormRequest::new() + ->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')) + ->headerImage('htzz.jpg') + ->sections([FormtemplateSectionRequest::new()->name('sname')->fields([$this->textField()->namiType(NamiType::BIRTHDAY)->forMembers(false)->hint('hhh')])]) + ->fake(); - $this->postJson(route('form.store'))->assertOk(); + $this->postJson(route('form.store'))->assertOk(); - $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('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')); - $this->assertEquals('2023-07-07', $form->from->format('Y-m-d')); - $this->assertEquals('2023-07-08', $form->to->format('Y-m-d')); - $this->assertEquals('Geburtstag', $form->config->sections->get(0)->fields->get(0)->namiType->value); - $this->assertEquals('hhh', $form->config->sections->get(0)->fields->get(0)->hint); - $this->assertFalse($form->config->sections->get(0)->fields->get(0)->forMembers); - $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(); - } + $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('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')); + $this->assertEquals('2023-07-07', $form->from->format('Y-m-d')); + $this->assertEquals('2023-07-08', $form->to->format('Y-m-d')); + $this->assertEquals('Geburtstag', $form->config->sections->get(0)->fields->get(0)->namiType->value); + $this->assertEquals('hhh', $form->config->sections->get(0)->fields->get(0)->hint); + $this->assertFalse($form->config->sections->get(0)->fields->get(0)->forMembers); + $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 testItStoresDefaultSorting(): void - { - Event::fake([Succeeded::class]); - $this->login()->loginNami()->withoutExceptionHandling(); - FormRequest::new()->fields([$this->textField()])->fake(); +it('testItStoresDefaultSorting', function () { + Event::fake([Succeeded::class]); + $this->login()->loginNami()->withoutExceptionHandling(); + FormRequest::new()->fields([$this->textField()])->fake(); - $this->postJson(route('form.store'))->assertOk(); + $this->postJson(route('form.store'))->assertOk(); - $form = Form::latest()->first(); - $this->assertEquals('id', $form->meta['sorting']['by']); - $this->assertFalse(false, $form->meta['sorting']['direction']); - } + $form = Form::latest()->first(); + $this->assertEquals('id', $form->meta['sorting']['by']); + $this->assertFalse(false, $form->meta['sorting']['direction']); +}); - public function testRegistrationDatesCanBeNull(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); +it('testRegistrationDatesCanBeNull', function () { + $this->login()->loginNami()->withoutExceptionHandling(); - $this->postJson(route('form.store'), FormRequest::new()->registrationFrom(null)->registrationUntil(null)->create())->assertOk(); + $this->postJson(route('form.store'), FormRequest::new()->registrationFrom(null)->registrationUntil(null)->create())->assertOk(); - $this->assertDatabaseHas('forms', [ - 'registration_until' => null, - 'registration_from' => null, - ]); - } + $this->assertDatabaseHas('forms', [ + 'registration_until' => null, + 'registration_from' => null, + ]); +}); - public function testItStoresExport(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); +it('testItStoresExport', function () { + $this->login()->loginNami()->withoutExceptionHandling(); - $this->postJson(route('form.store'), FormRequest::new()->export(ExportData::from(['root' => FileshareResourceData::from(['connection_id' => 2, 'resource' => '/dir']), 'group_by' => 'lala', 'to_group_field' => 'abc']))->create())->assertOk(); + $this->postJson(route('form.store'), FormRequest::new()->export(ExportData::from(['root' => FileshareResourceData::from(['connection_id' => 2, 'resource' => '/dir']), 'group_by' => 'lala', 'to_group_field' => 'abc']))->create())->assertOk(); - $form = Form::first(); - $this->assertEquals(2, $form->export->root->connectionId); - } - - public static function validationDataProvider(): Generator - { - yield [FormRequest::new()->name(''), ['name' => 'Name ist erforderlich.']]; - yield [FormRequest::new()->excerpt(''), ['excerpt' => 'Auszug ist erforderlich.']]; - yield [FormRequest::new()->description(null), ['description.blocks' => 'Beschreibung ist erforderlich.']]; - yield [FormRequest::new()->state(['from' => null]), ['from' => 'Start ist erforderlich']]; - yield [FormRequest::new()->state(['to' => null]), ['to' => 'Ende ist erforderlich']]; - yield [FormRequest::new()->state(['header_image' => null]), ['header_image' => 'Bild ist erforderlich']]; - } + $form = Form::first(); + $this->assertEquals(2, $form->export->root->connectionId); +}); - /** - * @param array $messages - */ - #[DataProvider('validationDataProvider')] - public function testItValidatesRequests(FormRequest $request, array $messages): void - { - $this->login()->loginNami(); - $request->fake(); +/** + * @param array $messages + */ +it('testItValidatesRequests', function (FormRequest $request, array $messages) { + $this->login()->loginNami(); + $request->fake(); - $this->postJson(route('form.store'))->assertJsonValidationErrors($messages); - } -} + $this->postJson(route('form.store'))->assertJsonValidationErrors($messages); +})->with([ + [FormRequest::new()->name(''), ['name' => 'Name ist erforderlich.']], + [FormRequest::new()->excerpt(''), ['excerpt' => 'Auszug ist erforderlich.']], + [FormRequest::new()->description(null), ['description.blocks' => 'Beschreibung ist erforderlich.']], + [FormRequest::new()->state(['from' => null]), ['from' => 'Start ist erforderlich']], + [FormRequest::new()->state(['to' => null]), ['to' => 'Ende ist erforderlich']], + [FormRequest::new()->state(['header_image' => null]), ['header_image' => 'Bild ist erforderlich']], + +]); diff --git a/tests/Feature/Form/FormTestCase.php b/tests/Feature/Form/FormTestCase.php deleted file mode 100644 index 4b1f1a39..00000000 --- a/tests/Feature/Form/FormTestCase.php +++ /dev/null @@ -1,36 +0,0 @@ -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 822ee8d0..3c549685 100644 --- a/tests/Feature/Form/FormUpdateActionTest.php +++ b/tests/Feature/Form/FormUpdateActionTest.php @@ -7,161 +7,152 @@ use App\Form\Data\ExportData; use App\Form\Models\Form; use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\RequestFactories\EditorRequestFactory; +use Tests\Lib\CreatesFormFields; -class FormUpdateActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItSetsCustomAttributesOfFields(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->create(); - $payload = FormRequest::new()->fields([ - $this->dateField()->state(['max_today' => true]), - ])->create(); +it('testItSetsCustomAttributesOfFields', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); + $payload = FormRequest::new()->fields([ + $this->dateField()->state(['max_today' => true]), + ])->create(); - $this->patchJson(route('form.update', ['form' => $form]), $payload) - ->assertOk(); + $this->patchJson(route('form.update', ['form' => $form]), $payload) + ->assertOk(); - $form = $form->fresh(); + $form = $form->fresh(); - $this->assertTrue($form->config->sections->get(0)->fields->get(0)->maxToday); - } + $this->assertTrue($form->config->sections->get(0)->fields->get(0)->maxToday); +}); - public function testItSetsRegistrationDates(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->create(); - $payload = FormRequest::new()->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->create(); +it('testItSetsRegistrationDates', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); + $payload = FormRequest::new()->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->create(); - $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertOk(); + $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertOk(); - $form = $form->fresh(); + $form = $form->fresh(); - $this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i')); - $this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i')); - } + $this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i')); + $this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i')); +}); - 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(); +it('testItSetsTexts', function () { + $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->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']); - } + $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(); - $form = Form::factory()->create(); +it('testItClearsFrontendCacheWhenFormUpdated', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); - $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->create()); + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->create()); - $this->assertFrontendCacheCleared(); - } + $this->assertFrontendCacheCleared(); +}); - public function testItUpdatesExport(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); +it('testItUpdatesExport', function () { + $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->create(); - $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->export(ExportData::from(['root' => FileshareResourceData::from(['connection_id' => 2, 'resource' => '/dir']), 'group_by' => 'lala', 'to_group_field' => 'abc']))->create()); + $form = Form::factory()->create(); + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->export(ExportData::from(['root' => FileshareResourceData::from(['connection_id' => 2, 'resource' => '/dir']), 'group_by' => 'lala', 'to_group_field' => 'abc']))->create()); - $this->assertEquals(2, $form->fresh()->export->root->connectionId); - } + $this->assertEquals(2, $form->fresh()->export->root->connectionId); +}); - public function testItUpdatesActiveColumnsWhenFieldRemoved(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->fields([ - $this->textField('firstname'), - $this->textField('geb'), - $this->textField('lastname'), - ]) - ->create(); - $payload = FormRequest::new()->fields([ - $this->textField('firstname'), - ])->create(); +it('testItUpdatesActiveColumnsWhenFieldRemoved', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->fields([ + $this->textField('firstname'), + $this->textField('geb'), + $this->textField('lastname'), + ]) + ->create(); + $payload = FormRequest::new()->fields([ + $this->textField('firstname'), + ])->create(); - $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); - $this->assertEquals(['firstname'], $form->fresh()->meta['active_columns']); - } + $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertEquals(['firstname'], $form->fresh()->meta['active_columns']); +}); - public function testItUpdatesIntroOfSections(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory() - ->sections([FormtemplateSectionRequest::new()->intro('aaa')]) - ->create(); - $payload = FormRequest::new()->sections([ - FormtemplateSectionRequest::new()->intro('aaa') - ])->create(); +it('testItUpdatesIntroOfSections', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory() + ->sections([FormtemplateSectionRequest::new()->intro('aaa')]) + ->create(); + $payload = FormRequest::new()->sections([ + FormtemplateSectionRequest::new()->intro('aaa') + ])->create(); - $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); - $this->assertEquals('aaa', $form->fresh()->config->sections[0]->intro); - } + $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertEquals('aaa', $form->fresh()->config->sections[0]->intro); +}); - public function testItUpdatesActiveState(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->create(); +it('testItUpdatesActiveState', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); - $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(false)->create())->assertSessionDoesntHaveErrors()->assertOk(); - $this->assertFalse($form->fresh()->is_active); - $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(true)->create())->assertSessionDoesntHaveErrors()->assertOk(); - $this->assertTrue($form->fresh()->is_active); - } + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(false)->create())->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertFalse($form->fresh()->is_active); + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(true)->create())->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertTrue($form->fresh()->is_active); +}); - public function testItUpdatesPrivateState(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->create(); +it('testItUpdatesPrivateState', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); - $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(false)->create())->assertSessionDoesntHaveErrors()->assertOk(); - $this->assertFalse($form->fresh()->is_private); - $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(true)->create())->assertSessionDoesntHaveErrors()->assertOk(); - $this->assertTrue($form->fresh()->is_private); - } + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(false)->create())->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertFalse($form->fresh()->is_private); + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(true)->create())->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertTrue($form->fresh()->is_private); +}); - public function testItUpdatesActiveColumnsWhenFieldsAdded(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory() - ->fields([]) - ->create(); - $payload = FormRequest::new()->fields([ - $this->textField('firstname'), - $this->textField('geb'), - $this->textField('lastname'), - ])->create(); +it('testItUpdatesActiveColumnsWhenFieldsAdded', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory() + ->fields([]) + ->create(); + $payload = FormRequest::new()->fields([ + $this->textField('firstname'), + $this->textField('geb'), + $this->textField('lastname'), + ])->create(); - $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); - $this->assertEquals(['firstname', 'geb', 'lastname'], $form->fresh()->meta['active_columns']); - } + $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertEquals(['firstname', 'geb', 'lastname'], $form->fresh()->meta['active_columns']); +}); - public function testItUpdatesPrevention(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->create(); - $payload = FormRequest::new() - ->preventionText(EditorRequestFactory::new()->text(10, 'lorem ipsum')) - ->state(['needs_prevention' => true, 'prevention_conditions' => ['mode' => 'all', 'ifs' => [['field' => 'vorname', 'value' => 'Max', 'comparator' => 'isEqual']]]]) - ->create(); +it('testItUpdatesPrevention', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); + $payload = FormRequest::new() + ->preventionText(EditorRequestFactory::new()->text(10, 'lorem ipsum')) + ->state(['needs_prevention' => true, 'prevention_conditions' => ['mode' => 'all', 'ifs' => [['field' => 'vorname', 'value' => 'Max', 'comparator' => 'isEqual']]]]) + ->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']); - $this->assertEquals(['mode' => 'all', 'ifs' => [['field' => 'vorname', 'value' => 'Max', 'comparator' => 'isEqual']]], $form->fresh()->prevention_conditions->toArray()); - } -} + $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']); + $this->assertEquals(['mode' => 'all', 'ifs' => [['field' => 'vorname', 'value' => 'Max', 'comparator' => 'isEqual']]], $form->fresh()->prevention_conditions->toArray()); +}); diff --git a/tests/Feature/Form/FormUpdateMetaActionTest.php b/tests/Feature/Form/FormUpdateMetaActionTest.php index d687e062..0202c0a3 100644 --- a/tests/Feature/Form/FormUpdateMetaActionTest.php +++ b/tests/Feature/Form/FormUpdateMetaActionTest.php @@ -4,45 +4,45 @@ namespace Tests\Feature\Form; use App\Form\Models\Form; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\Lib\CreatesFormFields; -class FormUpdateMetaActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItUpdatesMetaOfForm(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory() - ->sections([FormtemplateSectionRequest::new()->fields([ - $this->textField('textone'), - $this->dropdownField('texttwo'), - ])])->create(); +it('testItUpdatesMetaOfForm', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory() + ->sections([FormtemplateSectionRequest::new()->fields([ + $this->textField('textone'), + $this->dropdownField('texttwo'), + ])])->create(); - $this->patchJson(route('form.update-meta', ['form' => $form]), [ - 'active_columns' => ['textone'], - 'sorting' => ['by' => 'textone', 'direction' => false], - ])->assertOk() - ->assertJsonPath('active_columns.0', 'textone') - ->assertJsonPath('sorting.by', 'textone'); + $this->patchJson(route('form.update-meta', ['form' => $form]), [ + 'active_columns' => ['textone'], + 'sorting' => ['by' => 'textone', 'direction' => false], + ])->assertOk() + ->assertJsonPath('active_columns.0', 'textone') + ->assertJsonPath('sorting.by', 'textone'); - $form = Form::latest()->first(); - $this->assertEquals(['by' => 'textone', 'direction' => false], $form->meta['sorting']); - $this->assertEquals(['textone'], $form->meta['active_columns']); - } + $form = Form::latest()->first(); + $this->assertEquals(['by' => 'textone', 'direction' => false], $form->meta['sorting']); + $this->assertEquals(['textone'], $form->meta['active_columns']); +}); - public function testItCanSetCreatedAtMeta(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->create(); +it('testItCanSetCreatedAtMeta', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); - $this->patchJson(route('form.update-meta', ['form' => $form]), [ - 'active_columns' => ['created_at'], - 'sorting' => ['by' => 'textone', 'direction' => false], - ])->assertOk(); + $this->patchJson(route('form.update-meta', ['form' => $form]), [ + 'active_columns' => ['created_at'], + 'sorting' => ['by' => 'textone', 'direction' => false], + ])->assertOk(); - $form = Form::latest()->first(); - $this->assertEquals(['by' => 'textone', 'direction' => false], $form->fresh()->meta['sorting']); - $this->assertEquals(['created_at'], $form->fresh()->meta['active_columns']); - } -} + $form = Form::latest()->first(); + $this->assertEquals(['by' => 'textone', 'direction' => false], $form->fresh()->meta['sorting']); + $this->assertEquals(['created_at'], $form->fresh()->meta['active_columns']); +}); diff --git a/tests/Feature/Form/FormtemplateStoreActionTest.php b/tests/Feature/Form/FormtemplateStoreActionTest.php index dd02a620..a1b7df04 100644 --- a/tests/Feature/Form/FormtemplateStoreActionTest.php +++ b/tests/Feature/Form/FormtemplateStoreActionTest.php @@ -9,83 +9,73 @@ use App\Form\Models\Formtemplate; use App\Lib\Events\Succeeded; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Event; -use Generator; -use PHPUnit\Framework\Attributes\DataProvider; +use Tests\Lib\CreatesFormFields; use Tests\RequestFactories\EditorRequestFactory; -class FormtemplateStoreActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +it('testItStoresTemplates', function () { + Event::fake([Succeeded::class]); + $this->login()->loginNami()->withoutExceptionHandling(); + $payload = FormtemplateRequest::new()->name('testname')->sections([ + FormtemplateSectionRequest::new()->name('Persönliches')->fields([ + $this->textField('a')->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false)->hint('hhh')->intro('intro'), + $this->textareaField('b')->name('lala2')->required(false)->specialType(SpecialType::FIRSTNAME)->rows(10), + ]), + ]) + ->mailTop(EditorRequestFactory::new()->text(10, 'lala')) + ->mailBottom(EditorRequestFactory::new()->text(10, 'lblb')) + ->create(); - public function testItStoresTemplates(): void - { - Event::fake([Succeeded::class]); - $this->login()->loginNami()->withoutExceptionHandling(); - $payload = FormtemplateRequest::new()->name('testname')->sections([ - FormtemplateSectionRequest::new()->name('Persönliches')->fields([ - $this->textField('a')->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false)->hint('hhh')->intro('intro'), - $this->textareaField('b')->name('lala2')->required(false)->specialType(SpecialType::FIRSTNAME)->rows(10), - ]), - ]) - ->mailTop(EditorRequestFactory::new()->text(10, 'lala')) - ->mailBottom(EditorRequestFactory::new()->text(10, 'lblb')) - ->create(); + $this->postJson(route('formtemplate.store'), $payload)->assertOk(); - $this->postJson(route('formtemplate.store'), $payload)->assertOk(); + $formtemplate = Formtemplate::latest()->first(); + $this->assertEquals('Persönliches', $formtemplate->config->sections->get(0)->name); + $this->assertEquals('lala1', $formtemplate->config->sections->get(0)->fields->get(0)->name); + $this->assertNull($formtemplate->config->sections->get(0)->fields->get(0)->specialType); + $this->assertEquals('hhh', $formtemplate->config->sections->get(0)->fields->get(0)->hint); + $this->assertEquals('intro', $formtemplate->config->sections->get(0)->fields->get(0)->intro); + $this->assertInstanceOf(TextField::class, $formtemplate->config->sections->get(0)->fields->get(0)); + $this->assertInstanceOf(TextareaField::class, $formtemplate->config->sections->get(0)->fields->get(1)); + $this->assertEquals(false, $formtemplate->config->sections->get(0)->fields->get(1)->required); + $this->assertEquals(SpecialType::FIRSTNAME, $formtemplate->config->sections->get(0)->fields->get(1)->specialType); + $this->assertEquals(['mobile' => 2, 'tablet' => 2, 'desktop' => 1], $formtemplate->config->sections->get(0)->fields->get(0)->columns->toArray()); + $this->assertEquals(10, $formtemplate->config->sections->get(0)->fields->get(1)->rows); + $this->assertEquals('lala', $formtemplate->mail_top->blocks[0]['data']['text']); + $this->assertEquals('lblb', $formtemplate->mail_bottom->blocks[0]['data']['text']); + $this->assertFalse($formtemplate->config->sections->get(0)->fields->get(0)->required); + Event::assertDispatched(Succeeded::class, fn(Succeeded $event) => $event->message === 'Vorlage gespeichert.'); +}); - $formtemplate = Formtemplate::latest()->first(); - $this->assertEquals('Persönliches', $formtemplate->config->sections->get(0)->name); - $this->assertEquals('lala1', $formtemplate->config->sections->get(0)->fields->get(0)->name); - $this->assertNull($formtemplate->config->sections->get(0)->fields->get(0)->specialType); - $this->assertEquals('hhh', $formtemplate->config->sections->get(0)->fields->get(0)->hint); - $this->assertEquals('intro', $formtemplate->config->sections->get(0)->fields->get(0)->intro); - $this->assertInstanceOf(TextField::class, $formtemplate->config->sections->get(0)->fields->get(0)); - $this->assertInstanceOf(TextareaField::class, $formtemplate->config->sections->get(0)->fields->get(1)); - $this->assertEquals(false, $formtemplate->config->sections->get(0)->fields->get(1)->required); - $this->assertEquals(SpecialType::FIRSTNAME, $formtemplate->config->sections->get(0)->fields->get(1)->specialType); - $this->assertEquals(['mobile' => 2, 'tablet' => 2, 'desktop' => 1], $formtemplate->config->sections->get(0)->fields->get(0)->columns->toArray()); - $this->assertEquals(10, $formtemplate->config->sections->get(0)->fields->get(1)->rows); - $this->assertEquals('lala', $formtemplate->mail_top->blocks[0]['data']['text']); - $this->assertEquals('lblb', $formtemplate->mail_bottom->blocks[0]['data']['text']); - $this->assertFalse($formtemplate->config->sections->get(0)->fields->get(0)->required); - Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Vorlage gespeichert.'); - } +/** + * @param array $messages + */ +it('testItValidatesRequests', function (FormtemplateRequest $request, array $messages) { + $this->login()->loginNami(); + $request->fake(); - public static function validationDataProvider(): Generator - { - yield [FormtemplateRequest::new()->name(''), ['name' => 'Name ist erforderlich.']]; - yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->name('')]), ['config.sections.0.name' => 'Sektionsname ist erforderlich.']]; - yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ - static::textField()->name(''), - ])]), ['config.sections.0.fields.0.name' => 'Feldname ist erforderlich.']]; - yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ - FormtemplateFieldRequest::type('') - ])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist erforderlich.']]; - yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ - FormtemplateFieldRequest::type('aaaaa'), - ])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.']]; - yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ - static::textField(''), - ])]), ['config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.']]; - yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ - static::textField('a b'), - ])]), ['config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.']]; - yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ - static::textField()->required('la') - ])]), ['config.sections.0.fields.0.required' => 'Erforderlich muss ein Wahrheitswert sein.']]; - } - - /** - * @param array $messages - */ - #[DataProvider('validationDataProvider')] - public function testItValidatesRequests(FormtemplateRequest $request, array $messages): void - { - $this->login()->loginNami(); - $request->fake(); - - $this->postJson(route('formtemplate.store')) - ->assertJsonValidationErrors($messages); - } -} + $this->postJson(route('formtemplate.store')) + ->assertJsonValidationErrors($messages); +})->with([ + fn () => [FormtemplateRequest::new()->name(''), ['name' => 'Name ist erforderlich.']], + fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->name('')]), ['config.sections.0.name' => 'Sektionsname ist erforderlich.']], + fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ + test()->textField()->name(''), + ])]), ['config.sections.0.fields.0.name' => 'Feldname ist erforderlich.']], + fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ + FormtemplateFieldRequest::type('') + ])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist erforderlich.']], + fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ + FormtemplateFieldRequest::type('aaaaa'), + ])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.']], + fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ + test()->textField(''), + ])]), ['config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.']], + fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ + test()->textField('a b'), + ])]), ['config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.']], + fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ + test()->textField()->required('la') + ])]), ['config.sections.0.fields.0.required' => 'Erforderlich muss ein Wahrheitswert sein.']], +]); diff --git a/tests/Feature/Form/IsDirtyActionTest.php b/tests/Feature/Form/IsDirtyActionTest.php index a425ed3d..4935973a 100644 --- a/tests/Feature/Form/IsDirtyActionTest.php +++ b/tests/Feature/Form/IsDirtyActionTest.php @@ -4,22 +4,24 @@ namespace Tests\Feature\Form; use App\Form\Models\Form; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\Lib\CreatesFormFields; -class IsDirtyActionTest extends FormTestCase -{ - use DatabaseTransactions; +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - public function testItChecksIfFormIsDirty(): void - { - $this->login()->loginNami(); - $form = Form::factory()->fields([ - $this->textField(), - ])->create(); +beforeEach(function () { + test()->setUpForm(); +}); - $this->postJson(route('form.is-dirty', ['form' => $form]), ['config' => $form->config->toArray()])->assertJsonPath('result', false); +it('testItChecksIfFormIsDirty', function () { + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->textField(), + ])->create(); - $modifiedConfig = $form->config->toArray(); - data_set($modifiedConfig, 'sections.0.name', 'mod'); - $this->postJson(route('form.is-dirty', ['form' => $form]), ['config' => $modifiedConfig])->assertJsonPath('result', true); - } -} + $this->postJson(route('form.is-dirty', ['form' => $form]), ['config' => $form->config->toArray()])->assertJsonPath('result', false); + + $modifiedConfig = $form->config->toArray(); + data_set($modifiedConfig, 'sections.0.name', 'mod'); + $this->postJson(route('form.is-dirty', ['form' => $form]), ['config' => $modifiedConfig])->assertJsonPath('result', true); +}); diff --git a/tests/Feature/Form/ParticipantDestroyActionTest.php b/tests/Feature/Form/ParticipantDestroyActionTest.php index 9e0fdb84..e27954e0 100644 --- a/tests/Feature/Form/ParticipantDestroyActionTest.php +++ b/tests/Feature/Form/ParticipantDestroyActionTest.php @@ -5,23 +5,24 @@ namespace Tests\Feature\Form; use App\Form\Models\Form; use App\Form\Models\Participant; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\Lib\CreatesFormFields; -class ParticipantDestroyActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItCanDestroyAParticipant(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory() - ->has(Participant::factory()) - ->sections([]) - ->create(); +it('testItCanDestroyAParticipant', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory() + ->has(Participant::factory()) + ->sections([]) + ->create(); - $this->deleteJson(route('participant.destroy', ['participant' => $form->participants->first()])) - ->assertOk(); + $this->deleteJson(route('participant.destroy', ['participant' => $form->participants->first()])) + ->assertOk(); - $this->assertDatabaseCount('participants', 0); - } -} + $this->assertDatabaseCount('participants', 0); +}); diff --git a/tests/Feature/Form/ParticipantExportActionTest.php b/tests/Feature/Form/ParticipantExportActionTest.php index d39aa289..5c182567 100644 --- a/tests/Feature/Form/ParticipantExportActionTest.php +++ b/tests/Feature/Form/ParticipantExportActionTest.php @@ -6,34 +6,35 @@ use App\Form\Models\Form; use App\Form\Models\Participant; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Storage; +use Tests\Lib\CreatesFormFields; -class ParticipantExportActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItShowsParticipantsAndColumns(): void - { - Storage::fake('temp'); - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory() - ->has(Participant::factory()->data(['stufe' => 'Pfadfinder', 'vorname' => 'Max', 'select' => ['A', 'B']])) - ->sections([ - FormtemplateSectionRequest::new()->fields([ - $this->textField('vorname')->name('Vorname'), - $this->checkboxesField('select')->name('Abcselect')->options(['A', 'B', 'C']), - $this->dropdownField('stufe')->name('Stufe')->options(['Wölfling', 'Jungpfadfinder', 'Pfadfinder']), - ]), - ]) - ->name('ZEM 2024') - ->create(); +it('testItShowsParticipantsAndColumns', function () { + Storage::fake('temp'); + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory() + ->has(Participant::factory()->data(['stufe' => 'Pfadfinder', 'vorname' => 'Max', 'select' => ['A', 'B']])) + ->sections([ + FormtemplateSectionRequest::new()->fields([ + $this->textField('vorname')->name('Vorname'), + $this->checkboxesField('select')->name('Abcselect')->options(['A', 'B', 'C']), + $this->dropdownField('stufe')->name('Stufe')->options(['Wölfling', 'Jungpfadfinder', 'Pfadfinder']), + ]), + ]) + ->name('ZEM 2024') + ->create(); - $this->get(route('form.export', ['form' => $form]))->assertDownload('tn-zem-2024.xlsx'); - $contents = Storage::disk('temp')->get('tn-zem-2024.xlsx'); - $this->assertExcelContent('Max', $contents); - $this->assertExcelContent('A, B', $contents); - $this->assertExcelContent('Pfadfinder', $contents); - $this->assertExcelContent('Stufe', $contents); - $this->assertExcelContent('Abcselect', $contents); - } -} + $this->get(route('form.export', ['form' => $form]))->assertDownload('tn-zem-2024.xlsx'); + $contents = Storage::disk('temp')->get('tn-zem-2024.xlsx'); + $this->assertExcelContent('Max', $contents); + $this->assertExcelContent('A, B', $contents); + $this->assertExcelContent('Pfadfinder', $contents); + $this->assertExcelContent('Stufe', $contents); + $this->assertExcelContent('Abcselect', $contents); +}); diff --git a/tests/Feature/Form/ParticipantFieldsActionTest.php b/tests/Feature/Form/ParticipantFieldsActionTest.php index 30db23fb..e9ceba72 100644 --- a/tests/Feature/Form/ParticipantFieldsActionTest.php +++ b/tests/Feature/Form/ParticipantFieldsActionTest.php @@ -5,32 +5,33 @@ namespace Tests\Feature\Form; use App\Form\Models\Form; use App\Form\Models\Participant; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\Lib\CreatesFormFields; -class ParticipantFieldsActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItShowsParticipantsFields(): void - { - $this->login()->loginNami()->withoutExceptionHandling(); - $participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']]) - ->for(Form::factory()->sections([ - FormtemplateSectionRequest::new()->name('Sektion')->fields([ - $this->textField('vorname')->name('Vorname'), - $this->checkboxesField('select')->options(['A', 'B', 'C']), - ]) +it('testItShowsParticipantsFields', function () { + $this->login()->loginNami()->withoutExceptionHandling(); + $participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']]) + ->for(Form::factory()->sections([ + FormtemplateSectionRequest::new()->name('Sektion')->fields([ + $this->textField('vorname')->name('Vorname'), + $this->checkboxesField('select')->options(['A', 'B', 'C']), + ]) - ])) - ->create(); + ])) + ->create(); - $this->callFilter('participant.fields', [], ['participant' => $participant->id]) - ->assertOk() - ->assertJsonPath('data.id', $participant->id) - ->assertJsonPath('data.config.sections.0.name', 'Sektion') - ->assertJsonPath('data.config.sections.0.fields.0.key', 'vorname') - ->assertJsonPath('data.config.sections.0.fields.0.value', 'Max') - ->assertJsonPath('data.config.sections.0.fields.1.key', 'select') - ->assertJsonPath('data.config.sections.0.fields.1.value', ['A', 'B']); - } -} + $this->callFilter('participant.fields', [], ['participant' => $participant->id]) + ->assertOk() + ->assertJsonPath('data.id', $participant->id) + ->assertJsonPath('data.config.sections.0.name', 'Sektion') + ->assertJsonPath('data.config.sections.0.fields.0.key', 'vorname') + ->assertJsonPath('data.config.sections.0.fields.0.value', 'Max') + ->assertJsonPath('data.config.sections.0.fields.1.key', 'select') + ->assertJsonPath('data.config.sections.0.fields.1.value', ['A', 'B']); +}); diff --git a/tests/Feature/Form/ParticipantStoreActionTest.php b/tests/Feature/Form/ParticipantStoreActionTest.php index 9172658d..eb4b32af 100644 --- a/tests/Feature/Form/ParticipantStoreActionTest.php +++ b/tests/Feature/Form/ParticipantStoreActionTest.php @@ -6,38 +6,38 @@ use App\Form\Actions\ExportSyncAction; use App\Form\Models\Form; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Queue; +use Tests\Lib\CreatesFormFields; -class ParticipantStoreActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItStoresParticipant(): void - { - Queue::fake(); - $this->login()->loginNami()->withoutExceptionHandling(); - $form = Form::factory()->fields([ - $this->textField('vorname')->name('Vorname')->required(true), - ]) - ->create(); +it('testItStoresParticipant', function () { + Queue::fake(); + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->fields([ + $this->textField('vorname')->name('Vorname')->required(true), + ]) + ->create(); - $this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => 'Jane']) - ->assertOk(); + $this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => 'Jane']) + ->assertOk(); - $this->assertEquals('Jane', $form->participants->first()->data['vorname']); - ExportSyncAction::assertPushed(); - } + $this->assertEquals('Jane', $form->participants->first()->data['vorname']); + ExportSyncAction::assertPushed(); +}); - public function testItHasValidation(): void - { - Queue::fake(); - $this->login()->loginNami(); - $form = Form::factory()->fields([ - $this->textField('vorname')->name('Vorname')->required(true), - ]) - ->create(); +it('testItHasValidation', function () { + Queue::fake(); + $this->login()->loginNami(); + $form = Form::factory()->fields([ + $this->textField('vorname')->name('Vorname')->required(true), + ]) + ->create(); - $this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => '']) - ->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']); - } -} + $this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => '']) + ->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']); +}); diff --git a/tests/Feature/Form/ParticipantUpdateActionTest.php b/tests/Feature/Form/ParticipantUpdateActionTest.php index e1dc3fa9..b6e141ae 100644 --- a/tests/Feature/Form/ParticipantUpdateActionTest.php +++ b/tests/Feature/Form/ParticipantUpdateActionTest.php @@ -7,39 +7,39 @@ use App\Form\Models\Form; use App\Form\Models\Participant; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Queue; +use Tests\Lib\CreatesFormFields; -class ParticipantUpdateActionTest extends FormTestCase -{ +uses(DatabaseTransactions::class); +uses(CreatesFormFields::class); - use DatabaseTransactions; +beforeEach(function () { + test()->setUpForm(); +}); - public function testItUpdatesParticipant(): void - { - Queue::fake(); - $this->login()->loginNami()->withoutExceptionHandling(); - $participant = Participant::factory()->data(['vorname' => 'Max']) - ->for(Form::factory()->fields([ - $this->textField('vorname')->name('Vorname'), - ])) - ->create(); +it('testItUpdatesParticipant', function () { + Queue::fake(); + $this->login()->loginNami()->withoutExceptionHandling(); + $participant = Participant::factory()->data(['vorname' => 'Max']) + ->for(Form::factory()->fields([ + $this->textField('vorname')->name('Vorname'), + ])) + ->create(); - $this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => 'Jane']) - ->assertOk(); + $this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => 'Jane']) + ->assertOk(); - $this->assertEquals('Jane', $participant->fresh()->data['vorname']); - ExportSyncAction::assertPushed(); - } + $this->assertEquals('Jane', $participant->fresh()->data['vorname']); + ExportSyncAction::assertPushed(); +}); - public function testItHasValidation(): void - { - $this->login()->loginNami(); - $participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']]) - ->for(Form::factory()->fields([ - $this->textField('vorname')->name('Vorname')->required(true), - ])) - ->create(); +it('testItHasValidation', function () { + $this->login()->loginNami(); + $participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']]) + ->for(Form::factory()->fields([ + $this->textField('vorname')->name('Vorname')->required(true), + ])) + ->create(); - $this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => '']) - ->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']); - } -} + $this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => '']) + ->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']); +}); diff --git a/tests/Lib/CreatesFormFields.php b/tests/Lib/CreatesFormFields.php index a8c4690a..07ca411a 100644 --- a/tests/Lib/CreatesFormFields.php +++ b/tests/Lib/CreatesFormFields.php @@ -13,11 +13,54 @@ use App\Form\Fields\NumberField; use App\Form\Fields\RadioField; use App\Form\Fields\TextareaField; use App\Form\Fields\TextField; -use Faker\Generator; +use App\Form\FormSettings; +use App\Form\Models\Form; +use App\Member\Member; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Storage; +use Illuminate\Testing\TestResponse; use Tests\Feature\Form\FormtemplateFieldRequest; trait CreatesFormFields { + + + /** + * @param array $attributes + */ + public function createMember(array $attributes, ?callable $factoryCallback = null): Member + { + return call_user_func($factoryCallback ?: fn ($factory) => $factory, Member::factory()->defaults()) + ->create($attributes); + } + + /** + * @param array $payload + */ + public function register(Form $form, array $payload): TestResponse + { + return $this->postJson(route('form.register', ['form' => $form]), $payload); + } + + public function setUpForm() { + app(FormSettings::class)->fill(['clearCacheUrl' => 'http://event.com/clear-cache'])->save(); + + Http::fake(function ($request) { + if ($request->url() === 'http://event.com/clear-cache') { + return Http::response('', 200); + } + }); + + Storage::fake('temp'); + } + + protected function assertFrontendCacheCleared(): void + { + Http::assertSent(fn ($request) => $request->url() === 'http://event.com/clear-cache' + && $request->method() === 'GET' + ); + } + protected static function namiField(?string $key = null): FormtemplateFieldRequest { return FormtemplateFieldRequest::type(NamiField::class)->key($key ?? static::randomKey());