Loheide-eu-mitarbeiterplatt.../tests/Feature/Permissions/MavScenarioTest.php
Friederich Loheide 9ddc35a62c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m30s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m28s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m36s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 1m45s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 1m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 1m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 54s
Rollen, Rechte und Zielgruppen
- Rollen mit globalen Rechten und Rechten für einzelne Objekte (z. B. „Beiträge
  schreiben in Kategorie MAV-Info“); Rollen sind additiv, Zuweisungen merken
  sich ihre Herkunft (manuell oder später Identity-Provider-Gruppe)
- Systemrolle „Administration“ ersetzt users.is_admin (Migration übernimmt
  bestehende Administratoren); nicht löschbar, man kann sie sich nicht selbst entziehen
- Rechte je Person vorberechnet und gecacht, Versionszähler invalidiert bei Änderungen
- Gate-Anbindung: registrierte Rechte entscheidet das Rechtesystem, Objektregeln
  die Policies; Admin-Seiten und Plugin-Ressourcen hinter passenden Rechten
- Redaktion nach Kategorie: Schreiben und Veröffentlichen getrennt, Liste im
  Admin auf eigene Kategorien beschränkt, ohne Veröffentlichungsrecht bleibt
  ein Beitrag Entwurf
- Zielgruppen (Rolle/Abteilung/Standort) für Kategorien und Termine als
  indizierte SQL-Bedingung in Listen, Detailseiten, Kacheln und .ics-Export
- Admin: Rollen-Verwaltung, „Rechte prüfen“, Redaktion und Sichtbarkeit direkt
  an Kategorie bzw. Termin
- Kern-Ressourcen explizit registriert, damit nach Updates liegengebliebene
  alte Dateien nicht doppelt geladen werden
- docs/PLUGINS.md für Plugin-Entwicklung; Demo-Rolle „MAV“

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

143 lines
5.8 KiB
PHP

<?php
namespace Tests\Feature\Permissions;
use App\Models\Role;
use App\Models\User;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inhaus\Plugins\News\Filament\Resources\NewsCategories\Pages\ManageNewsCategories;
use Inhaus\Plugins\News\Filament\Resources\NewsPosts\Pages\CreateNewsPost;
use Inhaus\Plugins\News\Filament\Resources\NewsPosts\Pages\EditNewsPost;
use Inhaus\Plugins\News\Filament\Resources\NewsPosts\Pages\ListNewsPosts;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Die Anforderung aus dem Plan: Die MAV schreibt nur in ihrer eigenen Kategorie.
*/
class MavScenarioTest extends TestCase
{
use RefreshDatabase;
private NewsCategory $mavCategory;
private NewsCategory $otherCategory;
private User $mav;
protected function setUp(): void
{
parent::setUp();
$this->mavCategory = NewsCategory::factory()->create(['name' => 'MAV-Info', 'slug' => 'mav-info']);
$this->otherCategory = NewsCategory::factory()->create(['name' => 'Unternehmen', 'slug' => 'unternehmen']);
$role = Role::query()->create(['name' => 'MAV']);
$role->grants()->create(['ability' => 'news.post.create', 'resource_type' => 'news_category', 'resource_id' => $this->mavCategory->id]);
$this->mav = User::factory()->withRoles([$role])->create();
}
public function test_mav_sees_only_its_own_posts_in_admin(): void
{
$own = NewsPost::factory()->create(['category_id' => $this->mavCategory->id, 'title' => 'MAV-Sprechstunde']);
NewsPost::factory()->create(['category_id' => $this->otherCategory->id, 'title' => 'Quartalszahlen']);
$this->actingAs($this->mav)->get('/admin/neuigkeiten')->assertOk()->assertSee('MAV-Sprechstunde')->assertDontSee('Quartalszahlen');
Livewire::actingAs($this->mav)->test(ListNewsPosts::class)->assertCanSeeTableRecords([$own])->assertCountTableRecords(1);
}
public function test_mav_cannot_manage_categories_or_other_sections(): void
{
$this->actingAs($this->mav)->get('/admin/kategorien')->assertForbidden();
$this->actingAs($this->mav)->get('/admin/benutzer')->assertForbidden();
$this->actingAs($this->mav)->get('/admin/termine')->assertForbidden();
}
public function test_mav_can_write_in_its_category_but_publishing_needs_the_publish_right(): void
{
$this->actingAs($this->mav);
Livewire::test(CreateNewsPost::class)
->fillForm([
'title' => 'Einladung zur Mitarbeiterversammlung',
'category_id' => $this->mavCategory->id,
'published_at' => now()->subMinute()->format('Y-m-d H:i'),
'is_featured' => true,
])
->call('create')
->assertHasNoFormErrors();
$post = NewsPost::query()->sole();
$this->assertNull($post->published_at, 'Ohne Veröffentlichungsrecht bleibt der Beitrag ein Entwurf.');
$this->assertFalse($post->is_featured);
$this->assertSame($this->mav->id, $post->author_id);
}
public function test_mav_with_publish_right_can_publish(): void
{
Role::query()->where('name', 'MAV')->sole()->grants()->create(['ability' => 'news.post.publish', 'resource_type' => 'news_category', 'resource_id' => $this->mavCategory->id]);
$this->actingAs($this->mav->fresh());
Livewire::test(CreateNewsPost::class)
->fillForm([
'title' => 'Ergebnisse der MAV-Wahl',
'category_id' => $this->mavCategory->id,
'published_at' => now()->subMinute()->format('Y-m-d H:i'),
])
->call('create')
->assertHasNoFormErrors();
$this->assertTrue(NewsPost::query()->sole()->isPublished());
}
public function test_mav_cannot_write_in_foreign_categories(): void
{
$this->actingAs($this->mav);
Livewire::test(CreateNewsPost::class)
->fillForm(['title' => 'Fremd', 'category_id' => $this->otherCategory->id])
->call('create')
->assertHasFormErrors(['category_id']);
$foreign = NewsPost::factory()->create(['category_id' => $this->otherCategory->id]);
$this->get("/admin/neuigkeiten/{$foreign->id}/bearbeiten")->assertNotFound();
}
public function test_editing_without_publish_right_does_not_change_publication(): void
{
$post = NewsPost::factory()->create(['category_id' => $this->mavCategory->id, 'published_at' => null]);
$this->actingAs($this->mav);
Livewire::test(EditNewsPost::class, ['record' => $post->getRouteKey()])
->fillForm(['title' => 'Überarbeitet', 'published_at' => now()->format('Y-m-d H:i')])
->call('save')
->assertHasNoFormErrors();
$post->refresh();
$this->assertSame('Überarbeitet', $post->title);
$this->assertNull($post->published_at);
}
public function test_admin_grants_editorial_rights_directly_on_the_category(): void
{
$redaktion = Role::query()->create(['name' => 'Redaktion Hamburg']);
$person = User::factory()->withRoles([$redaktion])->create();
$this->actingAs(User::factory()->admin()->create());
Livewire::test(ManageNewsCategories::class)
->callAction(TestAction::make('edit')->table($this->otherCategory), data: [
'writers' => [$redaktion->id],
'publishers' => [$redaktion->id],
])
->assertHasNoFormErrors();
$this->assertTrue($person->fresh()->can('news.post.create', $this->otherCategory));
$this->assertTrue($person->fresh()->can('news.post.publish', $this->otherCategory));
$this->assertFalse($person->fresh()->can('news.post.create', $this->mavCategory));
}
}