, fail: bool} */ private object $extractor; protected function setUp(): void { parent::setUp(); Storage::fake('cms_incoming'); Storage::fake('media_derived'); Storage::fake('media_public'); Storage::fake('media_originals'); Filament::setCurrentPanel(Filament::getPanel('verwaltung')); $jpeg = $this->jpeg(1600, 900); $this->extractor = new class($jpeg) extends ThumbnailDeriver { /** @var list */ public array $calls = []; public bool $fail = false; public function __construct(private readonly string $jpeg) {} public function frameJpeg(ContentElementVersion $version, float $seconds, int $width = 640, bool $original = false): ?string { $this->calls[] = ['second' => $seconds, 'width' => $width, 'original' => $original]; return $this->fail ? null : $this->jpeg; } }; $this->app->instance(ThumbnailDeriver::class, $this->extractor); } public function test_a_chosen_frame_becomes_one_image_medium_that_is_released_for_the_website(): void { $film = $this->film(); $editor = User::factory()->redakteur()->create(); $still = app(FilmStill::class)->fromSecond($film, 41.5, $editor)->refresh(); $this->assertSame('bild', $still->output_class); $this->assertSame('Standbild: Lara im Park (Fassung 1, 41,5 s)', $still->display_title); $this->assertSame('Szene aus dem Film „Lara im Park“', $still->website_alt); $this->assertSame('Foto: Lara und Felix', $still->website_credit, 'Die Quellenzeile des Films wird übernommen.'); $this->assertSame(0, $still->versions()->count(), 'Es entsteht keine Inhaltsfassung.'); $this->assertSame(0, Grant::query()->count(), 'Es entsteht keine Berechtigung.'); $this->assertNotNull($still->website_released_at, 'Die Warteschlange gibt das abgeleitete Bild für die Website frei.'); $this->assertSame([['second' => 41.5, 'width' => 1600, 'original' => true]], $this->extractor->calls, 'Aus der Mediendatei in voller Auflösung, nicht aus der kleinen Stufe.'); $this->assertSame($still->id, FilmStillSource::query()->where('film_element_id', $film->id)->value('still_element_id')); $this->assertSame($editor->id, FilmStillSource::query()->value('actor_id')); $this->assertSame(1, ContentElement::query()->where('output_class', 'film')->count(), 'Der Film bleibt unverändert.'); } public function test_the_same_frame_is_never_extracted_or_stored_twice(): void { $film = $this->film(); $first = app(FilmStill::class)->fromSecond($film, 12); $second = app(FilmStill::class)->fromSecond($film, 12.0); $other = app(FilmStill::class)->fromSecond($film, 13); $this->assertSame($first->id, $second->id); $this->assertNotSame($first->id, $other->id); $this->assertCount(2, $this->extractor->calls, 'Die zweite Anforderung derselben Stelle liest das gespeicherte Bild.'); $this->assertSame(2, ContentElement::query()->where('output_class', 'bild')->count()); $first->update(['archived_at' => now()]); $this->assertNotSame($first->id, app(FilmStill::class)->fromSecond($film, 12)->id, 'Ein archiviertes Bild wird nicht wiederverwendet.'); } public function test_the_thumbnail_of_the_film_is_taken_over_in_full_resolution_or_as_its_own_file(): void { $film = $this->film(); $film->forceFill(['thumbnail_path' => 'vorschau/element-'.$film->id.'.jpg', 'thumbnail_source' => 'frame:'.$film->versions()->value('id').'@41.5'])->save(); Storage::disk('media_derived')->put($film->thumbnail_path, $this->jpeg(640, 360)); $fromFrame = app(FilmStill::class)->fromThumbnail($film); $this->assertSame([['second' => 41.5, 'width' => 1600, 'original' => true]], $this->extractor->calls, 'Eine Filmstelle wird in voller Auflösung neu gezogen.'); $this->assertSame($fromFrame->id, app(FilmStill::class)->fromSecond($film, 41.5)->id, 'Dieselbe Stelle ergibt dasselbe Medium, egal auf welchem Weg.'); $this->assertCount(1, $this->extractor->calls); $own = $this->film('Interview'); $own->forceFill(['thumbnail_path' => 'vorschau/element-'.$own->id.'.jpg', 'thumbnail_source' => 'eigen'])->save(); Storage::disk('media_derived')->put($own->thumbnail_path, $this->jpeg(800, 450)); $copied = app(FilmStill::class)->fromThumbnail($own); $this->assertSame($copied->id, app(FilmStill::class)->fromThumbnail($own)->id, 'Dieselbe Vorschaudatei ergibt dasselbe Medium.'); $this->assertCount(1, $this->extractor->calls, 'Ein eigenes Vorschaubild wird kopiert, nicht aus dem Film gezogen.'); $this->assertStringStartsWith('Standbild: Interview (Vorschaubild)', $copied->refresh()->display_title); } public function test_confidential_blocked_or_unusable_films_yield_no_image(): void { $content = app(ContentService::class); $guide = $this->film('Traineranleitung als Film'); $guide->update(['editorial_content_kind_id' => EditorialContentKind::query()->where('slug', 'traineranleitung')->value('id')]); $archived = $this->film('Archiviert'); $archived->update(['archived_at' => now()]); $draftOnly = $content->createElement('film', 'film', 'Nur Entwurf'); $content->draftElementVersion($draftOnly, 'Entwurf'); $text = $content->createElement('text', 'text', 'Ein Text'); $noThumbnail = $this->film('Ohne Vorschaubild'); $cases = [ [fn () => app(FilmStill::class)->fromSecond($guide, 5), 'Traineranleitungen'], [fn () => app(FilmStill::class)->fromSecond($archived, 5), 'archiviert'], [fn () => app(FilmStill::class)->fromSecond($draftOnly, 5), 'bereitgestellte Fassung'], [fn () => app(FilmStill::class)->fromSecond($text, 5), 'Nur aus einem Film'], [fn () => app(FilmStill::class)->fromSecond($noThumbnail, -1), 'außerhalb des Films'], [fn () => app(FilmStill::class)->fromSecond($noThumbnail, 7201), 'außerhalb des Films'], [fn () => app(FilmStill::class)->fromThumbnail($noThumbnail), 'noch kein Vorschaubild'], ]; foreach ($cases as [$call, $message]) { try { $call(); $this->fail("Erwartete Ablehnung: $message"); } catch (DomainRuleException $exception) { $this->assertStringContainsString($message, $exception->getMessage()); } } $this->extractor->fail = true; try { app(FilmStill::class)->fromSecond($noThumbnail, 5); $this->fail('Eine nicht ziehbare Stelle wurde angenommen.'); } catch (DomainRuleException $exception) { $this->assertStringContainsString('kein Bild erzeugen', $exception->getMessage()); } $this->assertSame(0, ContentElement::query()->where('output_class', 'bild')->count(), 'Bei einer Ablehnung entsteht kein Medium.'); $this->assertSame(0, FilmStillSource::query()->count()); } public function test_the_card_shows_the_stored_image_and_a_page_view_never_extracts_a_frame(): void { $film = $this->film(); $still = app(FilmStill::class)->fromSecond($film, 41.5)->refresh(); $this->assertCount(1, $this->extractor->calls); $this->publishInProgram($film); $page = Page::query()->create(['path' => '/filmkarte']); $service = app(PageService::class); $service->publish($page, $service->saveRevision($page, [ ['layout' => 'u4', 'style' => [], 'slots' => [ ['type' => 'media_card', 'v' => 1, 'variant' => 'adaptive', 'settings' => [], 'data' => [], 'refs' => ['element' => $film->id, 'image' => $still->id]], ]], ], null, null, null)); for ($i = 0; $i < 3; $i++) { $html = $this->get('/filmkarte')->assertOk()->getContent(); } $this->assertStringContainsString('src="'.route('website.media', $still).'"', $html); $this->assertStringContainsString('alt="Szene aus dem Film „Lara im Park“"', htmlspecialchars_decode($html)); $this->assertStringNotContainsString('lf-pc__panel--film', $html, 'Mit freigegebenem Bild erscheint keine Filmtafel.'); $this->assertStringNotContainsString('/redaktion/', $html); $this->assertCount(1, $this->extractor->calls, 'Seitenaufrufe lesen nur die gespeicherte Datei und starten keine Bildextraktion.'); app(WebsiteRelease::class)->withdraw($still); $this->app->forgetScopedInstances(); $this->assertStringNotContainsString('main($this->get('/filmkarte')->getContent()), 'Zurückgenommen: das Bild entfällt, die Karte bleibt (Filmtafel).'); } public function test_the_editor_adopts_a_frame_into_the_image_field_and_rejects_foreign_targets(): void { $film = $this->film(); $film->forceFill(['thumbnail_path' => 'vorschau/element-'.$film->id.'.jpg', 'thumbnail_source' => 'eigen'])->save(); Storage::disk('media_derived')->put($film->thumbnail_path, $this->jpeg(640, 360)); $editor = User::factory()->redakteur()->create(); $editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); $page = Page::query()->create(['path' => '/filmbild-editor']); app(PageService::class)->saveRevision($page, [ ['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'media_card', 'v' => 1, 'variant' => 'adaptive', 'settings' => [], 'data' => [], 'refs' => ['element' => $film->id]]]], ], null, null, null); $component = Livewire::actingAs($editor, 'staff')->test(EditPage::class, ['record' => $page->id]); $component->call('adoptFilmFrame', 'data.ziel.image', $film->id, '12.5'); $frame = ContentElement::query()->where('output_class', 'bild')->latest('id')->firstOrFail(); $this->assertSame($frame->id, $component->get('data.ziel.image')); $this->assertSame('Standbild: Lara im Park (Fassung 1, 12,5 s)', $frame->display_title); $component->call('adoptFilmThumbnail', 'data.ziel2.image', $film->id); $this->assertNotNull($component->get('data.ziel2.image')); $before = ContentElement::query()->count(); $component->call('adoptFilmFrame', 'data.ziel3.image', $film->id, 'abc'); $component->call('adoptFilmFrame', 'umgebung.geheim', $film->id, '5'); $component->call('adoptFilmFrame', 'data.ziel4.bild', $film->id, '5'); $component->call('adoptFilmFrame', 'data.ziel5.image', 999999, '5'); $this->assertSame($before, ContentElement::query()->count(), 'Fremde Ziele, falsche Eingaben und fehlende Filme legen nichts an.'); $this->assertNull($component->get('data.ziel3.image')); } private function film(string $title = 'Lara im Park'): ContentElement { $content = app(ContentService::class); $film = $content->createElement('film', 'film', $title); $film->update(['website_credit' => 'Foto: Lara und Felix']); $content->publishElementVersion($film, 'Film'); return $film->refresh(); } private function publishInProgram(ContentElement $film): void { $content = app(ContentService::class); $composition = $content->createComposition('Programm Park'); $composition->editorial_content_kind_id = EditorialContentKind::query()->where('slug', 'programm')->value('id'); $composition->save(); $version = $content->publishComposition($composition, [['role' => 'Film', 'target' => $film->versions()->firstOrFail()]]); app(CompositionDisplayRelease::class)->release($version, User::factory()->create()); } private function main(string $html): string { preg_match('#]*>(.*)#s', $html, $match); return $match[1] ?? ''; } private function jpeg(int $width, int $height): string { $image = imagecreatetruecolor($width, $height); imagefill($image, 0, 0, imagecolorallocate($image, 40, 120, 90)); ob_start(); imagejpeg($image); return (string) ob_get_clean(); } }