adrema/tests/Feature/Form/FormStoreActionTest.php

120 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2023-12-27 22:54:58 +01:00
<?php
namespace Tests\Feature\Form;
2024-06-29 18:02:23 +02:00
use App\Fileshare\Data\FileshareResourceData;
use App\Form\Data\ExportData;
2024-02-16 14:18:16 +01:00
use App\Form\Enums\NamiType;
2023-12-27 22:54:58 +01:00
use App\Form\Models\Form;
use App\Lib\Events\Succeeded;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
use Generator;
2024-09-21 22:46:38 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
2024-02-02 01:05:45 +01:00
use Tests\RequestFactories\EditorRequestFactory;
2023-12-27 22:54:58 +01:00
2024-02-19 01:07:54 +01:00
class FormStoreActionTest extends FormTestCase
2023-12-27 22:54:58 +01:00
{
use DatabaseTransactions;
public function testItStoresForm(): void
{
Event::fake([Succeeded::class]);
$this->login()->loginNami()->withoutExceptionHandling();
2024-02-02 01:05:45 +01:00
$description = EditorRequestFactory::new()->text(10, 'Lorem');
2023-12-27 22:54:58 +01:00
FormRequest::new()
->name('formname')
2024-02-09 00:21:33 +01:00
->description($description)
2023-12-27 22:54:58 +01:00
->excerpt('avff')
2024-07-06 15:08:13 +02:00
->preventionText(EditorRequestFactory::new()->paragraphs(['lorem ipsum']))
2023-12-31 22:35:13 +01:00
->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->from('2023-07-07')->to('2023-07-08')
2024-04-19 17:42:59 +02:00
->mailTop(EditorRequestFactory::new()->text(11, 'lala'))
->mailBottom(EditorRequestFactory::new()->text(12, 'lalab'))
2024-01-12 23:29:18 +01:00
->headerImage('htzz.jpg')
2024-04-10 00:00:20 +02:00
->sections([FormtemplateSectionRequest::new()->name('sname')->fields([$this->textField()->namiType(NamiType::BIRTHDAY)->forMembers(false)->hint('hhh')])])
2023-12-27 22:54:58 +01:00
->fake();
$this->postJson(route('form.store'))->assertOk();
$form = Form::latest()->first();
2024-03-07 00:58:14 +01:00
$this->assertEquals('sname', $form->config->sections->get(0)->name);
2023-12-27 22:54:58 +01:00
$this->assertEquals('formname', $form->name);
2024-07-06 15:08:13 +02:00
$this->assertEquals('lorem ipsum', $form->prevention_text->blocks[0]['data']['text']);
2023-12-27 22:54:58 +01:00
$this->assertEquals('avff', $form->excerpt);
2024-07-14 19:29:42 +02:00
$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']);
2023-12-31 21:46:52 +01:00
$this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i'));
2024-05-27 18:30:05 +02:00
$this->assertEquals(true, $form->is_active);
2023-12-31 21:46:52 +01:00
$this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i'));
2023-12-31 22:35:13 +01:00
$this->assertEquals('2023-07-07', $form->from->format('Y-m-d'));
$this->assertEquals('2023-07-08', $form->to->format('Y-m-d'));
2024-03-07 00:58:14 +01:00
$this->assertEquals('Geburtstag', $form->config->sections->get(0)->fields->get(0)->namiType->value);
2024-04-10 00:00:20 +02:00
$this->assertEquals('hhh', $form->config->sections->get(0)->fields->get(0)->hint);
2024-03-07 00:58:14 +01:00
$this->assertFalse($form->config->sections->get(0)->fields->get(0)->forMembers);
2024-01-12 23:29:18 +01:00
$this->assertCount(1, $form->getMedia('headerImage'));
$this->assertEquals('formname.jpg', $form->getMedia('headerImage')->first()->file_name);
2024-01-01 21:19:38 +01:00
Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Veranstaltung gespeichert.');
$this->assertFrontendCacheCleared();
2023-12-27 22:54:58 +01:00
}
public function testItStoresDefaultSorting(): void
{
Event::fake([Succeeded::class]);
$this->login()->loginNami()->withoutExceptionHandling();
FormRequest::new()->fields([$this->textField()])->fake();
$this->postJson(route('form.store'))->assertOk();
$form = Form::latest()->first();
$this->assertEquals('id', $form->meta['sorting']['by']);
$this->assertFalse(false, $form->meta['sorting']['direction']);
}
2023-12-31 21:46:52 +01:00
public function testRegistrationDatesCanBeNull(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$this->postJson(route('form.store'), FormRequest::new()->registrationFrom(null)->registrationUntil(null)->create())->assertOk();
$this->assertDatabaseHas('forms', [
'registration_until' => null,
'registration_from' => null,
]);
}
2024-06-29 18:02:23 +02:00
public function testItStoresExport(): void
{
$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();
$form = Form::first();
$this->assertEquals(2, $form->export->root->connectionId);
}
2024-09-21 22:46:38 +02:00
public static function validationDataProvider(): Generator
2023-12-27 22:54:58 +01:00
{
yield [FormRequest::new()->name(''), ['name' => 'Name ist erforderlich.']];
yield [FormRequest::new()->excerpt(''), ['excerpt' => 'Auszug ist erforderlich.']];
2024-02-02 01:05:45 +01:00
yield [FormRequest::new()->description(null), ['description.blocks' => 'Beschreibung ist erforderlich.']];
2023-12-31 21:46:52 +01:00
yield [FormRequest::new()->state(['from' => null]), ['from' => 'Start ist erforderlich']];
yield [FormRequest::new()->state(['to' => null]), ['to' => 'Ende ist erforderlich']];
2024-01-12 23:29:18 +01:00
yield [FormRequest::new()->state(['header_image' => null]), ['header_image' => 'Bild ist erforderlich']];
2023-12-27 22:54:58 +01:00
}
2024-07-06 15:08:13 +02:00
2023-12-27 22:54:58 +01:00
/**
* @param array<string, string> $messages
*/
2024-09-21 22:46:38 +02:00
#[DataProvider('validationDataProvider')]
2023-12-27 22:54:58 +01:00
public function testItValidatesRequests(FormRequest $request, array $messages): void
{
$this->login()->loginNami();
$request->fake();
$this->postJson(route('form.store'))->assertJsonValidationErrors($messages);
}
}