Move FormTestCase in Trait
continuous-integration/drone/push Build is passing Details

This commit is contained in:
philipp lang 2025-06-15 23:29:26 +02:00
parent c3ecb9dc63
commit cff62ebc1d
14 changed files with 1425 additions and 1507 deletions

File diff suppressed because it is too large Load Diff

View File

@ -6,269 +6,262 @@ use App\Form\Enums\SpecialType;
use App\Form\Mails\ConfirmRegistrationMail; use App\Form\Mails\ConfirmRegistrationMail;
use App\Form\Models\Form; use App\Form\Models\Form;
use App\Form\Models\Participant; use App\Form\Models\Participant;
use Generator;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\RequestFactories\EditorRequestFactory; 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 dataset('blocks', fn() => [
{ fn () => [
$this->login()->loginNami()->withoutExceptionHandling(); ['mode' => 'all', 'ifs' => []],
test()->dropdownField('fieldkey')->options(['A', 'B']),
['fieldkey' => 'A'],
true,
],
$participant = Participant::factory()->for( fn () => [
Form::factory()->sections([ ['mode' => 'any', 'ifs' => []],
FormtemplateSectionRequest::new()->name('Persönliches')->fields([ test()->dropdownField('fieldkey')->options(['A', 'B']),
$this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME), ['fieldkey' => 'A'],
$this->textField('nachname')->specialType(SpecialType::LASTNAME), true,
]) ],
])
->mailTop(EditorRequestFactory::new()->text(10, 'mail top'))->mailBottom(EditorRequestFactory::new()->text(11, 'mail bottom')) fn () => [
) ['mode' => 'any', 'ifs' => []],
->data(['vorname' => 'Max', 'nachname' => 'Muster']) test()->dropdownField('fieldkey')->options(['A', 'B']),
->create(); ['fieldkey' => 'A'],
true,
],
$mail = new ConfirmRegistrationMail($participant); fn () => [
$mail->assertSeeInText('mail top'); ['mode' => 'any', 'ifs' => [
$mail->assertSeeInText('mail bottom'); ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'A']
} ]],
test()->dropdownField('fieldkey')->options(['A', 'B']),
['fieldkey' => 'A'],
true,
],
public function testItShowsParticipantGreeting(): void fn () => [
{ ['mode' => 'any', 'ifs' => [
$this->login()->loginNami()->withoutExceptionHandling(); ['field' => 'fieldkey', 'comparator' => 'isEqual', 'value' => 'B']
$participant = Participant::factory()->for(Form::factory()->sections([ ]],
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([ FormtemplateSectionRequest::new()->name('Persönliches')->fields([
$this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME), $this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME),
$this->textField('nachname')->specialType(SpecialType::LASTNAME), $this->textField('nachname')->specialType(SpecialType::LASTNAME),
$this->checkboxField('fullyear')->name('Volljährig'),
]) ])
])) ])
->data(['vorname' => 'Max', 'nachname' => 'Muster', 'fullyear' => true])
->create();
$mail = new ConfirmRegistrationMail($participant); ->mailTop(EditorRequestFactory::new()->text(10, 'mail top'))->mailBottom(EditorRequestFactory::new()->text(11, 'mail bottom'))
$mail->assertSeeInText('# Hallo Max Muster'); )
$mail->assertSeeInText('## Persönliches'); ->data(['vorname' => 'Max', 'nachname' => 'Muster'])
$mail->assertSeeInText('* Vorname: Max'); ->create();
$mail->assertSeeInText('* Volljährig: Ja');
$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<string, mixed> $conditions
* @param array<string, mixed> $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 /**
{ * @param array<string, mixed> $conditions
$this->login()->loginNami()->withoutExceptionHandling(); * @param array<string, mixed> $participantValues
$participant = Participant::factory()->for( */
Form::factory() it('testItFiltersForAttachments', function (array $conditions, FormtemplateFieldRequest $field, array $participantValues, bool $result) {
->fields([ $this->login()->loginNami()->withoutExceptionHandling();
$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); $participant = Participant::factory()->for(
$mail->assertHasAttachedData('content1', 'beispiel.pdf', ['mime' => 'application/pdf']); Form::factory()
$mail->assertHasAttachedData('content2', 'beispiel2.pdf', ['mime' => 'application/pdf']); ->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');
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<string, mixed> $conditions
* @param array<string, mixed> $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<string, mixed> $conditions
* @param array<string, mixed> $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']));
}
}
}

View File

@ -9,111 +9,103 @@ use App\Form\Models\Form;
use App\Lib\Events\Succeeded; use App\Lib\Events\Succeeded;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\RequestFactories\EditorRequestFactory; 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 it('testItStoresForm', function () {
{ Event::fake([Succeeded::class]);
Event::fake([Succeeded::class]); $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $description = EditorRequestFactory::new()->text(10, 'Lorem');
$description = EditorRequestFactory::new()->text(10, 'Lorem'); FormRequest::new()
FormRequest::new() ->name('formname')
->name('formname') ->description($description)
->description($description) ->excerpt('avff')
->excerpt('avff') ->preventionText(EditorRequestFactory::new()->paragraphs(['lorem ipsum']))
->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')
->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'))
->mailTop(EditorRequestFactory::new()->text(11, 'lala')) ->mailBottom(EditorRequestFactory::new()->text(12, 'lalab'))
->mailBottom(EditorRequestFactory::new()->text(12, 'lalab')) ->headerImage('htzz.jpg')
->headerImage('htzz.jpg') ->sections([FormtemplateSectionRequest::new()->name('sname')->fields([$this->textField()->namiType(NamiType::BIRTHDAY)->forMembers(false)->hint('hhh')])])
->sections([FormtemplateSectionRequest::new()->name('sname')->fields([$this->textField()->namiType(NamiType::BIRTHDAY)->forMembers(false)->hint('hhh')])]) ->fake();
->fake();
$this->postJson(route('form.store'))->assertOk(); $this->postJson(route('form.store'))->assertOk();
$form = Form::latest()->first(); $form = Form::latest()->first();
$this->assertEquals('sname', $form->config->sections->get(0)->name); $this->assertEquals('sname', $form->config->sections->get(0)->name);
$this->assertEquals('formname', $form->name); $this->assertEquals('formname', $form->name);
$this->assertEquals('lorem ipsum', $form->prevention_text->blocks[0]['data']['text']); $this->assertEquals('lorem ipsum', $form->prevention_text->blocks[0]['data']['text']);
$this->assertEquals('avff', $form->excerpt); $this->assertEquals('avff', $form->excerpt);
$this->assertEquals('Lorem', $form->description->blocks[0]['data']['text']); $this->assertEquals('Lorem', $form->description->blocks[0]['data']['text']);
$this->assertEquals('lala', $form->mail_top->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('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('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i'));
$this->assertEquals(true, $form->is_active); $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 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-07', $form->from->format('Y-m-d'));
$this->assertEquals('2023-07-08', $form->to->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('Geburtstag', $form->config->sections->get(0)->fields->get(0)->namiType->value);
$this->assertEquals('hhh', $form->config->sections->get(0)->fields->get(0)->hint); $this->assertEquals('hhh', $form->config->sections->get(0)->fields->get(0)->hint);
$this->assertFalse($form->config->sections->get(0)->fields->get(0)->forMembers); $this->assertFalse($form->config->sections->get(0)->fields->get(0)->forMembers);
$this->assertCount(1, $form->getMedia('headerImage')); $this->assertCount(1, $form->getMedia('headerImage'));
$this->assertEquals('formname.jpg', $form->getMedia('headerImage')->first()->file_name); $this->assertEquals('formname.jpg', $form->getMedia('headerImage')->first()->file_name);
Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Veranstaltung gespeichert.'); Event::assertDispatched(Succeeded::class, fn(Succeeded $event) => $event->message === 'Veranstaltung gespeichert.');
$this->assertFrontendCacheCleared(); $this->assertFrontendCacheCleared();
} });
public function testItStoresDefaultSorting(): void it('testItStoresDefaultSorting', function () {
{ Event::fake([Succeeded::class]);
Event::fake([Succeeded::class]); $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); FormRequest::new()->fields([$this->textField()])->fake();
FormRequest::new()->fields([$this->textField()])->fake();
$this->postJson(route('form.store'))->assertOk(); $this->postJson(route('form.store'))->assertOk();
$form = Form::latest()->first(); $form = Form::latest()->first();
$this->assertEquals('id', $form->meta['sorting']['by']); $this->assertEquals('id', $form->meta['sorting']['by']);
$this->assertFalse(false, $form->meta['sorting']['direction']); $this->assertFalse(false, $form->meta['sorting']['direction']);
} });
public function testRegistrationDatesCanBeNull(): void it('testRegistrationDatesCanBeNull', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$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', [ $this->assertDatabaseHas('forms', [
'registration_until' => null, 'registration_until' => null,
'registration_from' => null, 'registration_from' => null,
]); ]);
} });
public function testItStoresExport(): void it('testItStoresExport', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$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(); $form = Form::first();
$this->assertEquals(2, $form->export->root->connectionId); $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']];
}
/** /**
* @param array<string, string> $messages * @param array<string, string> $messages
*/ */
#[DataProvider('validationDataProvider')] it('testItValidatesRequests', function (FormRequest $request, array $messages) {
public function testItValidatesRequests(FormRequest $request, array $messages): void $this->login()->loginNami();
{ $request->fake();
$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']],
]);

