Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit unveränderlichen Fassungen, Anfrage-Einstiege und eingegangene Anfragen. Eine gemeinsame Regelauswertung bedient Besucher, Vorschau und Anfrageservice; Werte nicht mehr relevanter Felder werden verworfen, Anliegen und Auswahlwerte serverseitig geprüft, doppeltes Absenden bleibt eine Anfrage. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
439 lines
20 KiB
PHP
439 lines
20 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Domain\Communication\CommunicationSettings;
|
||
use App\Domain\Communication\ExampleConfiguration;
|
||
use App\Domain\Communication\FormEvaluator;
|
||
use App\Domain\Communication\FormFactory;
|
||
use App\Domain\Communication\FormToken;
|
||
use App\Domain\Communication\Inquiry;
|
||
use App\Domain\Communication\InquiryEntry;
|
||
use App\Domain\Communication\InquiryField;
|
||
use App\Domain\Communication\InquiryService;
|
||
use App\Domain\Communication\InquiryStatus;
|
||
use App\Domain\Communication\InquiryTemplate;
|
||
use App\Domain\Communication\InquiryTemplateVersion;
|
||
use App\Domain\Communication\Publisher;
|
||
use App\Domain\Communication\SubmissionException;
|
||
use App\Mail\InquiryReceived;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Illuminate\Validation\ValidationException;
|
||
use Tests\TestCase;
|
||
|
||
class CommunicationInquiryTest 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();
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $values
|
||
* @return array{reference: string, created: bool}
|
||
*/
|
||
private function send(InquiryEntry $entry, array $values, array $extra = []): array
|
||
{
|
||
$version = $entry->template->publishedVersion;
|
||
|
||
return app(InquiryService::class)->submit($entry, array_merge([
|
||
'token' => FormToken::issue($entry->id, $version->id),
|
||
'values' => $values,
|
||
], $extra));
|
||
}
|
||
|
||
/** @return array<string, mixed> */
|
||
private function infoRequest(): array
|
||
{
|
||
return [
|
||
'anliegen' => 'kurse',
|
||
'unteranliegen' => 'kursinfo',
|
||
'kursformat' => 'schul_ag',
|
||
'region' => 'Hannover',
|
||
'vorname' => 'Mira',
|
||
'nachname' => 'Beispiel',
|
||
'email' => 'mira@example.org',
|
||
'nachricht' => 'Wir möchten gern mehr erfahren.',
|
||
];
|
||
}
|
||
|
||
public function test_the_example_configuration_is_published_and_only_installs_into_an_empty_area(): void
|
||
{
|
||
$this->assertSame(5, InquiryEntry::query()->live()->count());
|
||
$this->assertSame(4, InquiryTemplate::query()->whereNotNull('published_version_id')->count());
|
||
|
||
$this->expectException(\RuntimeException::class);
|
||
app(ExampleConfiguration::class)->install();
|
||
}
|
||
|
||
public function test_an_information_request_asks_only_for_few_details_and_stores_readable_answers(): void
|
||
{
|
||
$result = $this->send($this->entry('kurse_workshops'), $this->infoRequest());
|
||
|
||
$this->assertTrue($result['created']);
|
||
|
||
$inquiry = Inquiry::query()->firstOrFail();
|
||
$this->assertSame($result['reference'], $inquiry->reference);
|
||
$this->assertSame('Kurse & Workshops › Allgemeine Kursinformation', $inquiry->issueTitle());
|
||
$this->assertSame('Mira Beispiel', $inquiry->contact_name);
|
||
$this->assertSame('mira@example.org', $inquiry->contact_email);
|
||
$this->assertSame('Wir möchten gern mehr erfahren.', $inquiry->message);
|
||
$this->assertSame(InquiryStatus::Neu, $inquiry->status);
|
||
|
||
$answers = collect($inquiry->answers)->keyBy('key');
|
||
$this->assertSame('Schul-AG', $answers['kursformat']['display']);
|
||
$this->assertSame('Welches Format interessiert Sie?', $answers['kursformat']['label']);
|
||
$this->assertSame('Ihre Kontaktdaten', $answers['email']['group']);
|
||
$this->assertArrayNotHasKey('einrichtung_name', $answers->all());
|
||
}
|
||
|
||
public function test_values_of_fields_that_are_not_relevant_are_never_stored(): void
|
||
{
|
||
// Besucher hat erst „konkretes Vorhaben“ ausgefüllt, dann auf „Information“ gewechselt: Schritt 2 entfällt.
|
||
$values = $this->infoRequest() + [
|
||
'einrichtung_name' => 'Altschule',
|
||
'einrichtungstyp' => 'grundschule',
|
||
'anzahl_kinder' => '24',
|
||
];
|
||
|
||
$this->send($this->entry('kurse_workshops'), $values);
|
||
|
||
$keys = collect(Inquiry::query()->firstOrFail()->answers)->pluck('key')->all();
|
||
$this->assertNotContains('einrichtung_name', $keys);
|
||
$this->assertNotContains('anzahl_kinder', $keys);
|
||
$this->assertContains('region', $keys);
|
||
}
|
||
|
||
public function test_a_concrete_course_request_needs_the_additional_planning_details_and_shows_follow_up_fields(): void
|
||
{
|
||
$values = [
|
||
'anliegen' => 'kurse', 'unteranliegen' => 'kursvorhaben', 'kursformat' => 'schul_ag',
|
||
'einrichtung_name' => 'Grundschule Süd', 'einrichtungstyp' => 'grundschule',
|
||
'einrichtung_strasse' => 'Schulweg 1', 'einrichtung_plz' => '30159', 'einrichtung_ort' => 'Hannover',
|
||
'traeger_abweichend' => 'nein', 'ort_abweichend' => 'nein',
|
||
'alter' => '8 bis 10 Jahre', 'anzahl_kinder' => '20',
|
||
'vorname' => 'Mira', 'nachname' => 'Beispiel', 'email' => 'mira@example.org',
|
||
];
|
||
|
||
$entry = $this->entry('schul_ag');
|
||
|
||
// Fehlt etwas Bedingtes, wird es angemahnt: Schulart bei Grundschule, Laufzeit und Häufigkeit bei Schul-AG.
|
||
try {
|
||
$this->send($entry, $values);
|
||
$this->fail('Erwartet: Zurückweisung wegen bedingter Pflichtfelder.');
|
||
} catch (SubmissionException $e) {
|
||
$this->assertEqualsCanonicalizing(['schulart', 'laufzeit', 'haeufigkeit'], array_keys($e->fields));
|
||
}
|
||
|
||
$this->assertSame(0, Inquiry::query()->count());
|
||
|
||
$values += ['schulart' => 'gs', 'laufzeit' => 'halbjahr', 'haeufigkeit' => 'woechentlich'];
|
||
$this->send($entry, $values);
|
||
|
||
$answers = collect(Inquiry::query()->firstOrFail()->answers)->keyBy('key');
|
||
$this->assertSame('Ein Schulhalbjahr', $answers['laufzeit']['display']);
|
||
$this->assertArrayNotHasKey('traeger_name', $answers->all());
|
||
$this->assertSame('Ihr Vorhaben', $answers['alter']['step']);
|
||
}
|
||
|
||
public function test_a_deviating_provider_and_venue_make_their_fields_required(): void
|
||
{
|
||
$values = [
|
||
'anliegen' => 'kurse', 'unteranliegen' => 'kursvorhaben', 'kursformat' => 'kompakt',
|
||
'einrichtung_name' => 'Verein X', 'einrichtungstyp' => 'verein',
|
||
'einrichtung_strasse' => 'Weg 2', 'einrichtung_plz' => '30159', 'einrichtung_ort' => 'Hannover',
|
||
'traeger_abweichend' => 'ja', 'ort_abweichend' => 'ja',
|
||
'alter' => '9', 'anzahl_kinder' => '12',
|
||
'vorname' => 'A', 'nachname' => 'B', 'email' => 'a@example.org',
|
||
];
|
||
|
||
try {
|
||
$this->send($this->entry('kurse_workshops'), $values);
|
||
$this->fail('Erwartet: Zurückweisung.');
|
||
} catch (SubmissionException $e) {
|
||
$this->assertEqualsCanonicalizing(['traeger_name', 'ort_strasse', 'ort_plz', 'ort_ort'], array_keys($e->fields));
|
||
}
|
||
}
|
||
|
||
public function test_manipulated_issues_options_and_unknown_fields_are_rejected(): void
|
||
{
|
||
$entry = $this->entry('allgemeiner_kontakt');
|
||
$good = ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo'];
|
||
|
||
foreach ([
|
||
['anliegen' => 'kurse'] + $good, // Anliegen außerhalb des Einstiegs (nur „allgemein“ zugelassen)
|
||
$good + ['unbekannt' => 'x'], // Feld, das es nicht gibt
|
||
['email' => 'kein-mail'] + $good, // ungültiger Wert
|
||
] as $values) {
|
||
try {
|
||
$this->send($entry, $values);
|
||
$this->fail('Erwartet: Zurückweisung für '.json_encode($values));
|
||
} catch (SubmissionException $e) {
|
||
$this->assertSame(422, $e->getCode());
|
||
}
|
||
}
|
||
|
||
$course = $this->entry('kurse_workshops');
|
||
try {
|
||
$this->send($course, ['kursformat' => 'gibt_es_nicht'] + $this->infoRequest());
|
||
$this->fail('Erwartet: Zurückweisung eines nicht angebotenen Auswahlwerts.');
|
||
} catch (SubmissionException $e) {
|
||
$this->assertArrayHasKey('kursformat', $e->fields);
|
||
}
|
||
|
||
$this->assertSame(0, Inquiry::query()->count());
|
||
}
|
||
|
||
public function test_a_single_allowed_issue_is_not_asked_but_still_recorded(): void
|
||
{
|
||
$this->send($this->entry('allgemeiner_kontakt'), ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo']);
|
||
|
||
$inquiry = Inquiry::query()->firstOrFail();
|
||
$this->assertSame('allgemein', $inquiry->issue_key);
|
||
$this->assertNull($inquiry->subissue_key);
|
||
}
|
||
|
||
public function test_a_second_submission_with_the_same_token_creates_no_second_inquiry(): void
|
||
{
|
||
$entry = $this->entry('allgemeiner_kontakt');
|
||
$token = FormToken::issue($entry->id, $entry->template->published_version_id);
|
||
$payload = ['token' => $token, 'values' => ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo']];
|
||
|
||
$first = app(InquiryService::class)->submit($entry, $payload);
|
||
$second = app(InquiryService::class)->submit($entry, $payload);
|
||
|
||
$this->assertTrue($first['created']);
|
||
$this->assertFalse($second['created']);
|
||
$this->assertSame($first['reference'], $second['reference']);
|
||
$this->assertSame(1, Inquiry::query()->count());
|
||
}
|
||
|
||
public function test_bots_are_caught_by_the_decoy_field_and_by_a_too_fast_submission(): void
|
||
{
|
||
$entry = $this->entry('allgemeiner_kontakt');
|
||
$values = ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo'];
|
||
|
||
$result = $this->send($entry, $values, ['website' => 'http://spam.example']);
|
||
$this->assertFalse($result['created']);
|
||
$this->assertSame(0, Inquiry::query()->count());
|
||
|
||
config(['communication.min_fill_seconds' => 3]);
|
||
|
||
try {
|
||
$this->send($entry, $values);
|
||
$this->fail('Erwartet: zu schnelle Absendung.');
|
||
} catch (SubmissionException $e) {
|
||
$this->assertSame(429, $e->getCode());
|
||
}
|
||
|
||
$this->travel(5)->seconds();
|
||
$token = FormToken::issue($entry->id, $entry->template->published_version_id);
|
||
$this->travel(5)->seconds();
|
||
$this->assertTrue(app(InquiryService::class)->submit($entry, ['token' => $token, 'values' => $values])['created']);
|
||
}
|
||
|
||
public function test_a_forged_or_foreign_token_is_refused(): void
|
||
{
|
||
$entry = $this->entry('allgemeiner_kontakt');
|
||
$other = $this->entry('kooperation');
|
||
|
||
foreach (['x', '', FormToken::issue($other->id, $other->template->published_version_id)] as $token) {
|
||
try {
|
||
app(InquiryService::class)->submit($entry, ['token' => $token, 'values' => ['name' => 'a', 'email' => 'a@example.org', 'nachricht' => 'b']]);
|
||
$this->fail('Erwartet: Token abgelehnt.');
|
||
} catch (SubmissionException $e) {
|
||
$this->assertSame(419, $e->getCode());
|
||
}
|
||
}
|
||
}
|
||
|
||
public function test_the_internal_notification_is_sent_to_the_recipients_without_personal_data(): void
|
||
{
|
||
Mail::fake();
|
||
CommunicationSettings::current()->update(['recipients' => ['team@example.org']]);
|
||
|
||
$this->send($this->entry('allgemeiner_kontakt'), ['name' => 'Mira Geheim', 'email' => 'mira@example.org', 'nachricht' => 'Vertraulich']);
|
||
|
||
Mail::assertSent(InquiryReceived::class, function (InquiryReceived $mail): bool {
|
||
$text = $mail->render();
|
||
|
||
return $mail->hasTo('team@example.org')
|
||
&& ! str_contains($text, 'Mira Geheim')
|
||
&& ! str_contains($text, 'mira@example.org')
|
||
&& ! str_contains($text, 'Vertraulich')
|
||
&& str_contains($text, $mail->inquiry->reference);
|
||
});
|
||
|
||
$this->assertNotNull(Inquiry::query()->firstOrFail()->notified_at);
|
||
}
|
||
|
||
public function test_a_failing_mail_server_does_not_lose_the_inquiry(): void
|
||
{
|
||
CommunicationSettings::current()->update(['recipients' => ['team@example.org']]);
|
||
Mail::shouldReceive('to')->andThrow(new \RuntimeException('smtp down'));
|
||
|
||
$result = $this->send($this->entry('allgemeiner_kontakt'), ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo']);
|
||
|
||
$this->assertTrue($result['created']);
|
||
$inquiry = Inquiry::query()->firstOrFail();
|
||
$this->assertNull($inquiry->notified_at);
|
||
$this->assertStringContainsString('smtp down', (string) $inquiry->notification_error);
|
||
}
|
||
|
||
public function test_missing_recipients_are_recorded_instead_of_silently_dropped(): void
|
||
{
|
||
$this->send($this->entry('allgemeiner_kontakt'), ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo']);
|
||
|
||
$this->assertStringContainsString('keine Empfänger', (string) Inquiry::query()->firstOrFail()->notification_error);
|
||
}
|
||
|
||
public function test_changing_the_catalog_or_republishing_never_reinterprets_an_old_inquiry(): void
|
||
{
|
||
$entry = $this->entry('allgemeiner_kontakt');
|
||
$this->send($entry, ['name' => 'Mira', 'email' => 'mira@example.org', 'nachricht' => 'Hallo']);
|
||
|
||
InquiryField::query()->where('key', 'nachricht')->update(['label' => 'Etwas ganz anderes']);
|
||
$template = $entry->template;
|
||
app(Publisher::class)->publishTemplate($template, null, 'Neue Bezeichnung');
|
||
|
||
$inquiry = Inquiry::query()->firstOrFail();
|
||
$label = collect($inquiry->answers)->firstWhere('key', 'nachricht')['label'];
|
||
$this->assertSame('Ihre Nachricht', $label);
|
||
$this->assertSame(1, $inquiry->templateVersion->version);
|
||
$this->assertSame(2, $template->fresh()->publishedVersion->version);
|
||
}
|
||
|
||
public function test_a_draft_stays_invisible_until_the_template_is_deliberately_published(): void
|
||
{
|
||
$entry = $this->entry('allgemeiner_kontakt');
|
||
$template = $entry->template;
|
||
$version = $template->publishedVersion;
|
||
|
||
$draft = $template->draft;
|
||
$draft['texts']['heading'] = 'Ganz neue Überschrift';
|
||
$template->update(['draft' => $draft]);
|
||
|
||
$this->assertTrue($template->fresh()->hasUnpublishedChanges());
|
||
$delivered = app(InquiryService::class)->deliver($entry->fresh());
|
||
$this->assertSame('Schreiben Sie uns', $delivered['form']['texts']['heading']);
|
||
|
||
app(Publisher::class)->publishTemplate($template);
|
||
$delivered = app(InquiryService::class)->deliver($entry->fresh());
|
||
$this->assertSame('Ganz neue Überschrift', $delivered['form']['texts']['heading']);
|
||
|
||
// Rückkehr zur früheren Fassung.
|
||
app(Publisher::class)->restoreVersion($template->fresh(), $version);
|
||
$delivered = app(InquiryService::class)->deliver($entry->fresh());
|
||
$this->assertSame('Schreiben Sie uns', $delivered['form']['texts']['heading']);
|
||
}
|
||
|
||
public function test_published_versions_are_immutable(): void
|
||
{
|
||
$version = $this->entry('allgemeiner_kontakt')->template->publishedVersion;
|
||
|
||
$version->definition = ['v' => 1];
|
||
$this->assertFalse($version->save());
|
||
$this->assertFalse($version->delete());
|
||
}
|
||
|
||
public function test_an_entry_can_only_be_published_against_a_published_template_and_a_fitting_configuration(): void
|
||
{
|
||
$template = InquiryTemplate::query()->create(['key' => 'neu', 'name' => 'Neu', 'draft' => InquiryTemplate::emptyDraft()]);
|
||
$entry = InquiryEntry::query()->create(['key' => 'x', 'name' => 'X', 'template_id' => $template->id, 'config' => InquiryEntry::emptyConfig()]);
|
||
|
||
try {
|
||
app(Publisher::class)->publishEntry($entry);
|
||
$this->fail('Erwartet: Vorlage nicht veröffentlicht.');
|
||
} catch (ValidationException $e) {
|
||
$this->assertStringContainsString('noch nicht veröffentlicht', $e->errors()['config'][0]);
|
||
}
|
||
|
||
$good = $this->entry('kurse_workshops');
|
||
$good->config = array_merge((array) $good->config, ['defaults' => ['kursformat' => 'gibt_es_nicht']]);
|
||
$this->expectException(ValidationException::class);
|
||
app(Publisher::class)->publishEntry($good);
|
||
}
|
||
|
||
public function test_publishing_a_template_is_blocked_when_the_draft_is_invalid_and_names_the_problems(): void
|
||
{
|
||
$template = $this->entry('kurse_workshops')->template;
|
||
$draft = $template->draft;
|
||
// Bedingung verweist auf ein Feld, das erst nach dem Ziel steht.
|
||
$draft['rules'][] = ['effect' => 'show', 'target' => 'field:kursformat', 'match' => 'all', 'conditions' => [['source' => 'einrichtungstyp', 'op' => 'eq', 'values' => ['verein']]], 'value' => null];
|
||
$template->update(['draft' => $draft]);
|
||
|
||
try {
|
||
app(Publisher::class)->publishTemplate($template);
|
||
$this->fail('Erwartet: Zurückweisung.');
|
||
} catch (ValidationException $e) {
|
||
$this->assertStringContainsString('vor ihrem Ziel stehen', implode(' ', $e->errors()['draft']));
|
||
}
|
||
|
||
$this->assertSame(1, InquiryTemplateVersion::query()->where('template_id', $template->id)->count());
|
||
}
|
||
|
||
public function test_the_evaluator_shows_groups_and_skips_steps_from_the_choice(): void
|
||
{
|
||
$entry = $this->entry('kurse_workshops');
|
||
$delivered = app(InquiryService::class)->deliver($entry);
|
||
$form = $delivered['form'];
|
||
$evaluator = app(FormEvaluator::class);
|
||
|
||
$info = $evaluator->evaluate($form, ['anliegen' => 'kurse', 'unteranliegen' => 'kursinfo']);
|
||
$this->assertFalse($info['steps']['skurs2']);
|
||
$this->assertFalse($info['fields']['einrichtung_name']);
|
||
$this->assertTrue($info['fields']['region']);
|
||
|
||
$plan = $evaluator->evaluate($form, ['anliegen' => 'kurse', 'unteranliegen' => 'kursvorhaben', 'kursformat' => 'schul_ag', 'ort_abweichend' => 'ja']);
|
||
$this->assertTrue($plan['steps']['skurs2']);
|
||
$this->assertTrue($plan['groups']['schul_ag']);
|
||
$this->assertTrue($plan['groups']['ort_angaben']);
|
||
$this->assertFalse($plan['groups']['traeger_angaben']);
|
||
$this->assertFalse($plan['fields']['region']);
|
||
|
||
// Wechsel des Formats blendet die Zusatzfragen wieder aus.
|
||
$other = $evaluator->evaluate($form, ['anliegen' => 'kurse', 'unteranliegen' => 'kursvorhaben', 'kursformat' => 'kompakt']);
|
||
$this->assertFalse($other['groups']['schul_ag']);
|
||
}
|
||
|
||
public function test_an_entry_can_limit_the_offered_issues_and_preselect_values(): void
|
||
{
|
||
$factory = app(FormFactory::class);
|
||
$definition = $this->entry('kurse_workshops')->template->publishedVersion->definition;
|
||
|
||
$form = $factory->effective($definition, ['allowed_subissues' => ['kursvorhaben'], 'defaults' => ['kursformat' => 'schul_ag', 'anliegen' => 'kurse', 'nonsens' => 'x']]);
|
||
|
||
$this->assertSame(['kursvorhaben'], array_column($form['issues'][0]['children'], 'key'));
|
||
$this->assertEquals(['kursformat' => 'schul_ag', 'anliegen' => 'kurse'], $form['defaults']);
|
||
$this->assertContains('unteranliegen', $form['controls']);
|
||
}
|
||
|
||
public function test_the_example_schul_ag_entry_preselects_the_format(): void
|
||
{
|
||
$delivered = app(InquiryService::class)->deliver($this->entry('schul_ag'));
|
||
|
||
$this->assertSame('schul_ag', $delivered['form']['defaults']['kursformat']);
|
||
$this->assertSame('wizard', $delivered['form']['mode']);
|
||
$this->assertSame('Schul-AG anfragen', $delivered['form']['texts']['heading']);
|
||
}
|
||
|
||
public function test_a_withdrawn_or_archived_entry_is_not_delivered(): void
|
||
{
|
||
$entry = $this->entry('kooperation');
|
||
app(Publisher::class)->withdrawEntry($entry);
|
||
|
||
$this->assertNull(app(InquiryService::class)->deliver($entry->fresh()));
|
||
}
|
||
}
|