Files
laraundfelix/tests/Feature/CmsPremiumWidgetsTest.php
T
LarsandClaude Opus 5.5 0bfb53463a
CI Feature / test (push) Successful in 41s
Deploy Test / test (push) Successful in 37s
Deploy Test / deploy (push) Successful in 33s
Führe einen gemeinsamen Layoutvertrag ein und integriere die Premium-Widgets.
Layoutvertrag (ADR-38) in der bestehenden Registry: Jede Widget-Art
erklärt zulässige Rasterbreiten (supportedUnits), Höhen aus einem
gemeinsamen Vokabular (heights, gespeichert als settings.height),
Darstellungsmodi und Verhalten bei wenig Platz. Unzulässige Breiten
werden beim Speichern verständlich abgewiesen; Höhe „fill“ streckt
den Handlungsaufruf auf die Abschnittszeile.

Premium-Widgets nach Brand-UI-Kit v1.6:
- hero Version 2: photo_split/editorial_solid, Höhen compact/normal/
  large, Label, Überschriftenebene, Akzent, Bildfokus in Prozent, zwei
  Schaltflächen; nur 3 oder 4 Einheiten. Upgrade aus Version 1.
- image_text Version 3: Fläche cream/clear/navy, Höhen compact/normal/
  spacious, Label, zwei Schaltflächen, Bildfokus; 1 bis 4 Einheiten.
  Upgrade aus Version 2 (format entfällt, Fokus in Prozent).
- Kit-CSS ersetzt das bisherige Hero- und Bild-und-Text-CSS; der
  Randlos-Mechanismus der Designiteration 1 entfällt.

Editor: Reiter Inhalt/Medien/Darstellung aus der Registry, Auswahl-
knöpfe mit Symbolen, Schieberegler für den Bildfokus und eine Live-
Vorschau im Dialog mit demselben Renderer in wählbarer Platzbreite.

Tests bewusst angepasst: Einstieg braucht jetzt 3 oder 4 Einheiten
(vorher Mindestbreite 2), image_text ist Version 3, Klassen der neuen
Ansichten; die Hero-/Bild-Tests der Designiteration 1 sind durch
CmsPremiumWidgetsTest ersetzt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:20:09 +01:00

