|
5 | 5 | use CodeZero\BrowserLocale\BrowserLocale;
|
6 | 6 | use CodeZero\Localizer\Middleware\SetLocale;
|
7 | 7 | use CodeZero\Localizer\Tests\TestCase;
|
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Foundation\Auth\User; |
8 | 10 | use Illuminate\Support\Facades\App;
|
9 | 11 | use Illuminate\Support\Facades\Config;
|
10 | 12 | use Illuminate\Support\Facades\Crypt;
|
@@ -71,6 +73,58 @@ public function you_can_configure_which_segment_to_use_as_locale()
|
71 | 73 | $this->assertEquals('nl', $response->original);
|
72 | 74 | }
|
73 | 75 |
|
| 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 | + |
74 | 128 | /** @test */
|
75 | 129 | public function it_looks_for_a_locale_in_the_session_if_not_found_in_the_url()
|
76 | 130 | {
|
|
0 commit comments