-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notifiables can set preferred locale
- Loading branch information
Showing
2 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
/** | ||
|
@@ -45,6 +46,7 @@ protected function getEnvironmentSetUp($app) | |
'*' => [ | ||
'*' => [ | ||
'en' => ['hi' => 'hello'], | ||
'es' => ['hi' => 'hola'], | ||
'fr' => ['hi' => 'bonjour'], | ||
], | ||
], | ||
|
@@ -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' => '[email protected]', | ||
'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' => '[email protected]', | ||
'email_locale' => 'fr', | ||
]), | ||
new NotifiableEmailLocalePreferredUser([ | ||
'email' => '[email protected]', | ||
'email_locale' => 'es', | ||
]), | ||
NotifiableLocalizedUser::forceCreate([ | ||
'email' => '[email protected]', | ||
]), | ||
]; | ||
|
||
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' => '[email protected]', | ||
'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' => '[email protected]', | ||
'email_locale' => 'es', | ||
]); | ||
|
||
NotificationFacade::locale('fr')->send( | ||
$recipient, new GreetingMailNotification() | ||
); | ||
|
||
$this->assertContains('bonjour', | ||
app('swift.transport')->messages()[0]->getBody() | ||
); | ||
} | ||
} | ||
|
||
class NotifiableLocalizedUser extends Model | ||
|
@@ -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) | ||
|