Files
laraundfelix/tests/Unit/PageSectionsTest.php
LarsandClaude Opus 5.5 6152451e2e
CI Feature / test (push) Successful in 34s
Deploy Test / test (push) Successful in 30s
Deploy Test / deploy (push) Successful in 28s
Füge duplizierte Abschnitte direkt hinter dem Original ein.
Filament hängt eine Kopie sonst ans Seitenende; auf langen Seiten war sie dort
schwer zu finden. Gefunden in der Browserprüfung.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 21:10:22 +01:00

272 lines
12 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\Validation\ValidationException;
use Tests\TestCase;
class PageSectionsTest extends TestCase
{
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'], array_keys(SectionLayouts::UNITS));
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'));
}
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(1, $stored[0]['v']);
$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']));
}
public function test_widget_minimum_width_is_enforced_per_slot(): void
{
$hero = ['type' => 'hero', 'v' => 1, 'data' => ['heading' => 'Einstieg']];
try {
$this->sections()->normalize([$this->section('u1_3', [$hero, null])]);
$this->fail('Einstieg im schmalen Platz wurde angenommen.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('sections.0.slots.0.units', $exception->errors());
}
$stored = $this->sections()->normalize([$this->section('u1_3', [null, $hero])]);
$this->assertSame('hero', $stored[0]['slots'][1]['type']);
$this->assertSame(2, app(WidgetRegistry::class)->get('hero')->minUnits());
}
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' => 'riesig']), 'sections.0.style.spacing'],
] 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', $sections[0]['style']['spacing']);
$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')));
$this->assertSame(['n' => 2], SectionEditor::insertCopyAfter($items, 'b', 'kopie')['kopie']);
$this->assertSame($items, SectionEditor::insertCopyAfter($items, 'fehlt', 'kopie'));
}
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));
}
}