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
62 lines
1.8 KiB
PHP
62 lines
1.8 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 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);
|
|
}
|
|
} |