Loheide-eu-mitarbeiterplatt.../tests/Feature/Downloads/DownloadsTest.php
Friederich Loheide f78cc96be7
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m38s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m54s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m50s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m55s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m18s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
„Dokumente“ heißt jetzt „Downloads“
Der Name „Dokumente“ bleibt dem kommenden QM-Modul (QM-Dokumente und
-Formulare) vorbehalten.

- Plugin plugins-core/downloads (Inhaus\Plugins\Downloads), Adresse
  /downloads, Rechte downloads.*, Objekttyp download_folder,
  Block downloads.folder, Kachel downloads.favorites
- Migration für bestehende Installationen: Tabellen und Spalten,
  Rechte, Sichtbarkeiten, Menü, Startseiten-Anordnung, Blöcke in
  Seiten und Fassungen, Plugin-Zustand; mit Test
- Alte Links unter /dokumente leiten dauerhaft weiter

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

222 lines
11 KiB
PHP

<?php
namespace Tests\Feature\Downloads;
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\Downloads\FolderTree;
use Inhaus\Plugins\Downloads\Models\Download;
use Inhaus\Plugins\Downloads\Models\DownloadFolder;
use Tests\TestCase;
class DownloadsTest extends TestCase
{
use RefreshDatabase;
private DownloadFolder $personal;
private DownloadFolder $vertraege;
private User $hr;
protected function setUp(): void
{
parent::setUp();
Storage::fake('local');
$this->personal = DownloadFolder::query()->create(['name' => 'Personal']);
$this->vertraege = DownloadFolder::query()->create(['name' => 'Verträge', 'parent_id' => $this->personal->id]);
$role = Role::query()->create(['name' => 'Personalabteilung']);
$role->grants()->create(['ability' => 'downloads.edit', 'resource_type' => 'download_folder', 'resource_id' => $this->personal->id, 'include_children' => true]);
$this->hr = User::factory()->withRoles([$role])->create();
}
private function uploadTo(DownloadFolder $folder, array $files)
{
return $this->actingAs($this->hr)->post("/downloads/ordner/{$folder->id}/dateien", ['files' => $files]);
}
public function test_editors_upload_downloads_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 Dateien hochgeladen.');
$doc = Download::query()->where('title', 'Dienstanweisung')->sole();
$this->assertSame(1, $doc->currentVersion->version);
Storage::disk('local')->assertExists($doc->currentVersion->path);
$this->post("/downloads/{$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("/downloads/{$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, Download::query()->count(), 'Nichts wird halb hochgeladen.');
}
public function test_rights_apply_per_folder_including_subfolders(): void
{
$other = DownloadFolder::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("/downloads/ordner/{$this->personal->id}/dateien", ['files' => [UploadedFile::fake()->create('y.pdf', 1)]])->assertForbidden();
$this->actingAs($employee)->get("/downloads/ordner/{$this->vertraege->id}")->assertOk()->assertInertia(fn (Assert $p) => $p
->where('folder.can_edit', false)
->where('folder.editors', ['Personalabteilung'])
->has('downloads', 1));
// Unterordner anlegen darf die Personalabteilung, oberste Ebene nicht.
$this->actingAs($this->hr)->post('/downloads/ordner', ['name' => 'Richtlinien', 'parent_id' => $this->personal->id])->assertRedirect();
$this->post('/downloads/ordner', ['name' => 'Neu oben'])->assertForbidden();
// Neuer Unterordner ist sofort mit abgedeckt.
$new = DownloadFolder::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 = Download::query()->where('folder_id', $new->id)->sole();
$this->put("/downloads/{$doc->id}", ['title' => 'Z', 'folder_id' => $other->id])->assertForbidden();
$this->put("/downloads/{$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 = Download::query()->sole();
Audiences::sync('download_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("/downloads/ordner/{$this->vertraege->id}")->assertNotFound();
$this->actingAs($nurse)->get('/downloads?q=Gehalt')->assertInertia(fn (Assert $p) => $p->where('downloads', []));
$this->actingAs($clerk)->get('/downloads?q=Gehalt')->assertInertia(fn (Assert $p) => $p->where('downloads.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)]);
Download::query()->sole()->update(['title' => 'Urlaubsantrag', 'description' => 'Für Erholungsurlaub']);
foreach (['Urlaub', 'Erholung', 'U12'] as $term) {
$this->get('/downloads?q='.$term)->assertInertia(fn (Assert $p) => $p->has('downloads', 1));
}
}
public function test_overview_lists_areas_and_recent_downloads(): void
{
$this->personal->update(['description' => 'Alles rund ums Personal']);
$this->uploadTo($this->vertraege, [UploadedFile::fake()->create('Vertrag.pdf', 5)]);
$this->get('/downloads')->assertInertia(fn (Assert $p) => $p
->where('children.0.description', 'Alles rund ums Personal')
->where('children.0.folders', 1)
->where('children.0.downloads', 0)
->where('recent.0.title', 'Vertrag')
->where('recent.0.folder_name', 'Verträge')
->where('recent.0.updated_by', $this->hr->name));
$this->get("/downloads/ordner/{$this->vertraege->id}")->assertInertia(fn (Assert $p) => $p->where('recent', [])->where('downloads.0.folder_name', null));
}
public function test_favorites_downloads_and_popular_downloads(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('Urlaubsantrag.pdf', 5, 'application/pdf'), UploadedFile::fake()->create('Reisekosten.xlsx', 5)]);
[$antrag, $reise] = [Download::query()->where('title', 'Urlaubsantrag')->sole(), Download::query()->where('title', 'Reisekosten')->sole()];
$employee = User::factory()->create(['department' => 'Pflege']);
$this->actingAs($employee)->post("/downloads/{$antrag->id}/favorit")->assertRedirect();
$this->post("/downloads/{$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()->download_count);
$this->assertSame(0, $antrag->fresh()->download_count, 'Vorschaubilder zählen nicht');
$this->assertEquals($before, $reise->fresh()->updated_at, 'Downloads sind keine Änderung');
$this->get('/downloads')->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', 'downloads.favorites')['props']['favorites'][0]['title'] === 'Urlaubsantrag'
));
$this->delete("/downloads/{$antrag->id}/favorit");
$this->get('/downloads')->assertInertia(fn (Assert $p) => $p->where('favorites', []));
// Ohne Sichtrecht kein Favorit.
Audiences::sync('download_folder', $this->personal->id, ['department' => ['Personal']]);
FolderTree::flush();
$this->post("/downloads/{$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(Download::query()->sole()->downloadUrl().'?vorschau=1')->assertNotFound();
}
public function test_folders_can_only_be_deleted_when_empty(): void
{
$this->actingAs($this->hr)->delete("/downloads/ordner/{$this->vertraege->id}")->assertRedirect(route('downloads.folder', $this->personal));
$this->assertNull(DownloadFolder::query()->find($this->vertraege->id));
$this->uploadTo($this->personal, [UploadedFile::fake()->create('a.pdf', 1)]);
$this->actingAs(User::factory()->admin()->create())->delete("/downloads/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' => 'downloads.folder', 'data' => ['folder' => (string) $this->personal->id]]]]), User::factory()->create());
$this->assertSame('Checkliste', $rendered['blocks'][0]['props']['downloads'][0]['title']);
}
public function test_deleting_a_download_removes_all_files(): void
{
$this->uploadTo($this->personal, [UploadedFile::fake()->create('a.pdf', 1)]);
$doc = Download::query()->sole();
$this->post("/downloads/{$doc->id}/versionen", ['file' => UploadedFile::fake()->create('a2.pdf', 1)]);
$paths = $doc->versions()->pluck('path');
$this->delete("/downloads/{$doc->id}")->assertSessionHasNoErrors();
foreach ($paths as $path) {
Storage::disk('local')->assertMissing($path);
}
}
}