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

[5.7] Mail recipient and notifiable preferred locale #25752

Merged
merged 2 commits into from
Sep 27, 2018
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
11 changes: 11 additions & 0 deletions src/Illuminate/Contracts/Translation/HasLocalePreference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Contracts\Translation;

interface HasLocalePreference
{
/**
* @return string|null
*/
public function preferredLocale();
}
5 changes: 5 additions & 0 deletions src/Illuminate/Mail/PendingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Mail;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;

class PendingMail
{
Expand Down Expand Up @@ -75,6 +76,10 @@ public function to($users)
{
$this->to = $users;

if (! $this->locale && $users instanceof HasLocalePreference) {
$this->locale($users->preferredLocale());
}

return $this;
}

Expand Down
20 changes: 19 additions & 1 deletion src/Illuminate/Notifications/NotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Localizable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Collection as ModelCollection;

class NotificationSender
Expand Down Expand Up @@ -95,7 +96,7 @@ public function sendNow($notifiables, $notification, array $channels = null)
continue;
}

$this->withLocale($notification->locale ?? $this->locale, function () use ($viaChannels, $notifiable, $original) {
$this->withLocale($this->preferredLocale($notifiable, $notification), function () use ($viaChannels, $notifiable, $original) {
$notificationId = Str::uuid()->toString();

foreach ((array) $viaChannels as $channel) {
Expand All @@ -105,6 +106,23 @@ public function sendNow($notifiables, $notification, array $channels = null)
}
}

/**
* Get the locale for the notification preferred by this notifiable.
*
* @param mixed $notifiable
* @param mixed $notification
*
* @return string|null
*/
protected function preferredLocale($notifiable, $notification)
{
return $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
if ($notifiable instanceof HasLocalePreference) {
return $notifiable->preferredLocale();
}
});
}

/**
* Send the given notification to the given notifiable via a channel.
*
Expand Down
82 changes: 82 additions & 0 deletions tests/Integration/Mail/SendingMailWithLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Contracts\Translation\HasLocalePreference;

/**
* @group integration
Expand Down Expand Up @@ -84,6 +86,73 @@ public function test_mail_is_sent_with_locale_updated_listeners_called()
$this->assertEquals('en', Carbon::getLocale());
}

public function test_locale_is_sent_with_model_preferred_locale()
{
$recipient = new TestEmailLocaleUser([
'email' => 'test@mail.com',
'email_locale' => 'ar',
]);

Mail::to($recipient)->send(new TestMail());

$this->assertContains('esm',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_sent_with_selected_locale_overriding_model_preferred_locale()
{
$recipient = new TestEmailLocaleUser([
'email' => 'test@mail.com',
'email_locale' => 'en',
]);

Mail::to($recipient)->locale('ar')->send(new TestMail());

$this->assertContains('esm',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_sent_with_model_preferred_locale_will_ignore_preferred_locale_of_the_cc_recipient()
{
$toRecipient = new TestEmailLocaleUser([
'email' => 'test@mail.com',
'email_locale' => 'ar',
]);

$ccRecipient = new TestEmailLocaleUser([
'email' => 'test.cc@mail.com',
'email_locale' => 'en',
]);

Mail::to($toRecipient)->cc($ccRecipient)->send(new TestMail());

$this->assertContains('esm',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_not_sent_with_model_preferred_locale_when_there_are_multiple_recipients()
{
$recipients = [
new TestEmailLocaleUser([
'email' => 'test@mail.com',
'email_locale' => 'ar',
]),
new TestEmailLocaleUser([
'email' => 'test.2@mail.com',
'email_locale' => 'ar',
]),
];

Mail::to($recipients)->send(new TestMail());

$this->assertContains('name',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_set_back_to_default_after_mail_sent()
{
Mail::to('test@mail.com')->locale('ar')->send(new TestMail());
Expand Down Expand Up @@ -114,6 +183,19 @@ public function build()
}
}

class TestEmailLocaleUser extends Model implements HasLocalePreference
{
protected $fillable = [
'email',
'email_locale',
];

public function preferredLocale()
{
return $this->email_locale;
}
}

class TimestampTestMail extends Mailable
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Notifications\Channels\MailChannel;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Support\Facades\Notification as NotificationFacade;

/**
Expand Down Expand Up @@ -45,6 +46,7 @@ protected function getEnvironmentSetUp($app)
'*' => [
'*' => [
'en' => ['hi' => 'hello'],
'es' => ['hi' => 'hola'],
'fr' => ['hi' => 'bonjour'],
],
],
Expand Down Expand Up @@ -155,6 +157,83 @@ public function test_mail_is_sent_with_locale_updated_listeners_called()

$this->assertSame('en', Carbon::getLocale());
}

public function test_locale_is_sent_with_notifiable_preferred_locale()
{
$recipient = new NotifiableEmailLocalePreferredUser([
'email' => 'test@mail.com',
'email_locale' => 'fr',
]);

$recipient->notify(new GreetingMailNotification());

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_sent_with_notifiable_preferred_locale_for_multiple_recipients()
{
$recipients = [
new NotifiableEmailLocalePreferredUser([
'email' => 'test@mail.com',
'email_locale' => 'fr',
]),
new NotifiableEmailLocalePreferredUser([
'email' => 'test.2@mail.com',
'email_locale' => 'es',
]),
NotifiableLocalizedUser::forceCreate([
'email' => 'test.3@mail.com',
]),
];

NotificationFacade::send(
$recipients, new GreetingMailNotification()
);

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
$this->assertContains('hola',
app('swift.transport')->messages()[1]->getBody()
);
$this->assertContains('hi',
app('swift.transport')->messages()[2]->getBody()
);
}

public function test_locale_is_sent_with_notification_selected_locale_overriding_notifiable_preferred_locale()
{
$recipient = new NotifiableEmailLocalePreferredUser([
'email' => 'test@mail.com',
'email_locale' => 'es',
]);

$recipient->notify(
(new GreetingMailNotification())->locale('fr')
);

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_sent_with_facade_selected_locale_overriding_notifiable_preferred_locale()
{
$recipient = new NotifiableEmailLocalePreferredUser([
'email' => 'test@mail.com',
'email_locale' => 'es',
]);

NotificationFacade::locale('fr')->send(
$recipient, new GreetingMailNotification()
);

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
}
}

class NotifiableLocalizedUser extends Model
Expand All @@ -165,6 +244,21 @@ class NotifiableLocalizedUser extends Model
public $timestamps = false;
}

class NotifiableEmailLocalePreferredUser extends Model implements HasLocalePreference
{
use Notifiable;

protected $fillable = [
'email',
'email_locale',
];

public function preferredLocale()
{
return $this->email_locale;
}
}

class GreetingMailNotification extends Notification
{
public function via($notifiable)
Expand Down