composition('Programm Abstand', 'programm'); $editor = $this->editor(); Livewire::actingAs($editor, 'staff') ->test(CreatePage::class) ->fillForm([ 'path' => '/themen-test', 'seo_title' => 'Themenseite', 'blocks' => $this->keyed([ ['type' => 'hero', 'data' => ['variant' => 'highlight', 'data' => ['heading' => 'Stark im Alltag', 'text' => 'Einstieg']]], ['type' => 'image_text', 'data' => ['variant' => 'image_right', 'data' => ['title' => 'Worum es geht', 'body' => '

Text mit Betonung

'], 'refs' => []]], ['type' => 'cards', 'data' => ['variant' => 'compact', 'data' => ['items' => $this->keyed([['title' => 'Prävention', 'url' => '/praevention']])]]], ['type' => 'catalog_cards', 'data' => ['variant' => 'standard', 'data' => ['heading' => 'Programme'], 'refs' => ['items' => $this->keyed([['type' => 'composition', 'id' => $program->composition_id]])]]], ]), ]) ->call('create') ->assertHasNoFormErrors(); $page = Page::query()->where('path', '/themen-test')->firstOrFail(); $blocks = $page->latestRevision->blocks; $this->assertNull($page->published_revision_id); $this->assertSame(['hero', 'image_text', 'cards', 'catalog_cards'], array_column($blocks, 'type')); $this->assertSame(['highlight', 'image_right', 'compact', 'standard'], array_column($blocks, 'variant')); $this->assertSame([2], array_values(array_unique([$blocks[1]['v']]))); $this->assertStringContainsString('Betonung', $blocks[1]['data']['body']); $this->assertSame([['title' => 'Prävention', 'text' => null, 'url' => '/praevention', 'image' => null]], $blocks[2]['data']['items']); $this->assertSame([['type' => 'composition', 'id' => $program->composition_id]], $blocks[3]['refs']['items']); $this->get('/themen-test')->assertNotFound(); } public function test_existing_page_opens_in_the_editor_and_saves_without_changing_the_live_page(): void { $page = Page::query()->where('path', '/')->firstOrFail(); $live = $page->published_revision_id; $before = $this->mainText($this->get('/')->getContent()); $component = Livewire::actingAs($this->editor(), 'staff') ->test(EditPage::class, ['record' => $page->id]); $state = $component->get('data.blocks'); $this->assertSame(['heading', 'prose', 'cards', 'call_to_action'], array_column(array_values($state), 'type')); $component->call('save')->assertHasNoFormErrors(); $page->refresh(); $this->assertSame($live, $page->published_revision_id); $this->assertNotSame($live, $page->latestRevision->id); $this->assertSame(['heading', 'prose', 'cards', 'call_to_action'], array_column($page->latestRevision->blocks, 'type')); $this->assertSame($before, $this->mainText($this->get('/')->getContent())); Livewire::actingAs($this->editor(), 'staff') ->test(EditPage::class, ['record' => $page->id]) ->callAction('publish'); $this->assertSame( trim(file_get_contents(base_path('tests/Fixtures/legacy-pages/home.txt'))), $this->mainText($this->get('/')->getContent()), 'Nach dem Speichern im neuen Format zeigt die Seite denselben Text.', ); } public function test_editor_reorders_and_duplicates_sections(): void { $page = Page::query()->where('path', '/')->firstOrFail(); $component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]); $state = $component->get('data.blocks'); $keys = array_keys($state); $reordered = [$keys[3] => $state[$keys[3]], $keys[0] => $state[$keys[0]], $keys[1] => $state[$keys[1]], $keys[2] => $state[$keys[2]]]; $copy = $state[$keys[1]]; $reordered[(string) Str::uuid()] = $copy; $component->set('data.blocks', $reordered)->call('save')->assertHasNoFormErrors(); $blocks = $page->fresh()->latestRevision->blocks; $this->assertSame(['call_to_action', 'heading', 'prose', 'cards', 'prose'], array_column($blocks, 'type')); $this->assertCount(5, array_unique(array_column($blocks, 'id')), 'Ein dupliziertes Widget erhält eine eigene Kennung.'); } public function test_unsafe_link_is_reported_at_the_section_and_nothing_is_saved(): void { $page = Page::query()->where('path', '/')->firstOrFail(); $revisions = $page->revisions()->count(); $component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]); $key = array_keys($component->get('data.blocks'))[3]; $component->set("data.blocks.$key.data.data.url", 'javascript:alert(1)') ->call('save') ->assertHasErrors(["data.blocks.$key.data.data.url"]); $this->assertSame($revisions, $page->revisions()->count()); } public function test_invalid_or_taken_paths_are_shown_at_the_path_field(): void { Livewire::actingAs($this->editor(), 'staff') ->test(CreatePage::class) ->fillForm(['path' => '/praevention', 'blocks' => []]) ->call('create') ->assertHasErrors(['data.path']); Livewire::actingAs($this->editor(), 'staff') ->test(CreatePage::class) ->fillForm(['path' => 'C:/Program Files/x', 'blocks' => []]) ->call('create') ->assertHasErrors(['data.path']); $this->assertSame(1, Page::query()->where('path', '/praevention')->count()); } public function test_block_preview_uses_the_public_widget_view_with_unsaved_state(): void { $html = WidgetEditor::renderPreview('image_text', [ 'variant' => 'image_right', 'data' => [ 'title' => 'Ungespeichert', 'body' => ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => 'Vorschautext']]]]], ], 'refs' => ['image' => null, '_upload' => []], ]); $this->assertStringContainsString('image-text image-text-right', $html); $this->assertStringContainsString('

Ungespeichert

', $html); $this->assertStringContainsString('Vorschautext', $html); $cards = WidgetEditor::renderPreview('cards', [ 'variant' => 'standard', 'data' => ['items' => [(string) Str::uuid() => ['title' => 'Karte A'], (string) Str::uuid() => ['title' => 'Karte B']]], ]); $this->assertStringContainsString('Karte A', $cards); $this->assertStringContainsString('Karte B', $cards); } public function test_editor_offers_every_registered_widget_with_description(): void { $page = Page::query()->where('path', '/')->firstOrFail(); Livewire::actingAs($this->editor(), 'staff') ->test(EditPage::class, ['record' => $page->id]) ->assertSee('Einstieg (Hero)') ->assertSee('Programm- und Modulkarten') ->assertSee('Titel und Beschreibung kommen automatisch aus den Fachdaten') ->assertSee('Entwurf speichern'); } public function test_preview_with_device_widths_is_only_available_to_staff(): void { $revision = Page::query()->where('path', '/')->firstOrFail()->latestRevision; $this->get(route('pages.preview.devices', $revision))->assertRedirect(); $this->actingAs(User::factory()->create())->get(route('pages.preview', $revision))->assertRedirect(); $this->actingAs(User::factory()->create(), 'staff')->get(route('pages.preview', $revision))->assertForbidden(); auth('web')->logout(); $this->actingAs($this->editor(), 'staff') ->get(route('pages.preview.devices', $revision)) ->assertOk() ->assertSee('data-width="390"', false) ->assertSee('data-width="768"', false) ->assertSee('data-width="1440"', false) ->assertSee(route('pages.preview', $revision), false); $this->actingAs($this->editor(), 'staff') ->get(route('pages.preview', $revision)) ->assertOk() ->assertSee('Vorschau einer unveröffentlichten Revision') ->assertSee('Registrieren') ->assertDontSee('Mitgliederbereich', false); } public function test_navigation_items_can_be_edited_hidden_reordered_and_removed(): void { $editor = $this->editor(); $start = NavigationItem::query()->where('label', 'Start')->firstOrFail(); $about = NavigationItem::query()->where('label', 'Über das Projekt')->firstOrFail(); Livewire::actingAs($editor, 'staff') ->test(ManageNavigationItems::class) ->assertCanSeeTableRecords([$start, $about]) ->callAction(TestAction::make('edit')->table($about), ['label' => 'Über uns']) ->assertHasNoFormErrors() ->callAction(TestAction::make('toggleVisibility')->table($start)); $this->assertSame('Über uns', $about->fresh()->label); $this->assertFalse($start->fresh()->is_visible); $this->get('/praevention')->assertSee('Über uns')->assertDontSee('>Start<', false); $order = NavigationItem::query()->orderByDesc('sort_order')->pluck('id')->map(fn ($id) => (string) $id)->all(); Livewire::actingAs($editor, 'staff')->test(ManageNavigationItems::class)->call('reorderTable', $order); $this->assertSame((int) $order[0], NavigationItem::query()->orderBy('sort_order')->first()->id); Livewire::actingAs($editor, 'staff') ->test(ManageNavigationItems::class) ->callAction(TestAction::make('delete')->table($about)); $this->assertNull($about->fresh()); $this->assertNotNull(Page::query()->where('path', '/ueber')->first(), 'Die Seite bleibt bestehen.'); } public function test_navigation_rejects_script_addresses(): void { Livewire::actingAs($this->editor(), 'staff') ->test(ManageNavigationItems::class) ->callAction('create', ['label' => 'Böse', 'placement' => 'header', 'url' => 'javascript:alert(1)', 'sort_order' => 9, 'is_visible' => true]) ->assertHasFormErrors(['url']); NavigationItem::query()->create(['label' => 'Alt', 'url' => 'javascript:alert(1)', 'placement' => 'footer', 'sort_order' => 99]); $this->get('/praevention')->assertDontSee('javascript:alert(1)', false); } private function editor(): User { $editor = User::factory()->redakteur()->create(); $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); return $editor; } /** * @param list> $items * @return array> */ private function keyed(array $items): array { $keyed = []; foreach ($items as $item) { $keyed[(string) Str::uuid()] = $item; } return $keyed; } private function mainText(string $html): string { preg_match('#]*>(.*)#s', $html, $match); $text = html_entity_decode(strip_tags(str_replace('<', ' <', $match[1] ?? '')), ENT_QUOTES | ENT_HTML5, 'UTF-8'); return trim(preg_replace('/\s+/u', ' ', $text)); } }