Loheide-eu-mitarbeiterplatt.../tests/Feature/Plugins/NewsTest.php
Friederich Loheide 28ef1e4a20 Plugin-System; Neuigkeiten und Termine als mitgelieferte Plugins
- Plugins nach WordPress-Prinzip: plugin.json-Manifest mit Versions- und
  Abhängigkeitsprüfung, eigenes PSR-4-Autoloading, register()/boot(),
  activate()/deactivate()/uninstall(), Migrationen, Routen und Views je Plugin
- plugins-core/ (mitgeliefert, aktiv bis zur Deaktivierung) und plugins/
  (Kunden-Plugins, geschützt vor Updates, inaktiv bis zur Aktivierung)
- Zustand in storage/app/plugins.json – Laden ohne Datenbankabfrage
- Wiederherstellungsmodus: Ausnahmen und Fatal Errors beim Laden
  deaktivieren das Plugin, das Portal bleibt erreichbar
- Hooks (Actions/Filter mit Prioritäten) und Registries für Navigation,
  Startseiten-Kacheln, Rechte und Admin-Seiten (Filament-Discovery)
- Admin-Seite „Plugins“: aktivieren, deaktivieren, per ZIP installieren
  (ZIP-Slip-Schutz), löschen mit oder ohne Daten; abschaltbar per
  INHAUS_DISALLOW_FILE_MODS
- Startseite komplett aus Kacheln; Plugin-Seiten als „slug::Seite“
- Version 0.3.0

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 07:06:16 +00:00

153 lines
6.1 KiB
PHP

<?php
namespace Tests\Feature\Plugins;
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_removed(): 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->assertStringEndsWith('news/cover-thumb.webp', $post->coverUrl('thumb'));
$post->delete();
Storage::disk('uploads')->assertMissing('news/cover.jpg');
Storage::disk('uploads')->assertMissing('news/cover-thumb.webp');
}
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.
$this->assertNotFalse(ini_set('memory_limit', '128M'));
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', '128M');
}
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(5)->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(1, $this->dashboardWidget($response, 'news.areas')['posts']);
}
}