Phase 6 (Teil 1): Dokumente und Organigramm

- Dokumente-Plugin: Ordnerbaum mit vererbten Rechten und Sichtbarkeit,
  Hochladen per Ziehen und Ablegen, Versionen, Suche, Pagebuilder-Block
- Organigramm aus den Vorgesetzten, Vorgesetzte im Admin pflegbar
- Personensuche im Kopf übernimmt nur noch Suchbegriffe des Verzeichnisses

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 11:37:59 +00:00
parent 21c706e5f5
commit d6d5d4b0fe
110 changed files with 2209 additions and 208 deletions

View file

@ -0,0 +1,160 @@
<?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)
->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);
}
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Tests\Feature;
use App\Filament\Resources\Users\Pages\EditUser;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Livewire\Livewire;
use Tests\TestCase;
class OrganigramTest extends TestCase
{
use RefreshDatabase;
public function test_organigram_shows_the_top_and_loads_teams_on_demand(): void
{
$ceo = User::factory()->create(['first_name' => 'Clara', 'last_name' => 'Chefin']);
$head = User::factory()->create(['first_name' => 'Holger', 'last_name' => 'Leiter', 'manager_id' => $ceo->id]);
$nurse = User::factory()->create(['first_name' => 'Nina', 'last_name' => 'Pflege', 'manager_id' => $head->id]);
User::factory()->inactive()->create(['manager_id' => $head->id]);
User::factory()->create(); // ohne Vorgesetzte und ohne Team
$this->actingAs($nurse)->get('/personen/organigramm')->assertOk()->assertInertia(fn (Assert $p) => $p
->component('People/Organigram')
->where('roots.0.id', $ceo->id)
->where('roots.0.reports', 1)
->has('roots', 1)
->where('unassigned', 1)
->where('path', [$ceo->id])
->where("teams.{$ceo->id}.0.id", $head->id));
$this->getJson("/personen/organigramm/{$head->id}/team")->assertOk()
->assertJsonPath('team.0.id', $nurse->id)
->assertJsonCount(1, 'team');
// Weg zur gesuchten Person ist aufgeklappt.
$this->get('/personen/organigramm?person='.$nurse->id)->assertInertia(fn (Assert $p) => $p
->where('path', [$ceo->id, $head->id, $nurse->id])
->where("teams.{$head->id}.0.id", $nurse->id));
}
public function test_admin_sets_managers_without_cycles(): void
{
$this->actingAs(User::factory()->admin()->create());
$boss = User::factory()->create();
$employee = User::factory()->create(['manager_id' => $boss->id]);
Livewire::test(EditUser::class, ['record' => $boss->getRouteKey()])
->fillForm(['manager_id' => $employee->id])
->call('save')
->assertHasFormErrors(['manager_id']);
$other = User::factory()->create();
Livewire::test(EditUser::class, ['record' => $employee->getRouteKey()])
->fillForm(['manager_id' => $other->id])
->call('save')
->assertHasNoFormErrors();
$this->assertSame($other->id, $employee->fresh()->manager_id);
}
}

View file

@ -22,7 +22,7 @@ class CorePluginToggleTest extends TestCase
$file = storage_path('app/plugins.json');
$this->previousState = is_file($file) ? file_get_contents($file) : null;
file_put_contents($file, json_encode(['news' => ['enabled' => false], 'events' => ['enabled' => false], 'pages' => ['enabled' => false], 'forms' => ['enabled' => false], 'wordpress-import' => ['enabled' => false]]));
file_put_contents($file, json_encode(['news' => ['enabled' => false], 'events' => ['enabled' => false], 'pages' => ['enabled' => false], 'forms' => ['enabled' => false], 'wordpress-import' => ['enabled' => false], 'documents' => ['enabled' => false]]));
// Plugins werden beim Start der Anwendung geladen – also neu starten.
$this->refreshApplication();