Files
laraundfelix/tests/Feature/CmsWidgetAppearanceTest.php
T
LarsandClaude Sonnet 5.5 25b2149d43
CI Feature / test (push) Successful in 8m11s
Deploy Test / test (push) Successful in 7m17s
Deploy Test / deploy (push) Successful in 42s
Lies die Palette nur, wenn site_settings existiert
Die Prüfregeln der Widgets lesen die Palette bei jedem Speichern. Beim
Einspielen der Migrationen speichert der Seeder eine Seite, bevor
site_settings existiert; die fehlgeschlagene Abfrage brach in PostgreSQL
die Transaktion ab (SQLSTATE 25P02) und ließ den CI-Schritt
„Migrationen gegen Wegwerf-Postgres“ scheitern. SQLite zeigt das lokal
nicht. Jetzt wird vorher geprüft, ob die Tabelle da ist.

Der Test schneidet Abfragen vor der Ausführung mit, weil eine
fehlgeschlagene Abfrage kein QueryExecuted auslöst.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-30 10:29:33 +01:00

292 lines
13 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\Support\WidgetAppearance;
use App\Domain\Cms\Widgets\WidgetRegistry;
use App\Domain\Design\DesignPalette;
use App\Filament\Resources\Pages\Pages\CreatePage;
use App\Filament\Resources\Pages\Pages\EditPage;
use App\Models\User;
use Filament\Actions\Testing\TestAction;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Farbe und Schriftgrößen je Widget (ADR-68): Prüfregeln, Darstellung, Rückfall und Verwendungsnachweis der Palette.
* Die Ausgabe bisheriger Widgets ohne diese Einstellungen sichert CmsSectionLegacyRenderTest.
*/
class CmsWidgetAppearanceTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
DesignPalette::flush();
DesignPalette::save(null, 'Dunkelblau', '#032551', '#ffffff');
DesignPalette::save(null, 'Helles Grün', '#e8f0de', '#032551');
}
/**
* @param array<string, mixed> $settings
* @return array<string, mixed>
*/
private function heading(array $settings = []): array
{
return ['type' => 'heading', 'v' => 1, 'settings' => $settings, 'data' => ['heading' => 'Titel', 'intro' => 'Einleitung']];
}
/**
* @param array<string, mixed> $settings
* @return array<string, mixed>
*/
private function cards(array $settings = []): array
{
return ['type' => 'cards', 'v' => 1, 'settings' => $settings, 'data' => ['items' => [['title' => 'Karte', 'text' => 'Text']]]];
}
/**
* @param array<string, mixed> $widget
*/
private function publish(string $path, array $widget): Page
{
$page = Page::query()->create(['path' => $path]);
$service = app(PageService::class);
$service->publish($page, $service->saveRevision($page, [['layout' => 'u4', 'style' => [], 'slots' => [$widget]]], null, null, null));
return $page->fresh();
}
/**
* @param array<string, mixed> $widget
*/
private function hull(string $path, array $widget): string
{
$this->publish($path, $widget);
$html = $this->get($path)->assertOk()->getContent();
preg_match('/<section class="block widget [^>]*>/', $html, $match);
return $match[0] ?? '';
}
public function test_sizes_are_shared_settings_of_widgets_that_declare_them(): void
{
$registry = app(WidgetRegistry::class);
$this->assertSame(['small', 'medium', 'large', 'xlarge'], array_keys($registry->get('heading')->settingOptions()['heading_size']));
$this->assertSame(['normal', 'large', 'xlarge'], array_keys($registry->get('heading')->settingOptions()['text_size']));
$this->assertArrayNotHasKey('heading_size', $registry->get('prose')->settingOptions());
$this->assertArrayHasKey('text_size', $registry->get('prose')->settingOptions());
$this->assertArrayNotHasKey('text_size', $registry->get('inquiry')->settingOptions());
}
public function test_description_text_has_no_step_below_the_body_size(): void
{
$this->assertArrayNotHasKey('small', WidgetAppearance::TEXT);
$this->assertSame('Normal', WidgetAppearance::TEXT['normal']);
}
public function test_invalid_sizes_and_unknown_slots_are_rejected_on_save(): void
{
$registry = app(WidgetRegistry::class);
foreach ([['heading_size' => 'gigantic'], ['text_size' => 'small'], ['palette' => 'c9']] as $settings) {
try {
$registry->normalize([$this->heading($settings) + ['id' => null, 'variant' => 'standard', 'refs' => []]]);
$this->fail('Erwartet: Ablehnung für '.json_encode($settings));
} catch (ValidationException $e) {
$this->assertNotEmpty($e->errors());
}
}
}
public function test_a_widget_without_the_setting_is_rejected_when_it_sends_it_anyway(): void
{
$registry = app(WidgetRegistry::class);
$this->expectException(ValidationException::class);
$registry->normalize([['id' => null, 'type' => 'inquiry', 'v' => 1, 'variant' => 'standard', 'settings' => ['heading_size' => 'large'], 'data' => [], 'refs' => []]]);
}
public function test_sizes_become_classes_on_the_widget_shell_and_only_then(): void
{
$plain = $this->hull('/a', $this->heading());
$this->assertStringNotContainsString('widget-hs', $plain);
$this->assertStringNotContainsString('widget-ts', $plain);
$this->assertStringNotContainsString('widget-palette', $plain);
$this->assertStringNotContainsString('style=', $plain);
$sized = $this->hull('/b', $this->heading(['heading_size' => 'large', 'text_size' => 'xlarge']));
$this->assertStringContainsString('widget-hs widget-hs-large', $sized);
$this->assertStringContainsString('widget-ts widget-ts-xlarge', $sized);
}
public function test_a_palette_colour_on_a_plain_widget_sets_the_surface_on_the_shell(): void
{
$dark = $this->hull('/dunkel', $this->heading(['palette' => 'c1']));
$this->assertStringContainsString('widget-palette widget-palette--hull', $dark);
$this->assertStringContainsString('scope-dark', $dark);
$this->assertStringContainsString('--color-surface: #032551', $dark);
$light = $this->hull('/hell', $this->heading(['palette' => 'c2']));
$this->assertStringContainsString('--color-surface: #e8f0de', $light);
$this->assertStringNotContainsString('scope-dark', $light);
// Hell ist ausdrücklich markiert, damit ein helles Widget in einem dunklen Abschnitt dessen Regeln nicht erbt.
$this->assertStringContainsString('scope-light', $light);
$this->assertStringNotContainsString('scope-light', $dark);
}
public function test_cards_without_their_own_face_colour_the_whole_widget(): void
{
$standard = $this->hull('/karten-standard', $this->cards(['palette' => 'c2']));
$this->assertStringContainsString('widget-palette--face', $standard);
$compact = $this->hull('/karten-kompakt', $this->cards(['palette' => 'c2']) + ['variant' => 'compact']);
$this->assertStringContainsString('widget-palette--hull', $compact);
$this->assertStringNotContainsString('widget-palette--face', $compact);
$lines = $this->hull('/karten-linie', $this->cards(['palette' => 'c2', 'divider' => 'line']));
$this->assertStringContainsString('widget-palette--hull', $lines);
}
public function test_widgets_with_their_own_face_colour_the_face_not_a_frame(): void
{
$cards = $this->hull('/karten', $this->cards(['palette' => 'c2']));
$this->assertStringContainsString('widget-palette widget-palette--face', $cards);
$this->assertStringNotContainsString('widget-palette--hull', $cards);
}
public function test_a_stored_slot_that_no_longer_exists_falls_back_without_error(): void
{
$this->publish('/alt', $this->heading(['palette' => 'c2']));
DesignPalette::flush();
\DB::table('site_settings')->where('key', 'design.palette')->update(['value' => json_encode(['next' => 3, 'slots' => []])]);
DesignPalette::flush();
$html = $this->get('/alt')->assertOk()->getContent();
preg_match('/<section class="block widget [^>]*>/', $html, $match);
$this->assertStringNotContainsString('widget-palette', $match[0] ?? '');
$this->assertStringContainsString('Titel', $html);
}
public function test_a_colour_used_only_by_a_widget_is_counted_and_cannot_be_removed(): void
{
$page = $this->publish('/nur-widget', $this->heading(['palette' => 'c2']));
$usage = DesignPalette::usage();
$this->assertArrayHasKey('c2', $usage);
$this->assertSame($page->id, $usage['c2'][0]['page_id']);
$this->assertSame(1, $usage['c2'][0]['published']);
$this->assertArrayNotHasKey('c1', $usage);
$this->assertNotEmpty(DesignPalette::remove('c2'));
$this->assertNotNull(DesignPalette::find('c2'));
}
public function test_a_draft_that_only_a_widget_uses_is_counted_as_draft(): void
{
$page = $this->publish('/entwurf', $this->heading());
app(PageService::class)->saveRevision($page, [['layout' => 'u4', 'style' => [], 'slots' => [$this->heading(['palette' => 'c1'])]]], null, null, null);
$usage = DesignPalette::usage();
$this->assertSame(0, $usage['c1'][0]['published']);
$this->assertSame(1, $usage['c1'][0]['draft']);
}
public function test_image_text_takes_the_palette_colour_on_its_own_face(): void
{
$widget = ['type' => 'image_text', 'v' => 3, 'variant' => 'image_left', 'settings' => ['palette' => 'c1', 'surface' => 'cream'], 'data' => ['title' => 'Titel', 'body' => '<p>Text</p>'], 'refs' => []];
$this->publish('/bt', $widget);
$html = $this->get('/bt')->assertOk()->getContent();
$this->assertStringContainsString('lf-bt lf-bt--palette', $html);
$this->assertStringNotContainsString('lf-bt--cream', $html);
$this->assertStringContainsString('widget-palette widget-palette--face scope-dark', $html);
}
public function test_the_notice_widget_keeps_its_own_surface_setting_and_offers_no_palette(): void
{
$this->assertArrayNotHasKey('palette', app(WidgetRegistry::class)->get('notice')->settingOptions());
$this->assertArrayHasKey('surface', app(WidgetRegistry::class)->get('notice')->settingOptions());
}
public function test_the_hero_offers_sizes_but_no_palette(): void
{
$options = app(WidgetRegistry::class)->get('hero')->settingOptions();
$this->assertArrayHasKey('heading_size', $options);
$this->assertArrayHasKey('text_size', $options);
$this->assertArrayNotHasKey('palette', $options);
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
public function test_editor_saves_sizes_and_colour_and_shows_them_in_the_widget_dialog(): void
{
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
$widget = ['type' => 'heading', 'data' => ['settings' => ['heading_size' => 'xlarge', 'text_size' => 'large', 'palette' => 'c2'], 'data' => ['heading' => 'Größe', 'intro' => '<p>Text</p>']]];
$slots = [(string) Str::uuid() => ['widget' => [(string) Str::uuid() => $widget]]];
Livewire::actingAs($this->editor(), 'staff')
->test(CreatePage::class)
->fillForm(['path' => '/groesse', 'sections' => [['id' => null, 'layout' => 'u4', 'style' => [], 'slots' => $slots]]])
->call('create')
->assertHasNoFormErrors();
$page = Page::query()->where('path', '/groesse')->firstOrFail();
$this->assertSame(
['heading_size' => 'xlarge', 'text_size' => 'large', 'palette' => 'c2'],
$page->latestRevision->sections[0]['slots'][0]['settings'],
);
$component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]);
$sectionKey = array_key_first($component->get('data.sections'));
$slotKey = array_key_first($component->get("data.sections.$sectionKey.slots"));
$itemKey = array_key_first($component->get("data.sections.$sectionKey.slots.$slotKey.widget"));
$component->mountAction(TestAction::make('edit')->schemaComponent("sections.$sectionKey.slots.$slotKey.widget")->arguments(['item' => $itemKey]))
->assertFormFieldExists('settings.heading_size')
->assertFormFieldExists('settings.text_size')
->assertFormFieldExists('settings.palette');
}
/**
* Beim Einspielen der Migrationen speichert der Seeder eine Seite, bevor site_settings existiert. In PostgreSQL bricht eine
* dort fehlgeschlagene Abfrage die ganze Transaktion ab (SQLSTATE 25P02), auch wenn der Fehler abgefangen wird; die CI fand
* das erst beim Wegwerf-Postgres. Darum darf die Palette ohne Tabelle gar nicht abgefragt werden.
*/
public function test_saving_a_widget_before_the_settings_table_exists_runs_no_query_against_it(): void
{
Schema::drop('site_settings');
DesignPalette::flush();
$queries = [];
// Vor der Ausführung mitschneiden: Eine fehlgeschlagene Abfrage löst QueryExecuted nie aus, und sie ist genau der Fehler.
DB::beforeExecuting(function (string $sql) use (&$queries): void {
$queries[] = $sql;
});
$saved = app(WidgetRegistry::class)->normalize([['id' => null, 'type' => 'heading', 'v' => 2, 'variant' => 'standard', 'settings' => [], 'data' => ['heading' => 'Titel'], 'refs' => []]]);
$this->assertCount(1, $saved);
$this->assertSame([], DesignPalette::slots());
$this->assertSame([], array_filter($queries, fn (string $sql): bool => str_contains($sql, 'site_settings') && ! str_contains($sql, 'sqlite_master')));
}
}