Files
laraundfelix/tests/Feature/MediaLibraryTest.php
T
LarsandCursor 3e56314cf9
Deploy Test / test (push) Successful in 45s
Deploy Test / deploy (push) Successful in 30s
Zeige den Medienbestand als Kacheln und Liste mit Fassung, Freigabe und Verwendung.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 19:44:51 +01:00

324 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\EditorialDimension;
use App\Domain\Catalog\IdentityDimension;
use App\Domain\Catalog\PillarService;
use App\Filament\Resources\ContentElements\ContentElementResource;
use App\Filament\Resources\ContentElements\Pages\ListContentElements;
use App\Filament\Resources\Courses\CourseResource;
use App\Filament\Resources\Exercises\ExerciseResource;
use App\Filament\Support\MediaLibrary;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
use Tests\TestCase;
class MediaLibraryTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
public function test_the_library_opens_as_tiles_and_keeps_filters_when_the_view_changes(): void
{
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Lara im Park');
$film->description = 'Lara spielt im Park.';
$film->thumbnail_path = 'vorschau/element-'.$film->id.'.jpg';
$film->thumbnail_alt = 'Lara auf dem Weg';
$film->save();
$content->publishElementVersion($film, 'Film');
$content->createElement('text', 'text', 'Trainerhinweis');
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->assertOk()
->assertSee('data-ansicht="kacheln"', false)
->assertDontSee('data-ansicht="liste"', false)
->assertDontSee('Arbeitstitel')
->assertSee('Lara im Park')
->assertSee('Film')
->assertSee('Lara spielt im Park.')
->assertSee('Fassung 1 freigegeben')
->assertSee('Lara auf dem Weg')
->assertSee(route('editorial.card', ['kind' => 'element', 'subject' => $film->id]), false)
->assertSee('Trainerhinweis')
->assertSee('Text')
->set('suche', 'Park')
->assertSee('Lara im Park')
->assertDontSee('Trainerhinweis')
->set('ansicht', 'liste')
->assertSet('suche', 'Park')
->assertSee('data-ansicht="liste"', false)
->assertDontSee('data-ansicht="kacheln"', false)
->assertSee('Lara im Park')
->assertDontSee('Trainerhinweis')
->assertSee('Ansehen')
->assertSee('Bearbeiten')
->call('resetFilters')
->assertSet('suche', '')
->assertSet('ansicht', 'liste')
->assertSee('Trainerhinweis');
}
public function test_a_text_medium_is_not_shown_as_a_film_or_as_a_released_version(): void
{
$content = app(ContentService::class);
$text = $content->createElement('text', 'text', 'Felix beim Onkel');
$content->draftElementVersion($text, 'Erst vorlesen.');
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->assertSee('Felix beim Onkel')
->assertSee('Text')
->assertSee('Entwurf, noch nicht freigegeben')
->assertSee('data-fallback="text"', false)
->assertDontSee('Fassung 1 freigegeben')
->assertDontSee('/redaktion/karte/', false);
}
public function test_filters_combine_and_a_draft_stays_distinct_from_a_released_version(): void
{
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Felix und der Autofahrer');
$content->publishElementVersion($film, 'Erste Fassung');
$content->draftElementVersion($film, 'Neue Schnittfassung');
$sheet = $content->createElement('dokument', 'document', 'Traineranleitung Abstand');
$content->publishElementVersion($sheet, 'Bitte vorlesen.');
$broken = $content->createElement('film', 'film', 'Unlesbare Datei');
$content->draftElementVersion($broken, null, ['error' => 'Die Datei ließ sich nicht lesen.']);
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->assertSee('Fassung 1 freigegeben')
->assertSee('Entwurf offen')
->assertDontSee('Fassung 2 freigegeben')
->set('suche', 'Autofahrer')
->set('art', 'film')
->set('freigabe', 'freigegeben')
->set('entwurf', 'offen')
->assertSee('Felix und der Autofahrer')
->assertDontSee('Traineranleitung Abstand')
->assertDontSee('Unlesbare Datei')
->set('art', 'document')
->assertDontSee('Felix und der Autofahrer')
->call('resetFilters')
->set('verarbeitung', 'fehler')
->assertSee('Unlesbare Datei')
->assertSee('Verarbeitungsfehler')
->assertDontSee('Felix und der Autofahrer');
}
public function test_a_blocked_version_does_not_mark_the_whole_medium_and_stays_findable(): void
{
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Lara im Park');
$first = $content->publishElementVersion($film, 'Alt');
$content->publishElementVersion($film, 'Neu');
$content->block($first, 'Veraltet');
$retired = $content->createElement('film', 'film', 'Alter Take');
$only = $content->publishElementVersion($retired, 'Take');
$content->block($only, 'Nicht verwenden');
$this->assertSame(ContentVersionStatus::Gesperrt, $first->fresh()->status);
$this->assertSame(ContentVersionStatus::Bereit, $film->versions()->where('number', 2)->first()->status);
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->assertSee('Lara im Park')
->assertSee('Fassung 2 freigegeben')
->assertSee('Fassung 1 deaktiviert')
->assertDontSee('Medium deaktiviert')
->assertDontSee('Alter Take')
->set('fassung', 'nur')
->assertSee('Alter Take')
->assertSee('Fassung 1 deaktiviert')
->assertDontSee('Lara im Park');
}
public function test_usage_follows_older_versions_to_the_module_and_the_program(): void
{
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Felix beim Onkel');
$first = $content->publishElementVersion($film, 'Erste Fassung');
$content->publishElementVersion($film, 'Zweite Fassung');
$module = $content->publishComposition(
$content->createComposition('Abstand üben', CompositionKind::Uebung),
[['role' => 'Film', 'target' => $first]],
);
$program = $content->publishComposition(
$content->createComposition('Vormittag', CompositionKind::Kurs),
[['role' => 'Film', 'target' => $first]],
);
$moduleUrl = ExerciseResource::getUrl('edit', ['record' => $module->composition_id]);
$programUrl = CourseResource::getUrl('edit', ['record' => $program->composition_id]);
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->assertSee('2 Verwendungen')
->assertSee('Modul')
->assertSee('Programm')
->assertSee('Abstand üben')
->assertSee('Vormittag')
->assertSee('Medium Fassung 1', false)
->assertSee($moduleUrl, false)
->assertSee($programUrl, false);
$this->assertSame($first->id, $module->members()->first()->content_element_version_id);
$this->assertSame($first->id, $program->members()->first()->content_element_version_id);
$this->assertSame(2, $film->versions()->count());
}
public function test_a_license_article_is_not_treated_as_library_access_or_as_usage(): void
{
$content = app(ContentService::class);
$sheet = $content->publishElementVersion(
$content->createElement('text', 'text', 'Merkblatt Grenzen'),
'Den Abstand ansprechen.',
);
$article = app(ArticleService::class)->create('grenzen-'.str()->lower(str()->random(4)));
app(ArticleService::class)->publishRelease($article, $sheet, 'Dieselbe Ausgabe.');
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->assertSee('In keinem Modul oder Programm verwendet')
->assertSee('Im Lizenzartikel '.$article->code.' genannt. Das belegt keinen Zugriff in der Bibliothek.')
->set('nutzung', 'verwendet')
->assertDontSee('Merkblatt Grenzen')
->set('nutzung', 'unbenutzt')
->assertSee('Merkblatt Grenzen');
}
public function test_a_pending_file_is_not_shown_as_released_and_archived_media_stay_behind_a_filter(): void
{
Queue::fake();
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Felix und der Autofahrer');
$content->draftElementVersion($film, null, ['incoming' => 'inhalte/fahrer.mp4', 'hold' => true]);
$archived = $content->createElement('text', 'text', 'Alte Anleitung');
$content->publishElementVersion($archived, 'Text');
$archived->archived_at = now();
$archived->save();
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->assertSee('Datei wird übernommen')
->assertSee('Entwurf, noch nicht freigegeben')
->assertDontSee('Alte Anleitung')
->set('archiv', 'archiviert')
->assertSee('Alte Anleitung')
->assertSee('Archiviert')
->assertDontSee('Felix und der Autofahrer');
}
public function test_pillar_and_classification_filters_narrow_the_same_library(): void
{
$content = app(ContentService::class);
$film = $content->createElement('film', 'film', 'Lara im Park');
$content->publishElementVersion($film, 'Film');
$other = $content->createElement('text', 'text', 'Ohne Säule');
$content->publishElementVersion($other, 'Text');
$pillar = app(PillarService::class)->create('Meine Grenzen');
app(PillarService::class)->assign($film, [$pillar->id]);
$dimension = EditorialDimension::query()->create(['slug' => 'zielgruppe', 'label' => 'Zielgruppe']);
$value = $dimension->values()->create(['slug' => 'grundschule', 'label' => 'Grundschule']);
app(IdentityDimension::class)->assign($film, $value);
Livewire::actingAs($this->editor(), 'staff')
->test(ListContentElements::class)
->set('saeule', (string) $pillar->id)
->assertSee('Lara im Park')
->assertDontSee('Ohne Säule')
->set('saeule', '')
->set('einordnung', (string) $value->id)
->assertSee('Lara im Park')
->assertDontSee('Ohne Säule');
}
public function test_a_catalog_keeper_can_open_the_library_but_cannot_edit(): void
{
app(ContentService::class)->publishElementVersion(
app(ContentService::class)->createElement('text', 'text', 'Hinweis'),
'Text',
);
$keeper = User::factory()->katalogverwalter()->create();
$keeper->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
Livewire::actingAs($keeper, 'staff')
->test(ListContentElements::class)
->assertOk()
->assertSee('Hinweis')
->assertSee('Ansehen')
->assertDontSee('Bearbeiten')
->assertDontSee('Medium anlegen');
}
public function test_usage_counts_are_loaded_without_a_query_per_card(): void
{
$content = app(ContentService::class);
foreach (['Lara im Park', 'Felix beim Onkel', 'Felix und der Autofahrer'] as $title) {
$version = $content->publishElementVersion($content->createElement('film', 'film', $title), 'Film');
$content->publishComposition(
$content->createComposition('Modul '.$title, CompositionKind::Uebung),
[['role' => 'Film', 'target' => $version]],
);
}
DB::flushQueryLog();
DB::enableQueryLog();
$cards = app(MediaLibrary::class)->cards([
'suche' => '',
'art' => '',
'freigabe' => '',
'entwurf' => '',
'fassung' => '',
'nutzung' => '',
'saeule' => '',
'einordnung' => '',
'verarbeitung' => '',
'archiv' => 'aktiv',
], true);
$queries = count(DB::getQueryLog());
$this->assertCount(3, $cards);
$this->assertSame(1, $cards[0]['usageCount']);
$this->assertLessThan(12, $queries);
}
public function test_an_editor_can_open_a_medium_from_the_library(): void
{
$element = app(ContentService::class)->createElement('film', 'film', 'Lara im Park');
app(ContentService::class)->publishElementVersion($element, 'Film');
$editor = $this->editor();
$this->actingAs($editor, 'staff')
->get(ContentElementResource::getUrl('index'))
->assertOk()
->assertSee(ContentElementResource::getUrl('view', ['record' => $element]), false)
->assertSee(ContentElementResource::getUrl('edit', ['record' => $element]), false);
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
}