Files
laraundfelix/tests/Feature/PresentationMediaTest.php
T
LarsandCursor 7b325f0d66
CI Feature / test (push) Successful in 53s
Deploy Test / test (push) Successful in 48s
Deploy Test / deploy (push) Successful in 22s
Lasse Präsentationsmedien ohne Inhaltsfassung zu.
Bilder, Filme und Hörstücke lassen sich zuordnen und für die Website freigeben, ohne eine fachliche Fassung anzulegen. Unterrichtsfilme behalten die Teaserfassung an ihrer bereitgestellten Fassung.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 08:20:35 +01:00

269 lines
12 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\CompositionDisplayRelease;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\CompositionMember;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Catalog\PresentationAssignment;
use App\Domain\Catalog\PresentationMedia;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\RenderContext;
use App\Domain\Cms\Widgets\Support\WebsiteImage;
use App\Domain\Cms\Widgets\WidgetRenderer;
use App\Domain\Entitlement\Grant;
use App\Domain\Media\PresentationFile;
use App\Domain\Media\WebsiteMedia;
use App\Domain\Media\WebsiteRelease;
use App\Filament\Resources\Courses\Pages\EditCourse;
use App\Filament\Support\EditorialCatalog;
use App\Filament\Support\MediumForm;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
class PresentationMediaTest 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_a_versionless_image_and_teaser_can_be_assigned_released_and_withdrawn(): void
{
$content = app(ContentService::class);
$program = $content->createComposition('Programm mit Teaser', CompositionKind::Kurs);
$program->editorial_content_kind_id = EditorialContentKind::query()->where('slug', 'programm')->value('id');
$program->display_title = 'Name aus der Redaktion';
$program->teaser = 'Teaser aus der Redaktion';
$program->description = 'Interne Langfassung';
$program->save();
$version = $content->publishComposition($program, [[
'role' => 'Text',
'target' => $content->publishElementVersion($content->createElement('text', 'text', 'Blatt'), 'Inhalt'),
]]);
app(CompositionDisplayRelease::class)->release($version, User::factory()->create());
$image = $this->store('bild', 'Pruefbild ohne Fassung', 'bild-bytes');
$teaser = $this->store('film', 'Teaser ohne Fassung', 'teaser-bytes');
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
$this->actingAs($editor, 'staff');
$found = app(EditorialCatalog::class)->presentationBrowse('ohne Fassung');
$targets = array_column($found['items'], 'target');
$this->assertContains('medium:'.$image->id, $targets);
$this->assertContains('medium:'.$teaser->id, $targets);
$didactic = app(EditorialCatalog::class)->browse('ohne Fassung', 'alle', '', $program->id);
$this->assertNotContains('medium:'.$image->id, array_column($didactic['items'], 'target'));
$this->assertNotContains('medium:'.$teaser->id, array_column($didactic['items'], 'target'));
Livewire::test(EditCourse::class, ['record' => $program->getKey()])
->call('openPresentationPicker')
->set('pickerQuery', 'Pruefbild ohne')
->set('presentationRole', 'titelbild')
->call('assignFromPicker', 'medium:'.$image->id)
->call('openPresentationPicker')
->set('pickerQuery', 'Teaser ohne')
->set('presentationRole', 'trailer')
->set('bindToVersion', false)
->call('assignFromPicker', 'medium:'.$teaser->id)
->assertSee('Pruefbild ohne Fassung')
->assertSee('Teaser ohne Fassung')
->assertSee('Eigenständiges Medium, ohne Bindung an eine Fassung.');
$this->assertSame(0, $image->fresh()->versions()->count());
$this->assertSame(0, $teaser->fresh()->versions()->count());
$this->assertSame(1, $program->fresh()->versions()->count());
$this->assertSame(0, Grant::query()->count());
$this->assertNull(PresentationAssignment::query()->where('content_element_id', $image->id)->value('source_version_id'));
$this->assertNull(PresentationAssignment::query()->where('content_element_id', $teaser->id)->value('source_version_id'));
$this->assertSame(1, CompositionMember::query()->where('composition_version_id', $version->id)->count());
app(WebsiteRelease::class)->release($image->fresh());
app(WebsiteRelease::class)->release($teaser->fresh());
$image->refresh();
$teaser->refresh();
$this->assertNotNull($image->website_released_at);
$this->assertNotNull($teaser->website_released_at);
$this->assertSame(0, $image->versions()->count());
$this->assertSame(0, $teaser->versions()->count());
$this->assertSame(0, Grant::query()->count());
Storage::disk('media_public')->assertExists($image->website_public_path);
Storage::disk('media_public')->assertExists($teaser->website_public_path);
$this->assertStringNotContainsString('originals', (string) $teaser->website_public_path);
$this->publish('/praesentation-ohne-fassung', [[
'layout' => 'u2_2',
'style' => [],
'slots' => [
$this->card('program_card', ['composition' => $program->id]),
$this->card('program_card', ['composition' => $program->id], ['heading' => 'Nur diese Karte']),
],
]]);
$this->get('/praesentation-ohne-fassung')->assertOk()
->assertSee('Name aus der Redaktion')
->assertSee('Teaser aus der Redaktion')
->assertSee('Nur diese Karte')
->assertSee('/website-medien/'.$image->id, false)
->assertDontSee('Interne Langfassung');
$film = app(WebsiteMedia::class)->present($teaser);
$this->assertNotNull($film);
$this->assertSame('film', $film->kind);
$this->get($film->url)->assertOk()->assertStreamedContent('teaser-bytes');
$program->teaser = 'Teaser nach der zentralen Änderung';
$program->save();
$this->app->forgetInstance(WebsiteImage::class);
$this->get('/praesentation-ohne-fassung')->assertOk()
->assertSee('Teaser nach der zentralen Änderung')
->assertSee('Nur diese Karte')
->assertDontSee('Teaser aus der Redaktion');
app(WebsiteRelease::class)->withdraw($image->fresh());
app(WebsiteRelease::class)->withdraw($teaser->fresh());
$this->app->forgetInstance(WebsiteImage::class);
$after = $this->get('/praesentation-ohne-fassung')->assertOk()->getContent();
$this->assertStringNotContainsString('/website-medien/'.$image->id, $after);
$this->assertStringContainsString('Teaser nach der zentralen Änderung', $after);
$this->assertStringContainsString('Nur diese Karte', $after);
$this->get(route('website.media', $teaser))->assertNotFound();
$this->get(route('website.media', $image))->assertNotFound();
Storage::disk('media_derived')->assertExists($image->thumbnail_path);
Storage::disk('media_derived')->assertExists($teaser->preview_path);
$this->assertSame(0, $teaser->fresh()->versions()->count());
$this->assertSame('Name aus der Redaktion', $program->fresh()->display_title);
}
public function test_creating_a_presentation_film_does_not_open_a_content_version(): void
{
Storage::disk('cms_incoming')->put('inhalte/teaser.mp4', 'teaser-bytes');
$editor = User::factory()->redakteur()->create();
$this->actingAs($editor, 'staff');
$element = MediumForm::create([
'working_title' => 'Marketingclip',
'shape' => 'film',
'incoming' => 'inhalte/teaser.mp4',
'without_version' => true,
'description' => 'Nur die Website',
], hold: false);
$this->assertSame(0, $element->versions()->count());
$this->assertSame('teaser/'.$element->id.'/datei.mp4', $element->preview_path);
$this->assertNull($element->website_released_at);
$this->assertSame(0, Grant::query()->count());
Storage::disk('media_derived')->assertExists($element->preview_path);
Storage::disk('media_originals')->assertDirectoryEmpty('/');
}
public function test_a_versionless_audio_preview_is_released_without_a_license(): void
{
$audio = $this->store('audio', 'Audiovorschau ohne Fassung', 'audio-bytes');
$this->assertNull(app(WebsiteMedia::class)->present($audio));
app(WebsiteRelease::class)->release($audio);
$audio->refresh();
$presentation = app(WebsiteMedia::class)->present($audio);
$this->assertNotNull($presentation);
$this->assertSame('audio', $presentation->kind);
$this->get($presentation->url)->assertOk()->assertStreamedContent('audio-bytes');
$this->assertSame(0, $audio->versions()->count());
$this->assertSame(0, Grant::query()->count());
app(WebsiteRelease::class)->withdraw($audio->fresh());
$this->assertNull(app(WebsiteMedia::class)->present($audio->fresh()));
$this->get(route('website.media', $audio))->assertNotFound();
Storage::disk('media_derived')->assertExists($audio->preview_path);
}
public function test_an_explicit_version_binding_stays_optional(): void
{
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Unterricht');
$version = $content->publishElementVersion($film, 'Fassung');
$clip = $this->store('film', 'Clip ohne Fassung', 'clip-bytes');
app(PresentationMedia::class)->assign($film, 'trailer', $clip->id, null);
$this->assertNull(PresentationAssignment::query()->value('source_version_id'));
app(PresentationMedia::class)->assign($film, 'trailer', $clip->id, $version->id);
$this->assertSame($version->id, PresentationAssignment::query()->value('source_version_id'));
$this->assertSame(0, $clip->fresh()->versions()->count());
$this->assertSame(1, $film->fresh()->versions()->count());
$this->assertSame(0, Grant::query()->count());
}
public function test_a_widget_with_nothing_but_a_missing_medium_is_omitted(): void
{
$image = $this->store('bild', 'Nur ein Bild', 'bild-bytes');
$html = app(WidgetRenderer::class)->renderAll([
['type' => 'heading', 'v' => 1, 'variant' => 'standard', 'settings' => [], 'data' => ['heading' => 'Die Überschrift bleibt'], 'refs' => []],
['type' => 'cards', 'v' => 1, 'variant' => 'standard', 'settings' => [], 'data' => ['items' => [
['title' => '', 'text' => null, 'url' => null, 'image' => $image->id],
]], 'refs' => []],
], RenderContext::publicPage())->toHtml();
$this->assertStringContainsString('Die Überschrift bleibt', $html);
$this->assertStringNotContainsString('widget-cards', $html);
$this->assertStringNotContainsString('<img', $html);
}
private function store(string $output, string $title, string $bytes): ContentElement
{
$extension = match ($output) {
'bild' => 'jpg',
'audio' => 'mp3',
default => 'mp4',
};
$path = 'inhalte/'.md5($title).'.'.$extension;
Storage::disk('cms_incoming')->put($path, $bytes);
$element = app(ContentService::class)->createElement($output, $output, $title);
$element->display_title = $title;
$element->save();
app(PresentationFile::class)->store($element, $path);
return $element->fresh();
}
/**
* @param list<array<string, mixed>> $sections
*/
private function publish(string $path, array $sections): void
{
$page = Page::query()->create(['path' => $path]);
$service = app(PageService::class);
$revision = $service->saveRevision($page, $sections, null, null, null);
$service->publish($page, $revision);
}
/**
* @param array<string, mixed> $refs
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
private function card(string $type, array $refs, array $data = []): array
{
return ['type' => $type, 'v' => 1, 'variant' => 'adaptive', 'settings' => [], 'data' => $data, 'refs' => $refs];
}
}