View File

@ -1,36 +0,0 @@
<?php
namespace Tests\Feature\Form;
use App\Form\FormSettings;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
use Tests\Lib\CreatesFormFields;
class FormTestCase extends TestCase
{
use CreatesFormFields;
private string $clearCacheUrl = 'http://event.com/clear-cache';
protected function setUp(): void
{
parent::setUp();
app(FormSettings::class)->fill(['clearCacheUrl' => 'http://event.com/clear-cache'])->save();
Http::fake(function ($request) {
if ($request->url() === $this->clearCacheUrl) {
return Http::response('', 200);
}
});
Storage::fake('temp');
}
protected function assertFrontendCacheCleared(): void
{
Http::assertSent(fn ($request) => $request->url() === $this->clearCacheUrl && $request->method() === 'GET');
}
}

View File

@ -7,161 +7,152 @@ use App\Form\Data\ExportData;
use App\Form\Models\Form; use App\Form\Models\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\RequestFactories\EditorRequestFactory; 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 it('testItSetsCustomAttributesOfFields', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$form = Form::factory()->create(); $payload = FormRequest::new()->fields([
$payload = FormRequest::new()->fields([ $this->dateField()->state(['max_today' => true]),
$this->dateField()->state(['max_today' => true]), ])->create();
])->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload) $this->patchJson(route('form.update', ['form' => $form]), $payload)
->assertOk(); ->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 it('testItSetsRegistrationDates', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$form = Form::factory()->create(); $payload = FormRequest::new()->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->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-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-07-07 01:00', $form->registration_until->format('Y-m-d H:i'));
} });
public function testItSetsTexts(): void it('testItSetsTexts', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$form = Form::factory()->create(); $payload = FormRequest::new()->fields([])
$payload = FormRequest::new()->fields([]) ->mailTop(EditorRequestFactory::new()->text(11, 'lala'))
->mailTop(EditorRequestFactory::new()->text(11, 'lala')) ->mailBottom(EditorRequestFactory::new()->text(12, 'lalab'))
->mailBottom(EditorRequestFactory::new()->text(12, 'lalab')) ->description(EditorRequestFactory::new()->text(12, 'desc'))
->description(EditorRequestFactory::new()->text(12, 'desc')) ->create();
->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload) $this->patchJson(route('form.update', ['form' => $form]), $payload)
->assertOk(); ->assertOk();
$this->assertEquals('lala', $form->fresh()->mail_top->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('lalab', $form->fresh()->mail_bottom->blocks[0]['data']['text']);
$this->assertEquals('desc', $form->fresh()->description->blocks[0]['data']['text']); $this->assertEquals('desc', $form->fresh()->description->blocks[0]['data']['text']);
} });
public function testItClearsFrontendCacheWhenFormUpdated(): void it('testItClearsFrontendCacheWhenFormUpdated', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$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 it('testItUpdatesExport', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling();
$form = Form::factory()->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->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 it('testItUpdatesActiveColumnsWhenFieldRemoved', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->fields([
$form = Form::factory()->fields([ $this->textField('firstname'),
$this->textField('firstname'), $this->textField('geb'),
$this->textField('geb'), $this->textField('lastname'),
$this->textField('lastname'), ])
]) ->create();
->create(); $payload = FormRequest::new()->fields([
$payload = FormRequest::new()->fields([ $this->textField('firstname'),
$this->textField('firstname'), ])->create();
])->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk();
$this->assertEquals(['firstname'], $form->fresh()->meta['active_columns']); $this->assertEquals(['firstname'], $form->fresh()->meta['active_columns']);
} });
public function testItUpdatesIntroOfSections(): void it('testItUpdatesIntroOfSections', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()
$form = Form::factory() ->sections([FormtemplateSectionRequest::new()->intro('aaa')])
->sections([FormtemplateSectionRequest::new()->intro('aaa')]) ->create();
->create(); $payload = FormRequest::new()->sections([
$payload = FormRequest::new()->sections([ FormtemplateSectionRequest::new()->intro('aaa')
FormtemplateSectionRequest::new()->intro('aaa') ])->create();
])->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk();
$this->assertEquals('aaa', $form->fresh()->config->sections[0]->intro); $this->assertEquals('aaa', $form->fresh()->config->sections[0]->intro);
} });
public function testItUpdatesActiveState(): void it('testItUpdatesActiveState', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$form = Form::factory()->create();
$this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(false)->create())->assertSessionDoesntHaveErrors()->assertOk(); $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(false)->create())->assertSessionDoesntHaveErrors()->assertOk();
$this->assertFalse($form->fresh()->is_active); $this->assertFalse($form->fresh()->is_active);
$this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(true)->create())->assertSessionDoesntHaveErrors()->assertOk(); $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(true)->create())->assertSessionDoesntHaveErrors()->assertOk();
$this->assertTrue($form->fresh()->is_active); $this->assertTrue($form->fresh()->is_active);
} });
public function testItUpdatesPrivateState(): void it('testItUpdatesPrivateState', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$form = Form::factory()->create();
$this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(false)->create())->assertSessionDoesntHaveErrors()->assertOk(); $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(false)->create())->assertSessionDoesntHaveErrors()->assertOk();
$this->assertFalse($form->fresh()->is_private); $this->assertFalse($form->fresh()->is_private);
$this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(true)->create())->assertSessionDoesntHaveErrors()->assertOk(); $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isPrivate(true)->create())->assertSessionDoesntHaveErrors()->assertOk();
$this->assertTrue($form->fresh()->is_private); $this->assertTrue($form->fresh()->is_private);
} });
public function testItUpdatesActiveColumnsWhenFieldsAdded(): void it('testItUpdatesActiveColumnsWhenFieldsAdded', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()
$form = Form::factory() ->fields([])
->fields([]) ->create();
->create(); $payload = FormRequest::new()->fields([
$payload = FormRequest::new()->fields([ $this->textField('firstname'),
$this->textField('firstname'), $this->textField('geb'),
$this->textField('geb'), $this->textField('lastname'),
$this->textField('lastname'), ])->create();
])->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk(); $this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk();
$this->assertEquals(['firstname', 'geb', 'lastname'], $form->fresh()->meta['active_columns']); $this->assertEquals(['firstname', 'geb', 'lastname'], $form->fresh()->meta['active_columns']);
} });
public function testItUpdatesPrevention(): void it('testItUpdatesPrevention', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$form = Form::factory()->create(); $payload = FormRequest::new()
$payload = FormRequest::new() ->preventionText(EditorRequestFactory::new()->text(10, 'lorem ipsum'))
->preventionText(EditorRequestFactory::new()->text(10, 'lorem ipsum')) ->state(['needs_prevention' => true, 'prevention_conditions' => ['mode' => 'all', 'ifs' => [['field' => 'vorname', 'value' => 'Max', 'comparator' => 'isEqual']]]])
->state(['needs_prevention' => true, 'prevention_conditions' => ['mode' => 'all', 'ifs' => [['field' => 'vorname', 'value' => 'Max', 'comparator' => 'isEqual']]]]) ->create();
->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload); $this->patchJson(route('form.update', ['form' => $form]), $payload);
$this->assertTrue($form->fresh()->needs_prevention); $this->assertTrue($form->fresh()->needs_prevention);
$this->assertEquals('lorem ipsum', $form->fresh()->prevention_text->blocks[0]['data']['text']); $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->assertEquals(['mode' => 'all', 'ifs' => [['field' => 'vorname', 'value' => 'Max', 'comparator' => 'isEqual']]], $form->fresh()->prevention_conditions->toArray());
} });
}

