Add FormTestCase

This commit is contained in:
philipp lang 2024-02-19 01:07:54 +01:00
parent 54b650fb91
commit f601167893
6 changed files with 136 additions and 91 deletions

View File

@ -3,25 +3,15 @@
namespace Tests\Feature\Form;
use App\Form\Enums\NamiType;
use App\Form\Fields\CheckboxesField;
use App\Form\Fields\CheckboxField;
use App\Form\Fields\DateField;
use App\Form\Fields\DropdownField;
use App\Form\Fields\GroupField;
use App\Form\Fields\NamiField;
use App\Form\Fields\RadioField;
use App\Form\Fields\TextareaField;
use App\Form\Fields\TextField;
use App\Form\Models\Form;
use App\Group;
use App\Member\Member;
use Carbon\Carbon;
use Generator;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use Illuminate\Support\Facades\Storage;
class FormRegisterActionTest extends TestCase
class FormRegisterActionTest extends FormTestCase
{
use DatabaseTransactions;
@ -39,11 +29,11 @@ class FormRegisterActionTest extends TestCase
$form = Form::factory()
->sections([
FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(TextField::class)->key('vorname'),
FormtemplateFieldRequest::type(TextField::class)->key('nachname'),
$this->textField('vorname'),
$this->textField('nachname'),
]),
FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(TextField::class)->key('spitzname'),
$this->textField('spitzname'),
]),
])
->create();
@ -83,151 +73,151 @@ class FormRegisterActionTest extends TestCase
public function validationDataProvider(): Generator
{
yield [
FormtemplateFieldRequest::type(DateField::class)->name('Geburtsdatum')->maxToday(false)->key('birthday'),
$this->dateField('birthday')->name('Geburtsdatum')->maxToday(false),
['birthday' => 'aa'],
['birthday' => 'Geburtsdatum muss ein gültiges Datum sein.']
];
yield [
FormtemplateFieldRequest::type(DateField::class)->name('Geburtsdatum')->maxToday(false)->key('birthday'),
$this->dateField('birthday')->name('Geburtsdatum')->maxToday(false),
['birthday' => '2021-05-06'],
null,
];
yield [
FormtemplateFieldRequest::type(DateField::class)->name('Geburtsdatum')->maxToday(true)->key('birthday'),
$this->dateField('birthday')->name('Geburtsdatum')->maxToday(true),
['birthday' => '2024-02-16'],
['birthday' => 'Geburtsdatum muss ein Datum vor oder gleich dem 15.02.2024 sein.'],
];
yield [
FormtemplateFieldRequest::type(DateField::class)->name('Geburtsdatum')->maxToday(true)->key('birthday'),
$this->dateField('birthday')->name('Geburtsdatum')->maxToday(true),
['birthday' => '2024-02-15'],
null,
];
yield [
FormtemplateFieldRequest::type(TextField::class)->name('Vorname der Mutter')->required(true)->key('vorname'),
$this->textField('vorname')->name('Vorname der Mutter')->required(true),
['vorname' => ''],
['vorname' => 'Vorname der Mutter ist erforderlich.']
];
yield [
FormtemplateFieldRequest::type(TextField::class)->name('Vorname der Mutter')->required(true)->key('vorname'),
$this->textField('vorname')->name('Vorname der Mutter')->required(true),
['vorname' => 5],
['vorname' => 'Vorname der Mutter muss ein String sein.']
];
yield [
FormtemplateFieldRequest::type(RadioField::class)->name('Ja oder Nein')->required(true)->key('yes_or_no'),
$this->radioField('yes_or_no')->name('Ja oder Nein')->required(true),
['yes_or_no' => null],
['yes_or_no' => 'Ja oder Nein ist erforderlich.']
];
yield [
FormtemplateFieldRequest::type(RadioField::class)->name('Buchstabe')->options(['A', 'B'])->required(false)->key('letter'),
$this->radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false),
['letter' => 'Z'],
['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.']
];
yield [
FormtemplateFieldRequest::type(RadioField::class)->name('Buchstabe')->options(['A', 'B'])->required(true)->key('letter'),
$this->radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true),
['letter' => 'Z'],
['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.']
];
yield [
FormtemplateFieldRequest::type(RadioField::class)->name('Buchstabe')->options(['A', 'B'])->required(true)->key('letter'),
$this->radioField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true),
['letter' => 'A'],
null
];
yield [
FormtemplateFieldRequest::type(CheckboxesField::class)->name('Buchstabe')->options(['A', 'B'])->key('letter'),
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']),
['letter' => ['Z']],
['letter.0' => 'Der gewählte Wert für Buchstabe ist ungültig.'],
];
yield [
FormtemplateFieldRequest::type(CheckboxesField::class)->name('Buchstabe')->options(['A', 'B'])->key('letter'),
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']),
['letter' => 77],
['letter' => 'Buchstabe muss ein Array sein.'],
];
yield [
FormtemplateFieldRequest::type(CheckboxesField::class)->name('Buchstabe')->options(['A', 'B'])->key('letter'),
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']),
['letter' => ['A']],
null,
];
yield [
FormtemplateFieldRequest::type(CheckboxesField::class)->name('Buchstabe')->options(['A', 'B'])->key('letter'),
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B']),
['letter' => []],
null,
];
yield [
FormtemplateFieldRequest::type(CheckboxField::class)->name('Datenschutz')->required(false)->key('data'),
$this->checkboxField('data')->name('Datenschutz')->required(false),
['data' => 5],
['data' => 'Datenschutz muss ein Wahrheitswert sein.'],
];
yield [
FormtemplateFieldRequest::type(CheckboxField::class)->name('Datenschutz')->required(false)->key('data'),
$this->checkboxField('data')->name('Datenschutz')->required(false),
['data' => false],
null
];
yield [
FormtemplateFieldRequest::type(CheckboxField::class)->name('Datenschutz')->required(true)->key('data'),
$this->checkboxField('data')->name('Datenschutz')->required(true),
['data' => false],
['data' => 'Datenschutz muss akzeptiert werden.'],
];
yield [
FormtemplateFieldRequest::type(CheckboxField::class)->name('Datenschutz')->required(true)->key('data'),
$this->checkboxField('data')->name('Datenschutz')->required(true),
['data' => true],
null,
];
yield [
FormtemplateFieldRequest::type(DropdownField::class)->name('Ja oder Nein')->required(true)->key('yes_or_no'),
$this->dropdownField('yes_or_no')->name('Ja oder Nein')->required(true),
['yes_or_no' => null],
['yes_or_no' => 'Ja oder Nein ist erforderlich.']
];
yield [
FormtemplateFieldRequest::type(DropdownField::class)->name('Buchstabe')->options(['A', 'B'])->required(false)->key('letter'),
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(false),
['letter' => 'Z'],
['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.']
];
yield [
FormtemplateFieldRequest::type(DropdownField::class)->name('Buchstabe')->options(['A', 'B'])->required(true)->key('letter'),
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true),
['letter' => 'Z'],
['letter' => 'Der gewählte Wert für Buchstabe ist ungültig.']
];
yield [
FormtemplateFieldRequest::type(DropdownField::class)->name('Buchstabe')->options(['A', 'B'])->required(true)->key('letter'),
$this->dropdownField('letter')->name('Buchstabe')->options(['A', 'B'])->required(true),
['letter' => 'A'],
null
];
yield [
FormtemplateFieldRequest::type(TextareaField::class)->name('Vorname der Mutter')->required(true)->key('vorname'),
$this->textareaField('vorname')->name('Vorname der Mutter')->required(true),
['vorname' => ''],
['vorname' => 'Vorname der Mutter ist erforderlich.']
];
yield [
FormtemplateFieldRequest::type(TextareaField::class)->name('Vorname der Mutter')->required(true)->key('vorname'),
$this->textareaField('vorname')->name('Vorname der Mutter')->required(true),
['vorname' => 5],
['vorname' => 'Vorname der Mutter muss ein String sein.']
];
yield [
FormtemplateFieldRequest::type(TextareaField::class)->name('Vorname der Mutter')->required(true)->key('vorname'),
$this->textareaField('vorname')->name('Vorname der Mutter')->required(true),
['vorname' => 5],
['vorname' => 'Vorname der Mutter muss ein String sein.']
];
@ -240,7 +230,7 @@ class FormRegisterActionTest extends TestCase
$foreignGroup = Group::factory()->create();
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(GroupField::class)->name('Gruppe')->parentGroup($group->id)->required(true)->key('group')
$this->groupField('group')->name('Gruppe')->parentGroup($group->id)->required(true)
])])
->create();
@ -250,12 +240,12 @@ class FormRegisterActionTest extends TestCase
->assertJsonValidationErrors(['group' => 'Der gewählte Wert für Gruppe ist ungültig.']);
}
public function testGroupCanBeNull(): void
public function testGroupFieldCanBeNullWhenNotRequired(): void
{
$this->login()->loginNami();
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(GroupField::class)->parentGroup(Group::factory()->create()->id)->required(false)->key('group')
$this->groupField('group')->parentGroup(Group::factory()->create()->id)->required(false)
])])
->create();
@ -270,8 +260,8 @@ class FormRegisterActionTest extends TestCase
$foreignGroup = Group::factory()->create();
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(GroupField::class)->name('Übergeordnete Gruppe')->parentGroup($group->id)->required(true)->key('parentgroup'),
FormtemplateFieldRequest::type(GroupField::class)->name('Gruppe')->parentField('parentgroup')->required(true)->key('group')
$this->groupField('parentgroup')->name('Übergeordnete Gruppe')->parentGroup($group->id)->required(true),
$this->groupField('group')->name('Gruppe')->parentField('parentgroup')->required(true),
])])
->create();
@ -290,9 +280,9 @@ class FormRegisterActionTest extends TestCase
$this->createMember(['mitgliedsnr' => '5506', 'firstname' => 'Def', 'birthday' => '2023-01-06']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(NamiField::class)->key('members'),
FormtemplateFieldRequest::type(TextField::class)->key('firstname')->required(true)->namiType(NamiType::FIRSTNAME),
FormtemplateFieldRequest::type(DateField::class)->key('birthday')->required(true)->namiType(NamiType::BIRTHDAY),
$this->namiField('members'),
$this->textField('firstname')->required(true)->namiType(NamiType::FIRSTNAME),
$this->dateField('birthday')->required(true)->namiType(NamiType::BIRTHDAY),
])])
->create();
@ -316,8 +306,8 @@ class FormRegisterActionTest extends TestCase
$this->createMember(['mitgliedsnr' => '5505']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(NamiField::class)->key('members'),
FormtemplateFieldRequest::type(TextField::class)->key('other')->namiType(null)->required(false),
$this->namiField('members'),
$this->textField('other')->namiType(null)->required(false),
])])
->create();
@ -334,8 +324,8 @@ class FormRegisterActionTest extends TestCase
$this->createMember(['mitgliedsnr' => '5506', 'firstname' => 'Paula', 'lastname' => 'Schirm']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(NamiField::class)->key('members'),
FormtemplateFieldRequest::type(TextField::class)->name('Andere')->key('other')->namiType(null)->required(true),
$this->namiField('members'),
$this->textField('other')->name('Andere')->namiType(null)->required(true),
])])
->create();
@ -349,8 +339,8 @@ class FormRegisterActionTest extends TestCase
$this->login()->loginNami();
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(NamiField::class)->key('members'),
FormtemplateFieldRequest::type(TextField::class)->name('Andere')->key('other')->namiType(null)->required(true),
$this->namiField('members'),
$this->textField('other')->name('Andere')->namiType(null)->required(true),
])])
->create();
@ -364,8 +354,8 @@ class FormRegisterActionTest extends TestCase
$this->createMember(['mitgliedsnr' => '5505', 'firstname' => 'Paula', 'lastname' => 'Schirm']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(NamiField::class)->key('members'),
FormtemplateFieldRequest::type(CheckboxesField::class)->name('Andere')->key('other')->namiType(null)->options(['A', 'B']),
$this->namiField('members'),
$this->checkboxesField('other')->name('Andere')->namiType(null)->options(['A', 'B']),
])])
->create();
@ -379,9 +369,9 @@ class FormRegisterActionTest extends TestCase
$this->createMember(['mitgliedsnr' => '5505', 'firstname' => 'Paula']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(NamiField::class)->key('members'),
FormtemplateFieldRequest::type(TextField::class)->key('other')->required(true)->namiType(null)->forMembers(false)->options(['A', 'B']),
FormtemplateFieldRequest::type(TextField::class)->key('firstname')->required(true)->namiType(NamiType::FIRSTNAME),
$this->namiField('members'),
$this->textField('other')->required(true)->namiType(null)->forMembers(false)->options(['A', 'B']),
$this->textField('firstname')->required(true)->namiType(NamiType::FIRSTNAME),
])])
->create();

