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) ->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_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); } } }