Loheide-eu-mitarbeiterplatt.../tests/Feature/DemoContentTest.php
Friederich Loheide 3addc09c38
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.3 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.4 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.4 (pull_request) Has been skipped
CI / Browser-Tests (pull_request) Successful in 8m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 9m8s
CI / PHP 8.3 / mysql:8.4-1 (pull_request) Successful in 17m55s
CI / Build-Artefakte aktuell (pull_request) Successful in 9m50s
Frag das Portal durchsucht alles, neue Demo-Inhalte (0.66.0)
- Wissensquellen: Termine, Downloads, Formulare, Stellen, Ideen, Umfragen,
  Personen, eigene IT-Tickets/Geräte (OpenNIT, je Anfrage zwischengespeichert)
- Ranking mit IDF, Abfrage je Begriff, Abkürzungen als ganze Wörter,
  LIKE unabhängig von Groß-/Kleinschreibung (JSON-Spalten), Datum im Prompt
- Demo-Inhalte für einen Träger der Eingliederungshilfe (DemoData),
  mehrseitige PDFs, vorhandene Inhalte je Bereich entfernen

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 20:01:00 +00:00

212 lines
10 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;
use App\Filament\Pages\DemoContent as DemoContentPage;
use App\Models\DashboardTile;
use App\Models\User;
use App\Support\Demo\DemoContent;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Inhaus\Plugins\Downloads\Models\Download;
use Inhaus\Plugins\Downloads\Models\DownloadVersion;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\Forms\Models\Form;
use Inhaus\Plugins\Gemeinsam\Models\Entry;
use Inhaus\Plugins\Glossar\Models\GlossaryEntry;
use Inhaus\Plugins\Ideen\Models\Idea;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Inhaus\Plugins\Pages\Models\Page;
use Inhaus\Plugins\Qm\Models\QmEntry;
use Inhaus\Plugins\Qm\Models\QmFile;
use Inhaus\Plugins\Qm\Models\QmFolder;
use Inhaus\Plugins\Stellen\Models\JobPosting;
use Inhaus\Plugins\Umfragen\Models\Poll;
use Livewire\Livewire;
use RuntimeException;
use Tests\TestCase;
class DemoContentTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
Storage::fake('local');
Http::fake(['picsum.photos/*' => Http::response(UploadedFile::fake()->image('foto.jpg', 32, 18)->getContent(), 200, ['Content-Type' => 'image/jpeg'])]);
}
public function test_install_creates_tracked_content_and_remove_deletes_it(): void
{
$admin = User::factory()->admin()->create();
NewsCategory::query()->create(['name' => 'Unternehmen', 'slug' => 'unternehmen']);
$demo = app(DemoContent::class);
$result = $demo->install($admin);
$this->assertSame([], $result['warnings']);
$this->assertTrue($demo->isInstalled());
$this->assertSame(26, $result['counts']['Personen']);
$this->assertSame(26, User::query()->where('email', 'like', '%@demo.inhaus.invalid')->count());
$this->assertNotNull(User::query()->where('email', 'thomas.albers@demo.inhaus.invalid')->value('manager_id'));
$this->assertSame('05931 4080-130', User::query()->where('email', 'jens.huesing@demo.inhaus.invalid')->value('phone'));
$this->assertSame(12, NewsPost::query()->published()->count());
$this->assertSame(2, NewsPost::query()->where('is_featured', true)->count());
$this->assertSame(1, NewsPost::query()->where('must_read', true)->count());
$post = NewsPost::query()->whereNotNull('cover_path')->firstOrFail();
Storage::disk('uploads')->assertExists($post->cover_path);
$this->assertStringContainsString('-thumb.webp', $post->coverUrl('thumb'));
$this->assertStringContainsString('<blockquote>', NewsPost::query()->where('title', 'Neues Gewaltschutzkonzept gilt ab sofort')->value('body_html'));
$this->assertSame(13, Event::query()->upcoming()->count());
$this->assertSame(16, Event::query()->where('title', 'Erste-Hilfe-Kurs (Auffrischung)')->value('capacity'));
$this->assertSame(31, Page::query()->whereNotNull('content')->count());
$this->assertNotEmpty(Page::query()->where('title', 'Urlaub beantragen')->firstOrFail()->content['blocks']);
$this->assertSame(9, Download::query()->count());
$version = DownloadVersion::query()->firstOrFail();
$this->assertStringStartsWith('%PDF-1.4', Storage::disk('local')->get($version->path));
$this->assertSame(7, QmFolder::query()->count());
$this->assertSame(15, QmEntry::query()->count());
$this->assertNotEmpty(app(Settings::class)->get('qm.home.intro'));
$qmFile = QmFile::query()->whereHas('entry', fn ($q) => $q->where('title', 'Umgang mit Medikamenten'))->firstOrFail();
Storage::disk('local')->assertExists($qmFile->path);
$this->assertSame('ok', $qmFile->text_status);
// Auch der Text auf der zweiten Seite ist durchsuchbar
$this->assertStringContainsString('Vier-Augen-Prinzip', (string) $qmFile->text);
$this->assertStringContainsString('Medikamentenschulung', (string) $qmFile->text);
$this->assertSame(35, GlossaryEntry::query()->count());
$this->assertSame(4, JobPosting::query()->open()->count());
$this->assertSame(0, JobPosting::query()->whereNull('announced_at')->count(), 'Demo-Stellen kündigen sich nicht an');
$this->assertSame(6, Idea::query()->count());
$this->assertTrue(Idea::query()->where('anonymous', true)->exists());
$this->assertSame(2, Poll::query()->count());
$this->assertSame(3, Form::query()->count());
$this->assertSame(5, Entry::query()->count());
$this->assertSame(1, DashboardTile::query()->count());
$this->assertSame('Vivendi PEP', app(Settings::class)->get('portal.quick_links')[0]['label']);
// Zweites Anlegen wird abgelehnt.
try {
$demo->install($admin);
$this->fail('Zweites Anlegen hätte abgelehnt werden müssen.');
} catch (RuntimeException $e) {
$this->assertStringContainsString('bereits angelegt', $e->getMessage());
}
// Ein Beitrag wurde inzwischen von Hand gelöscht – Entfernen bleibt fehlerfrei.
$post->delete();
$removed = $demo->remove();
$this->assertSame([], $removed['warnings']);
$this->assertFalse($demo->isInstalled());
$this->assertSame(0, User::query()->where('email', 'like', '%@demo.inhaus.invalid')->count());
$this->assertTrue(User::query()->whereKey($admin->id)->exists());
$this->assertSame(0, NewsPost::query()->count());
$this->assertSame(['unternehmen'], NewsCategory::query()->pluck('slug')->all());
$this->assertSame(0, Event::query()->count());
$this->assertSame(0, Page::query()->count());
$this->assertSame(0, Download::query()->count());
$this->assertSame(0, QmFolder::query()->count());
$this->assertSame(0, QmEntry::query()->count());
$this->assertSame(0, Entry::query()->count());
$this->assertSame(0, GlossaryEntry::query()->count());
$this->assertSame(0, JobPosting::query()->count());
$this->assertSame(0, Idea::query()->count());
$this->assertSame(0, Poll::query()->count());
$this->assertSame(0, Form::query()->count());
$this->assertSame(0, DashboardTile::query()->count());
$this->assertNull(app(Settings::class)->get('portal.quick_links'));
$this->assertNull(app(Settings::class)->get('qm.home.intro'));
$this->assertSame([], Storage::disk('uploads')->allFiles());
$this->assertSame([], Storage::disk('local')->allFiles());
// Doppeltes Entfernen schadet nicht.
$this->assertSame(['records' => 0, 'files' => 0, 'warnings' => []], $demo->remove());
}
public function test_existing_content_can_be_cleared_per_area(): void
{
$admin = User::factory()->admin()->create();
NewsPost::factory()->create(['title' => 'Neues Passwort!!!', 'body' => '<p>dfsafdfsafdfsafdfs</p>']);
Event::query()->create(['title' => 'Test', 'starts_at' => now(), 'is_published' => true]);
GlossaryEntry::query()->create(['term' => 'Quatsch']);
Livewire::actingAs($admin)->test(DemoContentPage::class)
->set('clearAreas', ['news', 'glossar'])
->set('clearConfirm', 'falsch')
->call('clear')
->assertNotified('Bitte zur Bestätigung „LÖSCHEN“ eintippen.');
$this->assertSame(1, NewsPost::query()->count());
Livewire::actingAs($admin)->test(DemoContentPage::class)
->set('clearAreas', ['news', 'glossar'])
->set('clearConfirm', 'löschen')
->call('clear');
$this->assertSame(0, NewsPost::query()->count());
$this->assertSame(0, NewsCategory::query()->count());
$this->assertSame(0, GlossaryEntry::query()->count());
$this->assertSame(1, Event::query()->count(), 'Nicht gewählte Bereiche bleiben');
$this->assertTrue(User::query()->whereKey($admin->id)->exists());
// Bei angelegten Demo-Inhalten zuerst diese entfernen
app(DemoContent::class)->install($admin);
$this->expectExceptionMessage('zuerst die Demo-Inhalte entfernen');
app(DemoContent::class)->clear(['events']);
}
public function test_existing_quick_links_are_kept_and_restored(): void
{
$links = [['label' => 'Intranet alt', 'url' => 'https://example.org', 'icon' => 'link', 'new_tab' => true]];
app(Settings::class)->set('portal.quick_links', $links);
$demo = app(DemoContent::class);
$demo->install(User::factory()->admin()->create());
$this->assertEquals($links, app(Settings::class)->get('portal.quick_links'));
$demo->remove();
$this->assertEquals($links, app(Settings::class)->get('portal.quick_links'));
}
public function test_images_fall_back_to_placeholders_without_network(): void
{
Http::fake(['picsum.photos/*' => Http::response('nicht gefunden', 404)]);
app(DemoContent::class)->install(User::factory()->admin()->create());
$path = Event::query()->whereNotNull('image_path')->value('image_path');
Storage::disk('uploads')->assertExists($path);
$this->assertSame('image/jpeg', getimagesizefromstring(Storage::disk('uploads')->get($path))['mime']);
}
public function test_admin_page_installs_and_removes_and_is_forbidden_for_others(): void
{
$this->actingAs(User::factory()->create())->get('/admin/demo-inhalte')->assertForbidden();
$admin = User::factory()->admin()->create();
$this->actingAs($admin)->get('/admin/demo-inhalte')->assertOk()
->assertSee('Keine Demo-Inhalte angelegt.')
->assertSee('Demo-Inhalte anlegen');
Livewire::test(DemoContentPage::class)->call('install')->assertNotified('Demo-Inhalte angelegt');
$this->assertTrue(app(DemoContent::class)->isInstalled());
$this->get('/admin/demo-inhalte')->assertOk()->assertSee('Demo-Inhalte entfernen')->assertSee('Personen');
Livewire::test(DemoContentPage::class)->call('remove')->assertNotified('Demo-Inhalte entfernt');
$this->assertFalse(app(DemoContent::class)->isInstalled());
}
}