author = User::factory()->create(); $this->post = NewsPost::factory()->create(['author_id' => $this->author->id, 'title' => 'Sommerfest']); } public function test_comment_and_reply_form_a_thread_and_notify(): void { $anna = User::factory()->create(['first_name' => 'Anna', 'last_name' => 'Arm', 'department' => 'Pflege']); $ben = User::factory()->create(['first_name' => 'Ben', 'last_name' => 'Bau']); $this->actingAs($anna)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Tolles Fest!'])->assertSessionHasNoErrors(); $first = NewsComment::query()->sole(); $this->actingAs($ben)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Finde ich auch', 'parent_id' => $first->id]); $reply = NewsComment::query()->latest('id')->first(); // Antwort auf die Antwort bleibt im selben Strang $this->actingAs($anna)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Danke, Ben', 'parent_id' => $reply->id]); $this->assertSame([null, $first->id, $first->id], NewsComment::query()->orderBy('id')->pluck('parent_id')->all()); $this->assertSame($ben->id, NewsComment::query()->latest('id')->first()->reply_to_user_id); $this->assertSame(3, $this->author->notifications()->count(), 'Autor erfährt von jedem Kommentar'); $this->assertStringContainsString('hat Ihnen geantwortet', $anna->notifications()->first()->data['title']); $this->actingAs($ben)->get("/neuigkeiten/{$this->post->slug}")->assertInertia(fn (Assert $page) => $page ->where('comments.count', 3) ->has('comments.threads', 1) ->where('comments.threads.0.author.department', 'Pflege') ->has('comments.threads.0.replies', 2) ->where('comments.threads.0.replies.1.reply_to', 'Ben Bau') ->where('comments.threads.0.can.edit', false) ->where('comments.threads.0.replies.0.can.edit', true)); } public function test_only_own_comments_can_be_edited_and_moderators_delete_any(): void { $anna = User::factory()->create(); $comment = $this->post->comments()->create(['user_id' => $anna->id, 'body' => 'Erster']); $reply = $this->post->comments()->create(['user_id' => $this->author->id, 'parent_id' => $comment->id, 'body' => 'Antwort']); $this->actingAs($this->author)->put("/kommentare/{$comment->id}", ['body' => 'Fremd'])->assertForbidden(); $this->actingAs($anna)->put("/kommentare/{$comment->id}", ['body' => 'Geändert'])->assertSessionHasNoErrors(); $this->assertNotNull($comment->fresh()->edited_at); $this->actingAs($this->author)->delete("/kommentare/{$comment->id}")->assertForbidden(); $moderators = Role::query()->create(['name' => 'Redaktion']); $moderators->grants()->create(['ability' => 'news.comments.moderate']); $this->actingAs(User::factory()->withRoles([$moderators])->create())->delete("/kommentare/{$comment->id}")->assertRedirect(); // Gelöschter Kommentar mit Antwort bleibt als Platzhalter $this->actingAs($anna)->get("/neuigkeiten/{$this->post->slug}")->assertInertia(fn (Assert $page) => $page ->where('comments.count', 1) ->where('comments.threads.0.deleted', true) ->where('comments.threads.0.body', null) ->where('comments.threads.0.replies.0.body', 'Antwort')); } public function test_closed_comments_and_invisible_posts_reject_comments(): void { $user = User::factory()->create(['department' => 'Werkstatt']); $this->post->update(['comments_enabled' => false]); $this->actingAs($user)->post("/neuigkeiten/{$this->post->slug}/kommentare", ['body' => 'Hallo'])->assertForbidden(); $care = NewsCategory::query()->create(['name' => 'Pflege intern', 'slug' => 'pflege-intern']); Audiences::sync('news_category', $care->id, ['department' => ['Pflege']]); $hidden = NewsPost::factory()->create(['category_id' => $care->id]); $this->actingAs($user)->post("/neuigkeiten/{$hidden->slug}/kommentare", ['body' => 'Hallo'])->assertNotFound(); $this->assertSame(0, NewsComment::query()->count()); } public function test_profile_lists_comments_on_visible_posts_only(): void { $anna = User::factory()->create(); $viewer = User::factory()->create(['department' => 'Werkstatt']); $this->post->comments()->create(['user_id' => $anna->id, 'body' => 'Sichtbar für alle']); $care = NewsCategory::query()->create(['name' => 'Pflege intern', 'slug' => 'pflege-intern']); Audiences::sync('news_category', $care->id, ['department' => ['Pflege']]); NewsPost::factory()->create(['category_id' => $care->id])->comments()->create(['user_id' => $anna->id, 'body' => 'Nur Pflege']); $this->actingAs($viewer)->get("/personen/{$anna->id}")->assertInertia(fn (Assert $page) => $page ->where('panels.0.key', 'news.comments') ->where('panels.0.props.total', 1) ->where('panels.0.props.comments.0.excerpt', 'Sichtbar für alle') ->where('panels.0.props.comments.0.post.title', 'Sommerfest')); // Ohne Kommentare kein Bereich $this->actingAs($viewer)->get("/personen/{$viewer->id}")->assertInertia(fn (Assert $page) => $page->where('panels', [])); } }