View File

@ -2,19 +2,16 @@
namespace Tests\Feature\Form;
use App\Form\Enums\NamiField;
use App\Form\Enums\NamiType;
use App\Form\Fields\TextField;
use App\Form\Models\Form;
use App\Lib\Events\Succeeded;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
use Generator;
use Illuminate\Support\Facades\Storage;
use Tests\RequestFactories\EditorRequestFactory;
class FormStoreActionTest extends TestCase
class FormStoreActionTest extends FormTestCase
{
use DatabaseTransactions;
@ -39,7 +36,7 @@ class FormStoreActionTest extends TestCase
->mailTop('Guten Tag')
->mailBottom('Viele Grüße')
->headerImage('htzz.jpg')
->sections([FormtemplateSectionRequest::new()->name('sname')->fields([FormtemplateFieldRequest::type(TextField::class)->namiType(NamiType::BIRTHDAY)])])
->sections([FormtemplateSectionRequest::new()->name('sname')->fields([$this->textField()->namiType(NamiType::BIRTHDAY)])])
->fake();
$this->postJson(route('form.store'))->assertOk();

View File

@ -0,0 +1,68 @@
<?php
namespace Tests\Feature\Form;
use Tests\TestCase;
use App\Form\Fields\CheckboxesField;
use App\Form\Fields\CheckboxField;
use App\Form\Fields\DateField;
use App\Form\Fields\DropdownField;
use App\Form\Fields\GroupField;
use App\Form\Fields\NamiField;
use App\Form\Fields\RadioField;
use App\Form\Fields\TextareaField;
use App\Form\Fields\TextField;
use Faker\Generator;
class FormTestCase extends TestCase
{
protected function namiField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(NamiField::class)->key($key ?? $this->randomKey());
}
protected function textField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(TextField::class)->key($key ?? $this->randomKey());
}
protected function checkboxesField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(CheckboxesField::class)->key($key ?? $this->randomKey());
}
protected function textareaField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(TextareaField::class)->key($key ?? $this->randomKey());
}
protected function dropdownField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(DropdownField::class)->key($key ?? $this->randomKey());
}
protected function dateField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(DateField::class)->key($key ?? $this->randomKey());
}
protected function radioField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(RadioField::class)->key($key ?? $this->randomKey());
}
protected function checkboxField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(CheckboxField::class)->key($key ?? $this->randomKey());
}
protected function groupField(?string $key = null): FormtemplateFieldRequest
{
return FormtemplateFieldRequest::type(GroupField::class)->key($key ?? $this->randomKey());
}
protected function randomKey(): string
{
return preg_replace('/[\-0-9]/', '', str()->uuid().str()->uuid());
}
}

