Skip to content

[7.x] Fix serialization of models when sending notifications #32051

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

Merged
merged 3 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 27 additions & 2 deletions src/Illuminate/Notifications/SendQueuedNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -50,7 +52,7 @@ class SendQueuedNotifications implements ShouldQueue
/**
* Create a new job instance.
*
* @param \Illuminate\Support\Collection $notifiables
* @param \Illuminate\Notifications\Notifiable|\Illuminate\Support\Collection $notifiables
* @param \Illuminate\Notifications\Notification $notification
* @param array|null $channels
* @return void
Expand All @@ -59,7 +61,7 @@ public function __construct($notifiables, $notification, array $channels = null)
{
$this->channels = $channels;
$this->notification = $notification;
$this->notifiables = Collection::wrap($notifiables);
$this->notifiables = $this->wrapNotifiables($notifiables);
$this->tries = property_exists($notification, 'tries') ? $notification->tries : null;
$this->timeout = property_exists($notification, 'timeout') ? $notification->timeout : null;
}
Expand Down Expand Up @@ -136,4 +138,27 @@ public function __clone()
$this->notifiables = clone $this->notifiables;
$this->notification = clone $this->notification;
}

/**
* Wrap the notifiable(s) in a collection.
*
* @param \Illuminate\Notifications\Notifiable|\Illuminate\Support\Collection $notifiables
* @return \Illuminate\Support\Collection
*/
protected function wrapNotifiables($notifiables)
{
if ($notifiables instanceof Collection) {
// If the notifiable(s) are already wrapped, pass them as is.
// This prevents any custom queueable collections from being re-wrapped
return $notifiables;
}

if ($notifiables instanceof Model) {
// In the case of a model we want to wrap it in an eloquent collection
// This way the job can take advantage of model serialization
return EloquentCollection::wrap($notifiables);
}

return Collection::wrap($notifiables);
}
}
25 changes: 25 additions & 0 deletions tests/Notifications/NotificationSendQueuedNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Illuminate\Tests\Notifications;

use Illuminate\Contracts\Database\ModelIdentifier;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Notifications\SendQueuedNotifications;
use Illuminate\Support\Collection;
use Illuminate\Tests\Integration\Notifications\NotifiableUser;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,4 +29,26 @@ public function testNotificationsCanBeSent()
});
$job->handle($manager);
}

public function testSerializationOfNotifiableModel()
{
$identifier = new ModelIdentifier(NotifiableUser::class, [null], [], null);
$serializedIdentifier = serialize($identifier);

$job = new SendQueuedNotifications(new NotifiableUser(), 'notification');
$serialized = serialize($job);

$this->assertStringContainsString($serializedIdentifier, $serialized);
}

public function testSerializationOfNormalNotifiable()
{
$notifiable = new AnonymousNotifiable();
$serializedNotifiable = serialize($notifiable);

$job = new SendQueuedNotifications($notifiable, 'notification');
$serialized = serialize($job);

$this->assertStringContainsString($serializedNotifiable, $serialized);
}
}