123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace Illuminate\Tests\Integration\Notifications;
- use Illuminate\Contracts\Translation\HasLocalePreference;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Foundation\Events\LocaleUpdated;
- use Illuminate\Mail\Mailable;
- use Illuminate\Notifications\Channels\MailChannel;
- use Illuminate\Notifications\Messages\MailMessage;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Notifications\Notification;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Event;
- use Illuminate\Support\Facades\Notification as NotificationFacade;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Support\Facades\View;
- use Illuminate\Testing\Assert;
- use Orchestra\Testbench\TestCase;
- class SendingNotificationsWithLocaleTest extends TestCase
- {
- public $mailer;
- protected function getEnvironmentSetUp($app)
- {
- $app['config']->set('mail.driver', 'array');
- $app['config']->set('app.locale', 'en');
- View::addLocation(__DIR__.'/Fixtures');
- app('translator')->setLoaded([
- '*' => [
- '*' => [
- 'en' => ['hi' => 'hello'],
- 'es' => ['hi' => 'hola'],
- 'fr' => ['hi' => 'bonjour'],
- ],
- ],
- ]);
- }
- protected function setUp(): void
- {
- parent::setUp();
- Schema::create('users', function (Blueprint $table) {
- $table->increments('id');
- $table->string('email');
- $table->string('name')->nullable();
- });
- }
- public function testMailIsSentWithDefaultLocale()
- {
- $user = NotifiableLocalizedUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- 'name' => 'Taylor Otwell',
- ]);
- NotificationFacade::send($user, new GreetingMailNotification);
- $this->assertStringContainsString('hello',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- }
- public function testMailIsSentWithFacadeSelectedLocale()
- {
- $user = NotifiableLocalizedUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- 'name' => 'Taylor Otwell',
- ]);
- NotificationFacade::locale('fr')->send($user, new GreetingMailNotification);
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- }
- public function testMailIsSentWithNotificationSelectedLocale()
- {
- $users = [
- NotifiableLocalizedUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- 'name' => 'Taylor Otwell',
- ]),
- NotifiableLocalizedUser::forceCreate([
- 'email' => 'mohamed@laravel.com',
- 'name' => 'Mohamed Said',
- ]),
- ];
- NotificationFacade::send($users, (new GreetingMailNotification)->locale('fr'));
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[1]->getBody()
- );
- }
- public function testMailableIsSentWithSelectedLocale()
- {
- $user = NotifiableLocalizedUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- 'name' => 'Taylor Otwell',
- ]);
- NotificationFacade::locale('fr')->send($user, new GreetingMailNotificationWithMailable);
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- }
- public function testMailIsSentWithLocaleUpdatedListenersCalled()
- {
- Carbon::setTestNow('2018-07-25');
- Event::listen(LocaleUpdated::class, function ($event) {
- Carbon::setLocale($event->locale);
- });
- $user = NotifiableLocalizedUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- 'name' => 'Taylor Otwell',
- ]);
- $user->notify((new GreetingMailNotification)->locale('fr'));
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- Assert::assertMatchesRegularExpression('/dans (1|un) jour/',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- $this->assertTrue($this->app->isLocale('en'));
- $this->assertSame('en', Carbon::getLocale());
- }
- public function testLocaleIsSentWithNotifiablePreferredLocale()
- {
- $recipient = new NotifiableEmailLocalePreferredUser([
- 'email' => 'test@mail.com',
- 'email_locale' => 'fr',
- ]);
- $recipient->notify(new GreetingMailNotification);
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- }
- public function testLocaleIsSentWithNotifiablePreferredLocaleForMultipleRecipients()
- {
- $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->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- $this->assertStringContainsString('hola',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[1]->getBody()
- );
- $this->assertStringContainsString('hello',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[2]->getBody()
- );
- }
- public function testLocaleIsSentWithNotificationSelectedLocaleOverridingNotifiablePreferredLocale()
- {
- $recipient = new NotifiableEmailLocalePreferredUser([
- 'email' => 'test@mail.com',
- 'email_locale' => 'es',
- ]);
- $recipient->notify(
- (new GreetingMailNotification)->locale('fr')
- );
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- }
- public function testLocaleIsSentWithFacadeSelectedLocaleOverridingNotifiablePreferredLocale()
- {
- $recipient = new NotifiableEmailLocalePreferredUser([
- 'email' => 'test@mail.com',
- 'email_locale' => 'es',
- ]);
- NotificationFacade::locale('fr')->send(
- $recipient, new GreetingMailNotification
- );
- $this->assertStringContainsString('bonjour',
- app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
- );
- }
- }
- class NotifiableLocalizedUser extends Model
- {
- use Notifiable;
- public $table = 'users';
- 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)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- return (new MailMessage)
- ->greeting(__('hi'))
- ->line(Carbon::tomorrow()->diffForHumans());
- }
- }
- class GreetingMailNotificationWithMailable extends Notification
- {
- public function via($notifiable)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- return new GreetingMailable;
- }
- }
- class GreetingMailable extends Mailable
- {
- public function build()
- {
- return $this->view('greeting');
- }
- }
|