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\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<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
{
$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<string, mixed> $conditions
* @param array<string, mixed> $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<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']));
}
}
}
})->with('blocks');

View File

@ -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<string, string> $messages
*/
#[DataProvider('validationDataProvider')]
public function testItValidatesRequests(FormRequest $request, array $messages): void
{
$this->login()->loginNami();
$request->fake();
/**
* @param array<string, string> $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']],
]);

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 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());
});

View File

@ -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']);
});

View File

@ -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<string, string> $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<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);
}
}
$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.']],
]);

View File

@ -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);
});

View File

@ -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);
});

View File

@ -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);
});

View File

@ -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']);
});

View File

@ -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.']);
});

View File

@ -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.']);
});

View File

@ -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<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
{
return FormtemplateFieldRequest::type(NamiField::class)->key($key ?? static::randomKey());