Add post comments with moderation plus view/like interaction endpoints
Deploy Backend / deploy (push) Successful in 2m41s
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
This commit is contained in:
1 parent
fbacb0c9a1
commit
e98807da05
11 files changed
+497
No files matched your search
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Blog;
|
||||
|
||||
use App\Models\Blog\Comment;
|
||||
use App\Models\Blog\Post;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PostInteractionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_view_increments_the_counter()
|
||||
{
|
||||
$post = Post::factory()->create(['views' => 5, 'likes' => 2]);
|
||||
|
||||
$response = $this->postJson("/api/blog/posts/{$post->slug}/view");
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJsonPath('views', 6)
|
||||
->assertJsonPath('likes', 2);
|
||||
|
||||
$this->assertDatabaseHas('posts', ['id' => $post->id, 'views' => 6]);
|
||||
}
|
||||
|
||||
public function test_like_toggles_the_counter()
|
||||
{
|
||||
$post = Post::factory()->create(['views' => 0, 'likes' => 3]);
|
||||
|
||||
$this->postJson("/api/blog/posts/{$post->slug}/like", ['liked' => true])
|
||||
->assertStatus(200)
|
||||
->assertJsonPath('likes', 4);
|
||||
|
||||
$this->postJson("/api/blog/posts/{$post->slug}/like", ['liked' => false])
|
||||
->assertStatus(200)
|
||||
->assertJsonPath('likes', 3);
|
||||
}
|
||||
|
||||
public function test_unlike_never_drives_likes_below_zero()
|
||||
{
|
||||
$post = Post::factory()->create(['likes' => 0]);
|
||||
|
||||
$this->postJson("/api/blog/posts/{$post->slug}/like", ['liked' => false])
|
||||
->assertStatus(200)
|
||||
->assertJsonPath('likes', 0);
|
||||
}
|
||||
|
||||
public function test_like_requires_the_liked_flag()
|
||||
{
|
||||
$post = Post::factory()->create();
|
||||
|
||||
$this->postJson("/api/blog/posts/{$post->slug}/like", [])
|
||||
->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_interactions_404_on_unknown_posts()
|
||||
{
|
||||
$this->postJson('/api/blog/posts/does-not-exist/view')->assertStatus(404);
|
||||
$this->postJson('/api/blog/posts/does-not-exist/like', ['liked' => true])->assertStatus(404);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user