Ersetze die Builder-Liste des Formular-Editors durch eine Formularansicht mit Ziehen und Ablegen
CI Feature / test (push) Successful in 4m18s

Der Aufbau eines Formulars zeigt Bereiche als Karten, Teilbereiche als eingerahmte Einheit darin und Felder als Zeilen
(Frage, Art der Antwort, Pflicht). Hilfetext, Auswahlwerte, Bedingung und weitere Einstellungen öffnen als Pop-up.
Bereiche, Teilbereiche und Felder lassen sich ziehen (SortableJS aus Filament) oder per Pfeiltasten und über
"Verschieben nach" ordnen; Rückgängig nimmt Änderungen bis zum Speichern zurück.

Der Browser führt den Baum und übergibt ihn beim Speichern als Text; FormEditor prüft ihn (validateTree) und überführt ihn
über fromTree in dieselbe flache Ansicht wie bisher. Entwurf, Katalog und Engine bleiben unverändert, keine Migration.

Tests: Die Livewire-Tests des Editors setzen jetzt den Baumtext (editor_json) statt der Builder-Blöcke; neu sind
Baumumwandlung ohne Verlust, Teilbereich mit Feldern, Prüfung und Konfiguration. Das ist bewusst: Der Builder entfällt.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-28 08:08:39 +01:00
1 parent 79c4e877ab
commit 380a3013c7
10 files changed
+1860 -225

No files matched your search

+143 -12
View File
@@ -203,26 +203,145 @@ 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<string, mixed> $overrides
* @return array<string, mixed>
*/
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<array<string, mixed>> $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]);
$areas = $component->get('data.editor.areas');
$this->assertCount(1, $areas);
$tree = json_decode((string) $component->get('data.editor_json'), true);
$this->assertCount(1, $tree['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->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'));
// Ein zweites Speichern legt nichts doppelt an, weil die Ansicht nach dem Speichern die festen Kennungen trägt.
// 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);
@@ -232,16 +351,28 @@ public function test_a_form_is_built_in_the_editor_saved_twice_without_duplicate
$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]);
$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');
$this->setTree($component, [$this->newField('Ihr Name', ['required' => true])]);
$component->call('save');
$component->callAction('publish', ['note' => null]);
$this->assertFalse($template->fresh()->isPublished());