View File

@ -2,13 +2,11 @@
namespace Tests\Feature\Form;
use App\Form\Fields\DateField;
use App\Form\Models\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use Illuminate\Support\Facades\Storage;
class FormUpdateActionTest extends TestCase
class FormUpdateActionTest extends FormTestCase
{
use DatabaseTransactions;
@ -26,7 +24,7 @@ class FormUpdateActionTest extends TestCase
$form = Form::factory()->create();
$payload = FormRequest::new()->sections([
FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(DateField::class)->state(['max_today' => true]),
$this->dateField()->state(['max_today' => true]),
])
])->create();

View File

@ -2,16 +2,13 @@
namespace Tests\Feature\Form;
use App\Form\Fields\TextareaField;
use App\Form\Fields\TextField;
use App\Form\Models\Formtemplate;
use App\Lib\Events\Succeeded;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
use Generator;
class FormtemplateStoreActionTest extends TestCase
class FormtemplateStoreActionTest extends FormTestCase
{
use DatabaseTransactions;
@ -22,8 +19,8 @@ class FormtemplateStoreActionTest extends TestCase
$this->login()->loginNami()->withoutExceptionHandling();
FormtemplateRequest::new()->name('testname')->sections([
FormtemplateSectionRequest::new()->name('Persönliches')->fields([
FormtemplateFieldRequest::type(TextField::class)->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false)->default('zuzu'),
FormtemplateFieldRequest::type(TextareaField::class)->name('lala2')->required(false)->rows(10),
$this->textField('a')->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false)->default('zuzu'),
$this->textareaField('b')->name('lala2')->required(false)->rows(10),
]),
])->fake();
@ -47,7 +44,7 @@ class FormtemplateStoreActionTest extends TestCase
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([
FormtemplateFieldRequest::type(TextField::class)->name(''),
$this->textField()->name(''),
])]), ['config.sections.0.fields.0.name' => 'Feldname ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type('')
@ -56,13 +53,13 @@ class FormtemplateStoreActionTest extends TestCase
FormtemplateFieldRequest::type('aaaaa'),
])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(TextField::class)->key(''),
$this->textField(''),
])]), ['config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(TextField::class)->key('a b'),
$this->textField('a b'),
])]), ['config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(TextField::class)->required('la')
$this->textField()->required('la')
])]), ['config.sections.0.fields.0.required' => 'Erforderlich muss ein Wahrheitswert sein.']];
}

