Skip to content

Commit

Permalink
Mailable recipient can set email locale
Browse files Browse the repository at this point in the history
  • Loading branch information
derekmd committed Feb 22, 2018
1 parent a432d9e commit 3c48884
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
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
108 changes: 108 additions & 0 deletions tests/Integration/Mail/SendingMailWithLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Contracts\Translation\HasLocalePreference;

/**
* @group integration
Expand Down Expand Up @@ -63,6 +66,98 @@ public function test_mail_is_sent_with_selected_locale()
);
}

public function test_locale_is_sent_with_model_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_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_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_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_sent_for_multiple_recipients_when_has_emaile_locale_contract_is_given()
{
$recipients = (new class extends Collection implements HasLocalePreference {
public function preferredLocale()
{
return 'ar';
}
})::make([
new TestEmailLocaleUser([
'email' => 'test@mail.com',
'email_locale' => 'es',
]),
new TestEmailLocaleUser([
'email' => 'test.2@mail.com',
'email_locale' => 'en',
]),
]);

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

$this->assertContains('esm',
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 @@ -92,3 +187,16 @@ public function build()
return $this->view('view');
}
}

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

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

0 comments on commit 3c48884

Please sign in to comment.