Loheide-eu-mitarbeiterplatt.../tests/Feature/Permissions/ExcludedRolesTest.php
Friederich Loheide 1877a0922f
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m45s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m49s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m59s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m21s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m30s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Rollen ausschließen: bei Inhalten (Zielgruppen) und für ganze Module
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:30:11 +00:00

71 lines
3.1 KiB
PHP

<?php
namespace Tests\Feature\Permissions;
use App\Filament\Pages\Plugins;
use App\Models\Role;
use App\Models\User;
use App\Plugins\PluginAccess;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\News\Models\NewsPost;
use Livewire\Livewire;
use Tests\TestCase;
class ExcludedRolesTest extends TestCase
{
use RefreshDatabase;
public function test_excluded_role_never_sees_the_object_even_if_another_rule_matches(): void
{
$clients = Role::query()->create(['name' => 'Klienten']);
$client = User::factory()->withRoles([$clients])->create(['department' => 'Werkstatt']);
$colleague = User::factory()->create(['department' => 'Werkstatt']);
$onlyExcluded = Event::factory()->create(['is_published' => true]);
Audiences::sync('event', $onlyExcluded->id, ['exclude_role' => [$clients->id]]);
$both = Event::factory()->create(['is_published' => true]);
Audiences::sync('event', $both->id, ['department' => ['Werkstatt'], 'exclude_role' => [$clients->id]]);
foreach ([$onlyExcluded, $both] as $event) {
$this->assertFalse(Audiences::allows('event', $event->id, $client));
$this->assertTrue(Audiences::allows('event', $event->id, $colleague));
$this->assertFalse(Audiences::matches(Audiences::get('event', $event->id), $client));
}
$this->assertSame([], Event::query()->visibleTo($client)->pluck('id')->all());
$this->assertEqualsCanonicalizing([$onlyExcluded->id, $both->id], Event::query()->visibleTo($colleague)->pluck('id')->all());
}
public function test_excluded_role_loses_the_whole_module(): void
{
$clients = Role::query()->create(['name' => 'Klienten']);
$client = User::factory()->withRoles([$clients])->create();
$admin = User::factory()->admin()->create();
NewsPost::factory()->create(['title' => 'Sommerfest intern']);
app(PluginAccess::class)->exclude('news', [$clients->id]);
$this->actingAs($client)->get('/neuigkeiten')->assertForbidden();
$page = $this->actingAs($client)->get('/')->assertOk()->viewData('page');
$this->assertNotContains('/neuigkeiten', array_column($page['props']['navigation'], 'href'));
$this->assertSame([], array_filter(array_column($page['props']['widgets'], 'key'), fn ($key) => str_starts_with($key, 'news.')));
$this->assertSame([], $this->actingAs($client)->getJson('/suche/live?q=sommerfest')->json('groups'));
// Verwaltung ist nie ausgeschlossen
$this->actingAs($admin)->get('/neuigkeiten')->assertOk();
}
public function test_admin_sets_module_access_on_the_plugins_page(): void
{
$clients = Role::query()->create(['name' => 'Klienten']);
Livewire::actingAs(User::factory()->admin()->create())
->test(Plugins::class)
->callAction('access', ['roles' => [(string) $clients->id]], ['slug' => 'events'])
->assertHasNoActionErrors();
$this->assertSame([$clients->id], app(PluginAccess::class)->excludedRoles('events'));
}
}