Skip to content

Commit 7a7582f

Browse files
authored
[7.x] Add plain text only notifications (#33781)
* Add test for html only notifications * Add ability to send plain text only notifications * Improve class name
1 parent c6a3174 commit 7a7582f

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

src/Illuminate/Notifications/Channels/MailChannel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function messageBuilder($notifiable, $notification, $message)
8989
*/
9090
protected function buildView($message)
9191
{
92-
if ($message->view) {
92+
if ($message->view || $message->textView) {
9393
return [
9494
'html' => $message->view,
9595
'text' => $message->textView,

src/Illuminate/Notifications/Messages/MailMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ protected function arrayOfAddresses($address)
331331
*/
332332
public function render()
333333
{
334-
if (isset($this->view)) {
334+
if (isset($this->view) || isset($this->textView)) {
335335
return Container::getInstance()->make('mailer')->render(
336336
[$this->view, $this->textView],
337337
$this->data()

tests/Integration/Notifications/SendingMailNotificationsTest.php

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ public function testMailIsSentUsingMailable()
249249
$user->notify($notification);
250250
}
251251

252-
public function testMailIsSentUsingMailMessageWithPlain()
252+
public function testMailIsSentUsingMailMessageWithHtmlAndPlain()
253253
{
254-
$notification = new TestMailNotificationWithPlain;
254+
$notification = new TestMailNotificationWithHtmlAndPlain;
255255
$notification->id = Str::uuid()->toString();
256256

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

271271
$message->shouldReceive('to')->once()->with(['[email protected]']);
272272

273-
$message->shouldReceive('subject')->once()->with('Test Mail Notification With Plain');
273+
$message->shouldReceive('subject')->once()->with('Test Mail Notification With Html And Plain');
274+
275+
$closure($message);
276+
277+
return true;
278+
})
279+
);
280+
281+
$user->notify($notification);
282+
}
283+
284+
public function testMailIsSentUsingMailMessageWithHtmlOnly()
285+
{
286+
$notification = new TestMailNotificationWithHtmlOnly;
287+
$notification->id = Str::uuid()->toString();
288+
289+
$user = NotifiableUser::forceCreate([
290+
'email' => '[email protected]',
291+
]);
292+
293+
$this->mailer->shouldReceive('send')->once()->with(
294+
['html' => 'html', 'text' => null],
295+
array_merge($notification->toMail($user)->toArray(), [
296+
'__laravel_notification_id' => $notification->id,
297+
'__laravel_notification' => get_class($notification),
298+
'__laravel_notification_queued' => false,
299+
]),
300+
m::on(function ($closure) {
301+
$message = m::mock(Message::class);
302+
303+
$message->shouldReceive('to')->once()->with(['[email protected]']);
304+
305+
$message->shouldReceive('subject')->once()->with('Test Mail Notification With Html Only');
306+
307+
$closure($message);
308+
309+
return true;
310+
})
311+
);
312+
313+
$user->notify($notification);
314+
}
315+
316+
public function testMailIsSentUsingMailMessageWithPlainOnly()
317+
{
318+
$notification = new TestMailNotificationWithPlainOnly;
319+
$notification->id = Str::uuid()->toString();
320+
321+
$user = NotifiableUser::forceCreate([
322+
'email' => '[email protected]',
323+
]);
324+
325+
$this->mailer->shouldReceive('send')->once()->with(
326+
['html' => null, 'text' => 'plain'],
327+
array_merge($notification->toMail($user)->toArray(), [
328+
'__laravel_notification_id' => $notification->id,
329+
'__laravel_notification' => get_class($notification),
330+
'__laravel_notification_queued' => false,
331+
]),
332+
m::on(function ($closure) {
333+
$message = m::mock(Message::class);
334+
335+
$message->shouldReceive('to')->once()->with(['[email protected]']);
336+
337+
$message->shouldReceive('subject')->once()->with('Test Mail Notification With Plain Only');
274338

275339
$closure($message);
276340

@@ -364,7 +428,7 @@ public function toMail($notifiable)
364428
}
365429
}
366430

367-
class TestMailNotificationWithPlain extends Notification
431+
class TestMailNotificationWithHtmlAndPlain extends Notification
368432
{
369433
public function via($notifiable)
370434
{
@@ -378,3 +442,33 @@ public function toMail($notifiable)
378442
->text('plain');
379443
}
380444
}
445+
446+
class TestMailNotificationWithHtmlOnly extends Notification
447+
{
448+
public function via($notifiable)
449+
{
450+
return [MailChannel::class];
451+
}
452+
453+
public function toMail($notifiable)
454+
{
455+
return (new MailMessage)
456+
->view('html')
457+
->text(null);
458+
}
459+
}
460+
461+
class TestMailNotificationWithPlainOnly extends Notification
462+
{
463+
public function via($notifiable)
464+
{
465+
return [MailChannel::class];
466+
}
467+
468+
public function toMail($notifiable)
469+
{
470+
return (new MailMessage)
471+
->view(null)
472+
->text('plain');
473+
}
474+
}

0 commit comments

Comments
 (0)