- Tabellen media und media_usages, Modelle Media/MediaUsage - App\Media\MediaLibrary (store/register/replace/delete/rebuild) und Registry App\Media\MediaUsages (Quellen: Seiten, Neuigkeiten inkl. künftiger content-Spalte, Termine, Startseiten-Kacheln), ImagePaths-Extraktor - Alle Uploads landen in der Mediathek (Seiten-Editor direkt, übrige über Beobachter beim Speichern); Daten-Migration und media:rebuild übernehmen bestehende Bilder - Recht media.manage, Zugang media.use für alle Redaktionsbereiche - /redaktion/mediathek (Suche, Filter, Upload mit Fortschritt, Details, Eingebunden in, Ersetzen, Löschen mit Warnung, Mehrfachauswahl), Abschnitt in der Redaktion und Menüpunkt im Admin-Bereich - MediaPicker (Hochladen/Mediathek) im Pagebuilder: Bildfelder und Galerie (Mehrfachauswahl), editor.pickMedia im provide-API - Bild-URLs mit ?v=Änderungszeit, null bei fehlender Datei; Modelle löschen geteilte Bilder nicht mehr selbst Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
307 lines
15 KiB
PHP
307 lines
15 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Media;
|
||
|
||
use App\Media\MediaLibrary;
|
||
use App\Models\DashboardTile;
|
||
use App\Models\Media;
|
||
use App\Models\MediaUsage;
|
||
use App\Models\Role;
|
||
use App\Models\User;
|
||
use App\Pagebuilder\Content;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Http\UploadedFile;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Inertia\Testing\AssertableInertia as Assert;
|
||
use Inhaus\Plugins\Events\Models\Event;
|
||
use Inhaus\Plugins\News\Models\NewsCategory;
|
||
use Inhaus\Plugins\News\Models\NewsPost;
|
||
use Inhaus\Plugins\Pages\Models\Page;
|
||
use Tests\TestCase;
|
||
|
||
class MediaLibraryTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private User $admin;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
Storage::fake('uploads');
|
||
Storage::fake('local');
|
||
$this->admin = User::factory()->admin()->create();
|
||
}
|
||
|
||
private function pageEditor(Page $page): User
|
||
{
|
||
$role = Role::query()->create(['name' => 'MAV']);
|
||
$role->grants()->create(['ability' => 'pages.edit', 'resource_type' => 'page', 'resource_id' => $page->id, 'include_children' => true]);
|
||
|
||
return User::factory()->withRoles([$role])->create();
|
||
}
|
||
|
||
private function putImage(string $path, int $width = 800, int $height = 600): string
|
||
{
|
||
Storage::disk('uploads')->put($path, UploadedFile::fake()->image(basename($path), $width, $height)->getContent());
|
||
|
||
return $path;
|
||
}
|
||
|
||
private function imageBlock(string $path, string $id = 'blk00001'): array
|
||
{
|
||
return ['id' => $id, 'type' => 'image', 'data' => ['path' => $path, 'alt' => 'Foto']];
|
||
}
|
||
|
||
public function test_page_editor_upload_lands_in_library_and_usage_is_tracked(): void
|
||
{
|
||
$page = Page::query()->create(['title' => 'Arbeitsschutz']);
|
||
|
||
$result = $this->actingAs($this->admin)->post('/redaktion/seiten/'.$page->id.'/bilder', ['image' => UploadedFile::fake()->image('Baustelle.jpg', 1200, 800)])
|
||
->assertOk()->json();
|
||
|
||
$media = Media::query()->sole();
|
||
$this->assertSame($result['path'], $media->path);
|
||
$this->assertSame('Baustelle.jpg', $media->original_name);
|
||
$this->assertSame($this->admin->id, $media->uploaded_by);
|
||
$this->assertSame(1200, $media->width);
|
||
$this->assertSame($media->id, $result['media']['id']);
|
||
Storage::disk('uploads')->assertExists($media->path);
|
||
|
||
// Entwurf: als Entwurf erfasst
|
||
$this->putJson('/redaktion/seiten/'.$page->id.'/entwurf', ['title' => 'Arbeitsschutz', 'content' => ['blocks' => [$this->imageBlock($media->path)]]])->assertOk();
|
||
$usage = MediaUsage::query()->sole();
|
||
$this->assertSame('pages', $usage->source_type);
|
||
$this->assertSame($page->id, $usage->source_id);
|
||
$this->assertSame('Bild-Block', $usage->context);
|
||
$this->assertTrue($usage->draft);
|
||
$this->assertSame('/redaktion/seiten/'.$page->id, $usage->url);
|
||
|
||
// Veröffentlicht: nicht mehr Entwurf
|
||
$this->postJson('/redaktion/seiten/'.$page->id.'/veroeffentlichen', ['title' => 'Arbeitsschutz', 'content' => ['blocks' => [$this->imageBlock($media->path)]]])->assertOk();
|
||
$this->assertFalse(MediaUsage::query()->sole()->draft);
|
||
|
||
// Bild entfernt: keine Verwendung mehr
|
||
$page->fresh()->publish('Arbeitsschutz', ['blocks' => []], $this->admin);
|
||
$this->assertSame(0, MediaUsage::query()->count());
|
||
}
|
||
|
||
public function test_gallery_hero_and_inline_images_are_found_with_context(): void
|
||
{
|
||
$a = $this->putImage('pagebuilder/pages/1/a.webp');
|
||
$b = $this->putImage('mediathek/2026/10/b.webp');
|
||
$c = $this->putImage('news/inline/c.webp');
|
||
|
||
$content = app(Content::class)->sanitize(['blocks' => [
|
||
['id' => 'gal00001', 'type' => 'gallery', 'data' => ['images' => [['path' => $a], ['path' => $b]]]],
|
||
['id' => 'sec00001', 'type' => 'section', 'data' => [], 'items' => [['blocks' => [
|
||
['id' => 'txt00001', 'type' => 'text', 'data' => ['html' => '<p>Hallo <img src="/uploads/'.$c.'?v=1" alt=""></p>']],
|
||
['id' => 'her00001', 'type' => 'hero', 'data' => ['path' => $a, 'title' => 'Willkommen']],
|
||
]]]],
|
||
]]);
|
||
|
||
$page = Page::query()->create(['title' => 'Team']);
|
||
$page->publish('Team', $content, $this->admin);
|
||
|
||
$contexts = MediaUsage::query()->with('media')->get()->map(fn (MediaUsage $u) => $u->media->path.'|'.$u->context)->all();
|
||
$this->assertEqualsCanonicalizing([$a.'|Galerie', $a.'|Hero', $b.'|Galerie', $c.'|Im Text'], $contexts);
|
||
$this->assertSame(3, Media::query()->count());
|
||
|
||
$page->delete();
|
||
$this->assertSame(0, MediaUsage::query()->count());
|
||
}
|
||
|
||
public function test_news_cover_and_inline_images_are_tracked(): void
|
||
{
|
||
$cover = $this->putImage('news/cover.jpg', 1600, 900);
|
||
$inline = $this->putImage('news/inline/2026/10/bild.webp');
|
||
|
||
$post = NewsPost::factory()->create([
|
||
'title' => 'Sommerfest',
|
||
'cover_path' => $cover,
|
||
'body' => '<p>Text</p><img src="http://localhost/uploads/'.$inline.'" alt="">',
|
||
]);
|
||
|
||
$usages = MediaUsage::query()->where('source_type', 'news')->with('media')->get()->keyBy('context');
|
||
$this->assertSame($cover, $usages['Titelbild']->media->path);
|
||
$this->assertSame($inline, $usages['Im Text']->media->path);
|
||
$this->assertSame('Sommerfest', $usages['Titelbild']->label);
|
||
$this->assertSame(route('news.editor.edit', $post, false), $usages['Titelbild']->url);
|
||
|
||
// Titelbild entfernen: Datei bleibt in der Mediathek, Verwendung fällt weg.
|
||
$post->update(['cover_path' => null]);
|
||
$this->assertSame(['Im Text'], MediaUsage::query()->where('source_type', 'news')->pluck('context')->all());
|
||
Storage::disk('uploads')->assertExists($cover);
|
||
}
|
||
|
||
public function test_events_and_dashboard_tiles_are_tracked(): void
|
||
{
|
||
$image = $this->putImage('events/fest.jpg');
|
||
$tileImage = $this->putImage('dashboard/banner.png');
|
||
|
||
Event::factory()->create(['title' => 'Betriebsfest', 'image_path' => $image]);
|
||
DashboardTile::query()->create(['name' => 'Banner', 'type' => 'image', 'image_path' => $tileImage, 'is_active' => true]);
|
||
|
||
$this->assertSame(['dashboard_tiles', 'events'], MediaUsage::query()->orderBy('source_type')->pluck('source_type')->all());
|
||
// Standardwerte der Datenbank (veröffentlicht/aktiv) gelten nicht als Entwurf.
|
||
$this->assertSame(0, MediaUsage::query()->where('draft', true)->count());
|
||
}
|
||
|
||
public function test_replace_keeps_path_and_changes_url_version(): void
|
||
{
|
||
$media = app(MediaLibrary::class)->store(UploadedFile::fake()->image('alt.jpg', 800, 600), 'mediathek', $this->admin);
|
||
$path = $media->path;
|
||
$oldUrl = $media->url();
|
||
$this->assertStringContainsString('?v=', $oldUrl);
|
||
|
||
$this->actingAs($this->admin)->post('/redaktion/mediathek/'.$media->id.'/ersetzen', ['image' => UploadedFile::fake()->image('neu.png', 400, 300)])
|
||
->assertOk()
|
||
->assertJsonPath('media.path', $path)
|
||
->assertJsonPath('media.width', 400);
|
||
|
||
$media->refresh();
|
||
$this->assertSame($path, $media->path);
|
||
$this->assertSame(300, $media->height);
|
||
$this->assertSame('neu.png', $media->original_name);
|
||
$this->assertNotSame($oldUrl, $media->url());
|
||
Storage::disk('uploads')->assertExists($path);
|
||
Storage::disk('uploads')->assertExists(preg_replace('/\.webp$/', '', $path).'-thumb.webp');
|
||
}
|
||
|
||
public function test_delete_removes_files_and_usages_and_blocks_show_placeholder(): void
|
||
{
|
||
$path = $this->putImage('pagebuilder/pages/1/foto.webp');
|
||
$page = Page::query()->create(['title' => 'Team']);
|
||
$page->publish('Team', app(Content::class)->sanitize(['blocks' => [$this->imageBlock($path)]]), $this->admin);
|
||
$media = Media::query()->sole();
|
||
$this->assertSame(1, $media->usages()->count());
|
||
|
||
$this->actingAs($this->admin)->getJson('/redaktion/mediathek/'.$media->id)->assertOk()
|
||
->assertJsonPath('usages.0.context', 'Bild-Block')
|
||
->assertJsonPath('usages.0.label', 'Team')
|
||
->assertJsonPath('usages.0.type_label', 'Seiten');
|
||
|
||
$this->deleteJson('/redaktion/mediathek/'.$media->id)->assertOk();
|
||
|
||
$this->assertSame(0, Media::query()->count());
|
||
$this->assertSame(0, MediaUsage::query()->count());
|
||
Storage::disk('uploads')->assertMissing($path);
|
||
|
||
// Die Seite zeigt den Platzhalter statt eines kaputten Bilds.
|
||
$this->get('/seiten/team')->assertOk()->assertInertia(fn (Assert $p) => $p->where('content.blocks.0.props.url', null));
|
||
}
|
||
|
||
public function test_bulk_delete_only_unused(): void
|
||
{
|
||
$library = app(MediaLibrary::class);
|
||
$used = $library->register($this->putImage('mediathek/a.webp'));
|
||
$unused = $library->register($this->putImage('mediathek/b.webp'));
|
||
$page = Page::query()->create(['title' => 'Team']);
|
||
$page->publish('Team', app(Content::class)->sanitize(['blocks' => [$this->imageBlock($used->path)]]), $this->admin);
|
||
|
||
$this->actingAs($this->admin)->postJson('/redaktion/mediathek/loeschen', ['ids' => [$used->id, $unused->id], 'only_unused' => true])
|
||
->assertOk()->assertJsonPath('deleted', 1);
|
||
|
||
$this->assertModelExists($used);
|
||
$this->assertModelMissing($unused);
|
||
}
|
||
|
||
public function test_listing_filters_and_search(): void
|
||
{
|
||
$library = app(MediaLibrary::class);
|
||
$used = $library->register($this->putImage('mediathek/sommerfest.webp'), ['original_name' => 'Sommerfest.jpg'], $this->admin);
|
||
$other = $library->register($this->putImage('mediathek/buero.webp'), ['original_name' => 'Büro.jpg', 'alt' => 'Großraumbüro']);
|
||
$page = Page::query()->create(['title' => 'Team']);
|
||
$page->publish('Team', app(Content::class)->sanitize(['blocks' => [$this->imageBlock($used->path)]]), $this->admin);
|
||
|
||
$this->actingAs($this->admin);
|
||
$this->getJson('/redaktion/mediathek/liste')->assertOk()->assertJsonCount(2, 'items')->assertJsonPath('total', 2)->assertJsonPath('items.0.id', $other->id);
|
||
$this->getJson('/redaktion/mediathek/liste?q=raumbü')->assertJsonCount(1, 'items')->assertJsonPath('items.0.id', $other->id);
|
||
$this->getJson('/redaktion/mediathek/liste?status=unbenutzt')->assertJsonCount(1, 'items')->assertJsonPath('items.0.id', $other->id);
|
||
$this->getJson('/redaktion/mediathek/liste?in=pages')->assertJsonCount(1, 'items')->assertJsonPath('items.0.id', $used->id)->assertJsonPath('items.0.usage_count', 1);
|
||
$this->getJson('/redaktion/mediathek/liste?von='.$this->admin->id)->assertJsonCount(1, 'items');
|
||
$this->getJson('/redaktion/mediathek/liste?'.http_build_query(['pfade' => [$used->path]]))->assertJsonCount(1, 'items');
|
||
$this->getJson('/redaktion/mediathek/liste?sortierung=verwendet')->assertJsonPath('items.0.id', $used->id);
|
||
|
||
$this->putJson('/redaktion/mediathek/'.$other->id, ['alt' => 'Büro mit Pflanzen', 'caption' => 'Verwaltung', 'tags' => ['Büro', ' Verwaltung ', 'Büro']])
|
||
->assertOk()->assertJsonPath('media.alt', 'Büro mit Pflanzen')->assertJsonPath('media.tags', ['Büro', 'Verwaltung']);
|
||
$this->getJson('/redaktion/mediathek/liste?q=Verwaltung')->assertJsonCount(1, 'items');
|
||
|
||
$this->get('/redaktion/mediathek')->assertOk()->assertInertia(fn (Assert $p) => $p
|
||
->component('Media/Index')
|
||
->where('canManage', true)
|
||
->has('sources'));
|
||
}
|
||
|
||
public function test_permissions(): void
|
||
{
|
||
$media = app(MediaLibrary::class)->register($this->putImage('mediathek/a.webp'));
|
||
$employee = User::factory()->create();
|
||
|
||
$this->actingAs($employee)->get('/redaktion/mediathek')->assertForbidden();
|
||
$this->getJson('/redaktion/mediathek/liste')->assertForbidden();
|
||
$this->post('/redaktion/mediathek', ['image' => UploadedFile::fake()->image('x.jpg')])->assertForbidden();
|
||
|
||
// Wer Seiten bearbeitet, darf die Mediathek benutzen und hochladen – aber nicht löschen oder ersetzen.
|
||
$editor = $this->pageEditor(Page::query()->create(['title' => 'MAV']));
|
||
$this->actingAs($editor)->get('/redaktion/mediathek')->assertOk()->assertInertia(fn (Assert $p) => $p->where('canManage', false));
|
||
$this->getJson('/redaktion/mediathek/liste')->assertOk();
|
||
$this->post('/redaktion/mediathek', ['image' => UploadedFile::fake()->image('x.jpg', 300, 200)], ['Accept' => 'application/json'])->assertCreated()
|
||
->assertJsonPath('media.uploader.id', $editor->id);
|
||
$this->putJson('/redaktion/mediathek/'.$media->id, ['alt' => 'Beschreibung'])->assertOk();
|
||
$this->deleteJson('/redaktion/mediathek/'.$media->id)->assertForbidden();
|
||
$this->post('/redaktion/mediathek/'.$media->id.'/ersetzen', ['image' => UploadedFile::fake()->image('y.jpg')], ['Accept' => 'application/json'])->assertForbidden();
|
||
$this->postJson('/redaktion/mediathek/loeschen', ['ids' => [$media->id]])->assertForbidden();
|
||
|
||
// „Mediathek verwalten“ allein reicht für alles.
|
||
$role = Role::query()->create(['name' => 'Medien']);
|
||
$role->grants()->create(['ability' => 'media.manage']);
|
||
$manager = User::factory()->withRoles([$role])->create();
|
||
$this->actingAs($manager)->deleteJson('/redaktion/mediathek/'.$media->id)->assertOk();
|
||
}
|
||
|
||
public function test_rebuild_command_imports_referenced_files(): void
|
||
{
|
||
$cover = $this->putImage('news/alt-cover.jpg', 1600, 900);
|
||
$orphan = $this->putImage('news/verwaist.jpg');
|
||
$pageImage = $this->putImage('pagebuilder/pages/9/bild.webp');
|
||
$category = NewsCategory::factory()->create();
|
||
|
||
// Wie aus einer älteren Version: Inhalte ohne Mediathek-Einträge.
|
||
NewsPost::factory()->create(['category_id' => $category->id, 'cover_path' => $cover, 'published_at' => now()->subDay()]);
|
||
$page = Page::query()->create(['title' => 'Team']);
|
||
$page->publish('Team', app(Content::class)->sanitize(['blocks' => [$this->imageBlock($pageImage)]]), $this->admin);
|
||
MediaUsage::query()->delete();
|
||
Media::query()->delete();
|
||
|
||
$this->artisan('media:rebuild')->assertSuccessful();
|
||
|
||
$this->assertEqualsCanonicalizing([$cover, $pageImage], Media::query()->pluck('path')->all());
|
||
$this->assertSame(2, MediaUsage::query()->count());
|
||
$this->assertNull(Media::query()->where('path', $orphan)->first());
|
||
$this->assertFalse(MediaUsage::query()->where('source_type', 'news')->sole()->draft);
|
||
}
|
||
|
||
public function test_missing_file_resolves_to_null_url(): void
|
||
{
|
||
$media = app(MediaLibrary::class)->register($this->putImage('mediathek/weg.webp'));
|
||
Storage::disk('uploads')->delete($media->path);
|
||
|
||
$this->assertNull($media->url());
|
||
$this->assertTrue($media->toLibraryArray()['missing']);
|
||
}
|
||
|
||
public function test_image_field_accepts_library_paths_but_not_traversal(): void
|
||
{
|
||
$content = app(Content::class)->sanitize(['blocks' => [
|
||
$this->imageBlock('mediathek/2026/10/a.webp', 'blk00001'),
|
||
$this->imageBlock('../.env', 'blk00002'),
|
||
$this->imageBlock('news/x.php', 'blk00003'),
|
||
]]);
|
||
|
||
$this->assertSame('mediathek/2026/10/a.webp', $content['blocks'][0]['data']['path']);
|
||
$this->assertNull($content['blocks'][1]['data']['path']);
|
||
$this->assertNull($content['blocks'][2]['data']['path']);
|
||
}
|
||
}
|