Files
laraundfelix/tests/Feature/CommunicationPublicTest.php
T
LarsandClaude Sonnet 5.5 fb479c1b01 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>
2026-11-27 22:29:31 +01:00

269 lines
12 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\WidgetRegistry;
use App\Domain\Communication\ExampleConfiguration;
use App\Domain\Communication\Inquiry;
use App\Domain\Communication\InquiryEntry;
use App\Domain\Communication\Publisher;
use App\Jobs\SendInquiryNotification;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
class CommunicationPublicTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
config(['communication.min_fill_seconds' => 0]);
app(ExampleConfiguration::class)->install();
}
private function entry(string $key): InquiryEntry
{
return InquiryEntry::query()->where('key', $key)->firstOrFail();
}
private function pageWith(string $path, ?InquiryEntry $entry, string $anchor = 'anfrage'): Page
{
$page = Page::query()->create(['path' => $path]);
$revision = app(PageService::class)->saveRevision($page, [[
'name' => 'Anfrage', 'anchor' => $anchor, 'layout' => 'u4',
'slots' => [['type' => 'inquiry', 'v' => 1, 'variant' => 'standard', 'data' => [], 'refs' => ['entry' => $entry?->id]]],
]], null, null, null);
app(PageService::class)->publish($page, $revision);
return $page->fresh();
}
public function test_the_definition_contains_the_form_a_token_and_the_first_state_but_no_rules(): void
{
$entry = $this->entry('schul_ag');
$response = $this->getJson(route('inquiry.definition', $entry))->assertOk();
$response->assertHeader('Cache-Control');
$json = $response->json();
$this->assertSame('wizard', $json['mode']);
$this->assertSame('Schul-AG anfragen', $json['texts']['heading']);
$this->assertSame('schul_ag', $json['defaults']['kursformat']);
$this->assertNotEmpty($json['token']);
$this->assertNotEmpty($json['csrf']);
$this->assertArrayNotHasKey('rules', $json);
$this->assertArrayNotHasKey('purpose', $json['fields']['email']);
}
public function test_the_first_state_follows_the_preselection(): void
{
$json = $this->getJson(route('inquiry.definition', $this->entry('schul_ag')))->json();
$this->assertTrue($json['state']['steps']['skurs2']);
$this->assertTrue($json['state']['groups']['schul_ag']);
$this->assertSame('kursvorhaben', $json['state']['subissue']);
}
public function test_an_unpublished_or_archived_entry_is_not_delivered(): void
{
$entry = $this->entry('kooperation');
app(Publisher::class)->withdrawEntry($entry);
$this->getJson(route('inquiry.definition', $entry))->assertNotFound();
$this->postJson(route('inquiry.state', $entry), ['values' => []])->assertNotFound();
$this->postJson(route('inquiry.submit', $entry), ['values' => [], 'token' => 'x'])->assertStatus(419);
}
public function test_the_server_decides_which_fields_are_visible(): void
{
$entry = $this->entry('kurse_workshops');
$this->postJson(route('inquiry.state', $entry), ['values' => ['anliegen' => 'kurse', 'unteranliegen' => 'kursinfo']])
->assertOk()
->assertJsonPath('steps.skurs2', false)
->assertJsonPath('fields.region', true)
->assertJsonPath('fields.einrichtung_name', false);
$this->postJson(route('inquiry.state', $entry), ['values' => ['anliegen' => 'kurse', 'unteranliegen' => 'kursvorhaben', 'kursformat' => 'schul_ag', 'ort_abweichend' => 'ja']])
->assertOk()
->assertJsonPath('groups.schul_ag', true)
->assertJsonPath('groups.ort_angaben', true)
->assertJsonPath('required.laufzeit', true);
$this->postJson(route('inquiry.state', $entry), ['values' => 'kaputt'])->assertStatus(422);
}
public function test_a_visitor_sends_an_inquiry_without_an_account(): void
{
$entry = $this->entry('allgemeiner_kontakt');
$token = $this->getJson(route('inquiry.definition', $entry))->json('token');
$this->postJson(route('inquiry.submit', $entry), [
'token' => $token,
'source' => '/kontakt',
'values' => ['name' => 'Mira Beispiel', 'email' => 'mira@example.org', 'nachricht' => 'Hallo zusammen'],
])->assertCreated()->assertJsonStructure(['reference']);
$inquiry = Inquiry::query()->firstOrFail();
$this->assertSame('/kontakt', $inquiry->source_path);
$this->assertSame('Mira Beispiel', $inquiry->contact_name);
$this->assertNull(Inquiry::query()->first()->templateVersion->published_by);
}
public function test_validation_errors_name_the_fields_and_keep_the_inquiry_out_of_the_database(): void
{
$entry = $this->entry('allgemeiner_kontakt');
$token = $this->getJson(route('inquiry.definition', $entry))->json('token');
$this->postJson(route('inquiry.submit', $entry), ['token' => $token, 'values' => ['name' => 'Mira', 'email' => 'falsch']])
->assertStatus(422)
->assertJsonPath('fields.email', 'Bitte geben Sie eine gültige E-Mail-Adresse ein.')
->assertJsonPath('fields.nachricht', 'Bitte füllen Sie dieses Feld aus.');
$this->assertSame(0, Inquiry::query()->count());
}
public function test_submissions_are_rate_limited_per_address(): void
{
RateLimiter::clear('inq-m|127.0.0.1');
$entry = $this->entry('allgemeiner_kontakt');
$token = $this->getJson(route('inquiry.definition', $entry))->json('token');
$values = ['values' => ['name' => 'M', 'email' => 'x', 'nachricht' => 'y'], 'token' => $token];
$statuses = [];
for ($i = 0; $i < 7; $i++) {
$statuses[] = $this->postJson(route('inquiry.submit', $entry), $values)->status();
}
$this->assertContains(429, $statuses);
$this->assertSame(422, $statuses[0]);
}
public function test_the_widget_mounts_a_published_entry_on_two_pages_without_any_form_content(): void
{
$entry = $this->entry('allgemeiner_kontakt');
$this->pageWith('/kontakt', $entry);
$this->pageWith('/hilfe', $entry);
foreach (['/kontakt', '/hilfe'] as $path) {
$html = $this->get($path)->assertOk()
->assertSee('data-inquiry', false)
->assertSee('data-definition-url="'.route('inquiry.definition', $entry).'"', false)
->assertSee('id="anfrage"', false)
->getContent();
// Nichts aus der Konfiguration steht in der gespeicherten Seite.
$this->assertStringNotContainsString('Schreiben Sie uns', $html);
$this->assertStringNotContainsString('nachricht', $html);
}
// 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']['reasons']);
}
public function test_a_central_change_reaches_every_page_without_touching_the_pages(): void
{
$entry = $this->entry('allgemeiner_kontakt');
$this->pageWith('/kontakt', $entry);
$revisionId = Page::query()->where('path', '/kontakt')->value('published_revision_id');
$template = $entry->template;
$draft = $template->draft;
$draft['texts']['heading'] = 'Neue zentrale Überschrift';
$template->update(['draft' => $draft]);
$this->getJson(route('inquiry.definition', $entry))->assertJsonPath('texts.heading', 'Schreiben Sie uns');
app(Publisher::class)->publishTemplate($template);
$this->getJson(route('inquiry.definition', $entry))->assertJsonPath('texts.heading', 'Neue zentrale Überschrift');
$this->assertSame($revisionId, Page::query()->where('path', '/kontakt')->value('published_revision_id'), 'Die Seite bleibt unverändert.');
}
public function test_a_withdrawn_entry_leaves_the_page_valid_and_the_public_view_empty(): void
{
$entry = $this->entry('kooperation');
$this->pageWith('/partner', $entry);
app(Publisher::class)->withdrawEntry($entry);
$this->get('/partner')->assertOk()->assertDontSee('data-inquiry', false);
}
public function test_the_page_editor_rejects_an_unknown_entry_but_a_withdrawn_one_never_blocks_saving(): void
{
$entry = $this->entry('kooperation');
$page = $this->pageWith('/partner', $entry);
$widget = fn (int $id): array => [['layout' => 'u4', 'slots' => [['type' => 'inquiry', 'v' => 1, 'variant' => 'standard', 'data' => [], 'refs' => ['entry' => $id]]]]];
try {
app(PageService::class)->saveRevision($page, $widget(999999), null, null, null);
$this->fail('Erwartet: unbekannter Einstieg abgelehnt.');
} catch (ValidationException $e) {
$this->assertNotEmpty($e->errors());
}
// Zurückgezogen oder archiviert: die Seite bleibt speicherbar (nur die Auswahl im Editor bietet ihn nicht mehr an).
app(Publisher::class)->withdrawEntry($entry);
$entry->forceFill(['archived_at' => now()])->save();
$this->assertNotNull(app(PageService::class)->saveRevision($page, $widget($entry->id), 'Anderer Titel', null, null));
}
public function test_the_notification_link_is_built_from_the_web_request_not_inside_the_worker(): void
{
Queue::fake();
$entry = $this->entry('allgemeiner_kontakt');
$token = $this->getJson(route('inquiry.definition', $entry))->json('token');
$this->postJson('http://anfrage-test.example:3011/anfrage/'.$entry->id.'/senden', [
'token' => $token,
'values' => ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo'],
])->assertCreated();
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_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(['reasons', 'question', 'open_label'], array_map(fn ($field): string => $field->getName(), $type->dataFields()));
$this->assertSame(['entry'], array_keys($type->refRules()));
$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
{
$entry = $this->entry('kooperation');
$page = $this->pageWith('/partner', $entry);
app(Publisher::class)->withdrawEntry($entry);
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
$this->actingAs($editor, 'staff')
->get(route('pages.preview', $page->publishedRevision))
->assertOk()
->assertSee('nicht veröffentlicht', false);
}
}