Skip to content

Commit

Permalink
[7.x] Add plain text only notifications (#33781)
Browse files Browse the repository at this point in the history
* Add test for html only notifications

* Add ability to send plain text only notifications

* Improve class name
  • Loading branch information
pawelmysior authored Aug 7, 2020
1 parent c6a3174 commit 7a7582f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function messageBuilder($notifiable, $notification, $message)
*/
protected function buildView($message)
{
if ($message->view) {
if ($message->view || $message->textView) {
return [
'html' => $message->view,
'text' => $message->textView,
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ protected function arrayOfAddresses($address)
*/
public function render()
{
if (isset($this->view)) {
if (isset($this->view) || isset($this->textView)) {
return Container::getInstance()->make('mailer')->render(
[$this->view, $this->textView],
$this->data()
Expand Down
102 changes: 98 additions & 4 deletions tests/Integration/Notifications/SendingMailNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ public function testMailIsSentUsingMailable()
$user->notify($notification);
}

public function testMailIsSentUsingMailMessageWithPlain()
public function testMailIsSentUsingMailMessageWithHtmlAndPlain()
{
$notification = new TestMailNotificationWithPlain;
$notification = new TestMailNotificationWithHtmlAndPlain;
$notification->id = Str::uuid()->toString();

$user = NotifiableUser::forceCreate([
Expand All @@ -270,7 +270,71 @@ public function testMailIsSentUsingMailMessageWithPlain()

$message->shouldReceive('to')->once()->with(['taylor@laravel.com']);

$message->shouldReceive('subject')->once()->with('Test Mail Notification With Plain');
$message->shouldReceive('subject')->once()->with('Test Mail Notification With Html And Plain');

$closure($message);

return true;
})
);

$user->notify($notification);
}

public function testMailIsSentUsingMailMessageWithHtmlOnly()
{
$notification = new TestMailNotificationWithHtmlOnly;
$notification->id = Str::uuid()->toString();

$user = NotifiableUser::forceCreate([
'email' => 'taylor@laravel.com',
]);

$this->mailer->shouldReceive('send')->once()->with(
['html' => 'html', 'text' => null],
array_merge($notification->toMail($user)->toArray(), [
'__laravel_notification_id' => $notification->id,
'__laravel_notification' => get_class($notification),
'__laravel_notification_queued' => false,
]),
m::on(function ($closure) {
$message = m::mock(Message::class);

$message->shouldReceive('to')->once()->with(['taylor@laravel.com']);

$message->shouldReceive('subject')->once()->with('Test Mail Notification With Html Only');

$closure($message);

return true;
})
);

$user->notify($notification);
}

public function testMailIsSentUsingMailMessageWithPlainOnly()
{
$notification = new TestMailNotificationWithPlainOnly;
$notification->id = Str::uuid()->toString();

$user = NotifiableUser::forceCreate([
'email' => 'taylor@laravel.com',
]);

$this->mailer->shouldReceive('send')->once()->with(
['html' => null, 'text' => 'plain'],
array_merge($notification->toMail($user)->toArray(), [
'__laravel_notification_id' => $notification->id,
'__laravel_notification' => get_class($notification),
'__laravel_notification_queued' => false,
]),
m::on(function ($closure) {
$message = m::mock(Message::class);

$message->shouldReceive('to')->once()->with(['taylor@laravel.com']);

$message->shouldReceive('subject')->once()->with('Test Mail Notification With Plain Only');

$closure($message);

Expand Down Expand Up @@ -364,7 +428,7 @@ public function toMail($notifiable)
}
}

class TestMailNotificationWithPlain extends Notification
class TestMailNotificationWithHtmlAndPlain extends Notification
{
public function via($notifiable)
{
Expand All @@ -378,3 +442,33 @@ public function toMail($notifiable)
->text('plain');
}
}

class TestMailNotificationWithHtmlOnly extends Notification
{
public function via($notifiable)
{
return [MailChannel::class];
}

public function toMail($notifiable)
{
return (new MailMessage)
->view('html')
->text(null);
}
}

class TestMailNotificationWithPlainOnly extends Notification
{
public function via($notifiable)
{
return [MailChannel::class];
}

public function toMail($notifiable)
{
return (new MailMessage)
->view(null)
->text('plain');
}
}

0 comments on commit 7a7582f

Please sign in to comment.