|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use App\Models\Comment; |
| 6 | +use App\Models\Post; |
| 7 | +use App\Models\User; |
| 8 | +use App\Policies\CommentPolicy; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class CommentPolicyTest extends TestCase |
| 13 | +{ |
| 14 | + use RefreshDatabase; |
| 15 | + |
| 16 | + protected CommentPolicy $commentPolicy; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + $this->commentPolicy = new CommentPolicy(); |
| 22 | + } |
| 23 | + |
| 24 | + public function test_owner_can_update_comment() |
| 25 | + { |
| 26 | + $user = User::factory()->create(); |
| 27 | + $comment = Comment::factory()->create(['user_id' => $user->id]); |
| 28 | + |
| 29 | + $response = $this->commentPolicy->update($user, $comment); |
| 30 | + |
| 31 | + $this->assertTrue($response->allowed()); |
| 32 | + } |
| 33 | + |
| 34 | + public function test_non_owner_cannot_update_comment() |
| 35 | + { |
| 36 | + $owner = User::factory()->create(); |
| 37 | + $nonOwner = User::factory()->create(); |
| 38 | + $comment = Comment::factory()->create(['user_id' => $owner->id]); |
| 39 | + |
| 40 | + $response = $this->commentPolicy->update($nonOwner, $comment); |
| 41 | + |
| 42 | + $this->assertFalse($response->allowed()); |
| 43 | + $this->assertEquals('You do not own this comment.', $response->message()); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_owner_can_delete_comment() |
| 47 | + { |
| 48 | + $user = User::factory()->create(); |
| 49 | + $comment = Comment::factory()->create(['user_id' => $user->id]); |
| 50 | + |
| 51 | + $response = $this->commentPolicy->delete($user, $comment); |
| 52 | + |
| 53 | + $this->assertTrue($response->allowed()); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_post_owner_can_delete_comment() |
| 57 | + { |
| 58 | + $postOwner = User::factory()->create(); |
| 59 | + $commentOwner = User::factory()->create(); |
| 60 | + $post = Post::factory()->create(['user_id' => $postOwner->id]); |
| 61 | + $comment = Comment::factory()->create(['user_id' => $commentOwner->id, 'post_id' => $post->id]); |
| 62 | + |
| 63 | + $response = $this->commentPolicy->delete($postOwner, $comment); |
| 64 | + |
| 65 | + $this->assertTrue($response->allowed()); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_admin_can_delete_any_comment() |
| 69 | + { |
| 70 | + $adminUser = User::factory()->create(['role' => 'admin']); |
| 71 | + $commentOwner = User::factory()->create(); |
| 72 | + $comment = Comment::factory()->create(['user_id' => $commentOwner->id]); |
| 73 | + |
| 74 | + $response = $this->commentPolicy->delete($adminUser, $comment); |
| 75 | + |
| 76 | + $this->assertTrue($response->allowed()); |
| 77 | + } |
| 78 | + |
| 79 | + public function test_non_owner_cannot_delete_comment() |
| 80 | + { |
| 81 | + $owner = User::factory()->create(); |
| 82 | + $nonOwner = User::factory()->create(); |
| 83 | + $comment = Comment::factory()->create(['user_id' => $owner->id]); |
| 84 | + |
| 85 | + $response = $this->commentPolicy->delete($nonOwner, $comment); |
| 86 | + |
| 87 | + $this->assertFalse($response->allowed()); |
| 88 | + $this->assertEquals('You are not authorized to delete this comment.', $response->message()); |
| 89 | + } |
| 90 | +} |
0 commit comments