View File

@ -4,45 +4,45 @@ namespace Tests\Feature\Form;
use App\Form\Models\Form; use App\Form\Models\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions; 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 it('testItUpdatesMetaOfForm', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()
$form = Form::factory() ->sections([FormtemplateSectionRequest::new()->fields([
->sections([FormtemplateSectionRequest::new()->fields([ $this->textField('textone'),
$this->textField('textone'), $this->dropdownField('texttwo'),
$this->dropdownField('texttwo'), ])])->create();
])])->create();
$this->patchJson(route('form.update-meta', ['form' => $form]), [ $this->patchJson(route('form.update-meta', ['form' => $form]), [
'active_columns' => ['textone'], 'active_columns' => ['textone'],
'sorting' => ['by' => 'textone', 'direction' => false], 'sorting' => ['by' => 'textone', 'direction' => false],
])->assertOk() ])->assertOk()
->assertJsonPath('active_columns.0', 'textone') ->assertJsonPath('active_columns.0', 'textone')
->assertJsonPath('sorting.by', 'textone'); ->assertJsonPath('sorting.by', 'textone');
$form = Form::latest()->first(); $form = Form::latest()->first();
$this->assertEquals(['by' => 'textone', 'direction' => false], $form->meta['sorting']); $this->assertEquals(['by' => 'textone', 'direction' => false], $form->meta['sorting']);
$this->assertEquals(['textone'], $form->meta['active_columns']); $this->assertEquals(['textone'], $form->meta['active_columns']);
} });
public function testItCanSetCreatedAtMeta(): void it('testItCanSetCreatedAtMeta', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->create();
$form = Form::factory()->create();
$this->patchJson(route('form.update-meta', ['form' => $form]), [ $this->patchJson(route('form.update-meta', ['form' => $form]), [
'active_columns' => ['created_at'], 'active_columns' => ['created_at'],
'sorting' => ['by' => 'textone', 'direction' => false], 'sorting' => ['by' => 'textone', 'direction' => false],
])->assertOk(); ])->assertOk();
$form = Form::latest()->first(); $form = Form::latest()->first();
$this->assertEquals(['by' => 'textone', 'direction' => false], $form->fresh()->meta['sorting']); $this->assertEquals(['by' => 'textone', 'direction' => false], $form->fresh()->meta['sorting']);
$this->assertEquals(['created_at'], $form->fresh()->meta['active_columns']); $this->assertEquals(['created_at'], $form->fresh()->meta['active_columns']);
} });
}

