Skip to content

Commit ba43d7a

Browse files
authored
Add tests for UserDetector (#9)
1 parent 8b320e4 commit ba43d7a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/Feature/SetLocaleTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use CodeZero\BrowserLocale\BrowserLocale;
66
use CodeZero\Localizer\Middleware\SetLocale;
77
use CodeZero\Localizer\Tests\TestCase;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Foundation\Auth\User;
810
use Illuminate\Support\Facades\App;
911
use Illuminate\Support\Facades\Config;
1012
use Illuminate\Support\Facades\Crypt;
@@ -71,6 +73,58 @@ public function you_can_configure_which_segment_to_use_as_locale()
7173
$this->assertEquals('nl', $response->original);
7274
}
7375

76+
/** @test */
77+
public function it_looks_for_a_locale_on_the_authenticated_user_if_not_found_in_the_url()
78+
{
79+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
80+
$this->setSessionLocale('fr');
81+
$this->setBrowserLocales('it');
82+
$this->setAppLocale('en');
83+
$cookie = 'de';
84+
85+
$attribute = Config::get('localizer.user-attribute');
86+
$user = new User();
87+
$user->$attribute = 'nl';
88+
89+
Route::get('some/route', function () {
90+
return App::getLocale();
91+
})->middleware(['web', SetLocale::class]);
92+
93+
$response = $this->actingAs($user)->getWithCookie('some/route', $cookie);
94+
95+
$response->assertSessionHas($this->sessionKey, 'nl');
96+
$response->assertCookie($this->cookieName, 'nl');
97+
$this->assertEquals('nl', $response->original);
98+
}
99+
100+
/** @test */
101+
public function it_will_bypass_missing_attribute_exception_if_the_locale_attribute_is_missing_on_the_user_model()
102+
{
103+
if (version_compare(App::version(), '9.35.0') === -1) {
104+
$this->markTestSkipped('This test only applies to Laravel 9 and higher.');
105+
}
106+
107+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
108+
$this->setSessionLocale('fr');
109+
$this->setBrowserLocales('it');
110+
$this->setAppLocale('en');
111+
$cookie = 'de';
112+
113+
$user = new User();
114+
$user->exists = true;
115+
Model::preventAccessingMissingAttributes();
116+
117+
Route::get('some/route', function () {
118+
return App::getLocale();
119+
})->middleware(['web', SetLocale::class]);
120+
121+
$response = $this->actingAs($user)->getWithCookie('some/route', $cookie);
122+
123+
$response->assertSessionHas($this->sessionKey, 'fr');
124+
$response->assertCookie($this->cookieName, 'fr');
125+
$this->assertEquals('fr', $response->original);
126+
}
127+
74128
/** @test */
75129
public function it_looks_for_a_locale_in_the_session_if_not_found_in_the_url()
76130
{

0 commit comments

Comments
 (0)