Gib Website-Bilder aus dem Medienmanager frei, ohne sie zu lizenzieren.
Deploy Test / test (push) Successful in 48s
Deploy Test / deploy (push) Successful in 29s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-11-18 22:01:05 +01:00
1 parent 72130d66c5
commit 13530b8171
18 files changed
+940 -2

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Entitlement\Grant;
use App\Domain\Media\WebsiteIntake;
use App\Domain\Media\WebsiteMedia;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class WebsiteIntakeTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Storage::fake('cms_incoming');
Storage::fake('media_derived');
Storage::fake('media_public');
Storage::fake('media_originals');
}
public function test_an_uploaded_image_becomes_a_released_medium(): void
{
Storage::disk('cms_incoming')->put('cms/hero.png', $this->pngBytes());
$editor = User::factory()->redakteur()->create();
$element = app(WebsiteIntake::class)->acceptImage(
'cms/hero.png',
'Lara im Park',
'Foto: Lara und Felix',
'Lara sitzt auf einer Parkbank',
$editor,
);
$element->refresh();
$this->assertSame(0, $element->versions()->count());
$this->assertSame(0, Grant::query()->count());
$this->assertSame('Lara im Park', $element->display_title);
$this->assertSame('vorschau/element-'.$element->id.'.jpg', $element->thumbnail_path);
$this->assertSame('website/'.$element->id.'/datei.jpg', $element->website_public_path);
$this->assertNotNull($element->website_released_at);
Storage::disk('cms_incoming')->assertMissing('cms/hero.png');
Storage::disk('media_derived')->assertExists($element->thumbnail_path);
Storage::disk('media_originals')->assertMissing('cms/hero.png');
$presentation = app(WebsiteMedia::class)->present($element);
$this->assertNotNull($presentation);
$this->assertSame($element->id, $presentation->elementId);
$this->assertSame('Lara sitzt auf einer Parkbank', $presentation->alt);
$this->assertSame('Foto: Lara und Felix', $presentation->credit);
$this->assertSame('bild', $presentation->kind);
$this->assertStringStartsWith("\xFF\xD8", $this->get($presentation->url)->assertOk()->streamedContent());
}
public function test_a_missing_or_misplaced_file_creates_no_medium(): void
{
$intake = app(WebsiteIntake::class);
Storage::disk('cms_incoming')->put('cms/hero.png', $this->pngBytes());
try {
$intake->acceptImage('cms/hero.png', ' ');
$this->fail('Der Titel hätte abgelehnt werden müssen.');
} catch (DomainRuleException $exception) {
$this->assertSame('Der Titel fehlt.', $exception->getMessage());
}
foreach (['cms/fehlt.png', 'originals/geheim.png', 'cms/../originals/geheim.png', ''] as $path) {
try {
$intake->acceptImage($path, 'Ohne Datei');
$this->fail('Die Datei hätte abgelehnt werden müssen.');
} catch (DomainRuleException $exception) {
$this->assertSame('Die Bilddatei liegt nicht im Eingang.', $exception->getMessage());
}
}
$this->assertSame(0, ContentElement::query()->count());
Storage::disk('media_public')->assertMissing('website/1/datei.jpg');
}
public function test_an_unreadable_file_is_kept_but_not_released(): void
{
Storage::disk('cms_incoming')->put('cms/kaputt.jpg', 'kein bild');
$element = app(WebsiteIntake::class)->acceptImage('cms/kaputt.jpg', 'Unlesbar', 'Quelle');
$this->assertNull($element->fresh()->website_released_at);
$this->assertNotNull($element->fresh()->thumbnail_error);
$this->assertNull(app(WebsiteMedia::class)->present($element));
Storage::disk('cms_incoming')->assertExists('cms/kaputt.jpg');
Storage::disk('media_public')->assertMissing('website/'.$element->id.'/datei.jpg');
}
private function pngBytes(): string
{
$image = imagecreatetruecolor(8, 8);
ob_start();
imagepng($image);
$bytes = ob_get_clean();
imagedestroy($image);
return is_string($bytes) ? $bytes : '';
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Catalog\IdentityChange;
use App\Domain\Cms\Catalog\PublicMaterialReader;
use App\Domain\Entitlement\Grant;
use App\Domain\Media\MediaRendition;
use App\Domain\Media\WebsiteMedia;
use App\Domain\Media\WebsiteRelease;
use App\Filament\Resources\ContentElements\Pages\ViewContentElement;
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 WebsiteReleaseTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
Storage::fake('media_derived');
Storage::fake('media_public');
Storage::fake('media_originals');
}
public function test_a_preview_image_can_be_released_without_a_license_or_a_new_version(): void
{
$content = app(ContentService::class);
$element = $content->createElement('bild', 'bild', 'Hero Park');
$version = $content->publishElementVersion($element, null, ['file' => 'inhalte/hero.jpg']);
$element->forceFill([
'display_title' => 'Lara im Park',
'thumbnail_path' => 'vorschau/hero.jpg',
'license_note' => 'Interner Rechtevermerk',
])->save();
Storage::disk('media_derived')->put('vorschau/hero.jpg', 'hero-bytes');
Storage::disk('media_originals')->put('inhalte/hero.jpg', 'original-bytes');
$editor = User::factory()->redakteur()->create();
$this->actingAs($editor, 'staff');
Livewire::test(ViewContentElement::class, ['record' => $element->getKey()])
->set('websiteCredit', 'Foto: Lara und Felix, eigene Aufnahme')
->set('websiteAlt', 'Lara sitzt auf einer Parkbank')
->call('releaseForWebsite')
->assertSee('Für die Website freigegeben');
$element->refresh();
$this->assertSame(1, $element->versions()->count());
$this->assertSame($version->id, $element->versions()->first()->id);
$this->assertSame(0, Grant::query()->count());
$this->assertNotNull($element->website_released_at);
$this->assertSame('website/'.$element->id.'/datei.jpg', $element->website_public_path);
$this->assertSame('Foto: Lara und Felix, eigene Aufnahme', $element->website_credit);
Storage::disk('media_public')->assertExists($element->website_public_path);
Storage::disk('media_derived')->assertExists('vorschau/hero.jpg');
Storage::disk('media_originals')->assertExists('inhalte/hero.jpg');
$this->assertFalse(app(PublicMaterialReader::class)->isPublic($element));
$presentation = app(WebsiteMedia::class)->present($element);
$this->assertNotNull($presentation);
$this->assertSame('Lara im Park', $presentation->title);
$this->assertSame('Lara sitzt auf einer Parkbank', $presentation->alt);
$this->assertSame('Foto: Lara und Felix, eigene Aufnahme', $presentation->credit);
$this->assertSame('bild', $presentation->kind);
$this->get($presentation->url)
->assertOk()
->assertStreamedContent('hero-bytes');
$this->get('/medien/'.$element->website_public_path)->assertNotFound();
$this->assertTrue(IdentityChange::query()->where('subject_id', $element->id)->where('field', 'website_credit')->exists());
$this->assertTrue(IdentityChange::query()->where('subject_id', $element->id)->where('field', 'website_released_at')->exists());
}
public function test_a_film_releases_only_its_teaser_and_can_be_withdrawn(): void
{
$content = app(ContentService::class);
$element = $content->createElement('film', 'film', 'Teaser Park');
$version = $content->publishElementVersion($element, 'Teaser');
MediaRendition::query()->create([
'content_element_version_id' => $version->id,
'role' => 'master',
'label' => 'master',
'path' => 'filme/master.mp4',
]);
Storage::disk('media_derived')->put('filme/master.mp4', 'master-bytes');
$release = app(WebsiteRelease::class);
$this->assertNotNull($release->refusal($element));
$this->assertNull(app(WebsiteMedia::class)->present($element));
Storage::disk('media_public')->assertDirectoryEmpty('/');
MediaRendition::query()->create([
'content_element_version_id' => $version->id,
'role' => 'preview',
'label' => 'teaser',
'path' => 'teaser/kurz.mp4',
]);
Storage::disk('media_derived')->put('teaser/kurz.mp4', 'teaser-bytes');
$release->release($element);
$element->refresh();
$presentation = app(WebsiteMedia::class)->present($element);
$this->assertNotNull($presentation);
$this->assertSame('film', $presentation->kind);
$this->get($presentation->url)->assertOk()->assertStreamedContent('teaser-bytes');
Storage::disk('media_public')->assertMissing('website/'.$element->id.'/datei-master');
$this->assertStringNotContainsString('master', (string) $element->website_public_path);
$release->withdraw($element);
$element->refresh();
$this->assertNull($element->website_released_at);
$this->assertNull(app(WebsiteMedia::class)->present($element));
$this->get(route('website.media', $element))->assertNotFound();
Storage::disk('media_derived')->assertExists('filme/master.mp4');
Storage::disk('media_derived')->assertExists('teaser/kurz.mp4');
}
public function test_trainer_guides_and_unready_files_stay_unpublished(): void
{
$content = app(ContentService::class);
$guide = $content->createElement('text', 'text', 'Abstand Trainer');
$guide->editorial_content_kind_id = EditorialContentKind::query()->where('slug', 'traineranleitung')->value('id');
$guide->thumbnail_path = 'vorschau/intern.jpg';
$guide->output_class = 'bild';
$guide->save();
Storage::disk('media_derived')->put('vorschau/intern.jpg', 'intern');
$this->assertNotNull(app(WebsiteRelease::class)->refusal($guide));
app(WebsiteRelease::class)->release($guide);
$this->assertNull($guide->fresh()->website_released_at);
$image = $content->createElement('bild', 'bild', 'Ohne Datei');
$image->thumbnail_path = 'vorschau/../originals/geheim.jpg';
$image->save();
app(WebsiteRelease::class)->release($image);
$this->assertNull($image->fresh()->website_public_path);
Storage::disk('media_public')->assertDirectoryEmpty('/');
}
public function test_a_slideshow_keeps_only_released_media_in_order(): void
{
$released = $this->releasedImage('Erstes Bild');
$hidden = app(ContentService::class)->createElement('bild', 'bild', 'Zweites Bild');
$later = $this->releasedImage('Drittes Bild');
$presentations = app(WebsiteMedia::class)->presentMany([$released->id, $hidden->id, $later->id, 'kein-medium']);
$this->assertSame(
[$released->id, $later->id],
array_map(fn ($presentation) => $presentation->elementId, $presentations),
);
}
private function releasedImage(string $title): ContentElement
{
$element = app(ContentService::class)->createElement('bild', 'bild', $title);
app(ContentService::class)->publishElementVersion($element, null, ['file' => 'inhalte/'.$element->id.'.jpg']);
$element->thumbnail_path = 'vorschau/element-'.$element->id.'.jpg';
$element->save();
Storage::disk('media_derived')->put($element->thumbnail_path, 'bytes-'.$element->id);
app(WebsiteRelease::class)->release($element);
return $element->fresh();
}
}