create(); $replyAuthor = User::factory()->create(); $post = Post::factory()->create(); Subscription::subscribe($post, $parentAuthor); $parentComment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $parentAuthor->getKey(), 'commenter_type' => $parentAuthor->getMorphClass(), 'body' => '

Parent comment

', ]); $reply = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $replyAuthor->getKey(), 'commenter_type' => $replyAuthor->getMorphClass(), 'parent_id' => $parentComment->id, 'body' => '

A reply

', ]); $listener = new SendCommentRepliedNotification; $listener->handle(new CommentCreated($reply)); Notification::assertSentTo($parentAuthor, CommentRepliedNotification::class); }); it('does NOT send reply notification for top-level comments', function () { Notification::fake(); $author = User::factory()->create(); $subscriber = User::factory()->create(); $post = Post::factory()->create(); Subscription::subscribe($post, $subscriber); $comment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $author->getKey(), 'commenter_type' => $author->getMorphClass(), 'body' => '

Top-level comment

', ]); $listener = new SendCommentRepliedNotification; $listener->handle(new CommentCreated($comment)); Notification::assertNothingSent(); }); it('does NOT send reply notification to the reply author', function () { Notification::fake(); $user = User::factory()->create(); $post = Post::factory()->create(); Subscription::subscribe($post, $user); $parentComment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $user->getKey(), 'commenter_type' => $user->getMorphClass(), 'body' => '

My comment

', ]); $reply = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $user->getKey(), 'commenter_type' => $user->getMorphClass(), 'parent_id' => $parentComment->id, 'body' => '

My own reply

', ]); $listener = new SendCommentRepliedNotification; $listener->handle(new CommentCreated($reply)); Notification::assertNotSentTo($user, CommentRepliedNotification::class); }); it('sends UserMentionedNotification when a user is mentioned', function () { Notification::fake(); $author = User::factory()->create(); $mentioned = User::factory()->create(); $post = Post::factory()->create(); $comment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $author->getKey(), 'commenter_type' => $author->getMorphClass(), 'body' => '

Hey @someone

', ]); $listener = new SendUserMentionedNotification; $listener->handle(new UserMentioned($comment, $mentioned)); Notification::assertSentTo($mentioned, UserMentionedNotification::class); }); it('does NOT send mention notification to the comment author', function () { Notification::fake(); $author = User::factory()->create(); $post = Post::factory()->create(); $comment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $author->getKey(), 'commenter_type' => $author->getMorphClass(), 'body' => '

Hey @myself

', ]); $listener = new SendUserMentionedNotification; $listener->handle(new UserMentioned($comment, $author)); Notification::assertNotSentTo($author, UserMentionedNotification::class); }); it('does NOT send reply notification to unsubscribed user', function () { Notification::fake(); $author = User::factory()->create(); $unsubscribedUser = User::factory()->create(); $post = Post::factory()->create(); Subscription::subscribe($post, $unsubscribedUser); Subscription::unsubscribe($post, $unsubscribedUser); $parentComment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $unsubscribedUser->getKey(), 'commenter_type' => $unsubscribedUser->getMorphClass(), 'body' => '

Original

', ]); $reply = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $author->getKey(), 'commenter_type' => $author->getMorphClass(), 'parent_id' => $parentComment->id, 'body' => '

Reply

', ]); $listener = new SendCommentRepliedNotification; $listener->handle(new CommentCreated($reply)); Notification::assertNotSentTo($unsubscribedUser, CommentRepliedNotification::class); }); it('auto-subscribes the comment author when creating a comment', function () { Notification::fake(); $author = User::factory()->create(); $post = Post::factory()->create(); expect(Subscription::isSubscribed($post, $author))->toBeFalse(); $comment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $author->getKey(), 'commenter_type' => $author->getMorphClass(), 'body' => '

My comment

', ]); $listener = new SendCommentRepliedNotification; $listener->handle(new CommentCreated($comment)); expect(Subscription::isSubscribed($post, $author))->toBeTrue(); }); it('suppresses all notifications when notifications are disabled via config', function () { Notification::fake(); config()->set('comments.notifications.enabled', false); $author = User::factory()->create(); $subscriber = User::factory()->create(); $mentioned = User::factory()->create(); $post = Post::factory()->create(); Subscription::subscribe($post, $subscriber); $parentComment = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $subscriber->getKey(), 'commenter_type' => $subscriber->getMorphClass(), 'body' => '

Original

', ]); $reply = Comment::factory()->create([ 'commentable_id' => $post->id, 'commentable_type' => $post->getMorphClass(), 'commenter_id' => $author->getKey(), 'commenter_type' => $author->getMorphClass(), 'parent_id' => $parentComment->id, 'body' => '

Reply

', ]); $replyListener = new SendCommentRepliedNotification; $replyListener->handle(new CommentCreated($reply)); $mentionListener = new SendUserMentionedNotification; $mentionListener->handle(new UserMentioned($reply, $mentioned)); Notification::assertNothingSent(); });