View File

@ -9,83 +9,73 @@ use App\Form\Models\Formtemplate;
use App\Lib\Events\Succeeded; use App\Lib\Events\Succeeded;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Generator; use Tests\Lib\CreatesFormFields;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\RequestFactories\EditorRequestFactory; 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 $this->postJson(route('formtemplate.store'), $payload)->assertOk();
{
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(); $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); * @param array<string, string> $messages
$this->assertEquals('lala1', $formtemplate->config->sections->get(0)->fields->get(0)->name); */
$this->assertNull($formtemplate->config->sections->get(0)->fields->get(0)->specialType); it('testItValidatesRequests', function (FormtemplateRequest $request, array $messages) {
$this->assertEquals('hhh', $formtemplate->config->sections->get(0)->fields->get(0)->hint); $this->login()->loginNami();
$this->assertEquals('intro', $formtemplate->config->sections->get(0)->fields->get(0)->intro); $request->fake();
$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.');
}
public static function validationDataProvider(): Generator $this->postJson(route('formtemplate.store'))
{ ->assertJsonValidationErrors($messages);
yield [FormtemplateRequest::new()->name(''), ['name' => 'Name ist erforderlich.']]; })->with([
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->name('')]), ['config.sections.0.name' => 'Sektionsname ist erforderlich.']]; fn () => [FormtemplateRequest::new()->name(''), ['name' => 'Name ist erforderlich.']],
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->name('')]), ['config.sections.0.name' => 'Sektionsname ist erforderlich.']],
static::textField()->name(''), fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
])]), ['config.sections.0.fields.0.name' => 'Feldname ist erforderlich.']]; test()->textField()->name(''),
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ ])]), ['config.sections.0.fields.0.name' => 'Feldname ist erforderlich.']],
FormtemplateFieldRequest::type('') fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist erforderlich.']]; FormtemplateFieldRequest::type('')
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ ])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist erforderlich.']],
FormtemplateFieldRequest::type('aaaaa'), fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.']]; FormtemplateFieldRequest::type('aaaaa'),
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ ])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.']],
static::textField(''), fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
])]), ['config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.']]; test()->textField(''),
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ ])]), ['config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.']],
static::textField('a b'), fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
])]), ['config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.']]; test()->textField('a b'),
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([ ])]), ['config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.']],
static::textField()->required('la') fn () => [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
])]), ['config.sections.0.fields.0.required' => 'Erforderlich muss ein Wahrheitswert sein.']]; test()->textField()->required('la')
} ])]), ['config.sections.0.fields.0.required' => 'Erforderlich muss ein Wahrheitswert sein.']],
]);
/**
* @param array<string, string> $messages
*/
#[DataProvider('validationDataProvider')]
public function testItValidatesRequests(FormtemplateRequest $request, array $messages): void
{
$this->login()->loginNami();
$request->fake();
$this->postJson(route('formtemplate.store'))
->assertJsonValidationErrors($messages);
}
}

