adrema/tests/Feature/Form/FormtemplateStoreActionTest...

92 lines
5.0 KiB
PHP
Raw Normal View History

2023-12-22 20:40:24 +01:00
<?php
namespace Tests\Feature\Form;
2024-03-13 13:48:35 +01:00
use App\Form\Enums\SpecialType;
2024-03-07 00:58:14 +01:00
use App\Form\Fields\TextareaField;
use App\Form\Fields\TextField;
2023-12-26 20:06:57 +01:00
use App\Form\Models\Formtemplate;
use App\Lib\Events\Succeeded;
2023-12-22 20:40:24 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2023-12-26 20:06:57 +01:00
use Illuminate\Support\Facades\Event;
use Generator;
2024-09-21 22:46:38 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\RequestFactories\EditorRequestFactory;
2023-12-22 20:40:24 +01:00
2024-02-19 01:07:54 +01:00
class FormtemplateStoreActionTest extends FormTestCase
2023-12-22 20:40:24 +01:00
{
use DatabaseTransactions;
public function testItStoresTemplates(): void
{
2023-12-26 20:06:57 +01:00
Event::fake([Succeeded::class]);
2023-12-22 20:40:24 +01:00
$this->login()->loginNami()->withoutExceptionHandling();
$payload = FormtemplateRequest::new()->name('testname')->sections([
2023-12-26 20:06:57 +01:00
FormtemplateSectionRequest::new()->name('Persönliches')->fields([
2024-06-07 00:48:54 +02:00
$this->textField('a')->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false)->hint('hhh')->intro('intro'),
2024-03-13 13:48:35 +01:00
$this->textareaField('b')->name('lala2')->required(false)->specialType(SpecialType::FIRSTNAME)->rows(10),
2023-12-26 20:06:57 +01:00
]),
])
->mailTop(EditorRequestFactory::new()->text(10, 'lala'))
->mailBottom(EditorRequestFactory::new()->text(10, 'lblb'))
->create();
2023-12-22 20:40:24 +01:00
$this->postJson(route('formtemplate.store'), $payload)->assertOk();
2023-12-26 20:06:57 +01:00
$formtemplate = Formtemplate::latest()->first();
2024-03-07 00:58:14 +01:00
$this->assertEquals('Persönliches', $formtemplate->config->sections->get(0)->name);
$this->assertEquals('lala1', $formtemplate->config->sections->get(0)->fields->get(0)->name);
2024-03-13 13:48:35 +01:00
$this->assertNull($formtemplate->config->sections->get(0)->fields->get(0)->specialType);
2024-04-10 00:00:20 +02:00
$this->assertEquals('hhh', $formtemplate->config->sections->get(0)->fields->get(0)->hint);
2024-06-07 00:48:54 +02:00
$this->assertEquals('intro', $formtemplate->config->sections->get(0)->fields->get(0)->intro);
2024-03-07 00:58:14 +01:00
$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);
2024-03-13 13:48:35 +01:00
$this->assertEquals(SpecialType::FIRSTNAME, $formtemplate->config->sections->get(0)->fields->get(1)->specialType);
2024-03-07 00:58:14 +01:00
$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']);
2024-03-07 00:58:14 +01:00
$this->assertFalse($formtemplate->config->sections->get(0)->fields->get(0)->required);
2023-12-27 22:41:07 +01:00
Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Vorlage gespeichert.');
2023-12-26 20:06:57 +01:00
}
2024-09-21 22:46:38 +02:00
public static function validationDataProvider(): Generator
2023-12-26 20:06:57 +01:00
{
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([
2024-09-21 22:46:38 +02:00
static::textField()->name(''),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.name' => 'Feldname ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-04 03:19:28 +01:00
FormtemplateFieldRequest::type('')
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-04 03:19:28 +01:00
FormtemplateFieldRequest::type('aaaaa'),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-09-21 22:46:38 +02:00
static::textField(''),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-09-21 22:46:38 +02:00
static::textField('a b'),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-09-21 22:46:38 +02:00
static::textField()->required('la')
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.required' => 'Erforderlich muss ein Wahrheitswert sein.']];
}
/**
* @param array<string, string> $messages
*/
2024-09-21 22:46:38 +02:00
#[DataProvider('validationDataProvider')]
2023-12-26 20:06:57 +01:00
public function testItValidatesRequests(FormtemplateRequest $request, array $messages): void
{
$this->login()->loginNami();
$request->fake();
2023-12-22 20:40:24 +01:00
2023-12-26 20:06:57 +01:00
$this->postJson(route('formtemplate.store'))
->assertJsonValidationErrors($messages);
2023-12-22 20:40:24 +01:00
}
}