Skip to content

Commit 1116adb

Browse files
[6.x] Fix notifications database channel for anonymous notifiables (#33409)
* Prevent usage of database channel with anonymous notifiable * Prevent sending for anonymous notifiables and database channel * Update AnonymousNotifiable.php * Update NotificationRoutesNotificationsTest.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent c914a3e commit 1116adb

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

src/Illuminate/Notifications/AnonymousNotifiable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Notifications;
44

55
use Illuminate\Contracts\Notifications\Dispatcher;
6+
use InvalidArgumentException;
67

78
class AnonymousNotifiable
89
{
@@ -22,6 +23,10 @@ class AnonymousNotifiable
2223
*/
2324
public function route($channel, $route)
2425
{
26+
if ($channel === 'database') {
27+
throw new InvalidArgumentException('The database channel does not support on-demand notifications.');
28+
}
29+
2530
$this->routes[$channel] = $route;
2631

2732
return $this;

src/Illuminate/Notifications/NotificationSender.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public function sendNow($notifiables, $notification, array $channels = null)
102102
$notificationId = Str::uuid()->toString();
103103

104104
foreach ((array) $viaChannels as $channel) {
105-
$this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
105+
if (! ($notifiable instanceof AnonymousNotifiable && $channel === 'database')) {
106+
$this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
107+
}
106108
}
107109
});
108110
}

tests/Notifications/NotificationRoutesNotificationsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Container\Container;
66
use Illuminate\Contracts\Notifications\Dispatcher;
77
use Illuminate\Notifications\RoutesNotifications;
8+
use Illuminate\Support\Facades\Notification;
9+
use InvalidArgumentException;
810
use Mockery as m;
911
use PHPUnit\Framework\TestCase;
1012
use stdClass;
@@ -50,6 +52,15 @@ public function testNotificationOptionRouting()
5052
$this->assertSame('bar', $instance->routeNotificationFor('foo'));
5153
$this->assertSame('[email protected]', $instance->routeNotificationFor('mail'));
5254
}
55+
56+
public function testOnDemandNotificationsCannotUseDatabaseChannel()
57+
{
58+
$this->expectExceptionObject(
59+
new InvalidArgumentException('The database channel does not support on-demand notifications.')
60+
);
61+
62+
Notification::route('database', 'foo');
63+
}
5364
}
5465

5566
class RoutesNotificationsTestInstance

tests/Notifications/NotificationSenderTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
77
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
88
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Notifications\AnonymousNotifiable;
910
use Illuminate\Notifications\ChannelManager;
1011
use Illuminate\Notifications\Notifiable;
1112
use Illuminate\Notifications\Notification;
@@ -34,6 +35,32 @@ public function testItCanSendQueuedNotificationsWithAStringVia()
3435

3536
$sender->send($notifiable, new DummyQueuedNotificationWithStringVia());
3637
}
38+
39+
public function testItCanSendNotificationsWithAnEmptyStringVia()
40+
{
41+
$notifiable = new AnonymousNotifiable;
42+
$manager = m::mock(ChannelManager::class);
43+
$bus = m::mock(BusDispatcher::class);
44+
$bus->shouldNotReceive('dispatch');
45+
$events = m::mock(EventDispatcher::class);
46+
47+
$sender = new NotificationSender($manager, $bus, $events);
48+
49+
$sender->sendNow($notifiable, new DummyNotificationWithEmptyStringVia());
50+
}
51+
52+
public function testItCannotSendNotificationsViaDatabaseForAnonymousNotifiables()
53+
{
54+
$notifiable = new AnonymousNotifiable;
55+
$manager = m::mock(ChannelManager::class);
56+
$bus = m::mock(BusDispatcher::class);
57+
$bus->shouldNotReceive('dispatch');
58+
$events = m::mock(EventDispatcher::class);
59+
60+
$sender = new NotificationSender($manager, $bus, $events);
61+
62+
$sender->sendNow($notifiable, new DummyNotificationWithDatabaseVia());
63+
}
3764
}
3865

3966
class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue
@@ -51,3 +78,35 @@ public function via($notifiable)
5178
return 'mail';
5279
}
5380
}
81+
82+
class DummyNotificationWithEmptyStringVia extends Notification
83+
{
84+
use Queueable;
85+
86+
/**
87+
* Get the notification channels.
88+
*
89+
* @param mixed $notifiable
90+
* @return array|string
91+
*/
92+
public function via($notifiable)
93+
{
94+
return '';
95+
}
96+
}
97+
98+
class DummyNotificationWithDatabaseVia extends Notification
99+
{
100+
use Queueable;
101+
102+
/**
103+
* Get the notification channels.
104+
*
105+
* @param mixed $notifiable
106+
* @return array|string
107+
*/
108+
public function via($notifiable)
109+
{
110+
return 'database';
111+
}
112+
}

0 commit comments

Comments
 (0)