View File

@ -4,22 +4,24 @@ namespace Tests\Feature\Form;
use App\Form\Models\Form; use App\Form\Models\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Lib\CreatesFormFields;
class IsDirtyActionTest extends FormTestCase uses(DatabaseTransactions::class);
{ uses(CreatesFormFields::class);
use DatabaseTransactions;
public function testItChecksIfFormIsDirty(): void beforeEach(function () {
{ test()->setUpForm();
$this->login()->loginNami(); });
$form = Form::factory()->fields([
$this->textField(),
])->create();
$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(); $this->postJson(route('form.is-dirty', ['form' => $form]), ['config' => $form->config->toArray()])->assertJsonPath('result', false);
data_set($modifiedConfig, 'sections.0.name', 'mod');
$this->postJson(route('form.is-dirty', ['form' => $form]), ['config' => $modifiedConfig])->assertJsonPath('result', true); $modifiedConfig = $form->config->toArray();
} data_set($modifiedConfig, 'sections.0.name', 'mod');
} $this->postJson(route('form.is-dirty', ['form' => $form]), ['config' => $modifiedConfig])->assertJsonPath('result', true);
});

View File

@ -5,23 +5,24 @@ namespace Tests\Feature\Form;
use App\Form\Models\Form; use App\Form\Models\Form;
use App\Form\Models\Participant; use App\Form\Models\Participant;
use Illuminate\Foundation\Testing\DatabaseTransactions; 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 it('testItCanDestroyAParticipant', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()
$form = Form::factory() ->has(Participant::factory())
->has(Participant::factory()) ->sections([])
->sections([]) ->create();
->create();
$this->deleteJson(route('participant.destroy', ['participant' => $form->participants->first()])) $this->deleteJson(route('participant.destroy', ['participant' => $form->participants->first()]))
->assertOk(); ->assertOk();
$this->assertDatabaseCount('participants', 0); $this->assertDatabaseCount('participants', 0);
} });
}

