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?bereich=news')->assertOk()->assertInertia(fn ($page) => $page ->where('section.props.published', fn ($published) => count($published) === 1 && $published[0]['title'] === 'MAV-Sprechstunde')); } 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' => '
Am Donnerstag …
']]]]), ]; $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)); } }