Files
laraundfelix/tests/Feature/FilmStillTest.php
T
LarsandClaude Sonnet 5.5 cd66561ca9 Melde die Wiederverwendung eines Filmbilds ehrlich und erlaube das Löschen der Medien.
- Wird eine schon übernommene Stelle erneut gewählt, nennt die Meldung den tatsächlichen Stand: bereits freigegeben,
  wird verarbeitet oder Website-Freigabe zurückgenommen (bleibt zurückgenommen). Ein bei der Verarbeitung
  gescheitertes oder archiviertes Bild wird nicht wiederverwendet; die Herkunft zeigt dann auf das neue Bild.
- „Medium löschen“ erreicht jetzt Standbilder (film_still_sources: cascade) und übernommene CMS-Bilder
  (website_media_aliases: Zuordnung bleibt als Sperre, die frühere Adresse /medien/cms/… liefert 404, das Widget
  zeigt kein Bild). Vorher hätte der Fremdschlüssel das Löschen mit einem Fehler abgebrochen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 19:36:07 +01:00

321 lines
16 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\CompositionDisplayRelease;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Entitlement\Grant;
use App\Domain\Media\FilmStill;
use App\Domain\Media\FilmStillSource;
use App\Domain\Media\ThumbnailDeriver;
use App\Domain\Media\WebsiteRelease;
use App\Filament\Resources\Pages\Pages\EditPage;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Bild aus einem Film für die Medien- und Materialkarte: einmal als Bild-Medium ablegen, danach nur lesen.
* Die Bildextraktion (ffmpeg) wird durch einen Stub ersetzt und mitgezählt.
*/
class FilmStillTest extends TestCase
{
use RefreshDatabase;
/** @var object{calls: list<array{second: float, width: int, original: bool}>, 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<array{second: float, width: int, original: bool}> */
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('<img', $this->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'));
}
public function test_a_withdrawn_still_is_reused_but_the_message_says_it_is_not_public(): void
{
$film = $this->film();
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
$page = Page::query()->create(['path' => '/zurueckgenommen']);
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');
$still = ContentElement::query()->where('output_class', 'bild')->latest('id')->firstOrFail();
$title = 'Standbild: Lara im Park (Fassung 1, 12,5 s)';
$component->assertNotified('Im Medienmanager angelegt: „'.$title.'“');
$component->call('adoptFilmFrame', 'data.ziel2.image', $film->id, '12.5');
$component->assertNotified('Bereits im Medienmanager: „'.$title.'“');
$this->assertSame($still->id, $component->get('data.ziel2.image'));
$this->assertCount(1, $this->extractor->calls, 'Die zweite Übernahme zieht nichts neu.');
app(WebsiteRelease::class)->withdraw($still);
$component->call('adoptFilmFrame', 'data.ziel3.image', $film->id, '12.5');
$component->assertNotified('Bild ausgewählt, aber nicht für die Website freigegeben: „'.$title.'“');
$this->assertSame($still->id, $component->get('data.ziel3.image'), 'Das zurückgenommene Bild bleibt ein und dasselbe Medium.');
$this->assertCount(1, $this->extractor->calls);
}
public function test_a_still_that_failed_during_processing_is_replaced_by_a_new_one_for_the_same_frame(): void
{
$film = $this->film();
$first = app(FilmStill::class)->fromSecond($film, 7);
$first->forceFill(['thumbnail_error' => 'Die Bilddatei ließ sich nicht lesen.'])->save();
$second = app(FilmStill::class)->fromSecond($film, 7);
$this->assertNotSame($first->id, $second->id, 'Ein gescheitertes Bild blockiert die Stelle nicht.');
$this->assertSame($second->id, FilmStillSource::query()->where('film_element_id', $film->id)->sole()->still_element_id, 'Die Herkunft zeigt auf das neue Bild.');
$this->assertSame($second->id, app(FilmStill::class)->fromSecond($film, 7)->id);
}
public function test_deleting_a_still_in_the_media_manager_works_and_the_frame_can_be_taken_over_again(): void
{
$film = $this->film();
$still = app(FilmStill::class)->fromSecond($film, 3);
$still->delete();
$this->assertSame(0, FilmStillSource::query()->count(), 'Die Herkunftszeile entfällt mit dem Bild.');
$this->assertSame($film->id, ContentElement::query()->where('output_class', 'film')->value('id'), 'Der Film bleibt.');
$again = app(FilmStill::class)->fromSecond($film, 3);
$this->assertNotSame($still->id, $again->id);
}
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('#<main[^>]*>(.*)</main>#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();
}
}