Loheide-eu-mitarbeiterplatt.../tests/Feature/Permissions/AudienceTest.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

97 lines
4.7 KiB
PHP

<?php
namespace Tests\Feature\Permissions;
use App\Models\Role;
use App\Models\User;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Tests\TestCase;
/**
* Sichtbarkeit: „nur für Standort Hamburg“, „nur für die Pflege“, „nur für die MAV“.
*/
class AudienceTest extends TestCase
{
use RefreshDatabase;
public function test_category_audiences_filter_posts_everywhere(): void
{
$hamburg = NewsCategory::factory()->create(['name' => 'Standort Hamburg']);
$open = NewsCategory::factory()->create(['name' => 'Alle']);
Audiences::sync('news_category', $hamburg->id, ['location' => ['Hamburg']]);
$local = NewsPost::factory()->create(['category_id' => $hamburg->id, 'title' => 'Hamburger Sommerfest']);
NewsPost::factory()->create(['category_id' => $open->id, 'title' => 'Für alle']);
NewsPost::factory()->create(['category_id' => null, 'title' => 'Ohne Kategorie']);
$hamburger = User::factory()->create(['location' => 'Hamburg']);
$berliner = User::factory()->create(['location' => 'Berlin']);
$this->actingAs($hamburger)->get('/neuigkeiten')->assertInertia(fn (Assert $page) => $page->where('posts.meta.total', 3));
$this->actingAs($berliner)->get('/neuigkeiten')->assertInertia(fn (Assert $page) => $page
->where('posts.meta.total', 2)
->where('categories', fn ($categories) => collect($categories)->pluck('name')->all() === ['Alle']));
$this->actingAs($berliner)->get("/neuigkeiten/{$local->slug}")->assertNotFound();
$this->actingAs($hamburger)->get("/neuigkeiten/{$local->slug}")->assertOk();
$latest = $this->dashboardWidget($this->actingAs($berliner)->get('/'), 'news.latest')['posts'];
$this->assertNotContains('Hamburger Sommerfest', array_column($latest, 'title'));
}
public function test_rules_are_combined_with_or_and_roles_count(): void
{
$category = NewsCategory::factory()->create();
$mav = Role::query()->create(['name' => 'MAV']);
Audiences::sync('news_category', $category->id, ['role' => [$mav->id], 'department' => ['Pflege']]);
NewsPost::factory()->create(['category_id' => $category->id]);
$pflege = User::factory()->create(['department' => 'Pflege']);
$mavMember = User::factory()->withRoles([$mav])->create(['department' => 'IT']);
$other = User::factory()->create(['department' => 'IT']);
$this->assertSame(1, NewsPost::query()->visibleTo($pflege)->count());
$this->assertSame(1, NewsPost::query()->visibleTo($mavMember)->count());
$this->assertSame(0, NewsPost::query()->visibleTo($other)->count());
}
public function test_news_managers_see_everything(): void
{
$category = NewsCategory::factory()->create();
Audiences::sync('news_category', $category->id, ['location' => ['Nirgendwo']]);
NewsPost::factory()->create(['category_id' => $category->id]);
$this->assertSame(1, NewsPost::query()->visibleTo(User::factory()->admin()->create())->count());
}
public function test_event_audiences(): void
{
$event = Event::factory()->create(['title' => 'Pflegefortbildung']);
Event::factory()->create(['title' => 'Betriebsfest']);
Audiences::sync('event', $event->id, ['department' => ['Pflege']]);
$pflege = User::factory()->create(['department' => 'Pflege']);
$it = User::factory()->create(['department' => 'IT']);
$this->actingAs($it)->get('/termine')->assertInertia(fn (Assert $page) => $page->has('events.data', 1));
$this->actingAs($it)->get("/termine/{$event->id}")->assertNotFound();
$this->actingAs($it)->get("/termine/{$event->id}/kalender.ics")->assertNotFound();
$this->actingAs($pflege)->get('/termine')->assertInertia(fn (Assert $page) => $page->has('events.data', 2));
$this->assertCount(1, $this->dashboardWidget($this->actingAs($it)->get('/'), 'events.upcoming')['events']);
}
public function test_audience_sync_replaces_rules(): void
{
Audiences::sync('event', 5, ['department' => ['Pflege', 'Pflege', ''], 'location' => ['Hamburg']]);
$this->assertSame(['role' => [], 'department' => ['Pflege'], 'location' => ['Hamburg'], 'exclude_role' => []], Audiences::get('event', 5));
Audiences::sync('event', 5, []);
$this->assertSame(['role' => [], 'department' => [], 'location' => [], 'exclude_role' => []], Audiences::get('event', 5));
$this->assertTrue(Audiences::allows('event', 5, null));
}
}