Files
laraundfelix/tests/Feature/CmsImageFramingTest.php
T
LarsandClaude Sonnet 5.5 9c4bbc7b44
CI Feature / test (push) Successful in 7m5s
Deploy Test / test (push) Successful in 6m24s
Deploy Test / deploy (push) Waiting to run
Lass die Höhe des Einstiegs mit Foto auf breiten Plätzen mitwachsen (ADR-71)
Bei Abschnittsbreite Breit oder Vollbreite blieb die Mindesthöhe fest, das Foto wurde zu
einem schmalen Streifen beschnitten (bei 2560 px nur 28 Prozent der Bildhöhe). Die
Mindesthöhe folgt nun der Breite oberhalb des Standardabschnitts, begrenzt auf 80 Prozent
der Fensterhöhe. Standardabschnitt, Tablet und Handy bleiben unverändert.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-01 13:32:43 +01:00

224 lines
10 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\PillarService;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\Support\ImageFraming;
use App\Domain\Cms\Widgets\WidgetRegistry;
use Filament\Forms\Components\Slider;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Schemas\Schema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use Livewire\Component;
use Tests\TestCase;
/**
* Bildausschnitt und Zoom (ADR-70): Karten tragen ihn je Karte, Einstieg, Bild und Text und die Premium-Karten je Widget.
* Ohne gesetzten Wert bleibt die Ausgabe unverändert.
*/
class CmsImageFramingTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Storage::fake('media_public');
Storage::disk('media_public')->put('cms/bild.jpg', 'bild');
}
public function test_a_card_with_framing_gets_position_origin_scale_and_a_larger_source_size(): void
{
$html = $this->publish('/karten', [$this->cards([
['title' => 'Eng', 'image' => 'cms/bild.jpg', 'focus_x' => 30, 'focus_y' => 0, 'zoom' => 180],
['title' => 'Normal', 'image' => 'cms/bild.jpg'],
])]);
$this->assertStringContainsString('style="object-position: 30% 0%; transform-origin: 30% 0%; scale: 1.8"', $html);
$this->assertSame(1, substr_count($html, 'style="object-position'), 'Die Karte ohne Ausschnitt trägt keinen Stil.');
$this->assertStringContainsString('class="card-image" src=', $html);
}
public function test_cards_without_framing_render_exactly_as_before(): void
{
$html = $this->publish('/karten-alt', [$this->cards([['title' => 'Alt', 'image' => 'cms/bild.jpg']])]);
$this->assertDoesNotMatchRegularExpression('/<img class="card-image"[^>]* style=/', $html);
$this->assertStringNotContainsString('transform-origin', $html);
}
public function test_the_framing_of_a_card_is_saved_only_when_it_differs_and_only_with_an_image(): void
{
$page = Page::query()->create(['path' => '/karten-speichern']);
$service = app(PageService::class);
$revision = $service->saveRevision($page, [$this->cards([
['title' => 'Ausschnitt', 'image' => 'cms/bild.jpg', 'focus_x' => 20, 'focus_y' => 50, 'zoom' => 150],
['title' => 'Vorgabe', 'image' => 'cms/bild.jpg', 'focus_x' => 50, 'focus_y' => 50, 'zoom' => 100],
['title' => 'Ohne Bild', 'focus_x' => 10, 'zoom' => 200],
])], null, null, null);
$items = $revision->sections[0]['slots'][0]['data']['items'];
$this->assertSame(20, $items[0]['focus_x']);
$this->assertSame(150, $items[0]['zoom']);
$this->assertArrayNotHasKey('focus_y', $items[0], 'Die Mitte ist die Vorgabe und wird nicht gespeichert.');
$this->assertArrayNotHasKey('focus_x', $items[1]);
$this->assertArrayNotHasKey('zoom', $items[1]);
$this->assertArrayNotHasKey('zoom', $items[2]);
}
public function test_a_zoom_above_the_cap_is_rejected(): void
{
$page = Page::query()->create(['path' => '/karten-zu-gross']);
$this->expectException(ValidationException::class);
app(PageService::class)->saveRevision($page, [$this->cards([['title' => 'X', 'image' => 'cms/bild.jpg', 'zoom' => 400]])], null, null, null);
}
public function test_the_hero_puts_photo_and_video_into_a_clipping_frame_only_with_zoom(): void
{
$zoomed = $this->publish('/einstieg-zoom', [$this->widget('hero', 'photo_split', ['heading' => 'Stark', 'focus_x' => 40, 'focus_y' => 10, 'zoom' => 160], ['image' => 'cms/bild.jpg'])]);
$this->assertStringContainsString('--lf-hero-focus-x: 40%; --lf-hero-focus-y: 10%; --lf-hero-zoom: 1.6', $zoomed);
$this->assertMatchesRegularExpression('#<div class="lf-hero__frame">\s*<img class="lf-hero__photo"#', $zoomed);
$plain = $this->publish('/einstieg-ohne', [$this->widget('hero', 'photo_split', ['heading' => 'Stark'], ['image' => 'cms/bild.jpg'])]);
$this->assertStringNotContainsString('lf-hero__frame', $plain);
$this->assertStringNotContainsString('--lf-hero-zoom', $plain);
}
public function test_image_and_text_wraps_the_image_in_a_frame_only_with_zoom(): void
{
$zoomed = $this->publish('/bt-zoom', [$this->widget('image_text', 'image_left', ['title' => 'Bild', 'body' => '<p>Text</p>', 'zoom' => 200], ['image' => 'cms/bild.jpg'])]);
$this->assertStringContainsString('--bt-zoom: 2', $zoomed);
$this->assertMatchesRegularExpression('#<div class="lf-bt__frame">\s*<img class="lf-bt__image"#', $zoomed);
$plain = $this->publish('/bt-ohne', [$this->widget('image_text', 'image_left', ['title' => 'Bild', 'body' => '<p>Text</p>'], ['image' => 'cms/bild.jpg'])]);
$this->assertStringNotContainsString('lf-bt__frame', $plain);
$this->assertStringNotContainsString('--bt-zoom', $plain);
}
public function test_the_premium_card_zooms_the_cut_out_but_never_the_whole_image(): void
{
$pillars = app(PillarService::class);
$pillar = $pillars->create('Hilfe holen');
$pillars->present($pillar, 'Hilfe holen', 'hilfe-holen', 'Kurztext', true, 10);
$cover = $this->publish('/pc-zoom', [$this->widget('topic_card', 'adaptive', ['alt' => 'Park', 'zoom' => 150], ['pillar' => $pillar->id, 'image' => 'cms/bild.jpg'])]);
$this->assertStringContainsString('lf-pc--zoom', $cover);
$this->assertStringContainsString('--pc-zoom: 1.5', $cover);
$contain = $this->publish('/pc-ganz', [$this->widget('topic_card', 'adaptive', ['alt' => 'Park', 'zoom' => 150], ['pillar' => $pillar->id, 'image' => 'cms/bild.jpg'], ['fit' => 'contain'])]);
$this->assertStringNotContainsString('lf-pc--zoom', $contain);
$this->assertStringNotContainsString('--pc-zoom', $contain);
}
public function test_every_zooming_stylesheet_keeps_the_frame_filled_and_clipped(): void
{
$hero = (string) file_get_contents(resource_path('css/components/lf-hero.css'));
$text = (string) file_get_contents(resource_path('css/components/lf-bild-text.css'));
$card = (string) file_get_contents(resource_path('css/components/lf-premium-card.css'));
$this->assertMatchesRegularExpression('/\.lf-hero__frame\s*\{[^}]*overflow: hidden/', $hero);
$this->assertMatchesRegularExpression('/\.lf-bt__frame\s*\{[^}]*overflow: hidden/', $text);
$this->assertMatchesRegularExpression('/\.lf-pc--zoom \.lf-pc__visual\s*\{[^}]*overflow: hidden/', $card);
$this->assertStringContainsString('scale: var(--lf-hero-zoom, 1)', $hero);
$this->assertStringContainsString('scale: var(--bt-zoom, 1)', $text);
$this->assertStringContainsString('scale: var(--pc-zoom, 1)', $card);
}
public function test_the_hero_photo_keeps_its_aspect_ratio_on_wide_places(): void
{
$css = (string) file_get_contents(resource_path('css/components/lf-hero.css'));
// Die Mindesthöhe wächst mit der Breite (cqi), begrenzt auf 80 % der Fensterhöhe; schmale Plätze behalten min-block-size: 0.
$this->assertMatchesRegularExpression('/\.lf-hero--photo-split\s*\{\s*min-block-size: min\(.*100cqi \* var\(--ds-media\) \* 0\.0009146.*80svh/s', $css);
$this->assertGreaterThan(
strpos($css, '.lf-hero--photo-split {'),
strpos($css, '@container lf-hero (max-width: 859.99px) {'),
'Die Regel für breite Plätze steht vor dem Sicherheitsumbruch, der die Mindesthöhe wieder auf 0 setzt.',
);
}
public function test_the_dialogs_offer_a_zoom_slider_next_to_the_focus(): void
{
$hero = app(WidgetRegistry::class)->get('hero');
$fields = collect($hero->mediaSchema())->flatMap(fn ($group) => $group->getDefaultChildComponents())->keyBy(fn ($field) => method_exists($field, 'getName') ? $field->getName() : spl_object_id($field));
$this->assertInstanceOf(Slider::class, $fields['zoom']);
$this->assertInstanceOf(Slider::class, $fields['focus_x']);
}
public function test_the_sliders_show_the_default_of_the_widget_when_nothing_is_saved_and_keep_saved_values(): void
{
$livewire = new class extends Component implements HasSchemas
{
use InteractsWithSchemas;
public ?array $data = [];
public function render(): string
{
return '';
}
};
// Filaments Regler machte aus „kein Wert“ eine 0 und zeigte sie statt der Vorgabe der Darstellung.
Schema::make($livewire)->statePath('data')
->components(ImageFraming::sliders(68, 50))
->fill(['focus_y' => 0, 'zoom' => 150]);
$this->assertSame(68, $livewire->data['focus_x']);
$this->assertSame(0, (int) $livewire->data['focus_y'], 'Ein gespeicherter Wert 0 bleibt 0.');
$this->assertSame(150, (int) $livewire->data['zoom']);
Schema::make($livewire)->statePath('data')
->components(ImageFraming::sliders())
->fill([]);
$this->assertSame([50, 50, 100], [$livewire->data['focus_x'], $livewire->data['focus_y'], $livewire->data['zoom']]);
}
/**
* @param list<array<string, mixed>> $items
* @return array<string, mixed>
*/
private function cards(array $items): array
{
return ['type' => 'cards', 'v' => 2, 'settings' => [], 'data' => ['items' => $items]];
}
/**
* @param array<string, mixed> $data
* @param array<string, mixed> $refs
* @param array<string, mixed> $settings
* @return array<string, mixed>
*/
private function widget(string $type, string $variant, array $data, array $refs = [], array $settings = []): array
{
return ['type' => $type, 'v' => $type === 'hero' ? 2 : ($type === 'image_text' ? 3 : 1), 'variant' => $variant, 'settings' => $settings, 'data' => $data, 'refs' => $refs];
}
/**
* @param list<array<string, mixed>> $widgets
*/
private function publish(string $path, array $widgets): string
{
$page = Page::query()->create(['path' => $path]);
$service = app(PageService::class);
$service->publish($page, $service->saveRevision($page, [['layout' => 'u4', 'style' => [], 'slots' => $widgets]], null, null, null));
return $this->get($path)->assertOk()->getContent();
}
}