|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Notifications\Notification as BaseNotification; |
| 4 | +use Illuminate\Support\Facades\Notification as NotificationFacade; |
| 5 | +use Kirschbaum\Commentions\Comment; |
| 6 | +use Kirschbaum\Commentions\Config; |
| 7 | +use Kirschbaum\Commentions\Events\UserWasMentionedEvent; |
| 8 | +use Kirschbaum\Commentions\Listeners\SendUserMentionedNotification; |
| 9 | +use Kirschbaum\Commentions\Notifications\UserMentionedInComment; |
| 10 | +use Tests\Models\Post; |
| 11 | +use Tests\Models\User; |
| 12 | + |
| 13 | +test('it sends a mention notification when enabled with configured channels', function () { |
| 14 | + config()->set('commentions.notifications.mentions.enabled', true); |
| 15 | + config()->set('commentions.notifications.mentions.channels', ['mail', 'database']); |
| 16 | + |
| 17 | + /** @var User $author */ |
| 18 | + $author = User::factory()->create(); |
| 19 | + /** @var User $mentioned */ |
| 20 | + $mentioned = User::factory()->create(); |
| 21 | + /** @var Post $post */ |
| 22 | + $post = Post::factory()->create(); |
| 23 | + |
| 24 | + $comment = $post->comment( |
| 25 | + sprintf('Hey <span data-type="mention" data-id="%s">@%s</span>', $mentioned->id, $mentioned->name), |
| 26 | + $author |
| 27 | + ); |
| 28 | + |
| 29 | + NotificationFacade::fake(); |
| 30 | + |
| 31 | + $listener = app(SendUserMentionedNotification::class); |
| 32 | + $listener->handle(new UserWasMentionedEvent($comment, $mentioned)); |
| 33 | + |
| 34 | + NotificationFacade::assertSentTo( |
| 35 | + $mentioned, |
| 36 | + UserMentionedInComment::class, |
| 37 | + function (UserMentionedInComment $notification, array $channels) use ($comment) { |
| 38 | + expect($channels)->toEqualCanonicalizing(['mail', 'database']); |
| 39 | + |
| 40 | + $payload = $notification->toArray($comment->author); |
| 41 | + expect($payload['comment_id'])->toBe($comment->getId()); |
| 42 | + expect($payload['author_name'])->toBe($comment->getAuthorName()); |
| 43 | + |
| 44 | + return true; |
| 45 | + } |
| 46 | + ); |
| 47 | +}); |
| 48 | + |
| 49 | +class TestCustomMentionNotification extends BaseNotification |
| 50 | +{ |
| 51 | + public function __construct(public Comment $comment, public array $channels) {} |
| 52 | + |
| 53 | + public function via(object $notifiable): array |
| 54 | + { |
| 55 | + return ['database']; |
| 56 | + } |
| 57 | + |
| 58 | + public function toArray(object $notifiable): array |
| 59 | + { |
| 60 | + return [ |
| 61 | + 'custom' => true, |
| 62 | + 'comment_id' => $this->comment->getId(), |
| 63 | + ]; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +test('it allows overriding the notification class via config', function () { |
| 68 | + config()->set('commentions.notifications.mentions.enabled', true); |
| 69 | + config()->set('commentions.notifications.mentions.channels', ['mail']); |
| 70 | + config()->set('commentions.notifications.mentions.notification', TestCustomMentionNotification::class); |
| 71 | + |
| 72 | + /** @var User $author */ |
| 73 | + $author = User::factory()->create(); |
| 74 | + /** @var User $mentioned */ |
| 75 | + $mentioned = User::factory()->create(); |
| 76 | + /** @var Post $post */ |
| 77 | + $post = Post::factory()->create(); |
| 78 | + |
| 79 | + $comment = $post->comment('Hello world', $author); |
| 80 | + |
| 81 | + NotificationFacade::fake(); |
| 82 | + |
| 83 | + $listener = app(SendUserMentionedNotification::class); |
| 84 | + $listener->handle(new UserWasMentionedEvent($comment, $mentioned)); |
| 85 | + |
| 86 | + NotificationFacade::assertSentTo( |
| 87 | + $mentioned, |
| 88 | + TestCustomMentionNotification::class, |
| 89 | + function (TestCustomMentionNotification $notification) use ($comment) { |
| 90 | + $payload = $notification->toArray($comment->author); |
| 91 | + expect($payload['custom'])->toBeTrue(); |
| 92 | + expect($payload['comment_id'])->toBe($comment->getId()); |
| 93 | + |
| 94 | + return true; |
| 95 | + } |
| 96 | + ); |
| 97 | +}); |
| 98 | + |
| 99 | +test('it does not send a mention notification when disabled', function () { |
| 100 | + config()->set('commentions.notifications.mentions.enabled', false); |
| 101 | + config()->set('commentions.notifications.mentions.channels', ['mail']); |
| 102 | + |
| 103 | + /** @var User $author */ |
| 104 | + $author = User::factory()->create(); |
| 105 | + /** @var User $mentioned */ |
| 106 | + $mentioned = User::factory()->create(); |
| 107 | + /** @var Post $post */ |
| 108 | + $post = Post::factory()->create(); |
| 109 | + |
| 110 | + $comment = $post->comment('Hello world', $author); |
| 111 | + |
| 112 | + NotificationFacade::fake(); |
| 113 | + |
| 114 | + $listener = app(SendUserMentionedNotification::class); |
| 115 | + $listener->handle(new UserWasMentionedEvent($comment, $mentioned)); |
| 116 | + |
| 117 | + NotificationFacade::assertNothingSent(); |
| 118 | +}); |
| 119 | + |
| 120 | +test('it does not send a mention notification when channels are empty', function () { |
| 121 | + config()->set('commentions.notifications.mentions.enabled', true); |
| 122 | + config()->set('commentions.notifications.mentions.channels', []); |
| 123 | + |
| 124 | + /** @var User $author */ |
| 125 | + $author = User::factory()->create(); |
| 126 | + /** @var User $mentioned */ |
| 127 | + $mentioned = User::factory()->create(); |
| 128 | + /** @var Post $post */ |
| 129 | + $post = Post::factory()->create(); |
| 130 | + |
| 131 | + $comment = $post->comment('Hello world', $author); |
| 132 | + |
| 133 | + NotificationFacade::fake(); |
| 134 | + |
| 135 | + $listener = app(SendUserMentionedNotification::class); |
| 136 | + $listener->handle(new UserWasMentionedEvent($comment, $mentioned)); |
| 137 | + |
| 138 | + NotificationFacade::assertNothingSent(); |
| 139 | +}); |
| 140 | + |
| 141 | +test('it uses the configured comment URL resolver in notification payload', function () { |
| 142 | + config()->set('commentions.notifications.mentions.enabled', true); |
| 143 | + config()->set('commentions.notifications.mentions.channels', ['database']); |
| 144 | + |
| 145 | + $resolvedUrl = 'https://example.test/some-resource#comment-123'; |
| 146 | + Config::resolveCommentUrlUsing(function (Comment $comment) use ($resolvedUrl) { |
| 147 | + return $resolvedUrl; |
| 148 | + }); |
| 149 | + |
| 150 | + /** @var User $author */ |
| 151 | + $author = User::factory()->create(); |
| 152 | + /** @var User $mentioned */ |
| 153 | + $mentioned = User::factory()->create(); |
| 154 | + /** @var Post $post */ |
| 155 | + $post = Post::factory()->create(); |
| 156 | + |
| 157 | + $comment = $post->comment('Hello world', $author); |
| 158 | + |
| 159 | + NotificationFacade::fake(); |
| 160 | + |
| 161 | + $listener = app(SendUserMentionedNotification::class); |
| 162 | + $listener->handle(new UserWasMentionedEvent($comment, $mentioned)); |
| 163 | + |
| 164 | + NotificationFacade::assertSentTo( |
| 165 | + $mentioned, |
| 166 | + UserMentionedInComment::class, |
| 167 | + function (UserMentionedInComment $notification) use ($resolvedUrl, $mentioned) { |
| 168 | + $payload = $notification->toArray($mentioned); |
| 169 | + expect($payload['url'])->toBe($resolvedUrl); |
| 170 | + |
| 171 | + return true; |
| 172 | + } |
| 173 | + ); |
| 174 | +}); |
0 commit comments