install(); } /** * @param array $draft * @return array */ private function structure(array $draft): array { return FormJson::normalize(['issues' => $draft['issues'], 'steps' => $draft['steps'], 'rules' => $draft['rules']]); } public function test_every_example_form_opens_and_saves_unchanged_in_the_editor(): void { $editor = app(FormEditor::class); foreach (InquiryTemplate::query()->get() as $template) { $draft = (array) $template->draft; $fieldsBefore = InquiryField::query()->get()->map->getAttributes()->all(); $groupsBefore = InquiryFieldGroup::query()->get()->map->getAttributes()->all(); $saved = $editor->persist($editor->expand($draft)); $this->assertSame($this->structure($draft), $this->structure($saved), 'Entwurf unverändert: '.$template->name); $this->assertSame($fieldsBefore, InquiryField::query()->get()->map->getAttributes()->all(), 'Feldkatalog unverändert: '.$template->name); $this->assertSame($groupsBefore, InquiryFieldGroup::query()->get()->map->getAttributes()->all(), 'Feldgruppen unverändert: '.$template->name); } } public function test_the_example_courses_form_shows_its_conditions_at_the_elements_and_keeps_the_rest_as_rules(): void { $view = app(FormEditor::class)->expand((array) InquiryTemplate::query()->where('key', 'kursanfragen')->firstOrFail()->draft); $second = $view['areas'][1]; $this->assertSame('skip', $second['cond']['effect']); $this->assertSame('unteranliegen', $second['cond']['conditions'][0]['source']); $sections = collect($second['items'])->where('type', 'section')->keyBy('data.key'); $this->assertSame('show', $sections['traeger_angaben']['data']['cond']['effect']); $this->assertSame('ja', $sections['traeger_angaben']['data']['cond']['conditions'][0]['values'][0]); $fields = collect($view['areas'])->flatMap(fn (array $area) => $area['items'])->where('type', 'field')->keyBy('data.key'); $this->assertSame('show', $fields['schulart']['data']['cond']['effect'], 'Die erste Regel am Ziel erscheint am Feld.'); // Die zweite Regel desselben Ziels (Pflichtfeld, wenn …) bleibt als weitere Regel erhalten. $this->assertCount(1, $view['rules']); $this->assertSame('require', $view['rules'][0]['effect']); $this->assertSame('field:schulart', $view['rules'][0]['target']); } public function test_new_fields_and_sections_are_created_in_the_catalog_and_the_form_publishes(): void { $editor = app(FormEditor::class); $view = [ 'issues' => [], 'rules' => [], 'areas' => [[ 'key' => 'sabc123', 'title' => 'Ihre Angaben', 'intro' => null, 'cond' => [], 'items' => [ ['type' => 'field', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'Wie heißen Sie?', 'help' => 'Vor- und Nachname', 'type' => 'text', 'required' => true, 'purpose' => 'name', 'options' => [], 'cond' => []]], ['type' => 'field', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'E-Mail für Rückfragen', 'type' => 'email', 'required' => true, 'purpose' => 'email', 'options' => [], 'cond' => []]], ['type' => 'section', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'Angaben zur Schule', 'description' => 'Nur für Schulen', 'cond' => []]], ['type' => 'field', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'Welche Schulart?', 'type' => 'select', 'required' => false, 'purpose' => null, 'options' => [['key' => '', 'label' => 'Grundschule'], ['key' => '', 'label' => 'Gymnasium']], 'cond' => []]], ], ]], ]; $draft = $editor->persist($view); $name = InquiryField::query()->where('label', 'Wie heißen Sie?')->firstOrFail(); $this->assertSame('wie_heissen_sie', $name->key); $this->assertSame('name', $name->purpose->value); $schule = InquiryField::query()->where('label', 'Welche Schulart?')->firstOrFail(); $this->assertSame(['grundschule', 'gymnasium'], array_column($schule->optionList(), 'key')); $group = InquiryFieldGroup::query()->where('label', 'Angaben zur Schule')->firstOrFail(); $this->assertSame([['field' => $schule->key, 'required' => false]], $group->itemList()); $this->assertSame([ ['kind' => 'field', 'field' => 'wie_heissen_sie', 'required' => true], ['kind' => 'field', 'field' => (string) InquiryField::query()->where('label', 'E-Mail für Rückfragen')->value('key'), 'required' => true], ['kind' => 'group', 'group' => $group->key], ], $draft['steps'][0]['items']); $template = InquiryTemplate::query()->create(['key' => 'neu', 'name' => 'Neu', 'draft' => [...InquiryTemplate::emptyDraft(), ...$draft]]); $result = app(TemplateCompiler::class)->compile((array) $template->draft); $this->assertSame([], $result['errors']); app(Publisher::class)->publishTemplate($template, null, null); $this->assertTrue($template->fresh()->isPublished()); } public function test_conditions_at_elements_become_engine_rules_in_document_order_with_keys_resolved(): void { $editor = app(FormEditor::class); $ja = [['source' => 'traeger_abweichend', 'op' => 'eq', 'values' => ['ja']]]; $view = [ 'issues' => [], 'rules' => [], 'areas' => [ ['key' => 'sa1', 'title' => 'Eins', 'intro' => null, 'cond' => [], 'items' => [ ['type' => 'field', 'data' => ['key' => 'traeger_abweichend', 'label' => 'Abweichender Träger?', 'type' => 'boolean', 'required' => false, 'options' => [], 'cond' => []]], ['type' => 'field', 'data' => ['key' => 'email', 'label' => 'Ihre E-Mail-Adresse', 'type' => 'email', 'required' => true, 'purpose' => 'email', 'options' => [], 'cond' => []]], ]], ['key' => 'sa2', 'title' => 'Zwei', 'intro' => null, 'cond' => ['effect' => 'show', 'match' => 'all', 'conditions' => $ja], 'items' => [ ['type' => 'field', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'Name des Trägers', 'type' => 'text', 'required' => true, 'options' => [], 'cond' => ['effect' => 'require', 'match' => 'all', 'conditions' => $ja]]], ]], ], ]; $draft = $editor->persist($view); $this->assertSame([ ['effect' => 'require', 'target' => 'field:name_des_traegers', 'match' => 'all', 'conditions' => $ja, 'value' => null], ['effect' => 'show', 'target' => 'step:sa2', 'match' => 'all', 'conditions' => $ja, 'value' => null], ], $draft['rules']); $template = InquiryTemplate::query()->create(['key' => 'bedingt', 'name' => 'Bedingt', 'draft' => [...InquiryTemplate::emptyDraft(), ...$draft]]); $this->assertSame([], app(TemplateCompiler::class)->compile((array) $template->draft)['errors']); } public function test_an_existing_field_keeps_its_key_and_type_and_options_keep_their_keys_when_renamed(): void { $editor = app(FormEditor::class); $view = $editor->expand((array) InquiryTemplate::query()->where('key', 'allgemein')->firstOrFail()->draft); $found = false; foreach ($view['areas'] as &$area) { foreach ($area['items'] as &$block) { if ($block['type'] === 'field' && $block['data']['key'] === 'nachricht') { $block['data']['label'] = 'Was möchten Sie uns mitteilen?'; $block['data']['type'] = 'text'; $found = true; } } } unset($area, $block); $this->assertTrue($found); $editor->persist($view); $field = InquiryField::query()->where('key', 'nachricht')->firstOrFail(); $this->assertSame('Was möchten Sie uns mitteilen?', $field->label); $this->assertSame('textarea', $field->type->value, 'Der Feldtyp eines vorhandenen Feldes ändert sich nicht.'); $options = InquiryField::query()->where('key', 'kursformat')->firstOrFail()->optionList(); $view = $editor->expand((array) InquiryTemplate::query()->where('key', 'kursanfragen')->firstOrFail()->draft); foreach ($view['areas'] as &$area) { foreach ($area['items'] as &$block) { if ($block['type'] === 'field' && $block['data']['key'] === 'kursformat') { $block['data']['options'][0]['label'] = 'Umbenannt'; } } } unset($area, $block); $editor->persist($view); $renamed = InquiryField::query()->where('key', 'kursformat')->firstOrFail()->optionList(); $this->assertSame($options[0]['key'], $renamed[0]['key']); $this->assertSame('Umbenannt', $renamed[0]['label']); } private function staff(): User { $user = User::factory()->redakteur()->create(); $user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); return $user; } private function emptyForm(string $key): InquiryTemplate { return InquiryTemplate::query()->create(['key' => $key, 'name' => ucfirst($key), 'draft' => [...InquiryTemplate::emptyDraft(), 'steps' => [['key' => 'sstart1', 'title' => 'Ihre Angaben', 'intro' => null, 'items' => []]]]]); } /** * @param array $overrides * @return array */ private function newField(string $label, array $overrides = []): array { return ['kind' => 'field', 'key' => FormEditor::temporaryKey(), 'label' => $label, 'type' => 'text', 'required' => false, 'purpose' => null, 'options' => [], 'cond' => [], ...$overrides]; } /** * @param list> $items */ private function setTree($component, array $items): void { $tree = json_decode((string) $component->get('data.editor_json'), true); $tree['areas'][0]['items'] = $items; $component->set('data.editor_json', json_encode($tree)); } public function test_the_tree_view_nests_fields_in_their_section_and_converts_back_without_loss(): void { $editor = app(FormEditor::class); foreach (InquiryTemplate::query()->get() as $template) { $draft = (array) $template->draft; $view = $editor->expand($draft); $tree = FormEditor::toTree($view); foreach ($tree['areas'] as $area) { foreach ($area['items'] as $item) { $this->assertContains($item['kind'], ['field', 'section', 'issue'], 'Im Baum gibt es keine Ende-Markierung: '.$template->name); } } $fieldsBefore = InquiryField::query()->get()->map->getAttributes()->all(); $groupsBefore = InquiryFieldGroup::query()->get()->map->getAttributes()->all(); $saved = $editor->persist(FormEditor::fromTree(json_decode((string) json_encode($tree), true))); $this->assertSame($this->structure($draft), $this->structure($saved), 'Über den Baum unverändert: '.$template->name); $this->assertSame($fieldsBefore, InquiryField::query()->get()->map->getAttributes()->all(), 'Feldkatalog unverändert: '.$template->name); $this->assertSame($groupsBefore, InquiryFieldGroup::query()->get()->map->getAttributes()->all(), 'Feldgruppen unverändert: '.$template->name); } $courses = FormEditor::toTree($editor->expand((array) InquiryTemplate::query()->where('key', 'kursanfragen')->firstOrFail()->draft)); $sections = collect($courses['areas'])->flatMap(fn (array $area) => $area['items'])->where('kind', 'section'); $this->assertTrue($sections->isNotEmpty()); $this->assertTrue($sections->every(fn (array $section): bool => $section['items'] !== []), 'Ein Teilbereich trägt seine Felder selbst.'); } public function test_a_field_after_a_section_stays_outside_it_and_a_second_section_ends_the_first(): void { $editor = app(FormEditor::class); $tree = ['areas' => [['key' => 'sx1', 'title' => 'Eins', 'intro' => null, 'cond' => [], 'items' => [ ['kind' => 'section', 'key' => FormEditor::temporaryKey(), 'label' => 'Erster Teil', 'items' => [$this->newField('Frage A'), $this->newField('Frage B')]], ['kind' => 'section', 'key' => FormEditor::temporaryKey(), 'label' => 'Zweiter Teil', 'items' => [$this->newField('Frage C')]], $this->newField('Frage D'), ]]]]; $this->assertSame([], $editor->validateTree($tree)); $draft = $editor->persist(FormEditor::fromTree($tree)); $items = $draft['steps'][0]['items']; $this->assertSame(['group', 'group', 'field'], array_column($items, 'kind')); $this->assertSame(['frage_a', 'frage_b'], array_column(InquiryFieldGroup::query()->where('label', 'Erster Teil')->firstOrFail()->itemList(), 'field')); $this->assertSame(['frage_c'], array_column(InquiryFieldGroup::query()->where('label', 'Zweiter Teil')->firstOrFail()->itemList(), 'field')); $this->assertSame('frage_d', $items[2]['field']); } public function test_the_tree_is_checked_before_saving_and_missing_details_are_named(): void { $editor = app(FormEditor::class); $tree = ['areas' => [ ['key' => 'sx1', 'title' => '', 'items' => [ $this->newField(''), $this->newField('Welche Art?', ['type' => 'select', 'options' => [['key' => '', 'label' => ' ']]]), ['kind' => 'section', 'key' => FormEditor::temporaryKey(), 'label' => '', 'items' => []], ['kind' => 'issue', 'issues' => []], ['kind' => 'issue', 'issues' => []], ]], ]]; $errors = implode("\n", $editor->validateTree($tree)); $this->assertStringContainsString('Der Titel fehlt', $errors); $this->assertStringContainsString('keine Frage oder Beschriftung', $errors); $this->assertStringContainsString('Bei „Welche Art?“ fehlen die Auswahlwerte', $errors); $this->assertStringContainsString('keine Überschrift', $errors); $this->assertStringContainsString('nur einmal', $errors); $same = $this->newField('Doppelt', ['key' => 'email']); $twice = ['areas' => [['key' => 'sx1', 'title' => 'Eins', 'items' => [$same, $same]]]]; $this->assertStringContainsString('steht mehrfach im Formular', implode("\n", $editor->validateTree($twice))); } public function test_the_client_configuration_lists_types_catalog_issues_and_effects(): void { $config = app(FormEditor::class)->clientConfig(); $this->assertContains('select', array_column($config['types'], 'value')); $this->assertContains('email', array_column($config['catalog'], 'key')); $this->assertSame(['show', 'hide', 'skip'], array_keys($config['effects']['step'])); $this->assertArrayHasKey('eq', $config['operators']); $this->assertNotEmpty($config['issues']); } public function test_the_form_view_renders_the_areas_without_the_builder_blocks(): void { $template = InquiryTemplate::query()->where('key', 'kursanfragen')->firstOrFail(); $html = Livewire::actingAs($this->staff(), 'staff')->test(EditInquiryTemplate::class, ['record' => $template->id])->html(); $this->assertStringContainsString('data-fe-root', $html); $this->assertStringContainsString('Bereich hinzufügen', $html); $this->assertStringContainsString('Vorhandenes Feld einfügen', $html); $this->assertStringNotContainsString('fi-fo-builder', $html); } public function test_a_form_is_built_in_the_editor_saved_twice_without_duplicates_and_published(): void { $editor = $this->staff(); $template = $this->emptyForm('frisch'); $component = Livewire::actingAs($editor, 'staff')->test(EditInquiryTemplate::class, ['record' => $template->id]); $tree = json_decode((string) $component->get('data.editor_json'), true); $this->assertCount(1, $tree['areas']); $this->setTree($component, [ $this->newField('Ihr Rufname', ['required' => true, 'purpose' => 'name']), $this->newField('Ihre Mailadresse zur Antwort', ['type' => 'email', 'required' => true, 'purpose' => 'email']), ]); $component->call('save')->assertHasNoFormErrors(); $this->assertSame(1, InquiryField::query()->where('label', 'Ihr Rufname')->count()); $draft = $template->fresh()->draft; $this->assertSame(['ihr_rufname', 'ihre_mailadresse_zur_antwort'], array_column($draft['steps'][0]['items'], 'field')); // Nach dem Speichern trägt der Baum die festen Kennungen; ein zweites Speichern legt nichts doppelt an. $saved = json_decode((string) $component->get('data.editor_json'), true); $this->assertSame('ihr_rufname', $saved['areas'][0]['items'][0]['key']); $component->call('save')->assertHasNoFormErrors(); $this->assertSame(1, InquiryField::query()->where('label', 'Ihr Rufname')->count()); $this->assertSame($draft, $template->fresh()->draft); $component->callAction('check')->assertNotified('Die Konfiguration ist in Ordnung.'); $component->callAction('publish', ['note' => 'Erste Fassung'])->assertHasNoActionErrors(); $this->assertTrue($template->fresh()->isPublished()); } public function test_an_incomplete_tree_is_rejected_with_the_reasons_and_changes_nothing(): void { $editor = $this->staff(); $template = $this->emptyForm('luecken'); $fieldsBefore = InquiryField::query()->count(); $component = Livewire::actingAs($editor, 'staff')->test(EditInquiryTemplate::class, ['record' => $template->id]); $this->setTree($component, [$this->newField('Mit Frage'), $this->newField('')]); $component->call('save')->assertHasErrors(['data.editor_json']); $this->assertSame($fieldsBefore, InquiryField::query()->count(), 'Nichts wird angelegt, solange etwas fehlt.'); $this->assertSame([], $template->fresh()->draft['steps'][0]['items']); } public function test_an_invalid_form_is_not_published(): void { $editor = $this->staff(); $template = $this->emptyForm('ohnemail'); $component = Livewire::actingAs($editor, 'staff')->test(EditInquiryTemplate::class, ['record' => $template->id]); $this->setTree($component, [$this->newField('Ihr Name', ['required' => true])]); $component->call('save'); $component->callAction('publish', ['note' => null]); $this->assertFalse($template->fresh()->isPublished()); } public function test_the_navigation_offers_forms_for_editors_and_the_technical_blocks_stay_reachable(): void { $this->actingAs($this->staff(), 'staff'); $html = $this->get('/verwaltung')->assertOk()->getContent(); $this->assertStringContainsString('Formulare & Befragungen', $html); $this->assertStringNotContainsString('Feldkatalog', $html); $this->assertStringNotContainsString('Anfrage-Einstiege', $html); $this->assertStringNotContainsString('Feldgruppen', $html); $this->get('/verwaltung/kommunikation/felder')->assertOk(); $this->get('/verwaltung/kommunikation/anliegen')->assertOk(); } public function test_the_usage_in_other_forms_is_reported(): void { $usage = app(FormEditor::class)->usage(); $this->assertContains('Allgemeine Kontaktaufnahme', $usage['fields']['email']); $this->assertGreaterThan(1, count($usage['fields']['email'])); } }