Loheide-eu-mitarbeiterplatt.../tests/Feature/Qm/QmReviewAcknowledgementTest.php
Friederich Loheide 4d855c8a74
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m49s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m29s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m38s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m40s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m7s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m24s
QM: Wiedervorlage und Lesebestätigung, Platzhalter für Profilbilder (0.21.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 07:36:47 +00:00

125 lines
6.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Qm;
use App\Models\Role;
use App\Models\User;
use App\Notifications\PortalNotification;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Qm\Models\QmCategory;
use Inhaus\Plugins\Qm\Models\QmEntry;
use Inhaus\Plugins\Qm\Models\QmFolder;
use Inhaus\Plugins\Qm\Reviews;
use Tests\TestCase;
class QmReviewAcknowledgementTest extends TestCase
{
use RefreshDatabase;
private User $qmb;
private QmFolder $folder;
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();
$category = QmCategory::query()->create(['name' => 'Pflege', 'sort' => 1]);
$this->folder = QmFolder::query()->create(['name' => 'Hygiene', 'category_id' => $category->id, 'color' => 'teal', 'icon' => 'shield']);
}
private function entry(array $extra = []): QmEntry
{
$this->actingAs($this->qmb)->post("/qm/ordner/{$this->folder->id}/eintraege", [
'type' => 'file', 'title' => 'Händedesinfektion', 'version' => '1.0',
'reading' => UploadedFile::fake()->create('haende.pdf', 20, 'application/pdf'),
...$extra,
])->assertSessionHasNoErrors();
return QmEntry::query()->latest('id')->firstOrFail();
}
public function test_review_dates_are_saved_reminded_and_completed(): void
{
Notification::fake();
$responsible = User::factory()->create();
$entry = $this->entry(['review_due_at' => today()->addDays(10)->toDateString(), 'review_interval_months' => 12, 'responsible_id' => $responsible->id]);
$this->assertSame(12, $entry->review_interval_months);
$this->assertSame('due', Reviews::state($entry));
// Erinnerung genau einmal
$this->assertSame(1, app(Reviews::class)->notifyDue());
$this->assertSame(0, app(Reviews::class)->notifyDue());
Notification::assertSentTo($responsible, PortalNotification::class, fn ($n) => str_contains($n->body, 'Händedesinfektion'));
// Verantwortliche sehen ihre Termine und können sie abschließen, andere nicht
$this->actingAs($responsible)->get('/qm/pruefungen')->assertInertia(fn (Assert $page) => $page
->component('qm::Reviews')->where('entries.0.can_complete', true)->where('entries.0.review.responsible.id', $responsible->id));
$this->actingAs(User::factory()->create())->get('/qm/pruefungen')->assertForbidden();
$this->actingAs(User::factory()->create())->post("/qm/eintraege/{$entry->id}/geprueft")->assertForbidden();
$this->actingAs($responsible)->post("/qm/eintraege/{$entry->id}/geprueft")->assertRedirect();
$entry->refresh();
$this->assertSame(today()->addMonthsNoOverflow(12)->toDateString(), $entry->review_due_at->toDateString());
$this->assertSame($responsible->id, $entry->reviewed_by);
$this->assertNull($entry->review_notified_at);
// Angaben bleiben beim Bearbeiten ohne diese Felder nicht hängen – neuer Termin setzt die Erinnerung zurück
$entry->forceFill(['review_notified_at' => now()])->save();
$this->actingAs($this->qmb)->post("/qm/eintraege/{$entry->id}", ['title' => 'Händedesinfektion', 'review_due_at' => today()->addDays(3)->toDateString(), 'responsible_id' => $responsible->id])->assertSessionHasNoErrors();
$this->assertNull($entry->fresh()->review_notified_at);
}
public function test_acknowledgements_per_version_with_report(): void
{
$entry = $this->entry(['requires_acknowledgement' => '1']);
$anna = User::factory()->create(['first_name' => 'Anna', 'last_name' => 'Arndt', 'name' => 'Anna Arndt']);
$ben = User::factory()->create(['first_name' => 'Ben', 'last_name' => 'Bauer', 'name' => 'Ben Bauer']);
$this->actingAs($anna)->get('/qm')->assertInertia(fn (Assert $page) => $page->where('toAcknowledge.0.id', $entry->id));
$this->actingAs($anna)->get('/')->assertInertia(fn (Assert $page) => $page->where('widgets', fn ($widgets) => collect($widgets)->contains('key', 'qm.acknowledgements')));
$this->actingAs($anna)->post("/qm/eintraege/{$entry->id}/gelesen")->assertRedirect();
$this->actingAs($anna)->get('/qm')->assertInertia(fn (Assert $page) => $page->where('toAcknowledge', []));
$this->actingAs($anna)->get("/qm/ordner/{$this->folder->id}")->assertInertia(fn (Assert $page) => $page->whereNot('entries.0.acknowledged_at', null));
$report = $this->actingAs($this->qmb)->getJson("/qm/eintraege/{$entry->id}/lesebestaetigungen")->assertOk()->json();
$this->assertSame([$anna->id], array_column($report['confirmed'], 'id'));
$this->assertContains($ben->id, array_column($report['missing'], 'id'));
$this->actingAs($anna)->getJson("/qm/eintraege/{$entry->id}/lesebestaetigungen")->assertForbidden();
$csv = $this->actingAs($this->qmb)->get("/qm/eintraege/{$entry->id}/lesebestaetigungen?format=csv")->assertOk();
$this->assertStringContainsString('"Anna Arndt";', $csv->getContent());
// Neue Fassung → erneut bestätigen
$this->actingAs($this->qmb)->post("/qm/eintraege/{$entry->id}", ['title' => 'Händedesinfektion', 'requires_acknowledgement' => '1', 'reading' => UploadedFile::fake()->create('haende-2.pdf', 20, 'application/pdf')])->assertSessionHasNoErrors();
$this->actingAs($anna)->get('/qm')->assertInertia(fn (Assert $page) => $page->where('toAcknowledge.0.id', $entry->id));
}
public function test_report_only_counts_people_who_may_see_the_folder(): void
{
$pflege = Role::query()->create(['name' => 'Pflege']);
Audiences::sync('qm_category', $this->folder->category_id, ['role' => [$pflege->id]]);
$entry = $this->entry(['requires_acknowledgement' => '1']);
$inside = User::factory()->withRoles([$pflege])->create();
$outside = User::factory()->create();
$report = $this->actingAs($this->qmb)->getJson("/qm/eintraege/{$entry->id}/lesebestaetigungen")->json();
$ids = array_column($report['missing'], 'id');
$this->assertContains($inside->id, $ids);
$this->assertNotContains($outside->id, $ids);
$this->actingAs($outside)->post("/qm/eintraege/{$entry->id}/gelesen")->assertNotFound();
}
}