Files
laraundfelix/tests/Unit/PageSectionsTest.php
LarsandClaude Sonnet 5.5 ed53bb1669 Zeige Name, Sprungmarke und Ausgeblendet im Seitenaufbau und biete die Felder am Abschnitt an
Kopierte Abschnitte geben ihre Sprungmarke ab, damit die Seite weiter speicherbar bleibt.

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

322 lines
15 KiB
PHP

<?php
namespace Tests\Unit;
use App\Domain\Cms\Sections\PageSections;
use App\Domain\Cms\Sections\SectionLayouts;
use App\Domain\Cms\Sections\SectionStyle;
use App\Domain\Cms\Widgets\WidgetRegistry;
use App\Filament\Support\SectionEditor;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
class PageSectionsTest extends TestCase
{
use RefreshDatabase;
private function sections(): PageSections
{
return app(PageSections::class);
}
/**
* @param list<array<string, mixed>|null> $slots
* @param array<string, mixed> $style
* @return array<string, mixed>
*/
private function section(string $layout, array $slots, array $style = []): array
{
return ['layout' => $layout, 'style' => $style, 'slots' => $slots];
}
/**
* @return array<string, mixed>
*/
private function heading(string $text): array
{
return ['type' => 'heading', 'v' => 1, 'data' => ['heading' => $text]];
}
public function test_required_layouts_exist_with_at_most_four_units(): void
{
$this->assertSame(['u4', 'u2_2', 'u1_3', 'u3_1', 'u1_2_1', 'u1_1_1_1', 'u1_1_2', 'u2_1_1'], array_keys(SectionLayouts::UNITS), 'Raster werden nur ergänzt (v1.7: 1 + 1 + 2 und 2 + 1 + 1 für gemischte Kartenraster).');
foreach (SectionLayouts::UNITS as $key => $units) {
$this->assertSame(4, array_sum($units), $key);
$this->assertLessThanOrEqual(4, count($units), $key);
}
$this->assertSame('1 + 2 + 1', SectionLayouts::short('u1_2_1'));
$this->assertSame('1 + 1 + 2', SectionLayouts::short('u1_1_2'));
$this->assertSame(['lg' => 'minmax(0, 1fr) minmax(0, 1fr) minmax(0, 2fr)', 'md' => 'minmax(0, 1fr)'], SectionLayouts::columns(SectionLayouts::units('u1_1_2')));
}
public function test_columns_follow_the_units_and_stack_unequal_layouts_on_tablets(): void
{
$this->assertSame(['lg' => 'minmax(0, 1fr) minmax(0, 3fr)', 'md' => 'minmax(0, 1fr)'], SectionLayouts::columns([1, 3]));
$this->assertSame('repeat(2, minmax(0, 1fr))', SectionLayouts::columns([1, 1, 1, 1])['md']);
$this->assertSame('repeat(3, minmax(0, 1fr))', SectionLayouts::columns([1, 1, 1])['md']);
$this->assertSame('repeat(2, minmax(0, 1fr))', SectionLayouts::columns([2, 2])['md']);
$this->assertSame(['lg' => 'minmax(0, 1fr)', 'md' => 'minmax(0, 1fr)'], SectionLayouts::columns([3]));
}
public function test_sections_are_normalized_with_slots_matching_the_layout(): void
{
$stored = $this->sections()->normalize([
$this->section('u1_3', [$this->heading('Links'), $this->heading('Rechts')], ['background' => 'dark', 'divider' => 'line']),
$this->section('u1_1_1_1', [$this->heading('Eins'), null, $this->heading('Drei')]),
]);
$this->assertCount(2, $stored);
$this->assertSame('u1_3', $stored[0]['layout']);
$this->assertSame(2, $stored[0]['v'], 'Hülle Version 2: drei unabhängige Abstände.');
$this->assertSame('dark', $stored[0]['style']['background']);
$this->assertSame('line', $stored[0]['style']['divider']);
$this->assertSame('standard', $stored[0]['style']['width']);
$this->assertCount(4, $stored[1]['slots'], 'Fehlende Plätze werden als leere Plätze ergänzt.');
$this->assertNull($stored[1]['slots'][1]);
$this->assertNull($stored[1]['slots'][3]);
$this->assertNotEmpty($stored[0]['id']);
}
public function test_flat_widgets_are_taken_over_as_full_width_sections(): void
{
$stored = $this->sections()->normalize([
['type' => 'heading', 'heading' => 'Altformat'],
$this->heading('Hülle'),
]);
$this->assertSame(['u4', 'u4'], array_column($stored, 'layout'));
$this->assertSame('Altformat', $stored[0]['slots'][0]['data']['heading']);
}
public function test_more_widgets_than_slots_are_rejected_instead_of_dropped(): void
{
try {
$this->sections()->normalize([
$this->section('u2_2', [$this->heading('A'), $this->heading('B'), $this->heading('C')]),
]);
$this->fail('Überzähliges Widget wurde angenommen.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.slots', $exception->errors());
$this->assertStringContainsString('2 Plätze', $exception->errors()['sections.0.slots'][0]);
}
}
public function test_surplus_empty_slots_are_compacted_without_losing_widgets(): void
{
$stored = $this->sections()->normalize([
$this->section('u2_2', [$this->heading('A'), null, $this->heading('C'), null]),
]);
$this->assertSame(['A', 'C'], array_map(fn ($slot) => $slot['data']['heading'], $stored[0]['slots']));
}
/**
* Layoutvertrag (ADR-38): Ein Platz muss eine der unterstützten Breiten haben. Der Premium-Einstieg ist seit
* Kit v1.6 nur für 3 oder 4 Einheiten vorgesehen (vorher Mindestbreite 2).
*/
public function test_widget_supported_widths_are_enforced_per_slot(): void
{
$hero = ['type' => 'hero', 'v' => 2, 'data' => ['heading' => 'Einstieg']];
foreach ([['u1_3', [$hero, null]], ['u2_2', [$hero, null]]] as [$layout, $slots]) {
try {
$this->sections()->normalize([$this->section($layout, $slots)]);
$this->fail("Einstieg im zu schmalen Platz ($layout) wurde angenommen.");
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.slots.0.units', $exception->errors());
$this->assertStringContainsString('3 oder 4 Rastereinheiten', $exception->errors()['sections.0.slots.0.units'][0]);
}
}
$stored = $this->sections()->normalize([$this->section('u1_3', [null, $hero])]);
$this->assertSame('hero', $stored[0]['slots'][1]['type']);
$this->assertSame([3, 4], app(WidgetRegistry::class)->get('hero')->supportedUnits());
$imageText = ['type' => 'image_text', 'v' => 3, 'data' => ['body' => '<p>Schmal</p>']];
$stored = $this->sections()->normalize([$this->section('u1_1_1_1', [$imageText, null, null, null])]);
$this->assertSame('image_text', $stored[0]['slots'][0]['type'], 'Bild und Text ist für 1 bis 4 Einheiten freigegeben.');
}
public function test_free_style_values_and_unknown_layouts_are_rejected(): void
{
foreach ([
[['layout' => 'u5', 'slots' => []], 'sections.0.layout'],
[$this->section('u4', [], ['background' => '#ff00ff']), 'sections.0.style.background'],
[$this->section('u4', [], ['css' => 'color: red']), 'sections.0.style.css'],
[$this->section('u4', [], ['spacing_top' => 'riesig']), 'sections.0.style.spacing_top'],
[$this->section('u4', [], ['spacing_bottom' => '0px']), 'sections.0.style.spacing_bottom'],
[$this->section('u4', [], ['gap' => '3rem']), 'sections.0.style.gap'],
] as [$section, $key]) {
try {
$this->sections()->normalize([$section]);
$this->fail('Freier Wert wurde angenommen: '.$key);
} catch (ValidationException $exception) {
$this->assertArrayHasKey($key, $exception->errors());
}
}
}
public function test_background_image_must_be_public_and_is_dropped_for_other_backgrounds(): void
{
try {
$this->sections()->normalize([$this->section('u4', [$this->heading('A')], ['background' => 'image', 'image' => 'bibliothek/standbild.jpg'])]);
$this->fail('Geschütztes Bild wurde als Hintergrund angenommen.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.style.image', $exception->errors());
}
try {
$this->sections()->normalize([$this->section('u4', [$this->heading('A')], ['background' => 'image'])]);
$this->fail('Hintergrundbild ohne Bild wurde angenommen.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.style.image', $exception->errors());
}
$stored = $this->sections()->normalize([
$this->section('u4', [$this->heading('A')], ['background' => 'image', 'image' => 'cms/flaeche.jpg', 'focus' => 'top', 'overlay' => 'dark']),
$this->section('u4', [$this->heading('B')], ['background' => 'light', 'image' => 'cms/flaeche.jpg']),
]);
$this->assertSame('cms/flaeche.jpg', $stored[0]['style']['image']);
$this->assertNull($stored[1]['style']['image']);
$this->assertSame(['cms/flaeche.jpg'], $this->sections()->mediaPaths($stored));
}
public function test_ids_are_unique_across_the_whole_page_after_duplicating_a_section(): void
{
$section = ['id' => 'abschnitt-a'] + $this->section('u2_2', [
['id' => 'widget-a'] + $this->heading('A'),
['id' => 'widget-b'] + $this->heading('B'),
]);
$stored = $this->sections()->normalize([$section, $section]);
$this->assertSame('abschnitt-a', $stored[0]['id']);
$this->assertNotSame('abschnitt-a', $stored[1]['id']);
$widgetIds = array_column(array_merge($stored[0]['slots'], $stored[1]['slots']), 'id');
$this->assertCount(4, array_unique($widgetIds));
$this->assertSame(['widget-a', 'widget-b'], array_slice($widgetIds, 0, 2));
}
public function test_all_errors_of_a_page_are_reported_together(): void
{
try {
$this->sections()->normalize([
$this->section('u2_2', [['type' => 'heading', 'v' => 1, 'data' => []], null]),
$this->section('u4', [['type' => 'call_to_action', 'v' => 1, 'data' => ['heading' => 'X', 'label' => 'Los', 'url' => 'javascript:alert(1)']]]),
]);
$this->fail('Fehlerhafte Seite wurde angenommen.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.slots.0.data.heading', $exception->errors());
$this->assertArrayHasKey('sections.1.slots.0.data.url', $exception->errors());
}
}
public function test_reading_without_sections_wraps_legacy_blocks_in_single_column_sections(): void
{
$sections = $this->sections()->read(null, [
['type' => 'heading', 'heading' => 'Alt'],
['type' => 'gibt_es_nicht'],
['type' => 'prose', 'v' => 1, 'data' => ['body' => 'Text']],
]);
$this->assertCount(2, $sections);
$this->assertSame('u4', $sections[0]['layout']);
$this->assertSame(['compact', 'compact', 'normal'], [$sections[0]['style']['spacing_top'], $sections[0]['style']['spacing_bottom'], $sections[0]['style']['gap']]);
$this->assertSame('none', $sections[1]['style']['spacing_top'], 'Wie bisher steht zwischen zwei Altbausteinen nur einmal der kompakte Abstand.');
$this->assertSame('Alt', $sections[0]['slots'][0]['data']['heading']);
$this->assertSame('prose', $sections[1]['slots'][0]['type']);
}
public function test_reading_is_lenient_with_unknown_stored_values(): void
{
$sections = $this->sections()->read([
['id' => 'x', 'layout' => 'u9', 'style' => ['background' => 'pink', 'width' => 'wide'], 'slots' => [['type' => 'heading', 'v' => 1, 'data' => ['heading' => 'A']]]],
]);
$this->assertSame('u4', $sections[0]['layout']);
$this->assertSame('default', $sections[0]['style']['background']);
$this->assertSame('wide', $sections[0]['style']['width']);
}
public function test_flatten_keeps_reading_order_for_the_rollback_column(): void
{
$stored = $this->sections()->normalize([
$this->section('u1_1_1_1', [$this->heading('1'), null, $this->heading('3'), $this->heading('4')]),
$this->section('u4', [$this->heading('5')]),
]);
$this->assertSame(['1', '3', '4', '5'], array_map(fn ($widget) => $widget['data']['heading'], $this->sections()->flatten($stored)));
$this->assertSame('1', $this->sections()->pageTitle($stored));
}
public function test_style_summary_names_background_and_divider(): void
{
$this->assertSame('Dunkle Markenfläche · Breit · Trenner: Dezente Linie', SectionStyle::summary(['background' => 'dark', 'width' => 'wide', 'divider' => 'line']));
}
public function test_duplicated_section_is_inserted_right_after_its_original(): void
{
$items = ['a' => ['n' => 1], 'b' => ['n' => 2], 'c' => ['n' => 3]];
$this->assertSame(['a', 'b', 'kopie', 'c'], array_keys(SectionEditor::insertCopyAfter($items, 'b', 'kopie')));
// Die Kopie hat nie eine Sprungmarke (sie wäre doppelt); ohne Namen bleibt sie ohne Namen.
$this->assertSame(['n' => 2, 'anchor' => null, 'name' => null], SectionEditor::insertCopyAfter($items, 'b', 'kopie')['kopie']);
$this->assertSame($items, SectionEditor::insertCopyAfter($items, 'fehlt', 'kopie'));
}
public function test_a_duplicated_section_gives_up_its_anchor_so_the_page_still_saves(): void
{
$items = ['a' => [
'name' => 'Kurse', 'anchor' => 'kurse', 'hidden' => true, 'layout' => 'u4', 'style' => [],
'slots' => [['widget' => []]],
]];
$copy = SectionEditor::insertCopyAfter($items, 'a', 'b');
$this->assertSame('kurse', $copy['a']['anchor']);
$this->assertNull($copy['b']['anchor']);
$this->assertSame('Kurse (Kopie)', $copy['b']['name']);
$this->assertTrue($copy['b']['hidden']);
// Beide Abschnitte lassen sich gemeinsam speichern.
$saved = $this->sections()->normalize(SectionEditor::fromEditorState($copy));
$this->assertSame(['kurse', null], array_column($saved, 'anchor'));
}
public function test_editor_state_round_trips_name_anchor_and_hidden(): void
{
$stored = $this->sections()->normalize([['name' => 'Kontakt', 'anchor' => 'kontakt', 'hidden' => true, 'layout' => 'u4', 'style' => [], 'slots' => [null]]]);
$state = SectionEditor::toEditorState($this->sections()->read($stored));
$this->assertSame(['Kontakt', 'kontakt', true], [$state[0]['name'], $state[0]['anchor'], $state[0]['hidden']]);
$back = SectionEditor::fromEditorState($state);
$this->assertSame(['Kontakt', 'kontakt', true], [$back[0]['name'], $back[0]['anchor'], $back[0]['hidden']]);
}
public function test_layout_change_in_the_editor_never_drops_a_widget(): void
{
$slots = [
'a' => ['widget' => ['w1' => ['type' => 'heading']]],
'b' => ['widget' => []],
'c' => ['widget' => ['w3' => ['type' => 'prose']]],
'd' => ['widget' => []],
];
$two = SectionEditor::arrangeSlots($slots, 'u2_2');
$this->assertSame(['a', 'c'], array_keys($two));
$one = SectionEditor::arrangeSlots($two, 'u4');
$this->assertCount(2, $one, 'Passen die Widgets nicht, bleiben alle erhalten; das Speichern weist die Seite ab.');
$four = SectionEditor::arrangeSlots($two, 'u1_1_1_1');
$this->assertCount(4, $four);
$this->assertSame(['a', 'c'], array_slice(array_keys($four), 0, 2));
$this->assertSame(2, SectionEditor::filledCount($four));
}
}