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_sees_only_its_own_posts_in_admin(): void { $own = NewsPost::factory()->create(['category_id' => $this->mavCategory->id, 'title' => 'MAV-Sprechstunde']); NewsPost::factory()->create(['category_id' => $this->otherCategory->id, 'title' => 'Quartalszahlen']); $this->actingAs($this->mav)->get('/admin/neuigkeiten')->assertOk()->assertSee('MAV-Sprechstunde')->assertDontSee('Quartalszahlen'); Livewire::actingAs($this->mav)->test(ListNewsPosts::class)->assertCanSeeTableRecords([$own])->assertCountTableRecords(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); Livewire::test(CreateNewsPost::class) ->fillForm([ 'title' => 'Einladung zur Mitarbeiterversammlung', 'category_id' => $this->mavCategory->id, 'published_at' => now()->subMinute()->format('Y-m-d H:i'), 'is_featured' => true, ]) ->call('create') ->assertHasNoFormErrors(); $post = NewsPost::query()->sole(); $this->assertNull($post->published_at, 'Ohne Veröffentlichungsrecht bleibt der Beitrag ein Entwurf.'); $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()); Livewire::test(CreateNewsPost::class) ->fillForm([ 'title' => 'Ergebnisse der MAV-Wahl', 'category_id' => $this->mavCategory->id, 'published_at' => now()->subMinute()->format('Y-m-d H:i'), ]) ->call('create') ->assertHasNoFormErrors(); $this->assertTrue(NewsPost::query()->sole()->isPublished()); } public function test_mav_cannot_write_in_foreign_categories(): void { $this->actingAs($this->mav); Livewire::test(CreateNewsPost::class) ->fillForm(['title' => 'Fremd', 'category_id' => $this->otherCategory->id]) ->call('create') ->assertHasFormErrors(['category_id']); $foreign = NewsPost::factory()->create(['category_id' => $this->otherCategory->id]); $this->get("/admin/neuigkeiten/{$foreign->id}/bearbeiten")->assertNotFound(); } public function test_editing_without_publish_right_does_not_change_publication(): void { $post = NewsPost::factory()->create(['category_id' => $this->mavCategory->id, 'published_at' => null]); $this->actingAs($this->mav); Livewire::test(EditNewsPost::class, ['record' => $post->getRouteKey()]) ->fillForm(['title' => 'Überarbeitet', 'published_at' => now()->format('Y-m-d H:i')]) ->call('save') ->assertHasNoFormErrors(); $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)); } }