Loheide-eu-mitarbeiterplatt.../tests/Feature/Documents/DocumentsTest.php
Friederich Loheide dda250aaad
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m55s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m44s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m25s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m39s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m47s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m51s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m16s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m25s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Menü-Editor, farbige Initialen, Seitenköpfe, Favoriten, Vorschaubilder
- Admin → Portal → Menü: Bereiche, Seiten (mit Unterseiten), Links und
  Gruppen mit Unterpunkten, sortierbar, je Rolle einschränkbar;
  Kopfzeile misst den Platz und legt Überzähliges unter „Mehr“
- Initialen in fester Farbe je Person, Abteilungsfarben im Organigramm
- PageHeader für alle Übersichtsseiten
- Dokumente: Favoriten (auch als Startseiten-Kachel), „Häufig genutzt“,
  Vorschaubilder (Bilder, erste PDF-Seite per pdf.js im Browser)
- Platzhalter beim Nachladen

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

222 lines
11 KiB
PHP

<?php
namespace Tests\Feature\Documents;
use App\Models\Role;
use App\Models\User;
use App\Pagebuilder\Content;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Documents\FolderTree;
use Inhaus\Plugins\Documents\Models\Document;
use Inhaus\Plugins\Documents\Models\DocumentFolder;
use Tests\TestCase;
class DocumentsTest extends TestCase
{
use RefreshDatabase;
private DocumentFolder $personal;
private DocumentFolder $vertraege;
private User $hr;
protected function setUp(): void
{
parent::setUp();
Storage::fake('local');
$this->personal = DocumentFolder::query()->create(['name' => 'Personal']);
$this->vertraege = DocumentFolder::query()->create(['name' => 'Verträge', 'parent_id' => $this->personal->id]);
$role = Role::query()->create(['name' => 'Personalabteilung']);
$role->grants()->create(['ability' => 'documents.edit', 'resource_type' => 'document_folder', 'resource_id' => $this->personal->id, 'include_children' => true]);
$this->hr = User::factory()->withRoles([$role])->create();
}
private function uploadTo(DocumentFolder $folder, array $files)
{
return $this->actingAs($this->hr)->post("/dokumente/ordner/{$folder->id}/dateien", ['files' => $files]);
}
public function test_editors_upload_documents_with_versions(): void
{
$this->uploadTo($this->vertraege, [
UploadedFile::fake()->create('Arbeitsvertrag Vorlage.docx', 40),
UploadedFile::fake()->create('Dienstanweisung.pdf', 120, 'application/pdf'),
])->assertSessionHas('status', '2 Dokumente hochgeladen.');
$doc = Document::query()->where('title', 'Dienstanweisung')->sole();
$this->assertSame(1, $doc->currentVersion->version);
Storage::disk('local')->assertExists($doc->currentVersion->path);
$this->post("/dokumente/{$doc->id}/versionen", ['file' => UploadedFile::fake()->create('Dienstanweisung 2027.pdf', 130, 'application/pdf'), 'note' => 'Neue Pausenregelung'])->assertSessionHasNoErrors();
$doc->refresh();
$this->assertSame(2, $doc->currentVersion->version);
$versions = $this->getJson("/dokumente/{$doc->id}/versionen")->assertOk()->json('versions');
$this->assertSame([2, 1], array_column($versions, 'version'));
$this->assertSame('Neue Pausenregelung', $versions[0]['note']);
$this->get($versions[1]['download_url'])->assertOk()->assertDownload('Dienstanweisung.pdf');
$this->get($versions[0]['download_url'])->assertOk()->assertDownload('Dienstanweisung 2027.pdf');
}
public function test_dangerous_files_are_rejected(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('ok.pdf', 10), UploadedFile::fake()->create('boese.php', 1)])
->assertSessionHasErrors('files.1');
$this->assertSame(0, Document::query()->count(), 'Nichts wird halb hochgeladen.');
}
public function test_rights_apply_per_folder_including_subfolders(): void
{
$other = DocumentFolder::query()->create(['name' => 'Technik']);
$employee = User::factory()->create();
$this->uploadTo($other, [UploadedFile::fake()->create('x.pdf', 1)])->assertForbidden();
$this->uploadTo($this->vertraege, [UploadedFile::fake()->create('x.pdf', 1)])->assertSessionHasNoErrors();
$this->actingAs($employee)->post("/dokumente/ordner/{$this->personal->id}/dateien", ['files' => [UploadedFile::fake()->create('y.pdf', 1)]])->assertForbidden();
$this->actingAs($employee)->get("/dokumente/ordner/{$this->vertraege->id}")->assertOk()->assertInertia(fn (Assert $p) => $p
->where('folder.can_edit', false)
->where('folder.editors', ['Personalabteilung'])
->has('documents', 1));
// Unterordner anlegen darf die Personalabteilung, oberste Ebene nicht.
$this->actingAs($this->hr)->post('/dokumente/ordner', ['name' => 'Richtlinien', 'parent_id' => $this->personal->id])->assertRedirect();
$this->post('/dokumente/ordner', ['name' => 'Neu oben'])->assertForbidden();
// Neuer Unterordner ist sofort mit abgedeckt.
$new = DocumentFolder::query()->where('name', 'Richtlinien')->sole();
$this->uploadTo($new, [UploadedFile::fake()->create('z.pdf', 1)])->assertSessionHasNoErrors();
// Verschieben nur in Ordner, in denen man pflegen darf.
$doc = Document::query()->where('folder_id', $new->id)->sole();
$this->put("/dokumente/{$doc->id}", ['title' => 'Z', 'folder_id' => $other->id])->assertForbidden();
$this->put("/dokumente/{$doc->id}", ['title' => 'Z', 'folder_id' => $this->personal->id])->assertSessionHasNoErrors();
}
public function test_visibility_is_inherited_and_protects_downloads(): void
{
$this->uploadTo($this->vertraege, [UploadedFile::fake()->create('Gehaltstabelle.xlsx', 5)]);
$doc = Document::query()->sole();
Audiences::sync('document_folder', $this->personal->id, ['department' => ['Personal']]);
FolderTree::flush();
$clerk = User::factory()->create(['department' => 'Personal']);
$nurse = User::factory()->create(['department' => 'Pflege']);
$this->actingAs($clerk)->get($doc->downloadUrl())->assertOk();
$this->actingAs($nurse)->get($doc->downloadUrl())->assertNotFound();
$this->actingAs($nurse)->get("/dokumente/ordner/{$this->vertraege->id}")->assertNotFound();
$this->actingAs($nurse)->get('/dokumente?q=Gehalt')->assertInertia(fn (Assert $p) => $p->where('documents', []));
$this->actingAs($clerk)->get('/dokumente?q=Gehalt')->assertInertia(fn (Assert $p) => $p->where('documents.0.title', 'Gehaltstabelle'));
}
public function test_search_finds_titles_descriptions_and_file_names(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('Formular-U12.pdf', 5)]);
Document::query()->sole()->update(['title' => 'Urlaubsantrag', 'description' => 'Für Erholungsurlaub']);
foreach (['Urlaub', 'Erholung', 'U12'] as $term) {
$this->get('/dokumente?q='.$term)->assertInertia(fn (Assert $p) => $p->has('documents', 1));
}
}
public function test_overview_lists_areas_and_recent_documents(): void
{
$this->personal->update(['description' => 'Alles rund ums Personal']);
$this->uploadTo($this->vertraege, [UploadedFile::fake()->create('Vertrag.pdf', 5)]);
$this->get('/dokumente')->assertInertia(fn (Assert $p) => $p
->where('children.0.description', 'Alles rund ums Personal')
->where('children.0.folders', 1)
->where('children.0.documents', 0)
->where('recent.0.title', 'Vertrag')
->where('recent.0.folder_name', 'Verträge')
->where('recent.0.updated_by', $this->hr->name));
$this->get("/dokumente/ordner/{$this->vertraege->id}")->assertInertia(fn (Assert $p) => $p->where('recent', [])->where('documents.0.folder_name', null));
}
public function test_favorites_downloads_and_popular_documents(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('Urlaubsantrag.pdf', 5, 'application/pdf'), UploadedFile::fake()->create('Reisekosten.xlsx', 5)]);
[$antrag, $reise] = [Document::query()->where('title', 'Urlaubsantrag')->sole(), Document::query()->where('title', 'Reisekosten')->sole()];
$employee = User::factory()->create(['department' => 'Pflege']);
$this->actingAs($employee)->post("/dokumente/{$antrag->id}/favorit")->assertRedirect();
$this->post("/dokumente/{$antrag->id}/favorit"); // doppelt schadet nicht
$this->get($reise->downloadUrl())->assertOk();
$this->get($reise->downloadUrl())->assertOk();
$before = $reise->fresh()->updated_at;
$this->get($antrag->downloadUrl().'?vorschau=1')->assertOk()->assertHeader('Content-Security-Policy');
$this->assertSame(2, $reise->fresh()->downloads);
$this->assertSame(0, $antrag->fresh()->downloads, 'Vorschaubilder zählen nicht');
$this->assertEquals($before, $reise->fresh()->updated_at, 'Downloads sind keine Änderung');
$this->get('/dokumente')->assertInertia(fn (Assert $p) => $p
->where('favorites.0.title', 'Urlaubsantrag')
->where('favorites.0.favorite', true)
->where('favorites.0.preview_kind', 'pdf')
->where('popular.0.title', 'Reisekosten')
->where('popular.0.preview_url', null));
$this->get('/')->assertInertia(fn (Assert $p) => $p->where(
'widgets',
fn ($widgets) => collect($widgets)->firstWhere('key', 'documents.favorites')['props']['favorites'][0]['title'] === 'Urlaubsantrag'
));
$this->delete("/dokumente/{$antrag->id}/favorit");
$this->get('/dokumente')->assertInertia(fn (Assert $p) => $p->where('favorites', []));
// Ohne Sichtrecht kein Favorit.
Audiences::sync('document_folder', $this->personal->id, ['department' => ['Personal']]);
FolderTree::flush();
$this->post("/dokumente/{$antrag->id}/favorit")->assertNotFound();
}
public function test_previews_are_only_served_for_safe_types(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('notiz.txt', 1, 'text/plain')]);
$this->get(Document::query()->sole()->downloadUrl().'?vorschau=1')->assertNotFound();
}
public function test_folders_can_only_be_deleted_when_empty(): void
{
$this->actingAs($this->hr)->delete("/dokumente/ordner/{$this->vertraege->id}")->assertRedirect(route('documents.folder', $this->personal));
$this->assertNull(DocumentFolder::query()->find($this->vertraege->id));
$this->uploadTo($this->personal, [UploadedFile::fake()->create('a.pdf', 1)]);
$this->actingAs(User::factory()->admin()->create())->delete("/dokumente/ordner/{$this->personal->id}")->assertSessionHasErrors('folder');
}
public function test_folder_block_in_pages(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('Checkliste.pdf', 5)]);
$content = app(Content::class);
$rendered = $content->render($content->sanitize(['blocks' => [['type' => 'documents.folder', 'data' => ['folder' => (string) $this->personal->id]]]]), User::factory()->create());
$this->assertSame('Checkliste', $rendered['blocks'][0]['props']['documents'][0]['title']);
}
public function test_deleting_a_document_removes_all_files(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('a.pdf', 1)]);
$doc = Document::query()->sole();
$this->post("/dokumente/{$doc->id}/versionen", ['file' => UploadedFile::fake()->create('a2.pdf', 1)]);
$paths = $doc->versions()->pluck('path');
$this->delete("/dokumente/{$doc->id}")->assertSessionHasNoErrors();
foreach ($paths as $path) {
Storage::disk('local')->assertMissing($path);
}
}
}