View File

@ -2,18 +2,13 @@
namespace Tests\Feature\Form;
use App\Form\Fields\CheckboxesField;
use App\Form\Fields\DateField;
use App\Form\Fields\DropdownField;
use App\Form\Fields\GroupField;
use App\Form\Fields\TextField;
use App\Form\Models\Form;
use App\Form\Models\Participant;
use App\Group;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class ParticipantIndexActionTest extends TestCase
class ParticipantIndexActionTest extends FormTestCase
{
use DatabaseTransactions;
@ -26,14 +21,14 @@ class ParticipantIndexActionTest extends TestCase
->has(Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B'], 'stufe' => 'Pfadfinder', 'test1' => '', 'test2' => '', 'test3' => '', 'birthday' => '1991-04-20', 'bezirk' => $group->id]))
->sections([
FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(TextField::class)->name('Vorname')->key('vorname'),
FormtemplateFieldRequest::type(CheckboxesField::class)->key('select')->options(['A', 'B', 'C']),
FormtemplateFieldRequest::type(DropdownField::class)->key('stufe')->options(['Wölfling', 'Jungpfadfinder', 'Pfadfinder']),
FormtemplateFieldRequest::type(TextField::class)->name('Test 1')->key('test1'),
FormtemplateFieldRequest::type(TextField::class)->name('Test 2')->key('test2'),
FormtemplateFieldRequest::type(TextField::class)->name('Test 3')->key('test3'),
FormtemplateFieldRequest::type(DateField::class)->name('Geburtsdatum')->key('birthday'),
FormtemplateFieldRequest::type(GroupField::class)->name('bezirk')->key('bezirk'),
$this->textField('vorname')->name('Vorname'),
$this->checkboxesField('select')->options(['A', 'B', 'C']),
$this->dropdownField('stufe')->options(['Wölfling', 'Jungpfadfinder', 'Pfadfinder']),
$this->textField('test1')->name('Test 1'),
$this->textField('test2')->name('Test 2'),
$this->textField('test3')->name('Test 3'),
$this->dateField('birthday')->name('Geburtsdatum'),
$this->groupField('bezirk')->name('bezirk'),
]),
])
->create();