Ergänze das CMS-Widget Hinweiszeile mit optional aufklappbarer Kurzdarstellung
Neues Widget notice (ADR-59): vollständiger Text und Kurztext als formatierte Texte, drei Modi (Volltext, responsiv mit Zeilengrenze, immer mit Kurzdarstellung), Symbol, Schriftgrößen XS bis L, Fläche, Rahmen und Akzentlinie. Der Server liefert immer den vollständigen Text; notice.js klappt ein und steht beim ersten Hinweis inline, damit nichts nach dem Laden springt (CLS 0,000). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a12818b752
commit
9e2d64a4a9
35 files changed
+1141
-40
No files matched your search
@@ -215,7 +215,7 @@ public function test_the_stylesheet_keeps_links_clickable_and_colors_readable_on
|
||||
$this->assertMatchesRegularExpression('/\.cards:not\(\.cards-compact, \.cards--lines\) \.card \.prose \{\s*--tc-marine: var\(--lf-navy\);/', $css);
|
||||
// Beide Verteilungen behalten gleich breite Spalten, damit die Trennstriche in der Mitte der Lücke liegen.
|
||||
$this->assertStringNotContainsString('justify-content: space-between', substr($css, (int) strpos($css, 'Redaktionelle Karten: Dachzeile')));
|
||||
$this->assertStringContainsString(':is(.widget-prose, .widget-heading, .widget-cards) .prose p', $css);
|
||||
$this->assertStringContainsString(':is(.widget-prose, .widget-heading, .widget-cards, .widget-notice) .prose p', $css);
|
||||
$this->assertStringContainsString('prefers-reduced-motion', substr($css, (int) strpos($css, '.cards--hover .card {')));
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ public function test_the_stylesheet_formats_the_intro_like_the_prose_widget(): v
|
||||
$css = (string) file_get_contents(resource_path('css/widgets.css'));
|
||||
|
||||
foreach (["span[data-size='xlarge']", "span.color[data-color='gruen']", '.ta-center', 'blockquote', 'p {', 'h3,'] as $needle) {
|
||||
$this->assertStringContainsString(':is(.widget-prose, .widget-heading, .widget-cards) .prose '.$needle, $css, $needle);
|
||||
$this->assertStringContainsString(':is(.widget-prose, .widget-heading, .widget-cards, .widget-notice) .prose '.$needle, $css, $needle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\PageService;
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Types\NoticeWidget;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetRegistry;
|
||||
use Filament\Forms\Components\RichEditor;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Hinweiszeile: formatierter Text in zwei Fassungen, drei Darstellungsmodi (Volltext, responsiv, immer mit Kurzdarstellung),
|
||||
* Symbol, Fläche, Rahmen und Akzentlinie. Der Server liefert immer den vollständigen Text; das Auf- und Zuklappen ist eine
|
||||
* Ergänzung durch resources/js/notice.js.
|
||||
*/
|
||||
class CmsNoticeWidgetTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private const SHORT = '<p>Mit KI erzeugt.</p>';
|
||||
|
||||
private const FULL = '<p>Dieses Bild wurde mit <strong>KI</strong> erzeugt und zeigt keine realen Personen. Mehr dazu in den <a href="/hinweise".</p>';
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<string, mixed> $settings
|
||||
*/
|
||||
private function publish(string $path, array $data, array $settings = []): string
|
||||
{
|
||||
$page = Page::query()->create(['path' => $path]);
|
||||
$service = app(PageService::class);
|
||||
$sections = [['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'notice', 'v' => 1, 'variant' => 'standard', 'settings' => $settings, 'data' => $data]]]];
|
||||
$service->publish($page, $service->saveRevision($page, $sections, null, null, null));
|
||||
|
||||
return $this->get($path)->assertOk()->getContent();
|
||||
}
|
||||
|
||||
private function type(): NoticeWidget
|
||||
{
|
||||
$type = app(WidgetRegistry::class)->get('notice');
|
||||
$this->assertInstanceOf(NoticeWidget::class, $type);
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
public function test_the_widget_is_registered_with_enumerated_settings_and_two_rich_text_fields(): void
|
||||
{
|
||||
$type = $this->type();
|
||||
|
||||
$this->assertSame('Hinweiszeile', $type->label());
|
||||
$this->assertSame([1, 2, 3, 4], $type->supportedUnits());
|
||||
$this->assertSame(['mode', 'line_limit', 'narrow', 'size', 'align', 'symbol', 'symbol_size', 'symbol_align', 'surface', 'border', 'bar'], array_keys($type->settings()));
|
||||
$this->assertSame(['full', 'responsive', 'collapsed'], array_keys(NoticeWidget::MODE));
|
||||
$this->assertSame(['xs', 's', 'm', 'l'], array_keys(NoticeWidget::SIZE));
|
||||
|
||||
// Die Grenze steht als Zeichenkette (nicht als Zahlenschlüssel), sonst verwürfe die Prüfung sie als Ganzzahl.
|
||||
foreach (array_keys(NoticeWidget::LINE_LIMIT) as $key) {
|
||||
$this->assertIsString($key);
|
||||
}
|
||||
|
||||
$children = collect($type->dataFields())->keyBy(fn ($field) => $field->getName());
|
||||
$this->assertInstanceOf(RichEditor::class, $children['text']);
|
||||
$this->assertInstanceOf(RichEditor::class, $children['short']);
|
||||
}
|
||||
|
||||
public function test_layout_falls_back_to_the_defaults_for_missing_and_invalid_values(): void
|
||||
{
|
||||
$default = NoticeWidget::layout([]);
|
||||
$this->assertSame(['mode' => 'full', 'limit' => null, 'narrow' => 40.0, 'size' => 's', 'align' => 'left', 'symbol' => null, 'symbolSize' => 's', 'symbolAlign' => 'top', 'surface' => null, 'border' => false, 'bar' => null], $default);
|
||||
|
||||
$invalid = NoticeWidget::layout(['mode' => 'weird', 'line_limit' => '99', 'narrow' => 'x', 'size' => 'xxl', 'align' => 'justify', 'symbol' => 'gibt-es-nicht', 'surface' => 'pink', 'border' => 'thick', 'bar' => 'diagonal']);
|
||||
$this->assertSame($default, $invalid);
|
||||
|
||||
$chosen = NoticeWidget::layout(['mode' => 'responsive', 'line_limit' => 'l2', 'narrow' => 'wide', 'size' => 'l', 'align' => 'center', 'symbol_size' => 'xs', 'surface' => 'yellow', 'border' => 'line', 'bar' => 'left']);
|
||||
$this->assertSame(['responsive', 2, 56.0, 'l', 'center', 'xs', 'yellow', true, 'left'], [$chosen['mode'], $chosen['limit'], $chosen['narrow'], $chosen['size'], $chosen['align'], $chosen['symbolSize'], $chosen['surface'], $chosen['border'], $chosen['bar']]);
|
||||
}
|
||||
|
||||
public function test_a_mode_without_a_short_text_shows_the_full_text_like_the_full_mode(): void
|
||||
{
|
||||
$this->assertSame('full', NoticeWidget::layout(['mode' => 'collapsed'], false)['mode']);
|
||||
$this->assertSame('collapsed', NoticeWidget::layout(['mode' => 'collapsed'], true)['mode']);
|
||||
|
||||
$html = $this->publish('/hinweis-ohne-kurztext', ['text' => self::FULL], ['mode' => 'collapsed']);
|
||||
|
||||
$this->assertStringContainsString('class="notice-full prose"', $html);
|
||||
$this->assertStringNotContainsString('data-notice=', $html);
|
||||
$this->assertStringNotContainsString('notice-toggle', $html);
|
||||
}
|
||||
|
||||
public function test_only_the_preview_tells_that_the_mode_needs_a_short_text(): void
|
||||
{
|
||||
$widget = ['data' => ['text' => self::FULL], 'settings' => ['mode' => 'responsive']];
|
||||
|
||||
$this->assertNull($this->type()->resolve($widget, RenderContext::publicPage())['notice']);
|
||||
$this->assertStringContainsString('Kurztext', (string) $this->type()->resolve($widget, RenderContext::preview())['notice']);
|
||||
$this->assertNull($this->type()->resolve(['data' => $widget['data'], 'settings' => []], RenderContext::preview())['notice']);
|
||||
$this->assertNull($this->type()->resolve(['data' => $widget['data'] + ['short' => self::SHORT], 'settings' => $widget['settings']], RenderContext::preview())['notice']);
|
||||
}
|
||||
|
||||
public function test_the_full_mode_outputs_the_text_only_without_any_toggle_markup(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-volltext', ['text' => self::FULL, 'short' => self::SHORT]);
|
||||
|
||||
$this->assertStringContainsString('<div class="notice">', $html);
|
||||
$this->assertStringContainsString('<div class="notice-full prose" data-notice-full>', $html);
|
||||
$this->assertStringContainsString('<strong>KI</strong>', $html);
|
||||
$this->assertStringContainsString('<a href="/hinweise"', $html);
|
||||
$this->assertStringNotContainsString('notice-short', $html);
|
||||
$this->assertStringNotContainsString('data-notice-toggle', $html);
|
||||
$this->assertStringNotContainsString('Mit KI erzeugt.', $html, 'Im Volltext erscheint der Kurztext nirgends.');
|
||||
}
|
||||
|
||||
public function test_the_responsive_mode_carries_its_rules_as_data_and_always_outputs_the_full_text(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-responsiv', ['text' => self::FULL, 'short' => self::SHORT], ['mode' => 'responsive', 'line_limit' => 'l2', 'narrow' => 'narrow']);
|
||||
|
||||
$this->assertStringContainsString('data-notice="" data-mode="responsive" data-narrow="30" data-limit="2"', $html);
|
||||
$this->assertStringContainsString('<div class="notice-short prose" data-notice-short><p>Mit KI erzeugt.</p></div>', $html);
|
||||
// Ohne JavaScript bleibt der vollständige Hinweis sichtbar: Er steht ohne hidden im Quelltext, der Schalter ist versteckt.
|
||||
$this->assertMatchesRegularExpression('~<div class="notice-full prose" data-notice-full><p>Dieses Bild wurde mit <strong>KI</strong>~', $html);
|
||||
$this->assertMatchesRegularExpression('~<button type="button" class="notice-toggle" data-notice-toggle aria-expanded="false" hidden>~', $html);
|
||||
$this->assertStringContainsString('Vollständigen Hinweis anzeigen', $html);
|
||||
$this->assertStringNotContainsString('data-ready', $html);
|
||||
}
|
||||
|
||||
public function test_without_a_line_limit_the_responsive_mode_has_no_limit_attribute_and_the_default_threshold(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-ohne-grenze', ['text' => self::FULL, 'short' => self::SHORT], ['mode' => 'responsive']);
|
||||
|
||||
$this->assertStringContainsString('data-mode="responsive" data-narrow="40">', $html);
|
||||
$this->assertStringNotContainsString('data-limit', $html);
|
||||
}
|
||||
|
||||
public function test_the_always_collapsed_mode_is_marked_so_that_width_and_limit_do_not_matter(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-immer', ['text' => self::FULL, 'short' => self::SHORT], ['mode' => 'collapsed', 'line_limit' => 'l1']);
|
||||
|
||||
$this->assertStringContainsString('data-mode="collapsed"', $html);
|
||||
|
||||
$script = (string) file_get_contents(base_path('resources/js/notice.js'));
|
||||
$this->assertStringContainsString("if (mode === 'responsive') {", $script);
|
||||
$this->assertMatchesRegularExpression("/var next = true;\s+if \(mode === 'responsive'\)/", $script, 'Im Modus „collapsed“ ist der Hinweis ohne Prüfung einklappbar.');
|
||||
}
|
||||
|
||||
public function test_the_icon_size_is_independent_of_the_font_size_and_the_symbol_is_decorative(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-symbol', ['text' => self::FULL], ['symbol' => 'buch', 'symbol_size' => 'xs', 'size' => 'xs', 'symbol_align' => 'center']);
|
||||
|
||||
$this->assertMatchesRegularExpression('~<svg[^>]*class="brand-symbol notice-symbol notice-symbol--xs"~', $html);
|
||||
$this->assertStringContainsString('aria-hidden="true"', $html);
|
||||
$this->assertStringContainsString('notice notice--size-xs notice--symbol-center', $html);
|
||||
|
||||
$css = (string) file_get_contents(base_path('resources/css/components/lf-hinweiszeile.css'));
|
||||
$this->assertMatchesRegularExpression('/\.notice-symbol--xs \{ --notice-sym: 1rem; \}/', $css);
|
||||
$this->assertMatchesRegularExpression('/\.notice--size-xs \{ --notice-fs: 0\.8125rem; \}/', $css);
|
||||
}
|
||||
|
||||
public function test_the_default_markup_has_no_class_beyond_the_base_class(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-vorgabe', ['text' => self::FULL]);
|
||||
|
||||
$this->assertStringContainsString('<div class="notice">', $html);
|
||||
$this->assertStringNotContainsString('notice-symbol', $html);
|
||||
$this->assertStringNotContainsString('scope-light', $html);
|
||||
}
|
||||
|
||||
public function test_surface_border_and_accent_bar_use_tokens_and_the_light_value_group(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-flaeche', ['text' => self::FULL], ['surface' => 'green', 'border' => 'line', 'bar' => 'left', 'align' => 'center']);
|
||||
|
||||
$this->assertStringContainsString('notice--center', $html);
|
||||
$this->assertStringContainsString('notice--surface scope-light notice--surface-green', $html);
|
||||
$this->assertStringContainsString('notice--border', $html);
|
||||
$this->assertStringContainsString('notice--bar notice--bar-left', $html);
|
||||
|
||||
$css = (string) file_get_contents(base_path('resources/css/components/lf-hinweiszeile.css'));
|
||||
$this->assertSame(0, preg_match('/#[0-9a-f]{3,8}\b/i', preg_replace('/#a8212b/', '', $css)), 'Farben nur über Tokens (die Rotmarkierung entspricht der der Fließtexte).');
|
||||
$this->assertStringContainsString('--notice-bg: var(--lf-green-tint)', $css);
|
||||
$this->assertStringContainsString('border-inline-start: 4px solid var(--color-action)', $css);
|
||||
}
|
||||
|
||||
public function test_clean_stores_both_texts_sanitised_and_requires_the_full_text(): void
|
||||
{
|
||||
$errors = new WidgetErrors;
|
||||
[$clean] = $this->type()->clean(['text' => '<p onclick="x()">Hinweis <a href="javascript:alert(1)">Link</a> <script>x()</script><em>kursiv</em></p>', 'short' => '<p></p>'], [], $errors);
|
||||
|
||||
$this->assertTrue($errors->isEmpty());
|
||||
$this->assertStringNotContainsString('onclick', $clean['text']);
|
||||
$this->assertStringNotContainsString('javascript:', $clean['text']);
|
||||
$this->assertStringNotContainsString('<script', $clean['text']);
|
||||
$this->assertStringContainsString('<em>kursiv</em>', $clean['text']);
|
||||
$this->assertArrayNotHasKey('short', $clean, 'Ein leerer Kurztext bleibt ohne Spur.');
|
||||
|
||||
$missing = new WidgetErrors;
|
||||
$this->type()->clean(['text' => '<p></p>'], [], $missing);
|
||||
$this->assertFalse($missing->isEmpty());
|
||||
}
|
||||
|
||||
public function test_colors_sizes_and_links_survive_in_both_texts(): void
|
||||
{
|
||||
$html = $this->publish('/hinweis-format', [
|
||||
'text' => '<p><span class="color" data-color="rot">Rot</span> und <span data-size="large">groß</span> mit <a href="/a"</p>',
|
||||
'short' => '<p><span class="color" data-color="gruen">Grün</span> <a href="#kurz"</p>',
|
||||
], ['mode' => 'collapsed']);
|
||||
|
||||
$this->assertStringContainsString('data-color="rot"', $html);
|
||||
$this->assertStringContainsString('data-size="large"', $html);
|
||||
$this->assertStringContainsString('data-color="gruen"', $html);
|
||||
$this->assertStringContainsString('<a href="/a"', $html);
|
||||
$this->assertStringContainsString('<a href="#kurz"', $html);
|
||||
}
|
||||
|
||||
public function test_switching_the_mode_loses_neither_text(): void
|
||||
{
|
||||
$registry = app(WidgetRegistry::class);
|
||||
$make = fn (array $settings): array => $registry->normalize([['type' => 'notice', 'v' => 1, 'variant' => 'standard', 'settings' => $settings, 'data' => ['text' => self::FULL, 'short' => self::SHORT]]])[0];
|
||||
|
||||
foreach (['full', 'responsive', 'collapsed'] as $mode) {
|
||||
$stored = $make(['mode' => $mode]);
|
||||
|
||||
$this->assertSame($mode, $stored['settings']['mode']);
|
||||
$this->assertStringContainsString('KI', $stored['data']['text'], $mode);
|
||||
$this->assertStringContainsString('Mit KI erzeugt.', $stored['data']['short'], $mode);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_values_outside_the_fixed_lists_are_rejected_on_save(): void
|
||||
{
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(WidgetRegistry::class)->normalize([['type' => 'notice', 'v' => 1, 'variant' => 'standard', 'settings' => ['mode' => 'sometimes'], 'data' => ['text' => self::FULL]]]);
|
||||
}
|
||||
|
||||
public function test_the_script_stands_inline_once_per_page_so_that_nothing_jumps_after_loading(): void
|
||||
{
|
||||
$page = Page::query()->create(['path' => '/hinweise-zwei']);
|
||||
$service = app(PageService::class);
|
||||
$slot = fn (array $settings): array => ['type' => 'notice', 'v' => 1, 'variant' => 'standard', 'settings' => $settings, 'data' => ['text' => self::FULL, 'short' => self::SHORT]];
|
||||
$sections = [
|
||||
['layout' => 'u4', 'style' => [], 'slots' => [$slot(['mode' => 'responsive'])]],
|
||||
['layout' => 'u4', 'style' => [], 'slots' => [$slot(['mode' => 'collapsed'])]],
|
||||
['layout' => 'u4', 'style' => [], 'slots' => [$slot([])]],
|
||||
];
|
||||
$service->publish($page, $service->saveRevision($page, $sections, null, null, null));
|
||||
$html = $this->get('/hinweise-zwei')->assertOk()->getContent();
|
||||
|
||||
$this->assertSame(1, substr_count($html, 'window.lfNotice = { scan: scan };'), 'Das Skript steht einmal je Seite im Quelltext.');
|
||||
$this->assertSame(2, substr_count($html, 'window.lfNotice&&window.lfNotice.scan(document.currentScript.parentNode)'), 'Jeder einklappbare Hinweis ruft es auf; der Volltext braucht es nicht.');
|
||||
$this->assertLessThan(strpos($html, 'notice-full prose', (int) strpos($html, 'data-mode="collapsed"')), (int) strpos($html, 'window.lfNotice = {'), 'Das Skript steht vor dem zweiten Hinweis.');
|
||||
|
||||
$plain = $this->publish('/hinweis-nur-volltext', ['text' => self::FULL, 'short' => self::SHORT]);
|
||||
$this->assertStringNotContainsString('lfNotice', $plain);
|
||||
}
|
||||
|
||||
public function test_the_inline_script_is_the_module_file_without_comments(): void
|
||||
{
|
||||
$boot = NoticeWidget::bootScript();
|
||||
|
||||
$this->assertStringNotContainsString('</script', $boot);
|
||||
$this->assertStringNotContainsString('/*', $boot);
|
||||
$this->assertStringNotContainsString('Kein Springen', $boot);
|
||||
$this->assertStringContainsString('window.lfNotice = { scan: scan };', $boot);
|
||||
$this->assertStringContainsString('lineCount(full) > limit', $boot);
|
||||
}
|
||||
|
||||
public function test_the_stylesheet_and_script_cover_progressive_enhancement_and_accessibility(): void
|
||||
{
|
||||
$css = (string) file_get_contents(base_path('resources/css/components/lf-hinweiszeile.css'));
|
||||
$script = (string) file_get_contents(base_path('resources/js/notice.js'));
|
||||
$app = (string) file_get_contents(base_path('resources/css/app.css'));
|
||||
$entry = (string) file_get_contents(base_path('resources/js/app.js'));
|
||||
|
||||
$this->assertStringContainsString("@import './components/lf-hinweiszeile.css';", $app);
|
||||
$this->assertStringContainsString("import './notice.js';", $entry);
|
||||
|
||||
// Grundzustand ohne JavaScript: Kurztext aus, Schalter versteckt; Kurzdarstellung erst mit data-state.
|
||||
$this->assertMatchesRegularExpression('/\.notice-short \{ display: none; \}/', $css);
|
||||
$this->assertStringContainsString('.notice-toggle[hidden] { display: none; }', $css);
|
||||
$this->assertStringContainsString(".notice[data-state='short'] .notice-short { display: block; }", $css);
|
||||
// Kein Flackern: unsichtbar bis zum ersten Ergebnis, mit Rückfall ohne Skript.
|
||||
$this->assertStringContainsString('html.js .notice[data-notice]:not([data-ready])', $css);
|
||||
$this->assertStringContainsString('animation: notice-failsafe', $css);
|
||||
// Links bleiben über der Klickfläche bedienbar; Fokus und Druck.
|
||||
$this->assertMatchesRegularExpression('/\.notice \.prose a \{\s*position: relative;\s*z-index: 1;/', $css);
|
||||
$this->assertStringContainsString('.notice:has(.notice-toggle:focus-visible)', $css);
|
||||
$this->assertStringContainsString('@media print', $css);
|
||||
|
||||
// Schalter: aria-expanded und aria-controls; Zeilen werden gezählt, nicht die Höhe geschätzt.
|
||||
$this->assertStringContainsString("toggle.setAttribute('aria-expanded'", $script);
|
||||
$this->assertStringContainsString("toggle.setAttribute('aria-controls'", $script);
|
||||
$this->assertStringContainsString('getClientRects', $script);
|
||||
$this->assertStringContainsString('ResizeObserver', $script);
|
||||
$this->assertStringContainsString('lineCount(full) > limit', $script);
|
||||
}
|
||||
|
||||
public function test_the_prose_rules_of_the_website_texts_apply_to_the_notice(): void
|
||||
{
|
||||
$css = (string) file_get_contents(base_path('resources/css/widgets.css'));
|
||||
|
||||
$this->assertStringContainsString('.scope-dark :is(.widget-prose, .widget-heading, .widget-cards, .widget-notice) .prose {', $css);
|
||||
$this->assertStringContainsString(":is(.widget-prose, .widget-heading, .widget-cards, .widget-notice) .prose span.color[data-color='rot']", $css);
|
||||
}
|
||||
|
||||
public function test_the_summary_names_the_start_of_the_full_text(): void
|
||||
{
|
||||
$summary = $this->type()->summary(['data' => ['text' => self::FULL]]);
|
||||
|
||||
$this->assertStringStartsWith('Dieses Bild wurde mit KI erzeugt', $summary);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user