Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fix notifications database channel for anonymous notifiables #33409

Merged
merged 4 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Illuminate/Notifications/AnonymousNotifiable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Notifications;

use Illuminate\Contracts\Notifications\Dispatcher;
use InvalidArgumentException;

class AnonymousNotifiable
{
Expand All @@ -22,6 +23,10 @@ class AnonymousNotifiable
*/
public function route($channel, $route)
{
if ($channel === 'database') {
throw new InvalidArgumentException('Cannot use database channel for on-demand notifications.');
}

$this->routes[$channel] = $route;

return $this;
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Notifications/NotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,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);
}
}
});
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Notifications/NotificationRoutesNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Notifications\Dispatcher;
use Illuminate\Notifications\RoutesNotifications;
use Illuminate\Support\Facades\Notification;
use InvalidArgumentException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -50,6 +52,15 @@ public function testNotificationOptionRouting()
$this->assertSame('bar', $instance->routeNotificationFor('foo'));
$this->assertSame('[email protected]', $instance->routeNotificationFor('mail'));
}

public function testOnDemandNotificationsCannotUseDatabaseChannel()
{
$this->expectExceptionObject(
new InvalidArgumentException('Cannot use database channel for on-demand notifications.')
);

Notification::route('database', 'foo');
}
}

class RoutesNotificationsTestInstance
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';
}
}