From 4223ea0189cecda572412b9459e88a0bd72e1f27 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 2 Jul 2020 13:26:44 +0200 Subject: [PATCH] Prevent sending for anonymous notifiables and database channel --- .../Notifications/NotificationSender.php | 8 ++- .../Notifications/NotificationSenderTest.php | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Notifications/NotificationSender.php b/src/Illuminate/Notifications/NotificationSender.php index 688c1cd5d8f7..5e94196c2264 100644 --- a/src/Illuminate/Notifications/NotificationSender.php +++ b/src/Illuminate/Notifications/NotificationSender.php @@ -94,7 +94,9 @@ public function sendNow($notifiables, $notification, array $channels = null) $original = clone $notification; foreach ($notifiables as $notifiable) { - if (empty($viaChannels = $channels ?: $notification->via($notifiable))) { + $viaChannels = $channels ?: $notification->via($notifiable); + + if (empty($viaChannels)) { continue; } @@ -102,7 +104,9 @@ public function sendNow($notifiables, $notification, array $channels = null) $notificationId = Str::uuid()->toString(); foreach ((array) $viaChannels as $channel) { - $this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel); + if (! ($notifiable instanceof AnonymousNotifiable && $channel === 'database')) { + $this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel); + } } }); } diff --git a/tests/Notifications/NotificationSenderTest.php b/tests/Notifications/NotificationSenderTest.php index 8dd398f47bf2..6c5a6aaf849d 100644 --- a/tests/Notifications/NotificationSenderTest.php +++ b/tests/Notifications/NotificationSenderTest.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher; use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\ChannelManager; use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notification; @@ -34,6 +35,32 @@ public function testItCanSendQueuedNotificationsWithAStringVia() $sender->send($notifiable, new DummyQueuedNotificationWithStringVia()); } + + public function testItCanSendNotificationsWithAnEmptyStringVia() + { + $notifiable = new AnonymousNotifiable; + $manager = m::mock(ChannelManager::class); + $bus = m::mock(BusDispatcher::class); + $bus->shouldNotReceive('dispatch'); + $events = m::mock(EventDispatcher::class); + + $sender = new NotificationSender($manager, $bus, $events); + + $sender->sendNow($notifiable, new DummyNotificationWithEmptyStringVia()); + } + + public function testItCannotSendNotificationsViaDatabaseForAnonymousNotifiables() + { + $notifiable = new AnonymousNotifiable; + $manager = m::mock(ChannelManager::class); + $bus = m::mock(BusDispatcher::class); + $bus->shouldNotReceive('dispatch'); + $events = m::mock(EventDispatcher::class); + + $sender = new NotificationSender($manager, $bus, $events); + + $sender->sendNow($notifiable, new DummyNotificationWithDatabaseVia()); + } } class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue @@ -51,3 +78,35 @@ public function via($notifiable) return 'mail'; } } + +class DummyNotificationWithEmptyStringVia extends Notification +{ + use Queueable; + + /** + * Get the notification channels. + * + * @param mixed $notifiable + * @return array|string + */ + public function via($notifiable) + { + return ''; + } +} + +class DummyNotificationWithDatabaseVia extends Notification +{ + use Queueable; + + /** + * Get the notification channels. + * + * @param mixed $notifiable + * @return array|string + */ + public function via($notifiable) + { + return 'database'; + } +}