Loheide-eu-mitarbeiterplatt.../tests/Feature/Permissions/PermissionsTest.php
Friederich Loheide 34d6cdbf1c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m6s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m20s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m17s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m18s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m10s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m41s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m44s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m59s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m51s
Reine Redaktionsrechte öffnen nicht mehr das Backend (0.25.1)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 09:16:48 +00:00

200 lines
8 KiB
PHP
Raw 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\Permissions;
use App\Filament\Resources\Roles\Pages\CreateRole;
use App\Filament\Resources\Roles\Pages\EditRole;
use App\Models\Grant;
use App\Models\Role;
use App\Models\User;
use App\Support\Permissions\Permissions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Inhaus\Plugins\News\Models\NewsCategory;
use Livewire\Livewire;
use Tests\TestCase;
class PermissionsTest extends TestCase
{
use RefreshDatabase;
private function role(string $name, array $global = [], array $objects = []): Role
{
$role = Role::query()->create(['name' => $name]);
foreach ($global as $ability) {
$role->grants()->create(['ability' => $ability]);
}
foreach ($objects as [$ability, $type, $id]) {
$role->grants()->create(['ability' => $ability, 'resource_type' => $type, 'resource_id' => $id]);
}
return $role;
}
public function test_administration_role_can_do_everything(): void
{
$admin = User::factory()->admin()->create();
$this->assertTrue($admin->isAdministrator());
$this->assertTrue($admin->can('users.manage'));
$this->assertTrue($admin->can('news.post.create', ['news_category', 999]));
$this->assertTrue($admin->canAccessAdmin());
}
public function test_roles_are_additive(): void
{
$user = User::factory()->withRoles([
$this->role('Redaktion', ['appearance.manage']),
$this->role('Personal', ['users.manage']),
])->create();
$this->assertTrue($user->can('appearance.manage'));
$this->assertTrue($user->can('users.manage'));
$this->assertFalse($user->can('plugins.manage'));
}
public function test_users_without_roles_have_no_special_rights(): void
{
$user = User::factory()->create();
$this->assertFalse($user->canAccessAdmin());
$this->assertFalse($user->can('users.manage'));
$this->get('/admin')->assertRedirect('/login');
$this->actingAs($user)->get('/admin')->assertForbidden();
}
public function test_deactivated_users_lose_all_rights(): void
{
$user = User::factory()->admin()->inactive()->create();
$this->assertFalse($user->can('users.manage'));
$this->assertFalse($user->canAccessAdmin());
}
public function test_object_grants_only_apply_to_their_objects(): void
{
$user = User::factory()->withRoles([$this->role('MAV', objects: [['news.post.create', 'news_category', 7]])])->create();
$this->assertTrue($user->can('news.post.create', ['news_category', 7]));
$this->assertFalse($user->can('news.post.create', ['news_category', 8]));
$this->assertSame([7], $user->permissions()->ids('news.post.create', 'news_category'));
// Reine Redaktionsrechte werden im Portal ausgeübt – kein Backend
$this->assertFalse($user->canAccessAdmin());
$qm = User::factory()->withRoles([$this->role('QM', global: ['qm.manage'])])->create();
$this->assertTrue($qm->canAccessAdmin());
}
public function test_grant_for_all_objects_of_a_type(): void
{
$user = User::factory()->withRoles([$this->role('Chefredaktion', objects: [['news.post.publish', 'news_category', null]])])->create();
$this->assertTrue($user->can('news.post.publish', ['news_category', 123]));
$this->assertTrue($user->permissions()->ids('news.post.publish', 'news_category'));
}
public function test_permissions_are_cached_and_invalidated_on_changes(): void
{
$role = $this->role('Redaktion', ['appearance.manage']);
$user = User::factory()->withRoles([$role])->create();
app(Permissions::class)->for($user);
$this->app->forgetInstance(Permissions::class);
DB::enableQueryLog();
$this->assertTrue(app(Permissions::class)->for($user)->can('appearance.manage'));
$this->assertCount(0, DB::getQueryLog(), 'Zweiter Aufruf muss aus dem Cache kommen.');
$role->grants()->create(['ability' => 'users.manage']);
$this->app->forgetInstance(Permissions::class);
$this->assertTrue(app(Permissions::class)->for($user->fresh())->can('users.manage'));
}
public function test_manual_role_sync_keeps_identity_provider_roles(): void
{
$manual = $this->role('Manuell');
$fromGroup = $this->role('Aus Entra-Gruppe');
$user = User::factory()->create();
$user->roles()->attach($fromGroup->id, ['source' => 'idp', 'source_ref' => 'entra:gruppe-1', 'created_at' => now()]);
$user->syncManualRoles([$manual->id]);
$user->syncManualRoles([]);
$this->assertTrue($user->hasRole($fromGroup->slug));
$this->assertFalse($user->hasRole($manual->slug));
}
public function test_system_role_cannot_be_deleted_and_admins_cannot_remove_themselves(): void
{
$admin = User::factory()->admin()->create();
$this->actingAs($admin);
$this->assertFalse($admin->can('delete', Role::administration()));
Livewire::test(EditRole::class, ['record' => Role::administration()->getRouteKey()])
->fillForm(['members' => []])
->call('save')
->assertHasFormErrors(['members']);
$this->assertTrue($admin->fresh()->isAdministrator());
}
public function test_roles_can_be_created_with_global_and_object_rights_and_members(): void
{
$this->actingAs(User::factory()->admin()->create());
$category = NewsCategory::factory()->create(['name' => 'MAV-Info']);
$member = User::factory()->create();
Livewire::test(CreateRole::class)
->fillForm([
'name' => 'MAV',
'description' => 'Mitarbeitervertretung',
'global_abilities' => ['feedback.send'],
'object_grants' => [
['ability' => 'news.post.create', 'resource_ids' => [(string) $category->id]],
['ability' => 'news.post.publish', 'resource_ids' => ['*']],
],
'members' => [$member->id],
])
->call('create')
->assertHasNoFormErrors();
$role = Role::query()->where('slug', 'mav')->sole();
$this->assertSame(3, $role->grants()->count());
$this->assertTrue($member->fresh()->can('news.post.create', $category));
$this->assertTrue($member->fresh()->can('news.post.publish', ['news_category', 42]));
$this->assertTrue($member->fresh()->can('feedback.send'));
$this->assertNull(Grant::query()->where('ability', 'news.post.publish')->value('resource_id'));
}
public function test_core_admin_pages_follow_their_rights(): void
{
$user = User::factory()->withRoles([$this->role('Gestaltung', ['appearance.manage'])])->create();
$this->actingAs($user)->get('/admin/erscheinungsbild/identitaet')->assertOk();
$this->actingAs($user)->get('/admin/startseite-menue/schnellzugriffe')->assertOk();
foreach (['/admin/benutzer', '/admin/rollen', '/admin/plugins', '/admin/aktualisierung', '/admin/systemzustand', '/admin/rueckmeldung', '/admin/rechte-pruefen', '/admin/neuigkeiten', '/admin/termine'] as $url) {
$this->actingAs($user)->get($url)->assertForbidden();
}
$this->actingAs($user)->postJson('/admin/update/check')->assertForbidden();
}
public function test_permission_checker_explains_where_rights_come_from(): void
{
$mav = $this->role('MAV', objects: [['news.post.create', 'news_category', NewsCategory::factory()->create(['name' => 'MAV-Info'])->id]]);
$person = User::factory()->withRoles([$mav])->create(['first_name' => 'Grete', 'last_name' => 'Müller']);
$this->actingAs(User::factory()->admin()->create())
->get('/admin/rechte-pruefen?person='.$person->id)
->assertOk()
->assertSee('Grete Müller')
->assertSee('MAV · manuell')
->assertSee('Beiträge schreiben')
->assertSee('Kategorie: MAV-Info');
}
}