Loheide-eu-mitarbeiterplatt.../tests/Feature/Plugins/NewsTest.php
Friederich Loheide 694ccf6ae0 Mediathek: zentrale Bildverwaltung mit Verwendungsnachweis und Bildauswahl
- 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>
2026-09-26 20:05:50 +00:00

161 lines
6.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Plugins;
use App\Models\Media;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Tests\TestCase;
class NewsTest extends TestCase
{
use RefreshDatabase;
public function test_only_published_posts_are_listed(): void
{
NewsPost::factory()->count(2)->create();
NewsPost::factory()->draft()->create();
NewsPost::factory()->scheduled()->create();
$this->actingAs(User::factory()->create())
->get('/neuigkeiten')
->assertOk()
->assertInertia(fn (Assert $page) => $page->component('news::Index')->where('posts.meta.total', 2));
}
public function test_posts_can_be_filtered_by_category(): void
{
$mav = NewsCategory::factory()->create(['name' => 'MAV', 'slug' => 'mav']);
NewsPost::factory()->create(['category_id' => $mav->id]);
NewsPost::factory()->count(2)->create();
$this->actingAs(User::factory()->create())
->get('/neuigkeiten?kategorie=mav')
->assertInertia(fn (Assert $page) => $page
->where('posts.meta.total', 1)
->where('posts.data.0.category.name', 'MAV')
->where('activeCategory', 'mav'));
}
public function test_published_post_is_shown_with_author(): void
{
$author = User::factory()->create(['first_name' => 'Grete', 'last_name' => 'Müller']);
$post = NewsPost::factory()->create(['author_id' => $author->id, 'title' => 'Sommerfest 2026']);
$this->assertSame('sommerfest-2026', $post->slug);
$this->actingAs(User::factory()->create())
->get('/neuigkeiten/sommerfest-2026')
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('news::Show')
->where('post.title', 'Sommerfest 2026')
->where('post.author.name', 'Grete Müller'));
}
public function test_drafts_and_scheduled_posts_are_not_reachable(): void
{
$draft = NewsPost::factory()->draft()->create();
$scheduled = NewsPost::factory()->scheduled()->create();
$user = User::factory()->create();
$this->actingAs($user)->get("/neuigkeiten/{$draft->slug}")->assertNotFound();
$this->actingAs($user)->get("/neuigkeiten/{$scheduled->slug}")->assertNotFound();
}
public function test_slugs_are_unique(): void
{
$a = NewsPost::factory()->create(['title' => 'Neuigkeit']);
$b = NewsPost::factory()->create(['title' => 'Neuigkeit']);
$this->assertSame('neuigkeit', $a->slug);
$this->assertSame('neuigkeit-2', $b->slug);
}
public function test_body_is_sanitized_when_saved(): void
{
$post = NewsPost::factory()->create([
'body' => '<p onclick="alert(1)">Hallo <strong>Welt</strong></p><script>alert(1)</script><a href="javascript:alert(1)">Link</a><iframe src="https://evil.example"></iframe>',
]);
$this->assertStringContainsString('<strong>Welt</strong>', $post->body_html);
$this->assertStringNotContainsString('script', $post->body_html);
$this->assertStringNotContainsString('onclick', $post->body_html);
$this->assertStringNotContainsString('javascript:', $post->body_html);
$this->assertStringNotContainsString('iframe', $post->body_html);
}
public function test_reading_time_is_calculated(): void
{
$post = NewsPost::factory()->create(['body' => '<p>'.str_repeat('Wort ', 450).'</p>']);
$this->assertSame(3, $post->reading_minutes);
}
public function test_cover_variants_are_created_and_image_stays_in_the_library(): void
{
Storage::fake('uploads');
Storage::disk('uploads')->put('news/cover.jpg', UploadedFile::fake()->image('cover.jpg', 2000, 1200)->getContent());
$post = NewsPost::factory()->create(['cover_path' => 'news/cover.jpg']);
Storage::disk('uploads')->assertExists('news/cover-thumb.webp');
Storage::disk('uploads')->assertExists('news/cover-large.webp');
$this->assertStringContainsString('news/cover-thumb.webp?v=', $post->coverUrl('thumb'));
// Das Bild kann auch anderswo eingebunden sein – es bleibt in der Mediathek, nur die Verwendung fällt weg.
$post->delete();
Storage::disk('uploads')->assertExists('news/cover.jpg');
$this->assertSame(0, Media::query()->where('path', 'news/cover.jpg')->sole()->usages()->count());
// Fehlt die Datei, gibt es keine Adresse (Platzhalter statt kaputtem Bild).
Storage::disk('uploads')->delete(['news/cover.jpg', 'news/cover-thumb.webp', 'news/cover-large.webp']);
$this->assertNull($post->coverUrl('thumb'));
}
public function test_large_photos_fit_into_shared_hosting_memory_limit(): void
{
Storage::fake('uploads');
// Testbild (12 Megapixel wie ein Handyfoto) mit großzügigem Limit erzeugen …
ini_set('memory_limit', '512M');
Storage::disk('uploads')->put('news/handyfoto.jpg', UploadedFile::fake()->image('handyfoto.jpg', 4000, 3000)->getContent());
gc_collect_cycles();
// … dann wie auf typischem Shared Hosting mit 128 MB verarbeiten. Der Test-Prozess
// selbst belegt schon Speicher (viele Tests in einem Lauf), daher 128 MB zusätzlich dazu.
$limit = memory_get_usage(true) + 128 * 1024 * 1024;
$this->assertNotFalse(ini_set('memory_limit', (string) $limit));
NewsPost::factory()->create(['cover_path' => 'news/handyfoto.jpg']);
Storage::disk('uploads')->assertExists('news/handyfoto-large.webp');
Storage::disk('uploads')->assertExists('news/handyfoto-thumb.webp');
ini_set('memory_limit', '512M');
}
public function test_dashboard_shows_featured_and_latest_news(): void
{
Storage::fake('uploads');
Storage::disk('uploads')->put('news/a.jpg', UploadedFile::fake()->image('a.jpg', 800, 450)->getContent());
$featured = NewsPost::factory()->create(['is_featured' => true, 'cover_path' => 'news/a.jpg']);
NewsPost::factory()->count(13)->create();
NewsPost::factory()->draft()->create(['is_featured' => true, 'cover_path' => 'news/a.jpg']);
$response = $this->actingAs(User::factory()->create())->get('/');
$hero = $this->dashboardWidget($response, 'news.hero')['posts'];
$this->assertCount(1, $hero);
$this->assertSame($featured->id, $hero[0]['id']);
$this->assertCount(4, $this->dashboardWidget($response, 'news.latest')['posts']);
$this->assertCount(8, $this->dashboardWidget($response, 'news.areas')['posts'], 'Zwei Reihen à vier Beiträge');
}
}