View File

@ -6,34 +6,35 @@ use App\Form\Models\Form;
use App\Form\Models\Participant; use App\Form\Models\Participant;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Storage; 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 it('testItShowsParticipantsAndColumns', function () {
{ Storage::fake('temp');
Storage::fake('temp'); $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()
$form = Form::factory() ->has(Participant::factory()->data(['stufe' => 'Pfadfinder', 'vorname' => 'Max', 'select' => ['A', 'B']]))
->has(Participant::factory()->data(['stufe' => 'Pfadfinder', 'vorname' => 'Max', 'select' => ['A', 'B']])) ->sections([
->sections([ FormtemplateSectionRequest::new()->fields([
FormtemplateSectionRequest::new()->fields([ $this->textField('vorname')->name('Vorname'),
$this->textField('vorname')->name('Vorname'), $this->checkboxesField('select')->name('Abcselect')->options(['A', 'B', 'C']),
$this->checkboxesField('select')->name('Abcselect')->options(['A', 'B', 'C']), $this->dropdownField('stufe')->name('Stufe')->options(['Wölfling', 'Jungpfadfinder', 'Pfadfinder']),
$this->dropdownField('stufe')->name('Stufe')->options(['Wölfling', 'Jungpfadfinder', 'Pfadfinder']), ]),
]), ])
]) ->name('ZEM 2024')
->name('ZEM 2024') ->create();
->create();
$this->get(route('form.export', ['form' => $form]))->assertDownload('tn-zem-2024.xlsx'); $this->get(route('form.export', ['form' => $form]))->assertDownload('tn-zem-2024.xlsx');
$contents = Storage::disk('temp')->get('tn-zem-2024.xlsx'); $contents = Storage::disk('temp')->get('tn-zem-2024.xlsx');
$this->assertExcelContent('Max', $contents); $this->assertExcelContent('Max', $contents);
$this->assertExcelContent('A, B', $contents); $this->assertExcelContent('A, B', $contents);
$this->assertExcelContent('Pfadfinder', $contents); $this->assertExcelContent('Pfadfinder', $contents);
$this->assertExcelContent('Stufe', $contents); $this->assertExcelContent('Stufe', $contents);
$this->assertExcelContent('Abcselect', $contents); $this->assertExcelContent('Abcselect', $contents);
} });
}

