0]); Queue::fake(); app(ExampleConfiguration::class)->install(); } private function template(string $key): InquiryTemplate { return InquiryTemplate::query()->where('key', $key)->firstOrFail(); } /** * @param list> $reasons * @param array $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 */ 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']); } }