Files
laraundfelix/tests/Feature/CmsSectionLabelsTest.php
LarsandClaude Sonnet 5.5 3a37e8a7a1 Speichere Name, Sprungmarke und Ausblenden je Abschnitt und gib sie auf der Website aus
Name nur zur Orientierung, Sprungmarke als Element-Kennung (je Seite eindeutig,
reservierte Kennungen gesperrt), ausgeblendete Abschnitte erscheinen nie öffentlich
und zählen weder für die Hauptüberschrift noch für Trenner. SafeUrl nimmt #marke an.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 06:57:38 +01:00

215 lines
8.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Sections\PageSections;
use App\Domain\Cms\Sections\SectionRenderer;
use App\Domain\Cms\Widgets\RenderContext;
use App\Domain\Cms\Widgets\Support\SafeUrl;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
/**
* Name, Sprungmarke und Ausblenden eines Abschnitts: Speichern, Lesen, öffentliche Darstellung und Vorschau.
*/
class CmsSectionLabelsTest extends TestCase
{
use RefreshDatabase;
/**
* @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]]],
];
}
private function sections(): PageSections
{
return app(PageSections::class);
}
/**
* @param list<array<string, mixed>> $sections
*/
private function html(array $sections, bool $preview = false): string
{
$read = $this->sections()->read($this->sections()->normalize($sections));
return app(SectionRenderer::class)->renderAll($read, $preview ? RenderContext::preview() : RenderContext::publicPage())->toHtml();
}
public function test_name_anchor_and_hidden_are_saved_and_read_back(): void
{
$saved = $this->sections()->normalize([
$this->section('Kurse', ['name' => ' Kurs Übersicht ', 'anchor' => ' Kurse-2026 ', 'hidden' => true]),
$this->section('Kontakt'),
]);
$this->assertSame('Kurs Übersicht', $saved[0]['name']);
$this->assertSame('kurse-2026', $saved[0]['anchor']);
$this->assertTrue($saved[0]['hidden']);
$this->assertNull($saved[1]['name']);
$this->assertNull($saved[1]['anchor']);
$this->assertFalse($saved[1]['hidden']);
$read = $this->sections()->read($saved);
$this->assertSame(['Kurs Übersicht', 'kurse-2026', true], [$read[0]['name'], $read[0]['anchor'], $read[0]['hidden']]);
}
public function test_sections_saved_before_these_fields_read_with_defaults(): void
{
$read = $this->sections()->read([['id' => 'a1', 'v' => 2, 'layout' => 'u4', 'style' => [], 'slots' => [null]]]);
$this->assertSame([null, null, false], [$read[0]['name'], $read[0]['anchor'], $read[0]['hidden']]);
}
public function test_an_invalid_stored_anchor_is_not_trusted_when_reading(): void
{
$read = $this->sections()->read([['id' => 'a1', 'layout' => 'u4', 'anchor' => 'Evil"><script>', 'name' => str_repeat('x', 200), 'slots' => [null]]]);
$this->assertNull($read[0]['anchor']);
$this->assertNull($read[0]['name']);
}
public function test_anchors_must_be_valid_and_unique_per_page(): void
{
foreach (['Mit Leerzeichen', 'ümlaut', '-anfang', 'ende-', 'doppel--strich', str_repeat('a', 61)] as $invalid) {
try {
$this->sections()->normalize([$this->section('A', ['anchor' => $invalid])]);
$this->fail("Sprungmarke „{$invalid}“ hätte abgewiesen werden müssen.");
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.anchor', $exception->errors(), $invalid);
}
}
try {
$this->sections()->normalize([$this->section('A', ['anchor' => 'kurse']), $this->section('B', ['anchor' => 'KURSE'])]);
$this->fail('Eine doppelte Sprungmarke muss abgewiesen werden.');
} catch (ValidationException $exception) {
$this->assertArrayNotHasKey('sections.0.anchor', $exception->errors());
$this->assertStringContainsString('schon', $exception->errors()['sections.1.anchor'][0]);
}
}
public function test_reserved_identifiers_cannot_be_used_as_anchor(): void
{
foreach (['inhalt', 'site-nav', 'bereich-abc'] as $reserved) {
try {
$this->sections()->normalize([$this->section('A', ['anchor' => $reserved])]);
$this->fail("„{$reserved}“ ist reserviert.");
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.anchor', $exception->errors(), $reserved);
}
}
}
public function test_a_name_longer_than_the_limit_is_reported(): void
{
try {
$this->sections()->normalize([$this->section('A', ['name' => str_repeat('n', 81)])]);
$this->fail('Ein zu langer Name muss gemeldet werden.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.name', $exception->errors());
}
}
public function test_the_anchor_becomes_the_section_id_and_the_name_is_never_public(): void
{
$html = $this->html([
$this->section('Kurse', ['name' => 'Geheimer interner Name', 'anchor' => 'kurse']),
$this->section('Kontakt'),
]);
$this->assertStringContainsString('id="kurse"', $html);
$this->assertSame(1, substr_count($html, 'id="bereich-'), 'Ohne Sprungmarke bleibt die bisherige Kennung.');
$this->assertStringNotContainsString('Geheimer interner Name', $html);
}
public function test_a_hidden_section_never_appears_on_the_website(): void
{
$html = $this->html([
$this->section('Sichtbar'),
$this->section('Versteckter Inhalt', ['name' => 'Geheim', 'anchor' => 'weg', 'hidden' => true]),
]);
$this->assertStringContainsString('Sichtbar', $html);
$this->assertStringNotContainsString('Versteckter Inhalt', $html);
$this->assertStringNotContainsString('id="weg"', $html);
$this->assertStringNotContainsString('section-hidden-note', $html);
}
public function test_the_preview_shows_only_a_note_for_a_hidden_section(): void
{
$html = $this->html([
$this->section('Sichtbar'),
$this->section('Versteckter Inhalt', ['name' => 'Kurse', 'hidden' => true]),
], preview: true);
$this->assertStringNotContainsString('Versteckter Inhalt', $html);
$this->assertStringContainsString('section-hidden-note', $html);
$this->assertStringContainsString('„Kurse“', $html);
}
public function test_a_hidden_first_section_does_not_take_the_page_heading(): void
{
$block = fn (string $text, bool $hidden): array => $this->section($text, ['hidden' => $hidden]);
foreach ([false, true] as $preview) {
$html = $this->html([$block('Verborgener Einstieg', true), $block('Echter Einstieg', false)], $preview);
$this->assertMatchesRegularExpression('/<h1[^>]*>\s*Echter Einstieg/u', $html, $preview ? 'Vorschau' : 'öffentlich');
$this->assertStringNotContainsString('Verborgener Einstieg', $html);
}
}
public function test_a_hidden_section_does_not_leave_a_divider_before_the_last_visible_section(): void
{
$html = $this->html([
$this->section('Erster', ['style' => ['divider' => 'line']]),
$this->section('Verborgen', ['hidden' => true, 'style' => ['divider' => 'line']]),
]);
$this->assertStringNotContainsString('section-divider', $html, 'Trenner stehen nie nach dem letzten sichtbaren Abschnitt.');
}
public function test_hidden_sections_keep_their_media_but_not_the_page_title(): void
{
$hiddenOnly = $this->sections()->normalize([
$this->section('Titel des verborgenen Abschnitts', ['hidden' => true]),
$this->section('Titel des sichtbaren Abschnitts'),
]);
$this->assertSame('Titel des sichtbaren Abschnitts', $this->sections()->pageTitle($hiddenOnly));
}
public function test_the_fields_survive_a_saved_revision_and_a_duplicate_save(): void
{
$page = Page::query()->create(['path' => '/sprungmarken']);
$service = app(PageService::class);
$revision = $service->saveRevision($page, [$this->section('Kurse', ['name' => 'Kurse', 'anchor' => 'kurse', 'hidden' => true])], null, null, null);
$stored = $revision->sections();
$this->assertSame(['Kurse', 'kurse', true], [$stored[0]['name'], $stored[0]['anchor'], $stored[0]['hidden']]);
}
public function test_safe_url_accepts_anchors_on_the_page_and_on_other_pages(): void
{
foreach (['#kurse', '#abschnitt_2', '/praevention#anmeldung', 'https://example.org/seite#teil'] as $ok) {
$this->assertTrue(SafeUrl::isSafe($ok), $ok);
}
foreach (['#', '# kurse', '#a b', '#"><x', '##x', 'javascript:alert(1)', '#'.str_repeat('a', 81)] as $bad) {
$this->assertFalse(SafeUrl::isSafe($bad), $bad);
}
}
}