View File

@ -5,32 +5,33 @@ namespace Tests\Feature\Form;
use App\Form\Models\Form; use App\Form\Models\Form;
use App\Form\Models\Participant; use App\Form\Models\Participant;
use Illuminate\Foundation\Testing\DatabaseTransactions; 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 it('testItShowsParticipantsFields', function () {
{ $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']])
$participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']]) ->for(Form::factory()->sections([
->for(Form::factory()->sections([ FormtemplateSectionRequest::new()->name('Sektion')->fields([
FormtemplateSectionRequest::new()->name('Sektion')->fields([ $this->textField('vorname')->name('Vorname'),
$this->textField('vorname')->name('Vorname'), $this->checkboxesField('select')->options(['A', 'B', 'C']),
$this->checkboxesField('select')->options(['A', 'B', 'C']), ])
])
])) ]))
->create(); ->create();
$this->callFilter('participant.fields', [], ['participant' => $participant->id]) $this->callFilter('participant.fields', [], ['participant' => $participant->id])
->assertOk() ->assertOk()
->assertJsonPath('data.id', $participant->id) ->assertJsonPath('data.id', $participant->id)
->assertJsonPath('data.config.sections.0.name', 'Sektion') ->assertJsonPath('data.config.sections.0.name', 'Sektion')
->assertJsonPath('data.config.sections.0.fields.0.key', 'vorname') ->assertJsonPath('data.config.sections.0.fields.0.key', 'vorname')
->assertJsonPath('data.config.sections.0.fields.0.value', 'Max') ->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.key', 'select')
->assertJsonPath('data.config.sections.0.fields.1.value', ['A', 'B']); ->assertJsonPath('data.config.sections.0.fields.1.value', ['A', 'B']);
} });
}

