|
4 | 4 |
|
5 | 5 | use Illuminate\Support\Facades\Event; |
6 | 6 | use Kirschbaum\Commentions\Comment; |
| 7 | +use Kirschbaum\Commentions\CommentionsPlugin; |
| 8 | +use Kirschbaum\Commentions\Config; |
7 | 9 | use Kirschbaum\Commentions\Events\UserWasMentionedEvent; |
8 | 10 | use Tests\Models\Post; |
9 | 11 | use Tests\Models\User; |
|
68 | 70 | ->toContain($mentionedUser1) |
69 | 71 | ->toContain($mentionedUser2); |
70 | 72 | }); |
| 73 | + |
| 74 | +test('it can disable editing of comments', function () { |
| 75 | + $user = User::factory()->create(); |
| 76 | + $post = Post::factory()->create(); |
| 77 | + $comment = $post->comment('This is a test comment', $user); |
| 78 | + |
| 79 | + // Set authenticated user to the comment author |
| 80 | + Config::resolveAuthenticatedUserUsing(fn () => $user); |
| 81 | + |
| 82 | + // Should be able to edit by default |
| 83 | + expect($comment->canEdit())->toBeTrue(); |
| 84 | + |
| 85 | + // Disable edits |
| 86 | + config(['commentions.allow_edits' => false]); |
| 87 | + expect($comment->canEdit())->toBeFalse(); |
| 88 | +}); |
| 89 | + |
| 90 | +test('it can disable deletion of comments', function () { |
| 91 | + $user = User::factory()->create(); |
| 92 | + $post = Post::factory()->create(); |
| 93 | + $comment = $post->comment('This is a test comment', $user); |
| 94 | + |
| 95 | + // Set authenticated user to the comment author |
| 96 | + Config::resolveAuthenticatedUserUsing(fn () => $user); |
| 97 | + |
| 98 | + // Should be able to delete by default |
| 99 | + expect($comment->canDelete())->toBeTrue(); |
| 100 | + |
| 101 | + // Disable deletes |
| 102 | + config(['commentions.allow_deletes' => false]); |
| 103 | + expect($comment->canDelete())->toBeFalse(); |
| 104 | +}); |
| 105 | + |
| 106 | +test('plugin can disallow edits and deletes', function () { |
| 107 | + $user = User::factory()->create(); |
| 108 | + $post = Post::factory()->create(); |
| 109 | + $comment = $post->comment('This is a test comment', $user); |
| 110 | + |
| 111 | + // Set authenticated user to the comment author |
| 112 | + Config::resolveAuthenticatedUserUsing(fn () => $user); |
| 113 | + |
| 114 | + // Reset to default values |
| 115 | + Config::allowEdits(true); |
| 116 | + Config::allowDeletes(true); |
| 117 | + |
| 118 | + // Should be able to edit and delete by default |
| 119 | + expect($comment->canEdit())->toBeTrue(); |
| 120 | + expect($comment->canDelete())->toBeTrue(); |
| 121 | + |
| 122 | + // Create a plugin instance and disallow edits |
| 123 | + $plugin = new CommentionsPlugin(); |
| 124 | + $plugin->disallowEdits(); |
| 125 | + $panel = new \Filament\Panel(); |
| 126 | + $plugin->register($panel); |
| 127 | + |
| 128 | + expect($comment->canEdit())->toBeFalse(); |
| 129 | + expect($comment->canDelete())->toBeTrue(); |
| 130 | + |
| 131 | + // Reset and test disallowing deletes |
| 132 | + Config::allowEdits(true); |
| 133 | + Config::allowDeletes(true); |
| 134 | + |
| 135 | + $plugin = new CommentionsPlugin(); |
| 136 | + $plugin->disallowDeletes(); |
| 137 | + $plugin->register($panel); |
| 138 | + |
| 139 | + expect($comment->canEdit())->toBeTrue(); |
| 140 | + expect($comment->canDelete())->toBeFalse(); |
| 141 | + |
| 142 | + // Test disallowing both |
| 143 | + Config::allowEdits(true); |
| 144 | + Config::allowDeletes(true); |
| 145 | + |
| 146 | + $plugin = new CommentionsPlugin(); |
| 147 | + $plugin->disallowEdits()->disallowDeletes(); |
| 148 | + $plugin->register($panel); |
| 149 | + |
| 150 | + expect($comment->canEdit())->toBeFalse(); |
| 151 | + expect($comment->canDelete())->toBeFalse(); |
| 152 | +}); |
0 commit comments