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' => []]]]]); } 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]); $areas = $component->get('data.editor.areas'); $this->assertCount(1, $areas); $areaId = array_key_first($areas); $component->set('data.editor.areas.'.$areaId.'.items', [ 'a' => ['type' => 'field', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'Ihr Rufname', 'type' => 'text', 'required' => true, 'purpose' => 'name', 'options' => [], 'cond' => []]], 'b' => ['type' => 'field', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'Ihre Mailadresse zur Antwort', 'type' => 'email', 'required' => true, 'purpose' => 'email', 'options' => [], 'cond' => []]], ])->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')); // Ein zweites Speichern legt nichts doppelt an, weil die Ansicht nach dem Speichern die festen Kennungen trägt. $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_invalid_form_is_not_published(): void { $editor = $this->staff(); $template = $this->emptyForm('ohnemail'); $component = Livewire::actingAs($editor, 'staff')->test(EditInquiryTemplate::class, ['record' => $template->id]); $areaId = array_key_first($component->get('data.editor.areas')); $component->set('data.editor.areas.'.$areaId.'.items', [ 'a' => ['type' => 'field', 'data' => ['key' => FormEditor::temporaryKey(), 'label' => 'Ihr Name', 'type' => 'text', 'required' => true, 'options' => [], 'cond' => []]], ])->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'])); } }