Allow deleting replies when comments are deleted.

This commit is contained in:
3m1n3nc3
2024-07-09 04:44:30 +01:00
parent d5a15036b8
commit e9663034fe
8 changed files with 154 additions and 8 deletions
+70 -1
View File
@@ -2,9 +2,13 @@
namespace BeyondCode\Comments\Tests;
use BeyondCode\Comments\Events\CommentAdded;
use BeyondCode\Comments\Events\CommentDeleted;
use BeyondCode\Comments\Tests\Models\ApprovedUser;
use BeyondCode\Comments\Tests\Models\Post;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Event;
class CommentsTest extends TestCase
{
@@ -145,4 +149,69 @@ class CommentsTest extends TestCase
$this->assertSame('this comment is approved', $post->comments()->approved()->first()->comment);
}
}
/** @test */
public function comments_are_deleted_when_posts_are_deleted()
{
$post = Post::create([
'title' => 'Some post'
]);
$comment = $post->comment('this comment will be deleted');
$post->delete();
$this->assertFalse($comment->exists());
}
/** @test */
public function replies_are_deleted_when_post_comments_are_deleted()
{
config(['comments.delete_replies_along_comments' => true]);
$post = Post::create([
'title' => 'Some post'
]);
$comment = $post->comment('this comment will be deleted');
$reply = $comment->comment('this comment will be deleted too');
$comment->delete();
$this->assertFalse($reply->exists());
}
/** @test */
public function comment_added_event_is_dispatched_when_comment_is_created()
{
Event::fake([CommentAdded::class]);
$post = Post::create([
'title' => 'Some post'
]);
$comment = $post->comment('this comment is added');
Event::assertDispatched(CommentAdded::class, function ($event) use ($comment) {
return $event->comment->is($comment);
});
}
/** @test */
public function comment_deleted_event_is_dispatched_when_comment_is_deleted()
{
Event::fake([CommentDeleted::class]);
$post = Post::create([
'title' => 'Some post'
]);
$comment = $post->comment('this comment is added');
$comment->delete();
Event::assertDispatched(CommentDeleted::class, function ($event) use ($comment) {
return $event->comment->is($comment);
});
}
}
+11 -1
View File
@@ -3,6 +3,7 @@
namespace BeyondCode\Comments\Tests\Models;
use BeyondCode\Comments\Traits\HasComments;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
@@ -10,4 +11,13 @@ class Post extends Model
use HasComments;
protected $guarded = [];
}
public static function boot(): void
{
parent::boot();
static::deleting(function (self $model) {
$model->comments()->delete();
});
}
}