Skip to content

Commit

Permalink
Prevent sending for anonymous notifiables and database channel
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jul 2, 2020
1 parent 7a98829 commit 4223ea0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Notifications/NotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,19 @@ 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;
}

$this->withLocale($this->preferredLocale($notifiable, $notification), function () use ($viaChannels, $notifiable, $original) {
$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);
}
}
});
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Notifications/NotificationSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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';
}
}

0 comments on commit 4223ea0

Please sign in to comment.