create(['name' => 'Eero Esimerkki']); $this->actingAs($user) ->from(route('home')) ->post(route('locale.update'), ['locale' => 'en']) ->assertRedirect(route('home')); expect($user->fresh()->locale)->toBe(Locale::English); // A new request loads the user again from the database. $this->actingAs($user->fresh()) ->get(route('home')) ->assertOk() ->assertSee('lang="en"', false) ->assertSee('Sign out'); }); it('keeps the language chosen on the signed-out pages in the session', function () { $this->from(route('login')) ->post(route('locale.update'), ['locale' => 'en']) ->assertRedirect(route('login')) ->assertSessionHas('locale', 'en'); $this->get(route('login')) ->assertOk() ->assertSee('lang="en"', false); }); it('uses the account language over the one in the session', function () { $user = User::factory()->create(['locale' => 'fi']); $this->actingAs($user) ->withSession(['locale' => 'en']) ->get(route('home')) ->assertSee('lang="fi"', false) ->assertSee('Kirjaudu ulos'); }); it('keeps the language on the login page after logging out', function () { $user = User::factory()->create(['locale' => 'en']); $this->actingAs($user)->post(route('logout'))->assertSessionHas('locale', 'en'); }); it('stores the language chosen on the login page on the account when signing in', function () { $user = User::factory()->create(['email' => 'eero@example.test', 'locale' => 'fi']); $this->post(route('locale.update'), ['locale' => 'en'])->assertSessionHas('locale_chosen', true); Livewire::test('users::login') ->set('email', 'eero@example.test') ->set('password', 'password') ->call('login') ->assertHasNoErrors(); expect($user->fresh()->locale)->toBe(Locale::English); }); it('keeps the account language when the language was not chosen before signing in', function () { $user = User::factory()->create(['email' => 'eero@example.test', 'locale' => 'en']); // The login page after logging out of a Finnish account, without a choice of its own. $this->withSession(['locale' => 'fi']); Livewire::test('users::login') ->set('email', 'eero@example.test') ->set('password', 'password') ->call('login') ->assertHasNoErrors(); expect($user->fresh()->locale)->toBe(Locale::English); }); it('keeps the language on the login page when a disabled account is signed out', function () { $user = User::factory()->create(['locale' => 'en']); $this->actingAs($user); $user->update(['is_active' => false]); $this->get(route('home')) ->assertRedirect(route('login', ['access' => 'ended'])) ->assertSessionHas('locale', 'en'); }); it('rejects an unknown language', function () { $user = User::factory()->create(); $this->actingAs($user) ->post(route('locale.update'), ['locale' => 'sv']) ->assertSessionHasErrors('locale'); expect($user->fresh()->locale)->toBe(Locale::Finnish); }); it('sends the set-password email in the language of the user', function () { Notification::fake(); $user = User::factory()->create(['locale' => 'en']); $user->notify(new ResetPassword('token')); Notification::assertSentTo($user, ResetPassword::class, function (ResetPassword $notification, array $channels, User $notifiable, ?string $locale) { return $locale === 'en'; }); });