create(); Comment::factory()->count(2)->for($post)->create(['is_approved' => true]); Comment::factory()->for($post)->create(['is_approved' => false]); $response = $this->getJson("/api/blog/posts/{$post->slug}/comments"); $response->assertStatus(200) ->assertJsonCount(2, 'comments') ->assertJsonPath('post_id', $post->id); } public function test_public_comments_are_created_pending() { $post = Post::factory()->create(); $response = $this->postJson("/api/blog/posts/{$post->slug}/comments", [ 'name' => 'Reader', 'email' => 'reader@example.com', 'body' => 'A thoughtful comment.', ]); $response->assertStatus(201) ->assertJsonPath('comment.name', 'Reader'); $this->assertDatabaseHas('blog_comments', [ 'post_id' => $post->id, 'name' => 'Reader', 'email' => 'reader@example.com', 'body' => 'A thoughtful comment.', 'is_approved' => false, ]); } public function test_comment_submission_validates_required_fields() { $post = Post::factory()->create(); $this->postJson("/api/blog/posts/{$post->slug}/comments", [ 'name' => '', 'body' => '', ])->assertStatus(422); $this->postJson("/api/blog/posts/{$post->slug}/comments", [ 'name' => 'Reader', 'email' => 'not-an-email', 'body' => 'ok', ])->assertStatus(422); } public function test_comment_endpoints_404_on_unknown_posts() { $this->getJson('/api/blog/posts/does-not-exist/comments')->assertStatus(404); $this->postJson('/api/blog/posts/does-not-exist/comments', [ 'name' => 'A', 'body' => 'B', ])->assertStatus(404); } public function test_admin_can_list_and_filter_comments() { $this->actingAsAdmin(); $post = Post::factory()->create(); Comment::factory()->for($post)->create(['is_approved' => true]); Comment::factory()->for($post)->create(['is_approved' => false]); $this->getJson('/api/blog/comments') ->assertStatus(200) ->assertJsonCount(2, 'data'); $this->getJson('/api/blog/comments?approved=1') ->assertStatus(200) ->assertJsonCount(1, 'data'); } public function test_admin_can_approve_then_the_comment_becomes_public() { $this->actingAsAdmin(); $post = Post::factory()->create(); $comment = Comment::factory()->for($post)->create(['is_approved' => false]); $this->getJson("/api/blog/posts/{$post->slug}/comments") ->assertJsonCount(0, 'comments'); $this->putJson("/api/blog/comments/{$comment->id}", ['is_approved' => true]) ->assertStatus(200) ->assertJsonPath('data.is_approved', true); $this->getJson("/api/blog/posts/{$post->slug}/comments") ->assertJsonCount(1, 'comments'); } public function test_admin_can_delete_a_comment() { $this->actingAsAdmin(); $post = Post::factory()->create(); $comment = Comment::factory()->for($post)->create(); $this->deleteJson("/api/blog/comments/{$comment->id}")->assertStatus(200); $this->assertDatabaseMissing('blog_comments', ['id' => $comment->id]); } public function test_moderation_requires_an_admin() { $comment = Comment::factory()->create(); $this->getJson('/api/blog/comments')->assertUnauthorized(); $this->putJson("/api/blog/comments/{$comment->id}", ['is_approved' => true]) ->assertUnauthorized(); $this->deleteJson("/api/blog/comments/{$comment->id}")->assertUnauthorized(); } }