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

146 lines
6.4 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\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_works_in_the_portal_only_and_sees_its_own_category(): void
{
NewsPost::factory()->create(['category_id' => $this->mavCategory->id, 'title' => 'MAV-Sprechstunde', 'published_at' => now()->subDay()]);
NewsPost::factory()->create(['category_id' => $this->otherCategory->id, 'title' => 'Quartalszahlen', 'published_at' => now()->subDay()]);
// Redaktionsrechte öffnen nicht das Backend
$this->assertFalse($this->mav->canAccessAdmin());
$this->actingAs($this->mav)->get('/admin/neuigkeiten')->assertForbidden();
$this->get('/')->assertInertia(fn ($page) => $page->where('auth.user.can_admin', false));
$this->get('/redaktion')->assertOk()->assertInertia(fn ($page) => $page
->where('sections', fn ($sections) => collect($sections)->firstWhere('key', 'news')['props']['published'][0]['title'] === 'MAV-Sprechstunde'
&& count(collect($sections)->firstWhere('key', 'news')['props']['published']) === 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);
$data = [
'title' => 'Einladung zur Mitarbeiterversammlung',
'category_id' => $this->mavCategory->id,
'published_at' => now()->subMinute()->toIso8601String(),
'is_featured' => true,
'content' => json_encode(['blocks' => [['type' => 'text', 'data' => ['html' => '<p>Am Donnerstag …</p>']]]]),
];
$this->post('/redaktion/neuigkeiten', [...$data, 'action' => 'publish'])->assertSessionHasErrors('action');
$this->post('/redaktion/neuigkeiten', [...$data, 'action' => 'submit'])->assertSessionHasNoErrors();
$post = NewsPost::query()->sole();
$this->assertFalse($post->isPublished(), 'Ohne Veröffentlichungsrecht geht der Beitrag zur Freigabe.');
$this->assertSame(NewsPost::PENDING, $post->status);
$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());
$this->post('/redaktion/neuigkeiten', [
'title' => 'Ergebnisse der MAV-Wahl',
'category_id' => $this->mavCategory->id,
'content' => json_encode(['blocks' => []]),
'action' => 'publish',
])->assertSessionHasNoErrors();
$this->assertTrue(NewsPost::query()->sole()->isPublished());
}
public function test_mav_cannot_write_in_foreign_categories(): void
{
$this->actingAs($this->mav);
$this->post('/redaktion/neuigkeiten', ['title' => 'Fremd', 'category_id' => $this->otherCategory->id, 'action' => 'draft'])
->assertSessionHasErrors('category_id');
$this->assertSame(0, NewsPost::query()->count());
$foreign = NewsPost::factory()->create(['category_id' => $this->otherCategory->id]);
$this->get("/admin/neuigkeiten/{$foreign->id}/bearbeiten")->assertForbidden();
$this->get("/redaktion/neuigkeiten/{$foreign->id}")->assertForbidden();
}
public function test_editing_without_publish_right_does_not_change_publication(): void
{
$post = NewsPost::factory()->draft()->create(['category_id' => $this->mavCategory->id, 'author_id' => $this->mav->id]);
$this->actingAs($this->mav);
$this->put("/redaktion/neuigkeiten/{$post->id}", [
'title' => 'Überarbeitet',
'category_id' => $this->mavCategory->id,
'published_at' => now()->toIso8601String(),
'action' => 'draft',
])->assertSessionHasNoErrors();
$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));
}
}