Deploy Backend / deploy (push) Successful in 2m41s
- blog_comments table + Comment model/factory; comments attach to the post,
public list shows approved rows only, public submission starts unapproved
- Admin moderation API: list/filter, approve, delete comments
- Public POST posts/{slug}/view and posts/{slug}/like toggle counters
- Route groups: comments throttled like the contact form, interactions at
60/min; comments resource exposed to the generic admin list
127 lines
4.1 KiB
PHP
127 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Blog;
|
|
|
|
use App\Models\Blog\Comment;
|
|
use App\Models\Blog\Post;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class CommentTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_only_approved_comments_are_public()
|
|
{
|
|
$post = Post::factory()->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();
|
|
}
|
|
} |