SendingNotificationsWithLocaleTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace Illuminate\Tests\Integration\Notifications;
  3. use Illuminate\Contracts\Translation\HasLocalePreference;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Foundation\Events\LocaleUpdated;
  7. use Illuminate\Mail\Mailable;
  8. use Illuminate\Notifications\Channels\MailChannel;
  9. use Illuminate\Notifications\Messages\MailMessage;
  10. use Illuminate\Notifications\Notifiable;
  11. use Illuminate\Notifications\Notification;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\Event;
  14. use Illuminate\Support\Facades\Notification as NotificationFacade;
  15. use Illuminate\Support\Facades\Schema;
  16. use Illuminate\Support\Facades\View;
  17. use Illuminate\Testing\Assert;
  18. use Orchestra\Testbench\TestCase;
  19. class SendingNotificationsWithLocaleTest extends TestCase
  20. {
  21. public $mailer;
  22. protected function getEnvironmentSetUp($app)
  23. {
  24. $app['config']->set('mail.driver', 'array');
  25. $app['config']->set('app.locale', 'en');
  26. View::addLocation(__DIR__.'/Fixtures');
  27. app('translator')->setLoaded([
  28. '*' => [
  29. '*' => [
  30. 'en' => ['hi' => 'hello'],
  31. 'es' => ['hi' => 'hola'],
  32. 'fr' => ['hi' => 'bonjour'],
  33. ],
  34. ],
  35. ]);
  36. }
  37. protected function setUp(): void
  38. {
  39. parent::setUp();
  40. Schema::create('users', function (Blueprint $table) {
  41. $table->increments('id');
  42. $table->string('email');
  43. $table->string('name')->nullable();
  44. });
  45. }
  46. public function testMailIsSentWithDefaultLocale()
  47. {
  48. $user = NotifiableLocalizedUser::forceCreate([
  49. 'email' => 'taylor@laravel.com',
  50. 'name' => 'Taylor Otwell',
  51. ]);
  52. NotificationFacade::send($user, new GreetingMailNotification);
  53. $this->assertStringContainsString('hello',
  54. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  55. );
  56. }
  57. public function testMailIsSentWithFacadeSelectedLocale()
  58. {
  59. $user = NotifiableLocalizedUser::forceCreate([
  60. 'email' => 'taylor@laravel.com',
  61. 'name' => 'Taylor Otwell',
  62. ]);
  63. NotificationFacade::locale('fr')->send($user, new GreetingMailNotification);
  64. $this->assertStringContainsString('bonjour',
  65. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  66. );
  67. }
  68. public function testMailIsSentWithNotificationSelectedLocale()
  69. {
  70. $users = [
  71. NotifiableLocalizedUser::forceCreate([
  72. 'email' => 'taylor@laravel.com',
  73. 'name' => 'Taylor Otwell',
  74. ]),
  75. NotifiableLocalizedUser::forceCreate([
  76. 'email' => 'mohamed@laravel.com',
  77. 'name' => 'Mohamed Said',
  78. ]),
  79. ];
  80. NotificationFacade::send($users, (new GreetingMailNotification)->locale('fr'));
  81. $this->assertStringContainsString('bonjour',
  82. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  83. );
  84. $this->assertStringContainsString('bonjour',
  85. app('mailer')->getSwiftMailer()->getTransport()->messages()[1]->getBody()
  86. );
  87. }
  88. public function testMailableIsSentWithSelectedLocale()
  89. {
  90. $user = NotifiableLocalizedUser::forceCreate([
  91. 'email' => 'taylor@laravel.com',
  92. 'name' => 'Taylor Otwell',
  93. ]);
  94. NotificationFacade::locale('fr')->send($user, new GreetingMailNotificationWithMailable);
  95. $this->assertStringContainsString('bonjour',
  96. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  97. );
  98. }
  99. public function testMailIsSentWithLocaleUpdatedListenersCalled()
  100. {
  101. Carbon::setTestNow('2018-07-25');
  102. Event::listen(LocaleUpdated::class, function ($event) {
  103. Carbon::setLocale($event->locale);
  104. });
  105. $user = NotifiableLocalizedUser::forceCreate([
  106. 'email' => 'taylor@laravel.com',
  107. 'name' => 'Taylor Otwell',
  108. ]);
  109. $user->notify((new GreetingMailNotification)->locale('fr'));
  110. $this->assertStringContainsString('bonjour',
  111. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  112. );
  113. Assert::assertMatchesRegularExpression('/dans (1|un) jour/',
  114. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  115. );
  116. $this->assertTrue($this->app->isLocale('en'));
  117. $this->assertSame('en', Carbon::getLocale());
  118. }
  119. public function testLocaleIsSentWithNotifiablePreferredLocale()
  120. {
  121. $recipient = new NotifiableEmailLocalePreferredUser([
  122. 'email' => 'test@mail.com',
  123. 'email_locale' => 'fr',
  124. ]);
  125. $recipient->notify(new GreetingMailNotification);
  126. $this->assertStringContainsString('bonjour',
  127. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  128. );
  129. }
  130. public function testLocaleIsSentWithNotifiablePreferredLocaleForMultipleRecipients()
  131. {
  132. $recipients = [
  133. new NotifiableEmailLocalePreferredUser([
  134. 'email' => 'test@mail.com',
  135. 'email_locale' => 'fr',
  136. ]),
  137. new NotifiableEmailLocalePreferredUser([
  138. 'email' => 'test.2@mail.com',
  139. 'email_locale' => 'es',
  140. ]),
  141. NotifiableLocalizedUser::forceCreate([
  142. 'email' => 'test.3@mail.com',
  143. ]),
  144. ];
  145. NotificationFacade::send(
  146. $recipients, new GreetingMailNotification
  147. );
  148. $this->assertStringContainsString('bonjour',
  149. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  150. );
  151. $this->assertStringContainsString('hola',
  152. app('mailer')->getSwiftMailer()->getTransport()->messages()[1]->getBody()
  153. );
  154. $this->assertStringContainsString('hello',
  155. app('mailer')->getSwiftMailer()->getTransport()->messages()[2]->getBody()
  156. );
  157. }
  158. public function testLocaleIsSentWithNotificationSelectedLocaleOverridingNotifiablePreferredLocale()
  159. {
  160. $recipient = new NotifiableEmailLocalePreferredUser([
  161. 'email' => 'test@mail.com',
  162. 'email_locale' => 'es',
  163. ]);
  164. $recipient->notify(
  165. (new GreetingMailNotification)->locale('fr')
  166. );
  167. $this->assertStringContainsString('bonjour',
  168. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  169. );
  170. }
  171. public function testLocaleIsSentWithFacadeSelectedLocaleOverridingNotifiablePreferredLocale()
  172. {
  173. $recipient = new NotifiableEmailLocalePreferredUser([
  174. 'email' => 'test@mail.com',
  175. 'email_locale' => 'es',
  176. ]);
  177. NotificationFacade::locale('fr')->send(
  178. $recipient, new GreetingMailNotification
  179. );
  180. $this->assertStringContainsString('bonjour',
  181. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  182. );
  183. }
  184. }
  185. class NotifiableLocalizedUser extends Model
  186. {
  187. use Notifiable;
  188. public $table = 'users';
  189. public $timestamps = false;
  190. }
  191. class NotifiableEmailLocalePreferredUser extends Model implements HasLocalePreference
  192. {
  193. use Notifiable;
  194. protected $fillable = [
  195. 'email',
  196. 'email_locale',
  197. ];
  198. public function preferredLocale()
  199. {
  200. return $this->email_locale;
  201. }
  202. }
  203. class GreetingMailNotification extends Notification
  204. {
  205. public function via($notifiable)
  206. {
  207. return [MailChannel::class];
  208. }
  209. public function toMail($notifiable)
  210. {
  211. return (new MailMessage)
  212. ->greeting(__('hi'))
  213. ->line(Carbon::tomorrow()->diffForHumans());
  214. }
  215. }
  216. class GreetingMailNotificationWithMailable extends Notification
  217. {
  218. public function via($notifiable)
  219. {
  220. return [MailChannel::class];
  221. }
  222. public function toMail($notifiable)
  223. {
  224. return new GreetingMailable;
  225. }
  226. }
  227. class GreetingMailable extends Mailable
  228. {
  229. public function build()
  230. {
  231. return $this->view('greeting');
  232. }
  233. }