From 6172364bf8274eb2dcd20f640cd8211bedaf0c71 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 27 Nov 2026 07:00:57 +0100 Subject: [PATCH] =?UTF-8?q?Biete=20in=20allen=20Widget-Linkfeldern=20einen?= =?UTF-8?q?=20Dialog=20f=C3=BCr=20interne=20Seiten=20und=20Sprungmarken=20?= =?UTF-8?q?an?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zweistufig (Seite, dann Sprungmarke) mit Veröffentlichungszustand und den noch ungespeicherten Marken der bearbeiteten Seite. Schreibt nur die Adresse ins Feld. Co-Authored-By: Claude Sonnet 5.5 --- app/Domain/Cms/LinkTargets.php | 131 +++++++++++ app/Domain/Cms/Widgets/Support/LinkField.php | 30 +++ .../Cms/Widgets/Support/PremiumFields.php | 5 +- .../Cms/Widgets/Types/CallToActionWidget.php | 8 +- app/Domain/Cms/Widgets/Types/CardsWidget.php | 8 +- .../Cms/Widgets/Types/FeaturesWidget.php | 8 +- .../Resources/Pages/Pages/CreatePage.php | 2 + .../Resources/Pages/Pages/EditPage.php | 2 + app/Filament/Support/ChoosesLinkTargets.php | 215 ++++++++++++++++++ .../views/filament/cms/bild-picker.blade.php | 1 + .../views/filament/cms/link-picker.blade.php | 104 +++++++++ resources/views/filament/cms/styles.blade.php | 1 + tests/Feature/CmsLinkPickerTest.php | 210 +++++++++++++++++ 13 files changed, 706 insertions(+), 19 deletions(-) create mode 100644 app/Domain/Cms/LinkTargets.php create mode 100644 app/Domain/Cms/Widgets/Support/LinkField.php create mode 100644 app/Filament/Support/ChoosesLinkTargets.php create mode 100644 resources/views/filament/cms/link-picker.blade.php create mode 100644 tests/Feature/CmsLinkPickerTest.php diff --git a/app/Domain/Cms/LinkTargets.php b/app/Domain/Cms/LinkTargets.php new file mode 100644 index 0000000..0052e1b --- /dev/null +++ b/app/Domain/Cms/LinkTargets.php @@ -0,0 +1,131 @@ + + */ + public function pages(string $search = '', ?int $exceptId = null): array + { + $needle = mb_strtolower(trim($search)); + + return Page::query()->with(['latestRevision', 'publishedRevision'])->orderBy('path')->get() + ->reject(fn (Page $page): bool => $page->getKey() === $exceptId) + ->map(fn (Page $page): array => [ + 'id' => (int) $page->getKey(), + 'titel' => $page->latestRevision?->contentTitle() ?: $page->path, + 'pfad' => $page->path, + 'zustand' => $this->state($page), + 'marken' => count($this->anchorsOf($page->latestRevision?->sections() ?? [])), + ]) + ->filter(fn (array $page): bool => $needle === '' || str_contains(mb_strtolower($page['titel'].' '.$page['pfad']), $needle)) + ->values() + ->all(); + } + + /** + * @return array{label: string, ton: string} + */ + public function state(Page $page): array + { + if ($page->published_revision_id === null) { + return ['label' => 'Nur Entwurf, öffentlich nicht erreichbar', 'ton' => 'offen']; + } + + return $page->latestRevision !== null && $page->latestRevision->id !== $page->published_revision_id + ? ['label' => 'Veröffentlicht · Entwurf neuer', 'ton' => 'gut'] + : ['label' => 'Veröffentlicht', 'ton' => 'gut']; + } + + /** + * Sprungmarken einer gespeicherten Seite: die des neuesten Entwurfs, je mit dem Hinweis, ob sie schon + * veröffentlicht sind und ob der Abschnitt ausgeblendet ist. + * + * @return list + */ + public function anchorsForPage(Page $page): array + { + $published = collect($this->anchorsOf($page->publishedRevision?->sections() ?? [])) + ->filter(fn (array $anchor): bool => ! $anchor['hidden']) + ->pluck('anchor') + ->all(); + + return array_map( + fn (array $anchor): array => $anchor + ['published' => in_array($anchor['anchor'], $published, true)], + $this->anchorsOf($page->latestRevision?->sections() ?? $page->publishedRevision?->sections() ?? []), + ); + } + + /** + * Sprungmarken der Abschnitte im Editor, auch ungespeicherte. Für „diese Seite“. + * + * @param list> $sections Abschnitte im Modell (nach SectionEditor::fromEditorState) + * @return list + */ + public function anchorsInEditor(array $sections): array + { + return array_map(fn (array $anchor): array => $anchor + ['published' => false], $this->anchorsOf($sections)); + } + + /** + * @param list> $sections + * @return list + */ + public function anchorsOf(array $sections): array + { + $anchors = []; + + foreach (array_values($sections) as $position => $section) { + $anchor = PageSections::cleanAnchor($section['anchor'] ?? null); + + if ($anchor === null) { + continue; + } + + $name = PageSections::cleanName($section['name'] ?? null); + $anchors[] = [ + 'anchor' => $anchor, + 'label' => $name ?? 'Abschnitt '.($position + 1), + 'hidden' => PageSections::isHidden($section['hidden'] ?? false), + ]; + } + + return $anchors; + } + + /** + * Adresse für ein Linkfeld: „/pfad“, „/pfad#marke“ oder (dieselbe Seite) „#marke“. + */ + public function href(?string $path, ?string $anchor): ?string + { + $anchor = $anchor !== null && $anchor !== '' ? PageSections::cleanAnchor($anchor) : null; + + if ($path === null) { + return $anchor !== null ? '#'.$anchor : null; + } + + return $path.($anchor !== null ? '#'.$anchor : ''); + } + + /** + * Vollständige öffentliche Adresse mit Sprungmarke, für Verweise von außen. + */ + public function publicUrl(Page $page, ?string $anchor = null): string + { + return $page->publicUrl().($anchor !== null && $anchor !== '' ? '#'.$anchor : ''); + } +} diff --git a/app/Domain/Cms/Widgets/Support/LinkField.php b/app/Domain/Cms/Widgets/Support/LinkField.php new file mode 100644 index 0000000..9bb6c63 --- /dev/null +++ b/app/Domain/Cms/Widgets/Support/LinkField.php @@ -0,0 +1,30 @@ +label($label) + ->maxLength(500) + ->rule(SafeUrl::rule()) + ->suffixAction(Action::make('linkWaehlen') + ->label('Interne Seite oder Sprungmarke wählen') + ->tooltip('Interne Seite oder Sprungmarke wählen') + ->icon('heroicon-o-link') + ->visible(fn ($livewire): bool => is_object($livewire) && method_exists($livewire, 'openLinkPicker')) + ->actionJs(fn ($component): string => '$wire.openLinkPicker('.Js::from($component->getStatePath()).')')); + } +} diff --git a/app/Domain/Cms/Widgets/Support/PremiumFields.php b/app/Domain/Cms/Widgets/Support/PremiumFields.php index ca65e38..a8be55a 100644 --- a/app/Domain/Cms/Widgets/Support/PremiumFields.php +++ b/app/Domain/Cms/Widgets/Support/PremiumFields.php @@ -53,10 +53,7 @@ public static function eyebrowField(): TextInput public static function ctaFields(): Grid { - $url = fn (string $name, string $label, string $pair) => TextInput::make($name) - ->label($label) - ->maxLength(500) - ->rule(SafeUrl::rule()) + $url = fn (string $name, string $label, string $pair) => LinkField::make($name, $label) ->requiredWith($pair) ->live(onBlur: true) ->placeholder('/programme'); diff --git a/app/Domain/Cms/Widgets/Types/CallToActionWidget.php b/app/Domain/Cms/Widgets/Types/CallToActionWidget.php index be1b06f..a8d1202 100644 --- a/app/Domain/Cms/Widgets/Types/CallToActionWidget.php +++ b/app/Domain/Cms/Widgets/Types/CallToActionWidget.php @@ -3,6 +3,7 @@ namespace App\Domain\Cms\Widgets\Types; use App\Domain\Cms\Widgets\Support\HeadingAccent; +use App\Domain\Cms\Widgets\Support\LinkField; use App\Domain\Cms\Widgets\Support\SafeUrl; use App\Domain\Cms\Widgets\WidgetErrors; use App\Domain\Cms\Widgets\WidgetType; @@ -82,12 +83,9 @@ public function dataFields(): array TextInput::make('heading')->label('Überschrift')->required()->maxLength(200), Textarea::make('text')->label('Text')->rows(3)->maxLength(1000), TextInput::make('label')->label('Beschriftung der Schaltfläche')->required()->maxLength(80), - TextInput::make('url') - ->label('Linkziel') + LinkField::make('url') ->required() - ->maxLength(500) - ->rule(SafeUrl::rule()) - ->helperText('Seitenpfad wie /angebote oder eine vollständige http(s)-Adresse.'), + ->helperText('Seitenpfad wie /angebote, Sprungmarke wie #kurse oder eine vollständige http(s)-Adresse; oder über das Linksymbol eine Seite oder Sprungmarke wählen.'), ]; } diff --git a/app/Domain/Cms/Widgets/Types/CardsWidget.php b/app/Domain/Cms/Widgets/Types/CardsWidget.php index 0699c47..9ee071d 100644 --- a/app/Domain/Cms/Widgets/Types/CardsWidget.php +++ b/app/Domain/Cms/Widgets/Types/CardsWidget.php @@ -5,6 +5,7 @@ use App\Domain\Cms\Widgets\RenderContext; use App\Domain\Cms\Widgets\Support\HeadingAccent; use App\Domain\Cms\Widgets\Support\ImageRefFields; +use App\Domain\Cms\Widgets\Support\LinkField; use App\Domain\Cms\Widgets\Support\PublicMedia; use App\Domain\Cms\Widgets\Support\SafeUrl; use App\Domain\Cms\Widgets\Support\WebsiteImage; @@ -127,11 +128,8 @@ public function dataFields(): array SymbolField::make('symbol', 'Symbol (optional)') ->helperText('Kleine Grafik aus dem Brand-Kit über dem Titel. Größe und Ausrichtung im Reiter „Darstellung“.'), Textarea::make('text')->label('Beschreibung')->rows(2)->maxLength(500), - TextInput::make('url') - ->label('Linkziel') - ->maxLength(500) - ->rule(SafeUrl::rule()) - ->helperText('Seitenpfad wie /praevention oder eine http(s)-Adresse. Leer lassen für eine Karte ohne Link.'), + LinkField::make('url') + ->helperText('Seitenpfad wie /praevention, Sprungmarke wie #kurse oder eine http(s)-Adresse; oder über das Linksymbol wählen. Leer lassen für eine Karte ohne Link.'), $image, $upload, $status, diff --git a/app/Domain/Cms/Widgets/Types/FeaturesWidget.php b/app/Domain/Cms/Widgets/Types/FeaturesWidget.php index 47dc19d..5622263 100644 --- a/app/Domain/Cms/Widgets/Types/FeaturesWidget.php +++ b/app/Domain/Cms/Widgets/Types/FeaturesWidget.php @@ -4,6 +4,7 @@ use App\Domain\Cms\Widgets\RenderContext; use App\Domain\Cms\Widgets\Support\HeadingAccent; +use App\Domain\Cms\Widgets\Support\LinkField; use App\Domain\Cms\Widgets\Support\SafeUrl; use App\Domain\Cms\Widgets\WidgetErrors; use App\Domain\Cms\Widgets\WidgetType; @@ -157,11 +158,8 @@ public function dataFields(): array SymbolField::make('symbol', 'Symbol', required: true), TextInput::make('title')->label('Kurztitel')->required()->maxLength(60), Textarea::make('text')->label('Satz')->rows(2)->maxLength(200), - TextInput::make('url') - ->label('Linkziel (optional)') - ->maxLength(500) - ->rule(SafeUrl::rule()) - ->helperText('Seitenpfad wie /materialien oder eine http(s)-Adresse. Leer lassen für ein Merkmal ohne Link.'), + LinkField::make('url', 'Linkziel (optional)') + ->helperText('Seitenpfad wie /materialien, Sprungmarke wie #kurse oder eine http(s)-Adresse; oder über das Linksymbol wählen. Leer lassen für ein Merkmal ohne Link.'), TextInput::make('link_label') ->label('Linktext') ->maxLength(60) diff --git a/app/Filament/Resources/Pages/Pages/CreatePage.php b/app/Filament/Resources/Pages/Pages/CreatePage.php index e1bc637..dbe6499 100644 --- a/app/Filament/Resources/Pages/Pages/CreatePage.php +++ b/app/Filament/Resources/Pages/Pages/CreatePage.php @@ -6,6 +6,7 @@ use App\Domain\Cms\Page; use App\Domain\Cms\PageService; use App\Filament\Resources\Pages\PageResource; +use App\Filament\Support\ChoosesLinkTargets; use App\Filament\Support\ChoosesWebsiteImages; use App\Filament\Support\ReportsSectionErrors; use App\Filament\Support\SectionEditor; @@ -18,6 +19,7 @@ class CreatePage extends CreateRecord { + use ChoosesLinkTargets; use ChoosesWebsiteImages; use ReportsSectionErrors; diff --git a/app/Filament/Resources/Pages/Pages/EditPage.php b/app/Filament/Resources/Pages/Pages/EditPage.php index a480415..f79a3ac 100644 --- a/app/Filament/Resources/Pages/Pages/EditPage.php +++ b/app/Filament/Resources/Pages/Pages/EditPage.php @@ -6,6 +6,7 @@ use App\Domain\Cms\Page; use App\Domain\Cms\PageService; use App\Filament\Resources\Pages\PageResource; +use App\Filament\Support\ChoosesLinkTargets; use App\Filament\Support\ChoosesWebsiteImages; use App\Filament\Support\ReportsSectionErrors; use App\Filament\Support\SectionEditor; @@ -22,6 +23,7 @@ class EditPage extends EditRecord { + use ChoosesLinkTargets; use ChoosesWebsiteImages; use ReportsSectionErrors; diff --git a/app/Filament/Support/ChoosesLinkTargets.php b/app/Filament/Support/ChoosesLinkTargets.php new file mode 100644 index 0000000..7084062 --- /dev/null +++ b/app/Filament/Support/ChoosesLinkTargets.php @@ -0,0 +1,215 @@ +linkZiel = $pfad; + $this->linkSuche = ''; + $this->linkSeite = null; + $this->forceRenderForLinkPicker(); + } + + public function closeLinkPicker(): void + { + $this->linkZiel = null; + $this->linkSeite = null; + $this->linkSuche = ''; + $this->forceRenderForLinkPicker(); + } + + public function chooseLinkPage(string $key): void + { + if ($this->linkZiel === null) { + return; + } + + if ($key === 'diese' ? ! $this->linkHasOwnPage() : ! Page::query()->whereKey((int) $key)->exists()) { + return; + } + + $this->linkSeite = $key; + } + + public function backToLinkPages(): void + { + $this->linkSeite = null; + } + + /** + * @param ?string $anchor Sprungmarke; leer = Seitenanfang (nur für andere Seiten) + */ + public function applyLink(?string $anchor = null): void + { + if ($this->linkZiel === null || $this->linkSeite === null) { + return; + } + + $targets = app(LinkTargets::class); + $anchor = filled($anchor) ? $anchor : null; + + if ($this->linkSeite === 'diese') { + $known = array_column($targets->anchorsInEditor($this->linkEditorSections()), 'anchor'); + + if ($anchor === null || ! in_array($anchor, $known, true)) { + return; + } + + $href = $targets->href(null, $anchor); + } else { + $page = Page::query()->with(['latestRevision', 'publishedRevision'])->find((int) $this->linkSeite); + + if (! $page instanceof Page) { + return; + } + + if ($anchor !== null && ! in_array($anchor, array_column($targets->anchorsForPage($page), 'anchor'), true)) { + return; + } + + $href = $targets->href($page->path, $anchor); + } + + if (! SafeUrl::isSafe($href)) { + return; + } + + data_set($this, $this->linkZiel, $href); + $this->closeLinkPicker(); + } + + /** + * Seiten im ersten Schritt (ohne die bearbeitete Seite, die als „Diese Seite“ oben steht). + * + * @return list> + */ + public function linkPages(): array + { + return app(LinkTargets::class)->pages($this->linkSuche, $this->linkOwnPageId()); + } + + /** + * Daten des zweiten Schritts: gewählte Seite, ihre Sprungmarken und die vollständige Adresse. + * + * @return array{titel: string, pfad: ?string, diese: bool, marken: list>, offen: bool, adresse: ?string}|null + */ + public function linkChosen(): ?array + { + if ($this->linkSeite === null) { + return null; + } + + $targets = app(LinkTargets::class); + + if ($this->linkSeite === 'diese') { + $record = $this->linkOwnPage(); + + return [ + 'titel' => 'Diese Seite', + 'pfad' => $record?->path, + 'diese' => true, + 'marken' => $targets->anchorsInEditor($this->linkEditorSections()), + 'offen' => $record === null, + 'adresse' => $record?->published_revision_id !== null ? $targets->publicUrl($record) : null, + ]; + } + + $page = Page::query()->with(['latestRevision', 'publishedRevision'])->find((int) $this->linkSeite); + + if (! $page instanceof Page) { + return null; + } + + return [ + 'titel' => $page->latestRevision?->contentTitle() ?: $page->path, + 'pfad' => $page->path, + 'diese' => false, + 'marken' => $targets->anchorsForPage($page), + 'offen' => false, + 'adresse' => $page->published_revision_id !== null ? $targets->publicUrl($page) : null, + ]; + } + + /** Hat die Seite im Editor schon Sprungmarken oder eine Adresse? Neue Seiten dürfen auf ihre eigenen Marken zeigen. */ + public function linkHasOwnPage(): bool + { + return property_exists($this, 'data'); + } + + public function linkOwnPage(): ?Page + { + $record = property_exists($this, 'record') ? ($this->record ?? null) : null; + + return $record instanceof Page ? $record : null; + } + + /** + * Sprungmarken der Seite im Editor (auch ungespeicherte). + * + * @return list> + */ + public function linkOwnAnchors(): array + { + return $this->linkHasOwnPage() ? app(LinkTargets::class)->anchorsInEditor($this->linkEditorSections()) : []; + } + + private function linkOwnPageId(): ?int + { + return $this->linkOwnPage()?->getKey(); + } + + /** + * @return list> + */ + private function linkEditorSections(): array + { + $state = is_array($this->data['sections'] ?? null) ? $this->data['sections'] : []; + + return SectionEditor::fromEditorState($state); + } + + /** + * Filament rendert bei offenen Aktionsdialogen nur den Dialogteil neu; der Auswahldialog liegt darüber und braucht + * die ganze Seite. + */ + public function hydrateChoosesLinkTargets(): void + { + if ($this->linkZiel !== null) { + $this->forceRenderForLinkPicker(); + } + } + + private function forceRenderForLinkPicker(): void + { + if (method_exists($this, 'forceRender')) { + $this->forceRender(); + } + } +} diff --git a/resources/views/filament/cms/bild-picker.blade.php b/resources/views/filament/cms/bild-picker.blade.php index e78c834..ab28536 100644 --- a/resources/views/filament/cms/bild-picker.blade.php +++ b/resources/views/filament/cms/bild-picker.blade.php @@ -1,4 +1,5 @@ {{-- Gemeinsamer Picker für Website-Bilder und Videos im CMS (ChoosesWebsiteImages). --}} +@include('filament.cms.link-picker') @if ($this->pickerOpen && in_array($this->pickerPurpose, ['website-bild', 'website-video'], true)) @include('filament.verwaltung.picker') @endif diff --git a/resources/views/filament/cms/link-picker.blade.php b/resources/views/filament/cms/link-picker.blade.php new file mode 100644 index 0000000..7d31d69 --- /dev/null +++ b/resources/views/filament/cms/link-picker.blade.php @@ -0,0 +1,104 @@ +{{-- Dialog „Interne Seite oder Sprungmarke wählen“ (ChoosesLinkTargets): Schritt 1 Seite, Schritt 2 Sprungmarke. Wählen ändert und veröffentlicht nichts. --}} +@if (property_exists($this, 'linkZiel') && $this->linkZiel !== null) + @php + $gewaehlt = $this->linkChosen(); + $seiten = $gewaehlt === null ? $this->linkPages() : []; + $eigene = $this->linkOwnPage(); + $eigeneMarken = $this->linkOwnAnchors(); + @endphp + +@endif diff --git a/resources/views/filament/cms/styles.blade.php b/resources/views/filament/cms/styles.blade.php index 3e78fa8..b8b3cc1 100644 --- a/resources/views/filament/cms/styles.blade.php +++ b/resources/views/filament/cms/styles.blade.php @@ -42,6 +42,7 @@ .luf-sections > .fi-fo-repeater-items > .fi-fo-repeater-item:has(.luf-abschnitt.is-ausgeblendet) { border-left-style: dashed; border-left-color: #c98a00; } .luf-sections > .fi-fo-repeater-items > .fi-fo-repeater-item:has(.luf-abschnitt.is-ausgeblendet) > .fi-fo-repeater-item-content { opacity: .72; } .luf-abschnitt-bezeichnung { margin-bottom: .5rem; } + .luf-link-pfad { font-family: ui-monospace, monospace; font-size: .85em; overflow-wrap: anywhere; } /* Rasterauswahl als grafische Kacheln */ .luf-layout-picker .fi-btn-group { flex-wrap: wrap; gap: .5rem; } diff --git a/tests/Feature/CmsLinkPickerTest.php b/tests/Feature/CmsLinkPickerTest.php new file mode 100644 index 0000000..ec81e9d --- /dev/null +++ b/tests/Feature/CmsLinkPickerTest.php @@ -0,0 +1,210 @@ +redakteur()->create(); + $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + + return $editor; + } + + /** + * @param array $extra + * @return array + */ + private function section(string $heading, array $extra = []): array + { + return $extra + ['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'heading', 'v' => 1, 'data' => ['heading' => $heading]]]]; + } + + /** + * @param list> $sections + */ + private function page(string $path, array $sections, bool $publish = false): Page + { + $service = app(PageService::class); + $page = Page::query()->create(['path' => $path]); + $revision = $service->saveRevision($page, $sections, null, null, null); + + if ($publish) { + $service->publish($page, $revision); + } + + return $page->fresh(); + } + + private function editorFor(Page $page): Testable + { + return Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]); + } + + public function test_the_editor_saves_name_anchor_and_hidden_and_reports_a_duplicate_anchor_at_the_field(): void + { + $page = $this->page('/sprung', [$this->section('A'), $this->section('B')]); + $component = $this->editorFor($page); + $keys = array_keys($component->get('data.sections')); + + $component + ->set("data.sections.{$keys[0]}.name", 'Kursübersicht') + ->set("data.sections.{$keys[0]}.anchor", 'kurse') + ->set("data.sections.{$keys[1]}.hidden", true) + ->call('save') + ->assertHasNoFormErrors(); + + $sections = $page->fresh()->latestRevision->sections(); + $this->assertSame(['Kursübersicht', 'kurse', false], [$sections[0]['name'], $sections[0]['anchor'], $sections[0]['hidden']]); + $this->assertTrue($sections[1]['hidden']); + + $component = $this->editorFor($page->fresh()); + $keys = array_keys($component->get('data.sections')); + $component + ->set("data.sections.{$keys[1]}.anchor", 'kurse') + ->call('save') + ->assertHasFormErrors(["sections.{$keys[1]}.anchor"]); + } + + public function test_a_change_of_the_section_name_counts_as_unsaved(): void + { + $page = $this->page('/stand', [$this->section('A')]); + $component = $this->editorFor($page); + $key = array_key_first($component->get('data.sections')); + + $this->assertFalse($component->instance()->hasUnsavedEditorChanges()); + + $component->set("data.sections.{$key}.name", 'Neu'); + $this->assertTrue($component->instance()->hasUnsavedEditorChanges()); + } + + public function test_the_picker_writes_a_page_with_and_without_anchor_into_the_chosen_field(): void + { + $other = $this->page('/kurse', [$this->section('Kurse', ['name' => 'Anmeldung', 'anchor' => 'anmeldung'])], publish: true); + $own = $this->page('/start', [$this->section('Start')]); + + $component = $this->editorFor($own); + + $component->call('openLinkPicker', 'data.seo_title')->assertSet('linkZiel', 'data.seo_title'); + $component->call('chooseLinkPage', (string) $other->id)->call('applyLink', 'anmeldung') + ->assertSet('data.seo_title', '/kurse#anmeldung') + ->assertSet('linkZiel', null); + + $component->call('openLinkPicker', 'data.seo_title')->call('chooseLinkPage', (string) $other->id)->call('applyLink') + ->assertSet('data.seo_title', '/kurse'); + } + + public function test_the_picker_offers_anchors_of_the_page_in_the_editor_including_unsaved_ones(): void + { + $own = $this->page('/eigene', [$this->section('A'), $this->section('B')]); + $component = $this->editorFor($own); + $keys = array_keys($component->get('data.sections')); + + $component->set("data.sections.{$keys[1]}.anchor", 'noch-nicht-gespeichert'); + $component->call('openLinkPicker', 'data.seo_description')->call('chooseLinkPage', 'diese'); + + $this->assertSame(['noch-nicht-gespeichert'], array_column($component->instance()->linkChosen()['marken'], 'anchor')); + + $component->call('applyLink', 'noch-nicht-gespeichert')->assertSet('data.seo_description', '#noch-nicht-gespeichert'); + } + + public function test_the_picker_refuses_unknown_anchors_and_foreign_targets(): void + { + $other = $this->page('/ziel', [$this->section('A', ['anchor' => 'da'])]); + $component = $this->editorFor($this->page('/quelle', [$this->section('Q')])); + + $component->call('openLinkPicker', 'data.seo_title')->call('chooseLinkPage', (string) $other->id) + ->call('applyLink', 'gibt-es-nicht') + ->assertSet('data.seo_title', null) + ->assertSet('linkZiel', 'data.seo_title'); + + // Kein Formularpfad, in den die Auswahl schreiben darf: der Dialog öffnet sich nicht. + $component->call('closeLinkPicker')->call('openLinkPicker', 'record.path')->assertSet('linkZiel', null); + $component->call('openLinkPicker', 'data.path; evil')->assertSet('linkZiel', null); + } + + public function test_the_page_list_leaves_out_the_page_in_the_editor_and_filters_by_title_and_path(): void + { + $this->page('/kurse', [$this->section('Kursangebot')]); + $own = $this->page('/hier', [$this->section('Hier')]); + + $component = $this->editorFor($own); + $paths = array_column($component->instance()->linkPages(), 'pfad'); + $this->assertContains('/kurse', $paths); + $this->assertNotContains('/hier', $paths); + + $component->set('linkSuche', 'angebot'); + $this->assertSame(['/kurse'], array_column($component->instance()->linkPages(), 'pfad')); + } + + public function test_anchors_of_other_pages_say_whether_they_are_published_or_hidden(): void + { + $service = app(PageService::class); + $page = $this->page('/stand', [$this->section('A', ['anchor' => 'alt']), $this->section('B', ['anchor' => 'versteckt'])], publish: true); + $service->saveRevision($page, [ + $this->section('A', ['anchor' => 'alt']), + $this->section('B', ['anchor' => 'versteckt', 'hidden' => true]), + $this->section('C', ['anchor' => 'neu', 'name' => 'Neuer Abschnitt']), + ], null, null, null); + + $anchors = collect(app(LinkTargets::class)->anchorsForPage($page->fresh()))->keyBy('anchor'); + + $this->assertTrue($anchors['alt']['published']); + $this->assertFalse($anchors['alt']['hidden']); + $this->assertTrue($anchors['versteckt']['hidden']); + $this->assertFalse($anchors['neu']['published']); + $this->assertSame('Neuer Abschnitt', $anchors['neu']['label']); + } + + public function test_the_href_forms_pass_the_link_check(): void + { + $targets = app(LinkTargets::class); + + $this->assertSame('#kurse', $targets->href(null, 'kurse')); + $this->assertSame('/seite#kurse', $targets->href('/seite', 'kurse')); + $this->assertSame('/seite', $targets->href('/seite', null)); + $this->assertSame('/seite', $targets->href('/seite', 'Ungültig!')); + $this->assertNull($targets->href(null, null)); + } + + public function test_widgets_link_to_a_section_of_the_same_page_and_to_another_page(): void + { + $this->page('/ziel', [$this->section('Ziel', ['anchor' => 'unten'])], publish: true); + $cta = fn (string $url): array => ['type' => 'call_to_action', 'v' => 1, 'data' => ['heading' => 'Mehr', 'label' => 'Zum Kurs', 'url' => $url]]; + $this->page('/quelle', [ + $this->section('Q', ['anchor' => 'oben', 'slots' => [$cta('#unten')]]), + $this->section('R', ['slots' => [$cta('/ziel#unten')]]), + ], publish: true); + + $html = $this->get('/quelle')->assertOk()->getContent(); + + $this->assertStringContainsString('href="#unten"', $html); + $this->assertStringContainsString('/ziel#unten', $html); + $this->assertStringContainsString('id="oben"', $html); + $this->get('/ziel')->assertOk()->assertSee('id="unten"', false); + } +}