View File

@ -6,38 +6,38 @@ use App\Form\Actions\ExportSyncAction;
use App\Form\Models\Form; use App\Form\Models\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Queue; 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 it('testItStoresParticipant', function () {
{ Queue::fake();
Queue::fake(); $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $form = Form::factory()->fields([
$form = Form::factory()->fields([ $this->textField('vorname')->name('Vorname')->required(true),
$this->textField('vorname')->name('Vorname')->required(true), ])
]) ->create();
->create();
$this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => 'Jane']) $this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => 'Jane'])
->assertOk(); ->assertOk();
$this->assertEquals('Jane', $form->participants->first()->data['vorname']); $this->assertEquals('Jane', $form->participants->first()->data['vorname']);
ExportSyncAction::assertPushed(); ExportSyncAction::assertPushed();
} });
public function testItHasValidation(): void it('testItHasValidation', function () {
{ Queue::fake();
Queue::fake(); $this->login()->loginNami();
$this->login()->loginNami(); $form = Form::factory()->fields([
$form = Form::factory()->fields([ $this->textField('vorname')->name('Vorname')->required(true),
$this->textField('vorname')->name('Vorname')->required(true), ])
]) ->create();
->create();
$this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => '']) $this->postJson(route('form.participant.store', ['form' => $form->id]), ['vorname' => ''])
->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']); ->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']);
} });
}

View File

@ -7,39 +7,39 @@ use App\Form\Models\Form;
use App\Form\Models\Participant; use App\Form\Models\Participant;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Queue; 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 it('testItUpdatesParticipant', function () {
{ Queue::fake();
Queue::fake(); $this->login()->loginNami()->withoutExceptionHandling();
$this->login()->loginNami()->withoutExceptionHandling(); $participant = Participant::factory()->data(['vorname' => 'Max'])
$participant = Participant::factory()->data(['vorname' => 'Max']) ->for(Form::factory()->fields([
->for(Form::factory()->fields([ $this->textField('vorname')->name('Vorname'),
$this->textField('vorname')->name('Vorname'), ]))
])) ->create();
->create();
$this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => 'Jane']) $this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => 'Jane'])
->assertOk(); ->assertOk();
$this->assertEquals('Jane', $participant->fresh()->data['vorname']); $this->assertEquals('Jane', $participant->fresh()->data['vorname']);
ExportSyncAction::assertPushed(); ExportSyncAction::assertPushed();
} });
public function testItHasValidation(): void it('testItHasValidation', function () {
{ $this->login()->loginNami();
$this->login()->loginNami(); $participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']])
$participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']]) ->for(Form::factory()->fields([
->for(Form::factory()->fields([ $this->textField('vorname')->name('Vorname')->required(true),
$this->textField('vorname')->name('Vorname')->required(true), ]))
])) ->create();
->create();
$this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => '']) $this->patchJson(route('participant.update', ['participant' => $participant->id]), ['vorname' => ''])
->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']); ->assertJsonValidationErrors(['vorname' => 'Vorname ist erforderlich.']);
} });
}

View File

@ -13,11 +13,54 @@ use App\Form\Fields\NumberField;
use App\Form\Fields\RadioField; use App\Form\Fields\RadioField;
use App\Form\Fields\TextareaField; use App\Form\Fields\TextareaField;
use App\Form\Fields\TextField; 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; use Tests\Feature\Form\FormtemplateFieldRequest;
trait CreatesFormFields trait CreatesFormFields
{ {
/**
* @param array<string, mixed> $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<string, mixed> $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 protected static function namiField(?string $key = null): FormtemplateFieldRequest
{ {
return FormtemplateFieldRequest::type(NamiField::class)->key($key ?? static::randomKey()); return FormtemplateFieldRequest::type(NamiField::class)->key($key ?? static::randomKey());