Liefere das Anfrageformular je Anfragegrund aus und gib dem Widget Gründe, Formularführung und Präsentation

Das Widget Anfrage wählt statt eines Anfrage-Einstiegs mehrere Anfragegründe, jeder zeigt auf ein veröffentlichtes Formular
(optional mit vorgewähltem Anliegen). Der Besucher erhält genau das Formular seines Grundes: /anfrage/formular/{vorlage}
mit Grund in der Adresse, dasselbe Token, dieselbe Auswertung und Annahme. Ein Dialog (dialog-Element) und die Grundauswahl
als Karten oder Auswahlfeld kommen aus resources/js/inquiry-widget.js. Seiten mit einem Einstieg bleiben darstellbar.
Bereiche lassen sich wie Gruppen anzeigen oder ausblenden.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-27 22:29:31 +01:00
1 parent f255bf5200
commit fb479c1b01
18 files changed
+1394 -57

No files matched your search

+14 -6
View File
@@ -165,10 +165,11 @@ public function test_the_widget_mounts_a_published_entry_on_two_pages_without_an
$this->assertStringNotContainsString('nachricht', $html);
}
// Die Revision enthält nur die Kennung.
// Die Revision enthält nur die Kennung des Einstiegs (Seiten der ersten Fassung); das Widget kennt seit ADR-61
// zusätzlich Anfragegründe, hier leer.
$blocks = Page::query()->where('path', '/kontakt')->firstOrFail()->publishedRevision->blocks;
$this->assertSame(['entry' => $entry->id], $blocks[0]['refs']);
$this->assertSame([], $blocks[0]['data']);
$this->assertSame([], $blocks[0]['data']['reasons']);
}
public function test_a_central_change_reaches_every_page_without_touching_the_pages(): void
@@ -233,14 +234,21 @@ public function test_the_notification_link_is_built_from_the_web_request_not_ins
Queue::assertPushed(SendInquiryNotification::class, fn ($job): bool => str_starts_with((string) $job->adminUrl, 'http://anfrage-test.example:3011/verwaltung/anfragen/'));
}
public function test_the_widget_offers_nothing_but_the_entry_choice_in_the_cms(): void
public function test_the_widget_offers_reasons_and_presentation_but_never_form_content_in_the_cms(): void
{
// Geändert mit ADR-61: Das Widget wählt Anfragegründe (Formular je Grund) und die Darstellung, nicht mehr einen
// Einstieg. Felder, Bedingungen, Texte und Empfänger bleiben der Kommunikationsverwaltung vorbehalten.
$type = app(WidgetRegistry::class)->get('inquiry');
$this->assertSame([], $type->dataRules());
$this->assertSame([], $type->dataFields());
$this->assertSame(['reasons', 'question', 'open_label'], array_map(fn ($field): string => $field->getName(), $type->dataFields()));
$this->assertSame(['entry'], array_keys($type->refRules()));
$this->assertSame([], $type->settings());
$this->assertSame(['mode', 'open', 'picker', 'width', 'spacing', 'columns', 'button'], array_keys($type->settings()));
$this->assertSame(['compact', 'wizard'], array_keys($type->settings()['mode']));
$this->assertSame(['inline', 'modal'], array_keys($type->settings()['open']));
foreach (array_keys($type->dataRules()) as $rule) {
$this->assertMatchesRegularExpression('/^(reasons|question|open_label)/', $rule);
}
}
public function test_the_preview_of_a_page_shows_a_note_for_an_unpublished_entry(): void
+281
View File
@@ -0,0 +1,281 @@
<?php
namespace Tests\Feature;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\RenderContext;
use App\Domain\Cms\Widgets\WidgetErrors;
use App\Domain\Cms\Widgets\WidgetRegistry;
use App\Domain\Communication\ExampleConfiguration;
use App\Domain\Communication\FormToken;
use App\Domain\Communication\Inquiry;
use App\Domain\Communication\InquiryTemplate;
use App\Domain\Communication\Publisher;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
/**
* Widget „Anfrage“ (ADR-61): Anfragegründe zeigen auf veröffentlichte Formulare; der Besucher erhält genau das Formular
* seines Grundes, ohne Anfrage-Einstieg.
*/
class CommunicationWidgetTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
config(['communication.min_fill_seconds' => 0]);
Queue::fake();
app(ExampleConfiguration::class)->install();
}
private function template(string $key): InquiryTemplate
{
return InquiryTemplate::query()->where('key', $key)->firstOrFail();
}
/**
* @param list<array<string, mixed>> $reasons
* @param array<string, string> $settings
*/
private function page(string $path, array $reasons, array $settings = []): Page
{
$page = Page::query()->create(['path' => $path]);
$revision = app(PageService::class)->saveRevision($page, [[
'name' => 'Anfrage', 'anchor' => 'anfrage', 'layout' => 'u4',
'slots' => [['type' => 'inquiry', 'v' => 1, 'variant' => 'standard', 'settings' => $settings, 'data' => ['reasons' => $reasons], 'refs' => []]],
]], null, null, null);
app(PageService::class)->publish($page, $revision);
return $page->fresh();
}
/** @return array<string, mixed> */
private function reason(string $label, string $template, ?string $issue = null, ?string $sub = null): array
{
return ['key' => null, 'label' => $label, 'description' => null, 'template' => $this->template($template)->id, 'issue' => $issue, 'subissue' => $sub];
}
public function test_a_reason_delivers_exactly_its_form_with_the_issue_preselected(): void
{
$template = $this->template('kursanfragen');
$json = $this->getJson(route('inquiry.form.definition', $template).'?anliegen=kurse&unteranliegen=kursvorhaben&modus=wizard')->assertOk()->json();
$this->assertSame('wizard', $json['mode']);
$this->assertSame('Kursanfrage', $json['texts']['heading']);
$this->assertSame('kurse', $json['defaults']['anliegen']);
$this->assertSame('kursvorhaben', $json['defaults']['unteranliegen']);
$this->assertCount(1, $json['issues']);
$this->assertFalse($json['state']['fields']['anliegen'], 'Das Anliegen ist vorgewählt und wird nicht erneut gefragt.');
$this->assertTrue($json['state']['steps']['skurs2']);
$this->assertArrayNotHasKey('rules', $json);
$this->assertSame('form', FormToken::read($json['token'])['k']);
}
public function test_an_unknown_issue_restricts_nothing_and_an_invalid_mode_falls_back_to_the_compact_form(): void
{
$json = $this->getJson(route('inquiry.form.definition', $this->template('kursanfragen')).'?anliegen=gibtesnicht&modus=quatsch')->assertOk()->json();
$this->assertSame('compact', $json['mode']);
$this->assertArrayNotHasKey('anliegen', (array) $json['defaults']);
}
public function test_an_unpublished_or_archived_form_is_not_delivered(): void
{
$draft = InquiryTemplate::query()->create(['key' => 'entwurf', 'name' => 'Entwurf', 'draft' => InquiryTemplate::emptyDraft()]);
$this->getJson(route('inquiry.form.definition', $draft))->assertNotFound();
$this->postJson(route('inquiry.form.state', $draft), ['values' => []])->assertNotFound();
$this->postJson(route('inquiry.form.submit', $draft), ['values' => [], 'token' => 'x'])->assertStatus(419);
$published = $this->template('allgemein');
$published->forceFill(['archived_at' => now()])->save();
$this->getJson(route('inquiry.form.definition', $published))->assertNotFound();
}
public function test_the_server_evaluates_the_state_for_the_chosen_reason(): void
{
$template = $this->template('kursanfragen');
$url = route('inquiry.form.state', $template).'?anliegen=kurse';
$info = $this->postJson($url, ['values' => ['unteranliegen' => 'kursinfo']])->assertOk()->json();
$this->assertFalse($info['steps']['skurs2']);
$project = $this->postJson($url, ['values' => ['unteranliegen' => 'kursvorhaben']])->assertOk()->json();
$this->assertTrue($project['steps']['skurs2']);
}
public function test_a_submission_is_stored_without_an_entry_and_points_to_the_form_version(): void
{
$template = $this->template('allgemein');
$definition = $this->getJson(route('inquiry.form.definition', $template).'?anliegen=allgemein')->assertOk()->json();
$this->postJson(route('inquiry.form.submit', $template).'?anliegen=allgemein', [
'token' => $definition['token'],
'source' => '/kontakt',
'values' => ['name' => 'Mira Beispiel', 'email' => 'mira@example.org', 'nachricht' => 'Hallo!'],
])->assertCreated()->assertJsonStructure(['reference']);
$inquiry = Inquiry::query()->firstOrFail();
$this->assertNull($inquiry->entry_id);
$this->assertSame($template->publishedVersion->id, $inquiry->template_version_id);
$this->assertSame('allgemein', $inquiry->issue_key);
$this->assertSame('/kontakt', $inquiry->source_path);
$this->assertSame('Formular „Allgemeine Kontaktaufnahme“', $inquiry->channelTitle());
}
public function test_the_submission_is_validated_like_every_other_inquiry(): void
{
$template = $this->template('allgemein');
$definition = $this->getJson(route('inquiry.form.definition', $template).'?anliegen=allgemein')->json();
$this->postJson(route('inquiry.form.submit', $template).'?anliegen=allgemein', [
'token' => $definition['token'],
'values' => ['name' => 'Mira', 'email' => 'keine-adresse', 'unbekannt' => 'x'],
])->assertStatus(422);
$this->assertSame(0, Inquiry::query()->count());
}
public function test_tokens_of_entries_and_forms_are_not_interchangeable(): void
{
$template = $this->template('allgemein');
$version = $template->publishedVersion;
$entry = $template->entries()->firstOrFail();
$entryToken = FormToken::issue($entry->id, $version->id);
$this->postJson(route('inquiry.form.submit', $template), ['token' => $entryToken, 'values' => []])->assertStatus(419);
$formToken = FormToken::issue($template->id, $version->id, 'form');
$this->postJson(route('inquiry.submit', $entry), ['token' => $formToken, 'values' => []])->assertStatus(419);
$other = $this->template('kursanfragen');
$this->postJson(route('inquiry.form.submit', $other), ['token' => $formToken, 'values' => []])->assertStatus(419);
}
public function test_the_page_carries_only_the_reasons_whose_form_is_published(): void
{
$page = $this->page('/kontakt', [
$this->reason('Allgemeine Anfrage', 'allgemein', 'allgemein'),
$this->reason('Kurse & Workshops', 'kursanfragen', 'kurse'),
$this->reason('Material', 'materialanfragen', 'materialien'),
], ['mode' => 'wizard', 'open' => 'modal', 'picker' => 'select', 'width' => 'wide', 'spacing' => 'airy', 'columns' => 'one', 'button' => 'outline']);
$this->template('materialanfragen')->forceFill(['archived_at' => now()])->save();
$html = $this->get('/kontakt')->assertOk()->getContent();
$this->assertStringContainsString('data-inquiry-widget', $html);
$this->assertStringContainsString('iw-width-wide iw-spacing-airy iw-cols-one iw-btn-outline', $html);
preg_match("/data-config='([^']+)'/", $html, $match);
$config = json_decode(html_entity_decode($match[1]), true);
$this->assertSame('modal', $config['open']);
$this->assertSame('select', $config['picker']);
$this->assertSame('Worum geht es?', $config['question']);
$this->assertSame('Anfrage stellen', $config['openLabel']);
$this->assertSame(['allgemeine_anfrage', 'kurse_workshops'], array_column($config['reasons'], 'key'));
$this->assertStringContainsString('anliegen=kurse', $config['reasons'][1]['definitionUrl']);
$this->assertStringContainsString('modus=wizard', $config['reasons'][1]['submitUrl']);
$this->assertStringContainsString('/anfrage/formular/'.$this->template('kursanfragen')->id, $config['reasons'][1]['stateUrl']);
$this->assertStringNotContainsString('rules', $match[1]);
}
public function test_a_page_without_any_available_form_shows_nothing_publicly_but_explains_it_in_the_preview(): void
{
$page = $this->page('/kontakt', [$this->reason('Allgemeine Anfrage', 'allgemein')]);
$this->template('allgemein')->forceFill(['archived_at' => now()])->save();
$this->assertStringNotContainsString('data-inquiry-widget', $this->get('/kontakt')->assertOk()->getContent());
$type = app(WidgetRegistry::class)->get('inquiry');
$widget = ['data' => ['reasons' => [$this->reason('Allgemeine Anfrage', 'allgemein')]], 'refs' => [], 'settings' => []];
$preview = $type->resolve($widget, RenderContext::preview());
$this->assertNull($preview['config']);
$this->assertSame(['Allgemeine Anfrage'], $preview['unavailable']);
}
public function test_pages_of_the_first_version_with_an_entry_are_still_rendered(): void
{
$entry = $this->template('allgemein')->entries()->firstOrFail();
$page = Page::query()->create(['path' => '/alt']);
$revision = app(PageService::class)->saveRevision($page, [[
'name' => 'Anfrage', 'anchor' => 'anfrage', 'layout' => 'u4',
'slots' => [['type' => 'inquiry', 'v' => 1, 'variant' => 'standard', 'data' => [], 'refs' => ['entry' => $entry->id]]],
]], null, null, null);
app(PageService::class)->publish($page, $revision);
$html = $this->get('/alt')->assertOk()->getContent();
$this->assertStringContainsString('data-definition-url="'.route('inquiry.definition', $entry).'"', $html);
$this->assertStringNotContainsString('data-inquiry-widget', $html);
}
public function test_reasons_get_stable_keys_and_a_widget_needs_at_least_one_reason(): void
{
$type = app(WidgetRegistry::class)->get('inquiry');
$errors = new WidgetErrors;
[$data] = $type->clean(['reasons' => [
['label' => 'Kurse & Workshops', 'template' => 1, 'issue' => 'kurse', 'subissue' => 'kursinfo'],
['key' => 'kurse_workshops', 'label' => 'Kurse & Workshops', 'template' => 1],
['label' => 'Kooperation', 'template' => 1, 'subissue' => 'unsinn'],
]], [], $errors);
$this->assertSame(['kurse_workshops', 'kurse_workshops_2', 'kooperation'], array_column($data['reasons'], 'key'));
$this->assertNull($data['reasons'][2]['subissue'], 'Ein Unteranliegen ohne Anliegen wird nicht gespeichert.');
$this->assertTrue($errors->isEmpty());
$empty = new WidgetErrors;
$type->clean(['reasons' => []], [], $empty);
$this->assertArrayHasKey('data.reasons', $empty->all());
$legacy = new WidgetErrors;
$type->clean(['reasons' => []], ['entry' => 3], $legacy);
$this->assertTrue($legacy->isEmpty());
}
public function test_a_page_can_be_saved_while_its_form_gets_withdrawn_later(): void
{
$template = $this->template('materialanfragen');
$page = $this->page('/material', [$this->reason('Material', 'materialanfragen')]);
$template->forceFill(['archived_at' => now()])->save();
$revision = app(PageService::class)->saveRevision($page, [[
'name' => 'Anfrage', 'anchor' => 'anfrage', 'layout' => 'u4',
'slots' => [['type' => 'inquiry', 'v' => 1, 'variant' => 'standard', 'settings' => [], 'data' => ['reasons' => [$this->reason('Material', 'materialanfragen')]], 'refs' => []]],
]], null, null, null);
$this->assertNotNull($revision->id);
}
public function test_a_step_can_be_shown_or_hidden_like_a_group(): void
{
$draft = [
'texts' => [], 'issues' => [],
'steps' => [
['key' => 'sa', 'title' => 'Kontakt', 'intro' => null, 'items' => [
['kind' => 'field', 'field' => 'nachricht', 'required' => false],
['kind' => 'field', 'field' => 'email', 'required' => true],
['kind' => 'field', 'field' => 'kursformat', 'required' => false],
]],
['key' => 'sb', 'title' => 'Schulangaben', 'intro' => null, 'items' => [['kind' => 'field', 'field' => 'region', 'required' => false]]],
],
'rules' => [['effect' => 'show', 'target' => 'step:sb', 'match' => 'all', 'conditions' => [['source' => 'kursformat', 'op' => 'eq', 'values' => ['schul_ag']]], 'value' => null]],
];
$template = InquiryTemplate::query()->create(['key' => 'schritte', 'name' => 'Schritte', 'draft' => $draft]);
app(Publisher::class)->publishTemplate($template, null, null);
$off = $this->postJson(route('inquiry.form.state', $template), ['values' => ['kursformat' => 'workshop']])->assertOk()->json();
$on = $this->postJson(route('inquiry.form.state', $template), ['values' => ['kursformat' => 'schul_ag']])->assertOk()->json();
$this->assertFalse($off['steps']['sb']);
$this->assertFalse($off['fields']['region']);
$this->assertTrue($on['steps']['sb']);
$this->assertTrue($on['fields']['region']);
}
}