Eine neue Fassung ohne neuen Upload (Textänderung, Marken) fiel in den Dateimodus, weil ihre media_renditions fehlten. MediaRendition::carryOver übernimmt die Zeilen der letzten Fassung mit derselben Datei, carryForward holt sie nach, wenn die Verarbeitung erst nach dem Speichern fertig wird. Die Auslieferung prüft Pfade über MediaRendition::owns: nur eigene Ableitungen und der Ordner des eigenen Masters. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
195 lines
9.3 KiB
PHP
195 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Domain\Catalog\ArticleService;
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentService;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Entitlement\GrantIssuer;
|
|
use App\Domain\Media\FilmPlayback;
|
|
use App\Domain\Media\FilmTranscoder;
|
|
use App\Domain\Media\MediaRendition;
|
|
use App\Http\SignedLink;
|
|
use App\Jobs\DeriveFilmRenditions;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Eine neue Filmfassung ohne neue Datei (Textänderung, Marken, Änderungsnotiz) behält die HLS-Stufen der Datei.
|
|
* Vorher fiel sie in den Dateimodus, weil die Verarbeitung nur beim Speichern einer neuen Datei startet und die
|
|
* Auslieferung nur Pfade unter ableitungen/{eigene Kennung} zuließ.
|
|
*/
|
|
class FilmRenditionCarryOverTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_a_new_film_version_without_a_new_file_keeps_streaming_over_hls(): void
|
|
{
|
|
Storage::fake('media_derived');
|
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
|
|
|
$second = app(ContentService::class)->publishElementVersion(
|
|
$film,
|
|
'Neuer Begleittext.',
|
|
['file' => 'inhalte/hof.mp4', 'decision_seconds' => 12],
|
|
);
|
|
|
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($second->id));
|
|
$this->assertSame(
|
|
MediaRendition::masterPath($first->id),
|
|
MediaRendition::masterPath($second->id),
|
|
);
|
|
$this->assertSame(
|
|
MediaRendition::query()->where('content_element_version_id', $first->id)->count(),
|
|
MediaRendition::query()->where('content_element_version_id', $second->id)->count(),
|
|
);
|
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($first->id));
|
|
}
|
|
|
|
public function test_a_new_file_does_not_inherit_renditions_of_the_old_file(): void
|
|
{
|
|
Storage::fake('media_derived');
|
|
[, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
|
|
|
$replaced = app(ContentService::class)->publishElementVersion($film, null, ['file' => 'inhalte/anderer.mp4']);
|
|
|
|
$this->assertSame('file', app(FilmPlayback::class)->mode($replaced->id));
|
|
}
|
|
|
|
public function test_a_member_streams_the_carried_over_renditions_but_never_another_films(): void
|
|
{
|
|
Storage::fake('media_derived');
|
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
|
[$foreign] = $this->filmWithRenditions('Fremdfilm', 'inhalte/fremd.mp4');
|
|
$second = app(ContentService::class)->publishElementVersion(
|
|
$film,
|
|
'Neuer Begleittext.',
|
|
['file' => 'inhalte/hof.mp4'],
|
|
);
|
|
[$article, $offer, $release] = $this->offerFor($second, ['film' => ['streamen']]);
|
|
$member = User::factory()->create();
|
|
app(GrantIssuer::class)->claim($member, $offer);
|
|
|
|
$ask = fn (string $path) => $this->actingAs($member)->get(SignedLink::temporary('member.rendition', now()->addMinutes(5), [
|
|
'article' => $article->id,
|
|
'release' => $release->id,
|
|
'version' => $second->id,
|
|
'datei' => $path,
|
|
'reader' => $member->id,
|
|
]));
|
|
|
|
$ask('ableitungen/'.$first->id.'/master.m3u8')->assertOk()->assertHeader('content-type', 'application/vnd.apple.mpegurl');
|
|
$ask('ableitungen/'.$first->id.'/480/segment.ts')->assertOk()->assertStreamedContent('segment-bytes');
|
|
$ask('ableitungen/'.$second->id.'/480/segment.ts')->assertNotFound();
|
|
$ask('ableitungen/'.$foreign->id.'/480/segment.ts')->assertNotFound();
|
|
$ask('ableitungen/'.$first->id.'/../'.$foreign->id.'/480/segment.ts')->assertNotFound();
|
|
$ask('ableitungen/'.$first->id.'x/480/segment.ts')->assertNotFound();
|
|
}
|
|
|
|
public function test_a_version_without_renditions_cannot_reach_the_files_of_another_version(): void
|
|
{
|
|
Storage::fake('media_derived');
|
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
|
$replaced = app(ContentService::class)->publishElementVersion($film, null, ['file' => 'inhalte/anderer.mp4']);
|
|
[$article, $offer, $release] = $this->offerFor($replaced, ['film' => ['streamen']]);
|
|
$member = User::factory()->create();
|
|
app(GrantIssuer::class)->claim($member, $offer);
|
|
|
|
$this->actingAs($member)->get(SignedLink::temporary('member.rendition', now()->addMinutes(5), [
|
|
'article' => $article->id,
|
|
'release' => $release->id,
|
|
'version' => $replaced->id,
|
|
'datei' => 'ableitungen/'.$first->id.'/480/segment.ts',
|
|
'reader' => $member->id,
|
|
]))->assertNotFound();
|
|
}
|
|
|
|
public function test_a_version_saved_while_the_file_is_still_being_processed_gets_the_renditions_afterwards(): void
|
|
{
|
|
Storage::fake('media_derived');
|
|
Storage::disk('media_derived')->put('inhalte/hof.mp4', 'filmbytes');
|
|
$content = app(ContentService::class);
|
|
$film = $content->createElement('film', 'film', 'Hofszene');
|
|
$first = $content->publishElementVersion($film, null, ['file' => 'inhalte/hof.mp4']);
|
|
// Die Verarbeitung der ersten Fassung läuft noch; die Redaktion speichert schon einen Text oder eine Marke.
|
|
$second = $content->publishElementVersion($film, 'Text.', ['file' => 'inhalte/hof.mp4']);
|
|
$replaced = $content->publishElementVersion($film, null, ['file' => 'inhalte/anderer.mp4']);
|
|
$this->assertSame('file', app(FilmPlayback::class)->mode($second->id));
|
|
|
|
$this->app->instance(FilmTranscoder::class, new class implements FilmTranscoder
|
|
{
|
|
public function available(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function derive(int $versionId, string $sourceRelative): array
|
|
{
|
|
$base = 'ableitungen/'.$versionId;
|
|
Storage::disk('media_derived')->put($base.'/master.m3u8', "#EXTM3U\n");
|
|
|
|
return [['role' => 'master', 'label' => 'master', 'path' => $base.'/master.m3u8']];
|
|
}
|
|
});
|
|
app(DeriveFilmRenditions::class, ['versionId' => $first->id])->handle(app(FilmTranscoder::class));
|
|
|
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($first->id));
|
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($second->id), 'Die spätere Fassung mit derselben Datei bekommt die Stufen nachträglich.');
|
|
$this->assertSame(MediaRendition::masterPath($first->id), MediaRendition::masterPath($second->id));
|
|
$this->assertSame('file', app(FilmPlayback::class)->mode($replaced->id), 'Eine andere Datei erbt nichts.');
|
|
}
|
|
|
|
public function test_the_editorial_preview_streams_the_carried_over_renditions(): void
|
|
{
|
|
Storage::fake('media_derived');
|
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
|
$second = app(ContentService::class)->publishElementVersion($film, 'Text.', ['file' => 'inhalte/hof.mp4']);
|
|
$editor = User::factory()->redakteur()->create();
|
|
|
|
$this->actingAs($editor, 'staff')
|
|
->get(route('content.rendition', ['version' => $second, 'datei' => 'ableitungen/'.$first->id.'/480/segment.ts']))
|
|
->assertOk();
|
|
$this->actingAs($editor, 'staff')
|
|
->get(route('content.rendition', ['version' => $second, 'datei' => 'ableitungen/999999/480/segment.ts']))
|
|
->assertNotFound();
|
|
}
|
|
|
|
/**
|
|
* @return array{0: ContentElementVersion, 1: ContentElement}
|
|
*/
|
|
private function filmWithRenditions(string $title, string $file): array
|
|
{
|
|
Storage::disk('media_derived')->put($file, 'filmbytes');
|
|
$content = app(ContentService::class);
|
|
$element = $content->createElement('film', 'film', $title);
|
|
$version = $content->publishElementVersion($element, null, ['file' => $file]);
|
|
$base = 'ableitungen/'.$version->id;
|
|
Storage::disk('media_derived')->put($base.'/480/index.m3u8', "#EXTM3U\n#EXTINF:6.0,\nsegment.ts\n");
|
|
Storage::disk('media_derived')->put($base.'/480/segment.ts', 'segment-bytes');
|
|
Storage::disk('media_derived')->put($base.'/master.m3u8', "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=800000\n480/index.m3u8\n");
|
|
MediaRendition::query()->create(['content_element_version_id' => $version->id, 'role' => 'variant', 'label' => '480', 'path' => $base.'/480/index.m3u8']);
|
|
MediaRendition::query()->create(['content_element_version_id' => $version->id, 'role' => 'master', 'label' => 'master', 'path' => $base.'/master.m3u8']);
|
|
|
|
return [$version, $element];
|
|
}
|
|
|
|
private function offerFor($version, array $capabilities): array
|
|
{
|
|
$articles = app(ArticleService::class);
|
|
$offers = app(OfferService::class);
|
|
$article = $articles->create('ausgabe-'.str()->random(6));
|
|
$release = $articles->publishRelease($article, $version, 'Stand.');
|
|
$rights = $offers->createRightsProfile('Rechte '.str()->random(4), 'ehrenamtlich', $capabilities);
|
|
$offer = $offers->create('Angebot '.str()->random(4));
|
|
$offers->activate($offer, [
|
|
['target' => $article->fresh(), 'rights' => $rights],
|
|
], ['grant_kind' => 'auf_abruf']);
|
|
|
|
return [$article->fresh(), $offer, $release];
|
|
}
|
|
}
|