Files
laraundfelix/tests/Feature/WebsiteFileTest.php
LarsandClaude Sonnet 5.5 5e28538551
CI Feature / test (push) Successful in 1m57s
Deploy Test / test (push) Successful in 1m53s
Deploy Test / deploy (push) Successful in 30s
Wähle das Vorschaubild auch aus der Website-Datei eines Films ohne Fassung
Die Filmstelle konnte nur aus der Datei einer Inhaltsfassung gezogen werden; ein Medium
mit reiner Website-Datei hatte keine Quelle. Jetzt zieht die Auswahl die Stelle aus der
Website-Datei (Herkunft frame:0@Sekunde, bestehende Leser bleiben gültig). Die Vorschau
des Einzelbilds liegt hinter einer Redaktions-Route.

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

262 lines
11 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Entitlement\Grant;
use App\Domain\Media\PresentationFile;
use App\Domain\Media\ThumbnailDeriver;
use App\Domain\Media\WebsiteMedia;
use App\Domain\Media\WebsiteRelease;
use App\Filament\Resources\ContentElements\Pages\ViewContentElement;
use App\Filament\Support\MediumForm;
use App\Jobs\PublishWebsiteMedia;
use App\Jobs\StorePresentationFile;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Website-Datei eines Films (zum Beispiel ein Trailer ohne Schutz). Der Web-Prozess hat `derived/` nur lesend
* (docker-compose.yml); die Datei muss deshalb die Warteschlange ablegen.
*/
class WebsiteFileTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
Storage::fake('cms_incoming');
Storage::fake('media_derived');
Storage::fake('media_public');
Storage::fake('media_originals');
}
public function test_the_web_process_does_not_write_to_the_derived_store_but_hands_the_copy_to_the_queue(): void
{
Queue::fake();
Storage::disk('cms_incoming')->put('inhalte/trailer.mp4', 'trailer-bytes');
$this->actingAs($this->editor(), 'staff');
$element = MediumForm::create([
'working_title' => 'Trailer',
'shape' => 'film',
'incoming' => 'inhalte/trailer.mp4',
'without_version' => true,
], hold: false);
Queue::assertPushed(StorePresentationFile::class, fn (StorePresentationFile $job): bool => $job->elementId === $element->id);
$this->assertEmpty(Storage::disk('media_derived')->allFiles());
$this->assertNull($element->fresh()->preview_path);
$this->assertSame(0, $element->versions()->count());
}
public function test_the_queue_places_the_file_and_the_film_can_then_be_released_without_a_license(): void
{
Storage::disk('cms_incoming')->put('inhalte/trailer.mp4', 'trailer-bytes');
$this->actingAs($this->editor(), 'staff');
$element = MediumForm::create([
'working_title' => 'Trailer',
'shape' => 'film',
'incoming' => 'inhalte/trailer.mp4',
'without_version' => true,
], hold: false)->fresh();
$this->assertSame('teaser/'.$element->id.'/datei.mp4', $element->preview_path);
Storage::disk('media_derived')->assertExists($element->preview_path);
Storage::disk('cms_incoming')->assertMissing('inhalte/trailer.mp4');
$this->assertNull(app(WebsiteRelease::class)->refusal($element));
app(WebsiteRelease::class)->release($element);
$this->assertNotNull(app(WebsiteMedia::class)->present($element->fresh()));
$this->assertSame(0, Grant::query()->count());
Storage::disk('media_originals')->assertDirectoryEmpty('/');
}
public function test_a_film_format_the_browser_cannot_play_is_refused_before_anything_is_created(): void
{
Storage::disk('cms_incoming')->put('inhalte/trailer.mov', 'mov-bytes');
$this->actingAs($this->editor(), 'staff');
try {
MediumForm::create([
'working_title' => 'Trailer als MOV',
'shape' => 'film',
'incoming' => 'inhalte/trailer.mov',
'without_version' => true,
], hold: false);
$this->fail('Eine MOV-Datei darf keine Website-Datei werden.');
} catch (DomainRuleException $e) {
$this->assertStringContainsString('.mov', $e->getMessage());
$this->assertStringContainsString('MP4', $e->getMessage());
}
$this->assertSame(0, ContentElement::query()->count());
}
public function test_a_failed_copy_is_shown_at_the_medium_instead_of_getting_lost(): void
{
$element = app(ContentService::class)->createElement('film', 'film', 'Trailer');
(new StorePresentationFile($element->id, 'inhalte/fehlt.mp4'))->handle(app(PresentationFile::class));
$this->assertStringContainsString('nicht im Eingang', (string) $element->fresh()->thumbnail_error);
$this->assertNull($element->fresh()->preview_path);
}
public function test_a_film_with_a_protected_version_gets_a_separate_unprotected_website_file(): void
{
Queue::fake([PublishWebsiteMedia::class]);
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Unterrichtsfilm');
$version = $content->publishElementVersion($film, null, ['incoming' => 'inhalte/ganz.mp4']);
$this->assertNotNull(app(WebsiteRelease::class)->refusal($film->fresh()));
$this->actingAs($this->editor(), 'staff');
Livewire::test(ViewContentElement::class, ['record' => $film->getKey()])
->set('bereich', 'website')
->set('websiteUpload', UploadedFile::fake()->create('trailer.mp4', 100, 'video/mp4'))
->call('storeWebsiteFile')
->assertSee('Die Website-Datei ist übernommen');
$film = $film->fresh();
$this->assertSame('teaser/'.$film->id.'/datei.mp4', $film->preview_path);
$this->assertSame([$version->id], $film->versions()->pluck('id')->all());
$this->assertNull(app(WebsiteRelease::class)->refusal($film));
}
public function test_the_upload_in_the_viewer_refuses_wrong_formats_and_missing_files(): void
{
$film = app(ContentService::class)->createElement('film', 'film', 'Film');
$this->actingAs($this->editor(), 'staff');
Livewire::test(ViewContentElement::class, ['record' => $film->getKey()])
->set('bereich', 'website')
->call('storeWebsiteFile')
->assertSee('Wähle zuerst eine Datei.')
->set('websiteUpload', UploadedFile::fake()->create('trailer.mov', 10))
->call('storeWebsiteFile')
->assertSee('kann der Browser nicht direkt abspielen');
$this->assertNull($film->fresh()->preview_path);
}
public function test_replacing_the_file_of_a_released_film_publishes_the_new_copy(): void
{
Storage::disk('cms_incoming')->put('inhalte/neu.mp4', 'neu');
$film = app(ContentService::class)->createElement('film', 'film', 'Trailer');
$film->website_released_at = now();
$film->save();
Queue::fake([PublishWebsiteMedia::class]);
app(PresentationFile::class)->store($film, 'inhalte/neu.mp4');
Queue::assertPushed(PublishWebsiteMedia::class, fn (PublishWebsiteMedia $job): bool => $job->elementId === $film->id && $job->action === 'release');
}
public function test_a_versionless_film_shows_its_website_file_in_the_media_manager(): void
{
Storage::disk('cms_incoming')->put('inhalte/trailer.mp4', '0123456789');
$this->actingAs($this->editor(), 'staff');
$film = MediumForm::create([
'working_title' => 'Trailer',
'shape' => 'film',
'incoming' => 'inhalte/trailer.mp4',
'without_version' => true,
], hold: false)->fresh();
$url = route('content.website-file', $film);
Livewire::test(ViewContentElement::class, ['record' => $film->getKey()])
->assertSeeHtml('<video controls')
->assertSeeHtml($url)
->assertSee('noch nicht für die Website freigegeben');
$this->get($url)->assertOk()->assertStreamedContent('0123456789');
$this->get($url, ['Range' => 'bytes=2-4'])->assertStatus(206)->assertStreamedContent('234');
$this->get($url, ['Range' => 'bytes=50-60'])->assertStatus(416);
}
public function test_a_film_frame_of_the_website_file_becomes_the_preview_image(): void
{
$image = imagecreatetruecolor(4, 4);
ob_start();
imagejpeg($image);
$jpeg = ob_get_clean();
imagedestroy($image);
$this->app->bind(ThumbnailDeriver::class, fn () => new class($jpeg) extends ThumbnailDeriver
{
public function __construct(private string $jpeg) {}
public function jpegAtWebsiteFile(ContentElement $element, float $seconds): ?string
{
return $this->jpeg;
}
});
$film = app(ContentService::class)->createElement('film', 'film', 'Trailer');
$film->preview_path = 'teaser/'.$film->id.'/datei.mp4';
$film->save();
Storage::disk('media_derived')->put($film->preview_path, 'x');
$this->actingAs($this->editor(), 'staff');
Livewire::test(ViewContentElement::class, ['record' => $film->getKey()])
->set('bereich', 'vorschaubild')
->assertSee('Diese Stelle übernehmen')
->assertSee('Aus der Website-Datei')
->set('frameSecond', '12.5')
->call('adoptFrame')
->assertSet('pictureFeedback', 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.');
$film->refresh();
$this->assertSame('vorschau/element-'.$film->id.'.jpg', $film->thumbnail_path);
$this->assertSame('frame:0@12.5', $film->thumbnail_source);
$this->assertSame(0, $film->versions()->count());
Storage::disk('media_derived')->assertExists($film->thumbnail_path);
}
public function test_the_frame_of_the_website_file_is_only_available_to_editors_and_for_a_film_with_a_file(): void
{
$film = app(ContentService::class)->createElement('film', 'film', 'Ohne Datei');
$this->get(route('content.website-frame', ['element' => $film, 'sekunde' => 3]))->assertRedirect();
$this->actingAs($this->editor(), 'staff');
$this->get(route('content.website-frame', ['element' => $film, 'sekunde' => 3]))->assertNotFound();
$this->get(route('content.website-frame', ['element' => $film]))->assertNotFound();
}
public function test_the_website_file_is_not_served_to_guests_or_without_a_file(): void
{
Storage::disk('media_derived')->put('teaser/1/datei.mp4', 'x');
$film = app(ContentService::class)->createElement('film', 'film', 'Trailer');
$film->preview_path = 'teaser/'.$film->id.'/datei.mp4';
$film->save();
Storage::disk('media_derived')->put($film->preview_path, 'x');
$leer = app(ContentService::class)->createElement('film', 'film', 'Ohne Datei');
$this->get(route('content.website-file', $film))->assertRedirect();
$this->actingAs($this->editor(), 'staff');
$this->get(route('content.website-file', $film))->assertOk();
$this->get(route('content.website-file', $leer))->assertNotFound();
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
}