Loheide-eu-mitarbeiterplatt.../tests/Feature/Qm/QmTest.php
Friederich Loheide 5a360d3cc7
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m47s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m6s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m7s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 4m3s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 3m56s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 3m26s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 3m34s
CI / Build-Artefakte aktuell (pull_request) Successful in 54s
QM-Startseite: Einführungstext, Hauptansprechpartner, QM-Beauftragte
- Pflege unter QM-Einstellungen → „Startseite des Qualitätsmanagements“
- QM-Beauftragte mit Zuständigkeit und optionalem Ordner-Link,
  Kontaktangaben wie bei den Ansprechpartnern

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

336 lines
17 KiB
PHP

<?php
namespace Tests\Feature\Qm;
use App\Models\Role;
use App\Models\User;
use App\Support\Permissions\Audiences;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Qm\Filament\Pages\QmSettings;
use Inhaus\Plugins\Qm\Indexer;
use Inhaus\Plugins\Qm\Models\QmCategory;
use Inhaus\Plugins\Qm\Models\QmEntry;
use Inhaus\Plugins\Qm\Models\QmFile;
use Inhaus\Plugins\Qm\Models\QmFolder;
use Inhaus\Plugins\Qm\Presenter;
use Inhaus\Plugins\Qm\QmAccess;
use Inhaus\Plugins\Qm\QmTree;
use Inhaus\Plugins\Qm\Text\Ocr;
use Livewire\Livewire;
use Tests\TestCase;
use ZipArchive;
class QmTest extends TestCase
{
use RefreshDatabase;
private User $qmb;
private QmCategory $general;
private QmFolder $personal;
protected function setUp(): void
{
parent::setUp();
Storage::fake('local');
Storage::fake('uploads');
$role = Role::query()->create(['name' => 'QM-Beauftragte']);
$role->grants()->create(['ability' => 'qm.manage']);
$this->qmb = User::factory()->withRoles([$role])->create();
$this->general = QmCategory::query()->create(['name' => 'Allgemein', 'sort' => 1]);
$this->personal = QmFolder::query()->create(['name' => 'Personal', 'category_id' => $this->general->id, 'color' => 'teal', 'icon' => 'users', 'intro' => '<p>Alles zu <strong>Personal</strong><script>alert(1)</script></p>']);
}
/** Ein echtes PDF mit Textebene. */
private function pdf(string $name, array $lines): UploadedFile
{
$content = 'BT /F1 12 Tf 50 750 Td 14 TL '.implode(' ', array_map(fn ($l) => '('.$l.') \'', $lines)).' ET';
$objects = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>',
'<< /Length '.strlen($content)." >>\nstream\n{$content}\nendstream",
'<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>',
];
$pdf = "%PDF-1.4\n";
$offsets = [];
foreach ($objects as $i => $object) {
$offsets[] = strlen($pdf);
$pdf .= ($i + 1)." 0 obj\n{$object}\nendobj\n";
}
$xref = strlen($pdf);
$pdf .= 'xref\n0 '.(count($objects) + 1)."\n0000000000 65535 f \n".implode('', array_map(fn ($o) => sprintf("%010d 00000 n \n", $o), $offsets));
$pdf = str_replace('xref\n', "xref\n", $pdf);
$pdf .= 'trailer << /Size '.(count($objects) + 1)." /Root 1 0 R >>\nstartxref\n{$xref}\n%%EOF";
return UploadedFile::fake()->createWithContent($name, $pdf);
}
private function docx(string $name, string $text): UploadedFile
{
$path = tempnam(sys_get_temp_dir(), 'docx');
$zip = new ZipArchive;
$zip->open($path, ZipArchive::OVERWRITE);
$zip->addFromString('[Content_Types].xml', '<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>');
$zip->addFromString('word/document.xml', '<?xml version="1.0"?><w:document xmlns:w="w"><w:body><w:p><w:r><w:t>'.htmlspecialchars($text).'</w:t></w:r></w:p></w:body></w:document>');
$zip->close();
return UploadedFile::fake()->createWithContent($name, (string) file_get_contents($path));
}
private function addFile(QmFolder $folder, string $title, UploadedFile $reading, array $extra = []): QmEntry
{
$this->actingAs($this->qmb)->post("/qm/ordner/{$folder->id}/eintraege", ['type' => 'file', 'title' => $title, 'version' => '1.0', 'valid_from' => '2026-03-01', 'reading' => $reading, ...$extra])
->assertSessionHasNoErrors();
return QmEntry::query()->where('title', $title)->sole();
}
public function test_start_page_shows_categories_with_colored_tiles_and_inherited_colors(): void
{
$sub = QmFolder::query()->create(['name' => 'Urlaub', 'parent_id' => $this->personal->id]);
QmFolder::query()->create(['name' => 'Arbeitsschutz', 'category_id' => QmCategory::query()->create(['name' => 'Sicherheit', 'sort' => 2])->id, 'color' => 'red']);
$this->actingAs(User::factory()->create())->get('/qm')->assertOk()->assertInertia(fn (Assert $p) => $p
->component('qm::Index')
->where('categories.0.name', 'Allgemein')
->where('categories.0.folders.0.name', 'Personal')
->where('categories.0.folders.0.color', 'teal')
->where('categories.0.folders.0.icon', ['name' => 'users'])
->where('categories.0.folders.0.folders', 1)
->where('categories.1.folders.0.color', 'red')
->where('adminUrl', null));
$node = app(QmTree::class)->folder($sub->id);
$this->assertSame('teal', $node['color'], 'Farbe wird vererbt');
$this->assertSame(['name' => 'users'], $node['icon'], 'Symbol wird vererbt');
}
public function test_start_page_with_intro_main_contacts_and_qm_officers(): void
{
$lead = User::factory()->create(['first_name' => 'Petra', 'last_name' => 'Leitung', 'phone' => '+49 1']);
$care = User::factory()->create(['first_name' => 'Klaus', 'last_name' => 'Pflege']);
$gone = User::factory()->inactive()->create();
$settings = app(Settings::class);
$settings->set(Presenter::HOME_INTRO, '<p>Willkommen im <em>QM</em></p><script>x()</script>');
$settings->set(Presenter::HOME_CONTACTS, [$lead->id]);
$settings->set(Presenter::OFFICERS, [
['user_id' => $care->id, 'area' => 'Pflege', 'folder_id' => $this->personal->id],
['user_id' => $gone->id, 'area' => 'Alt', 'folder_id' => null],
['user_id' => $lead->id, 'area' => null, 'folder_id' => 99999],
]);
$this->actingAs(User::factory()->create())->get('/qm')->assertInertia(fn (Assert $p) => $p
->where('intro', fn ($html) => str_contains($html, '<em>QM</em>') && ! str_contains($html, 'script'))
->where('contacts.0.name', 'Petra Leitung')
->has('officers', 2)
->where('officers.0.name', 'Klaus Pflege')
->where('officers.0.area', 'Pflege')
->where('officers.0.folder.path', 'Personal')
->where('officers.1.folder', null));
}
public function test_admin_maintains_the_start_page(): void
{
$lead = User::factory()->create();
$this->actingAs($this->qmb);
Livewire::test(QmSettings::class)
->set('data.home_intro', '<p>Hallo</p>')
->set('data.home_contacts', [(string) $lead->id])
->set('data.officers', ['a' => ['user_id' => (string) $lead->id, 'area' => 'Küche', 'folder_id' => (string) $this->personal->id]])
->call('save')
->assertHasNoErrors();
$settings = app(Settings::class);
$this->assertStringContainsString('Hallo', $settings->get(Presenter::HOME_INTRO));
$this->assertSame([$lead->id], $settings->get(Presenter::HOME_CONTACTS));
$this->assertSame([['user_id' => $lead->id, 'area' => 'Küche', 'folder_id' => $this->personal->id]], $settings->get(Presenter::OFFICERS));
}
public function test_folder_page_with_intro_contacts_subfolders_and_entries(): void
{
$anna = User::factory()->create(['first_name' => 'Anna', 'last_name' => 'Schmidt', 'phone' => '+49 40 123', 'job_title' => 'Personalleitung', 'location' => 'Hamburg']);
$gone = User::factory()->inactive()->create();
$this->personal->contacts()->attach([$anna->id => ['sort' => 1], $gone->id => ['sort' => 2]]);
app(Settings::class)->set(Presenter::CONTACT_FIELDS, ['phone', 'location']);
QmFolder::query()->create(['name' => 'Urlaub', 'parent_id' => $this->personal->id, 'icon' => 'plane']);
$this->addFile($this->personal, 'Dienstplanregelung', $this->pdf('dienstplan.pdf', ['Dienstplan']));
$this->actingAs(User::factory()->create())->get("/qm/ordner/{$this->personal->id}")->assertOk()->assertInertia(fn (Assert $p) => $p
->component('qm::Folder')
->where('folder.intro', fn ($html) => str_contains($html, '<strong>Personal</strong>') && ! str_contains($html, '<script'))
->has('folder.contacts', 1)
->where('folder.contacts.0.fields', [
['key' => 'phone', 'label' => 'Telefon', 'value' => '+49 40 123', 'type' => 'phone'],
['key' => 'location', 'label' => 'Standort', 'value' => 'Hamburg', 'type' => 'text'],
])
->where('children.0.icon', ['name' => 'plane'])
->where('children.0.color', 'teal')
->where('entries.0.title', 'Dienstplanregelung')
->where('entries.0.version', '1.0')
->where('entries.0.valid_from', '2026-03-01')
->where('entries.0.open_kind', 'view')
->where('canManage', false));
}
public function test_pdfs_open_in_the_portal_and_other_files_download(): void
{
$pdf = $this->addFile($this->personal, 'Leitbild', $this->pdf('leitbild.pdf', ['Unser Leitbild']));
$xlsx = $this->addFile($this->personal, 'Kostenstellen', UploadedFile::fake()->create('kosten.xlsx', 10));
$user = User::factory()->create();
$this->actingAs($user)->get("/qm/lesen/{$pdf->id}")->assertOk()->assertInertia(fn (Assert $p) => $p->component('qm::Viewer')->where('file.kind', 'pdf'));
$inline = $this->get($pdf->readingFile->url(inline: true))->assertOk();
$this->assertStringStartsWith('inline', (string) $inline->headers->get('Content-Disposition'));
$this->get("/qm/lesen/{$xlsx->id}")->assertRedirect($xlsx->readingFile->url());
}
public function test_links_and_online_forms_as_entries(): void
{
$this->actingAs($this->qmb)->post("/qm/ordner/{$this->personal->id}/eintraege", ['type' => 'link', 'title' => 'Urlaubsantrag', 'url' => 'https://hr.example.org/urlaub'])->assertSessionHasNoErrors();
$this->post("/qm/ordner/{$this->personal->id}/eintraege", ['type' => 'link', 'title' => 'Böse', 'url' => 'javascript:alert(1)'])->assertSessionHasErrors('url');
$entry = QmEntry::query()->where('title', 'Urlaubsantrag')->sole();
$this->assertSame(now()->toDateString(), $entry->valid_from->toDateString(), 'Stand ohne Angabe = heute');
$this->get("/qm/ordner/{$this->personal->id}")->assertInertia(fn (Assert $p) => $p->where('entries.0.open_kind', 'link')->where('entries.0.open_url', 'https://hr.example.org/urlaub'));
}
public function test_new_versions_archive_the_old_file(): void
{
$entry = $this->addFile($this->personal, 'Hygieneplan', $this->pdf('hygiene-v1.pdf', ['Alte Fassung']), ['editable' => $this->docx('hygiene.docx', 'Vorlage')]);
$old = $entry->readingFile;
$this->post("/qm/eintraege/{$entry->id}", ['title' => 'Hygieneplan', 'version' => '2.0', 'valid_from' => '2026-06-01', 'reading' => $this->pdf('hygiene-v2.pdf', ['Neue Fassung'])])
->assertSessionHasNoErrors();
$entry->refresh();
$this->assertSame('2.0', $entry->version);
$this->assertSame('hygiene-v2.pdf', $entry->readingFile->name);
$this->assertNotNull($old->fresh()->archived_at);
$this->assertSame('hygiene.docx', $entry->editableFile->name, 'Vorlage bleibt, wenn keine neue kommt');
$this->assertCount(3, $this->getJson("/qm/eintraege/{$entry->id}/archiv")->json('files'));
// Tippfehler korrigieren ändert nur die Angaben.
$this->post("/qm/eintraege/{$entry->id}", ['title' => 'Hygieneplan', 'version' => '2.1', 'valid_from' => '2026-06-01'])->assertSessionHasNoErrors();
$this->assertSame('2.1', $entry->readingFile()->first()->version);
// Archiv nur für das QM-Team
$this->actingAs(User::factory()->create())->get($old->url())->assertNotFound();
$this->getJson("/qm/eintraege/{$entry->id}/archiv")->assertForbidden();
$this->actingAs($this->qmb)->get($old->url())->assertOk();
}
public function test_only_the_qm_team_maintains_entries(): void
{
$this->actingAs(User::factory()->create())
->post("/qm/ordner/{$this->personal->id}/eintraege", ['type' => 'link', 'title' => 'X', 'url' => 'https://x.de'])->assertForbidden();
$this->actingAs(User::factory()->admin()->create())
->post("/qm/ordner/{$this->personal->id}/eintraege", ['type' => 'link', 'title' => 'X', 'url' => 'https://x.de'])->assertSessionHasNoErrors();
}
public function test_search_finds_text_inside_pdf_and_word_files(): void
{
$this->addFile($this->personal, 'Verfahrensanweisung Einarbeitung', $this->pdf('va.pdf', ['Neue Mitarbeitende erhalten einen Paten', 'fuer die ersten drei Monate']));
$this->addFile($this->personal, 'Checkliste', $this->docx('check.docx', 'Schluesselausgabe am ersten Arbeitstag'));
$user = User::factory()->create();
$this->actingAs($user)->get('/qm/suche?q=paten')->assertInertia(fn (Assert $p) => $p
->component('qm::Search')
->has('results', 1)
->where('results.0.title', 'Verfahrensanweisung Einarbeitung')
->where('results.0.in_content', true)
->where('results.0.snippet', fn ($s) => str_contains($s, '<mark>Paten</mark>'))
->where('results.0.folder.path', 'Personal'));
$this->get('/qm/suche?q=schluesselausgabe')->assertInertia(fn (Assert $p) => $p->where('results.0.title', 'Checkliste'));
$this->get('/qm/suche?q=checkliste')->assertInertia(fn (Assert $p) => $p->where('results.0.in_content', false));
}
public function test_excluded_roles_do_not_see_the_module(): void
{
$clients = Role::query()->create(['name' => 'Klient*innen']);
app(Settings::class)->set(QmAccess::EXCLUDED_ROLES, [$clients->id]);
$entry = $this->addFile($this->personal, 'Leitbild', $this->pdf('leitbild.pdf', ['Leitbild']));
$client = User::factory()->withRoles([$clients])->create();
$this->actingAs($client)->get('/qm')->assertNotFound();
$this->get("/qm/ordner/{$this->personal->id}")->assertNotFound();
$this->get('/qm/suche?q=leitbild')->assertNotFound();
$this->get($entry->readingFile->url())->assertNotFound();
$this->get('/')->assertInertia(fn (Assert $p) => $p->where('navigation', fn ($nav) => ! collect($nav)->contains('key', 'qm')));
$this->actingAs(User::factory()->create())->get('/')->assertInertia(fn (Assert $p) => $p->where('navigation', fn ($nav) => collect($nav)->contains('key', 'qm')));
$this->qmb->roles()->attach($clients->id);
$this->assertTrue(app(QmAccess::class)->allows($this->qmb->fresh()), 'Wer pflegt, sieht es immer');
}
public function test_categories_can_be_limited_to_audiences(): void
{
$secret = QmCategory::query()->create(['name' => 'Leitung']);
$folder = QmFolder::query()->create(['name' => 'Protokolle', 'category_id' => $secret->id]);
$this->addFile($folder, 'Protokoll', $this->pdf('p.pdf', ['Vertraulich']));
Audiences::sync('qm_category', $secret->id, ['department' => ['Leitung']]);
QmTree::flush();
$this->actingAs(User::factory()->create(['department' => 'Pflege']))->get("/qm/ordner/{$folder->id}")->assertNotFound();
$this->get('/qm/suche?q=vertraulich')->assertInertia(fn (Assert $p) => $p->where('results', []));
$this->actingAs(User::factory()->create(['department' => 'Leitung']))->get("/qm/ordner/{$folder->id}")->assertOk();
}
public function test_scanned_pdfs_are_queued_for_ocr_and_recognized_when_available(): void
{
$entry = $this->addFile($this->personal, 'Scan', $this->pdf('scan.pdf', []));
$this->assertSame('needs_ocr', $entry->readingFile->text_status);
$this->app->instance(Ocr::class, new class(app(Settings::class)) extends Ocr
{
public function enabled(): bool
{
return true;
}
public function available(): bool
{
return true;
}
public function recognize(string $pdf): string
{
return 'Erkannter Text aus dem Scan';
}
});
$this->app->forgetInstance(Indexer::class);
$this->assertSame(1, app(Indexer::class)->processOcr());
$this->assertSame('ocr', $entry->readingFile()->first()->text_status);
$this->actingAs(User::factory()->create())->get('/qm/suche?q=erkannter')->assertInertia(fn (Assert $p) => $p->where('results.0.title', 'Scan'));
}
public function test_admin_pages_for_the_qm_team(): void
{
$this->actingAs($this->qmb);
foreach (['/admin/qm-ordner', '/admin/qm-ordner/neu', "/admin/qm-ordner/{$this->personal->id}/edit", '/admin/qm-kategorien', '/admin/qm-einstellungen'] as $url) {
$this->get($url)->assertOk();
}
$this->actingAs(User::factory()->create())->get('/admin/qm-ordner')->assertForbidden();
}
public function test_deleting_an_entry_removes_its_files(): void
{
$entry = $this->addFile($this->personal, 'Weg', $this->pdf('weg.pdf', ['weg']));
$path = $entry->readingFile->path;
$this->delete("/qm/eintraege/{$entry->id}")->assertSessionHasNoErrors();
Storage::disk(QmFile::DISK)->assertMissing($path);
$this->assertSame(0, QmFile::query()->count());
}
}