SendingMailWithLocaleTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Illuminate\Tests\Integration\Mail;
  3. use Illuminate\Contracts\Translation\HasLocalePreference;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Foundation\Events\LocaleUpdated;
  6. use Illuminate\Mail\Mailable;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\Event;
  9. use Illuminate\Support\Facades\Mail;
  10. use Illuminate\Support\Facades\View;
  11. use Illuminate\Testing\Assert;
  12. use Orchestra\Testbench\TestCase;
  13. class SendingMailWithLocaleTest extends TestCase
  14. {
  15. protected function getEnvironmentSetUp($app)
  16. {
  17. $app['config']->set('mail.driver', 'array');
  18. $app['config']->set('app.locale', 'en');
  19. View::addLocation(__DIR__.'/Fixtures');
  20. app('translator')->setLoaded([
  21. '*' => [
  22. '*' => [
  23. 'en' => ['nom' => 'name'],
  24. 'ar' => ['nom' => 'esm'],
  25. 'es' => ['nom' => 'nombre'],
  26. ],
  27. ],
  28. ]);
  29. }
  30. public function testMailIsSentWithDefaultLocale()
  31. {
  32. Mail::to('test@mail.com')->send(new TestMail);
  33. $this->assertStringContainsString('name',
  34. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  35. );
  36. }
  37. public function testMailIsSentWithSelectedLocale()
  38. {
  39. Mail::to('test@mail.com')->locale('ar')->send(new TestMail);
  40. $this->assertStringContainsString('esm',
  41. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  42. );
  43. }
  44. public function testMailIsSentWithLocaleFromMailable()
  45. {
  46. $mailable = new TestMail;
  47. $mailable->locale('ar');
  48. Mail::to('test@mail.com')->send($mailable);
  49. $this->assertStringContainsString('esm',
  50. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  51. );
  52. }
  53. public function testMailIsSentWithLocaleUpdatedListenersCalled()
  54. {
  55. Carbon::setTestNow('2018-04-01');
  56. Event::listen(LocaleUpdated::class, function ($event) {
  57. Carbon::setLocale($event->locale);
  58. });
  59. Mail::to('test@mail.com')->locale('es')->send(new TimestampTestMail);
  60. Assert::assertMatchesRegularExpression('/nombre (en|dentro de) (un|1) día/',
  61. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  62. );
  63. $this->assertSame('en', Carbon::getLocale());
  64. }
  65. public function testLocaleIsSentWithModelPreferredLocale()
  66. {
  67. $recipient = new TestEmailLocaleUser([
  68. 'email' => 'test@mail.com',
  69. 'email_locale' => 'ar',
  70. ]);
  71. Mail::to($recipient)->send(new TestMail);
  72. $this->assertStringContainsString('esm',
  73. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  74. );
  75. }
  76. public function testLocaleIsSentWithSelectedLocaleOverridingModelPreferredLocale()
  77. {
  78. $recipient = new TestEmailLocaleUser([
  79. 'email' => 'test@mail.com',
  80. 'email_locale' => 'en',
  81. ]);
  82. Mail::to($recipient)->locale('ar')->send(new TestMail);
  83. $this->assertStringContainsString('esm',
  84. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  85. );
  86. }
  87. public function testLocaleIsSentWithModelPreferredLocaleWillIgnorePreferredLocaleOfTheCcRecipient()
  88. {
  89. $toRecipient = new TestEmailLocaleUser([
  90. 'email' => 'test@mail.com',
  91. 'email_locale' => 'ar',
  92. ]);
  93. $ccRecipient = new TestEmailLocaleUser([
  94. 'email' => 'test.cc@mail.com',
  95. 'email_locale' => 'en',
  96. ]);
  97. Mail::to($toRecipient)->cc($ccRecipient)->send(new TestMail);
  98. $this->assertStringContainsString('esm',
  99. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  100. );
  101. }
  102. public function testLocaleIsNotSentWithModelPreferredLocaleWhenThereAreMultipleRecipients()
  103. {
  104. $recipients = [
  105. new TestEmailLocaleUser([
  106. 'email' => 'test@mail.com',
  107. 'email_locale' => 'ar',
  108. ]),
  109. new TestEmailLocaleUser([
  110. 'email' => 'test.2@mail.com',
  111. 'email_locale' => 'ar',
  112. ]),
  113. ];
  114. Mail::to($recipients)->send(new TestMail);
  115. $this->assertStringContainsString('name',
  116. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  117. );
  118. }
  119. public function testLocaleIsSetBackToDefaultAfterMailSent()
  120. {
  121. Mail::to('test@mail.com')->locale('ar')->send(new TestMail);
  122. Mail::to('test@mail.com')->send(new TestMail);
  123. $this->assertSame('en', app('translator')->getLocale());
  124. $this->assertStringContainsString('esm',
  125. app('mailer')->getSwiftMailer()->getTransport()->messages()[0]->getBody()
  126. );
  127. $this->assertStringContainsString('name',
  128. app('mailer')->getSwiftMailer()->getTransport()->messages()[1]->getBody()
  129. );
  130. }
  131. }
  132. class TestMail extends Mailable
  133. {
  134. /**
  135. * Build the message.
  136. *
  137. * @return $this
  138. */
  139. public function build()
  140. {
  141. return $this->view('view');
  142. }
  143. }
  144. class TestEmailLocaleUser extends Model implements HasLocalePreference
  145. {
  146. protected $fillable = [
  147. 'email',
  148. 'email_locale',
  149. ];
  150. public function preferredLocale()
  151. {
  152. return $this->email_locale;
  153. }
  154. }
  155. class TimestampTestMail extends Mailable
  156. {
  157. /**
  158. * Build the message.
  159. *
  160. * @return $this
  161. */
  162. public function build()
  163. {
  164. return $this->view('timestamp');
  165. }
  166. }