Files
laraundfelix/tests/Feature/CmsLinkPickerTest.php
T
LarsandClaude Sonnet 5.5 6b43fbb7d1 Verlinke interne Seiten und Sprungmarken auch im formatierten Text der Website
Neues Werkzeug neben dem freien Link in Fließtext, Überschrift-Einleitung und Bild-Text.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 07:03:17 +01:00

233 lines
9.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\FormattedText;
use App\Domain\Cms\LinkTargets;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\Support\RichText;
use App\Domain\Cms\Widgets\Types\ImageTextWidget;
use App\Filament\Resources\Pages\Pages\EditPage;
use App\Filament\Support\RichField;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Auswahl interner Linkziele (Seiten und Sprungmarken) und die Felder für Name, Sprungmarke und Ausblenden im Editor.
*/
class CmsLinkPickerTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
/**
* @param array<string, mixed> $extra
* @return array<string, mixed>
*/
private function section(string $heading, array $extra = []): array
{
return $extra + ['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'heading', 'v' => 1, 'data' => ['heading' => $heading]]]];
}
/**
* @param list<array<string, mixed>> $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);
}
public function test_links_to_anchors_survive_the_text_sanitizers(): void
{
$html = '<p><a href="#kurse">hier</a> und <a href="/ziel#unten">dort</a></p>';
foreach ([RichText::sanitize($html), FormattedText::canonical($html, site: true)] as $clean) {
$this->assertStringContainsString('href="#kurse"', $clean);
$this->assertStringContainsString('href="/ziel#unten"', $clean);
}
}
public function test_website_text_editors_offer_the_internal_link_tool(): void
{
foreach ([RichField::make('body', 'Text', website: true), (new ImageTextWidget)->dataFields()[2]] as $editor) {
$buttons = (new \ReflectionProperty($editor, 'toolbarButtons'))->getValue($editor);
$this->assertContains('interneSeite', array_merge(...$buttons), 'Das Werkzeug fehlt in der Symbolleiste.');
}
}
}