204 lines
12 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Sections\SectionLayouts;
use App\Domain\Cms\Widgets\WidgetRegistry;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
/**
* Premium-Widgets nach Brand-UI-Kit v1.6 und gemeinsamer Layoutvertrag (ADR-38).
* Gespeicherte Daten früherer Versionen bleiben lesbar und speicherbar.
*/
class CmsPremiumWidgetsTest extends TestCase
{
use RefreshDatabase;
/**
* @param list<array<string, mixed>> $content
*/
private function publish(string $path, array $content): string
{
$page = Page::query()->create(['path' => $path]);
$service = app(PageService::class);
$service->publish($page, $service->saveRevision($page, $content, null, null, null));
return $this->get($path)->assertOk()->getContent();
}
/**
* @param list<array<string, mixed>|null> $slots
* @param array<string, string> $style
* @return array<string, mixed>
*/
private function section(string $layout, array $slots, array $style = []): array
{
return ['layout' => $layout, 'style' => $style, 'slots' => $slots];
}
public function test_hero_from_design_iteration_one_is_upgraded_and_stays_savable(): void
{
$registry = app(WidgetRegistry::class);
$standard = $registry->upcast(['type' => 'hero', 'v' => 1, 'variant' => 'stage', 'settings' => ['focus' => 'top_right', 'text_side' => 'left'], 'data' => ['heading' => 'Alt', 'cta2_label' => 'Zwei', 'cta2_url' => '/b']]);
$this->assertSame(2, $standard['v']);
$this->assertSame('photo_split', $standard['variant']);
$this->assertSame([], $standard['settings'], 'Nicht mehr vorhandene Einstellungen werden entfernt.');
$this->assertSame([100, 0], [$standard['data']['focus_x'], $standard['data']['focus_y']]);
$this->assertSame('/b', $standard['data']['cta2_url']);
$highlight = $registry->upcast(['type' => 'hero', 'v' => 1, 'variant' => 'highlight', 'data' => ['heading' => 'Alt']]);
$this->assertSame('editorial_solid', $highlight['variant']);
$this->assertSame('editorial_solid', $registry->upcast(['type' => 'hero', 'v' => 1, 'variant' => 'editorial', 'data' => ['heading' => 'Alt']])['variant']);
// Szenario „bestehende Seite unverändert speichern“: gespeicherte v1-Hülle geht ohne Fehler durch die Prüfung.
$saved = $registry->normalize([['type' => 'hero', 'v' => 1, 'variant' => 'stage', 'settings' => ['focus' => 'left', 'text_side' => 'right'], 'data' => ['heading' => 'Alt', 'alt' => 'Bild'], 'refs' => ['image' => 'cms/alt.jpg']]]);
$this->assertSame(['photo_split', 2, [0, 50]], [$saved[0]['variant'], $saved[0]['v'], [$saved[0]['data']['focus_x'], $saved[0]['data']['focus_y']]]);
}
public function test_image_text_from_design_iteration_one_is_upgraded_and_stays_savable(): void
{
$saved = app(WidgetRegistry::class)->normalize([[
'type' => 'image_text', 'v' => 2, 'variant' => 'image_left',
'settings' => ['format' => 'portrait', 'focus' => 'bottom'],
'data' => ['title' => 'Alt', 'body' => '<p>Text</p>', 'alt' => 'Bild'], 'refs' => ['image' => 'cms/alt.jpg'],
]])[0];
$this->assertSame(3, $saved['v']);
$this->assertSame([], $saved['settings']);
$this->assertSame([50, 100], [$saved['data']['focus_x'], $saved['data']['focus_y']]);
$this->assertSame('image_left', $saved['variant']);
}
public function test_premium_hero_renders_variants_heights_and_accents(): void
{
$html = $this->publish('/einstiege', [
$this->section('u4', [[
'type' => 'hero', 'v' => 2, 'variant' => 'photo_split', 'settings' => ['height' => 'compact', 'accent' => 'none'],
'data' => ['eyebrow' => '<script>alert(1)</script>', 'heading' => 'Erster Einstieg', 'alt' => 'Lara und Felix', 'focus_x' => 30, 'focus_y' => 70],
'refs' => ['image' => 'cms/park.jpg'],
]]),
$this->section('u3_1', [[
'type' => 'hero', 'v' => 2, 'variant' => 'editorial_solid', 'settings' => ['height' => 'large', 'heading_level' => 'h3', 'accent' => 'rays'],
'data' => ['heading' => 'Zweiter Einstieg', 'cta_label' => 'Konzept', 'cta_url' => '/konzept'],
], null]),
]);
$this->assertStringContainsString('class="lf-hero lf-hero--photo-split lf-hero--height-compact lf-hero--accent-none"', $html);
$this->assertStringContainsString('style="--lf-hero-focus-x: 30%; --lf-hero-focus-y: 70%"', $html);
$this->assertStringContainsString('alt="Lara und Felix" loading="eager" fetchpriority="high" decoding="async"', $html);
$this->assertStringContainsString('&lt;script&gt;alert(1)&lt;/script&gt;', $html);
$this->assertStringNotContainsString('<script>alert(1)</script>', $html);
$this->assertSame(1, substr_count($html, '<h1'), 'Genau eine Hauptüberschrift je Seite.');
$this->assertStringContainsString('<h1 class="lf-hero__title">Erster Einstieg</h1>', $html);
$this->assertStringContainsString('<h3 class="lf-hero__title">Zweiter Einstieg</h3>', $html);
$this->assertStringContainsString('lf-hero--editorial-solid lf-hero--height-large lf-hero--accent-rays', $html);
$this->assertStringContainsString('brand-rays lf-hero__rays', $html);
$this->assertSame(1, substr_count($html, 'lf-hero__title-accent'), 'Ohne Akzent keine Unterstreichung.');
$this->assertStringContainsString('widget-hero widget-variant-photo-split widget-height-compact', $html);
}
public function test_hero_rejects_unsafe_links_and_out_of_range_focus(): void
{
$cases = [
'blocks.0.data.cta2_url' => ['heading' => 'X', 'cta2_label' => 'Böse', 'cta2_url' => 'javascript:alert(1)'],
'blocks.0.data.focus_x' => ['heading' => 'X', 'focus_x' => 150],
];
foreach ($cases as $key => $data) {
try {
app(WidgetRegistry::class)->normalize([['type' => 'hero', 'v' => 2, 'data' => $data]]);
$this->fail("$key wurde angenommen.");
} catch (ValidationException $exception) {
$this->assertArrayHasKey($key, $exception->errors());
}
}
}
public function test_premium_image_text_renders_surface_height_side_and_sanitized_body(): void
{
$html = $this->publish('/bild-text', [
['type' => 'heading', 'v' => 1, 'data' => ['heading' => 'Seite']],
$this->section('u4', [[
'type' => 'image_text', 'v' => 3, 'variant' => 'image_left',
'settings' => ['surface' => 'navy', 'height' => 'spacious', 'heading_level' => 'h3'],
'data' => ['eyebrow' => 'Unser Ansatz', 'title' => 'Geschichten', 'body' => '<p>Text</p><script>alert(1)</script>', 'alt' => 'Park', 'cta_label' => 'Mehr', 'cta_url' => '/konzept', 'cta2_label' => 'Themen', 'cta2_url' => 'https://example.org/themen', 'focus_x' => 10, 'focus_y' => 90],
'refs' => ['image' => 'cms/park.jpg'],
]]),
$this->section('u4', [['type' => 'image_text', 'v' => 3, 'data' => ['body' => '<p>Ohne Bild</p>']]]),
]);
$this->assertStringContainsString('class="lf-bt lf-bt--navy lf-bt--image-left lf-bt--height-spacious"', $html);
$this->assertStringContainsString('style="--bt-focus-x: 10%; --bt-focus-y: 90%"', $html);
$this->assertStringContainsString('<h3 class="lf-bt__headline">Geschichten</h3>', $html);
$this->assertStringContainsString('<p class="lf-bt__eyebrow">Unser Ansatz</p>', $html);
$this->assertStringNotContainsString('<script>alert(1)</script>', $html);
$this->assertStringContainsString('<a class="lf-bt__button" href="/konzept">Mehr <span aria-hidden="true">→</span></a>', $html);
$this->assertStringContainsString('<a class="lf-bt__button lf-bt__button--secondary" href="https://example.org/themen">Themen</a>', $html);
$this->assertStringContainsString('lf-bt lf-bt--cream lf-bt--image-left lf-bt--height-normal lf-bt--without-image', $html);
}
/**
* Gemeinsamer Layoutvertrag für alle Bestandswidgets: In jeder zulässigen Platzbreite wird gerendert; nicht
* zulässige Breiten werden beim Speichern mit verständlicher Meldung abgewiesen.
*/
public function test_every_widget_renders_in_each_supported_width_and_rejects_the_others(): void
{
$samples = [
'heading' => ['heading' => 'Überschrift mit einem sehr langen Wort Gewaltpräventionsfortbildungsmaßnahme'],
'prose' => ['body' => 'Fließtext mit einer langen Adresse https://example.org/'.str_repeat('a', 80)],
'cards' => ['items' => [['title' => 'Karte mit langem Titel für schmale Plätze', 'text' => 'Beschreibung', 'url' => '/a']]],
'call_to_action' => ['heading' => 'Aufruf', 'text' => 'Text', 'label' => 'Eine ausgesprochen lange Schaltflächenbeschriftung zum Prüfen', 'url' => '/a'],
'faq' => ['items' => [['question' => 'Frage?', 'answer' => 'Antwort.']]],
'hero' => ['heading' => 'Einstieg mit einer langen Überschrift, die umbrechen muss'],
'image_text' => ['title' => 'Bild und Text', 'body' => '<p>Text</p>'],
];
$registry = app(WidgetRegistry::class);
$layouts = ['u4' => [4], 'u3_1' => [3, 1], 'u2_2' => [2, 2], 'u1_1_1_1' => [1, 1, 1, 1]];
$path = 0;
foreach ($samples as $type => $data) {
$widgetType = $registry->get($type);
$widget = ['type' => $type, 'v' => $widgetType->version(), 'data' => $data];
foreach ($layouts as $layout => $units) {
$slots = array_fill(0, count($units), null);
$slots[0] = $widget;
if ($widgetType->supportsUnits($units[0])) {
$html = $this->publish('/raster-'.(++$path), [$this->section($layout, $slots)]);
$this->assertStringContainsString('widget-'.str_replace('_', '-', $type), $html, "$type in $layout");
$this->assertStringContainsString('data-units="'.$units[0].'"', $html, "$type in $layout");
} else {
try {
app(PageService::class)->saveRevision(Page::query()->create(['path' => '/abgewiesen-'.(++$path)]), [$this->section($layout, $slots)], null, null, null);
$this->fail("$type in $layout ({$units[0]} Einheiten) wurde angenommen.");
} catch (ValidationException $exception) {
$this->assertStringContainsString($widgetType->unitsLabel().' Rastereinheiten', $exception->errors()['sections.0.slots.0.units'][0]);
}
}
}
}
$this->assertSame(['u4', 'u2_2', 'u1_3', 'u3_1', 'u1_2_1', 'u1_1_1_1'], array_keys(SectionLayouts::UNITS));
}
public function test_fill_height_marks_the_widget_and_defaults_to_content_height(): void
{
$html = $this->publish('/fuellen', [
$this->section('u2_2', [
['type' => 'call_to_action', 'v' => 1, 'settings' => ['height' => 'fill'], 'data' => ['heading' => 'Gleich hoch', 'label' => 'Los', 'url' => '/a']],
['type' => 'call_to_action', 'v' => 1, 'data' => ['heading' => 'Nach Inhalt', 'label' => 'Los', 'url' => '/b']],
]),
]);
$this->assertStringContainsString('widget-call-to-action widget-variant-standard widget-height-fill', $html);
$this->assertStringContainsString('widget-call-to-action widget-variant-standard widget-height-auto', $html);
$this->assertStringNotContainsString('widget-prose widget-variant-standard widget-height', $html);
}
}