Files
petriandClaude Opus 5.5 45dbb04344 Keep the chosen language across sign-in and forced sign-out
A language picked on a signed-out page is now stored on the account when
the user signs in (StoreLocaleChosenBeforeLogin), so the app does not
switch back to the account default. EnsureAccountIsUsable keeps the user's
language for the login page when it ends a disabled account's session.

ChangePreferredLocale runs in a transaction like the other use cases, the
language names come from layout.languages instead of the Locale enum, and
the translation test also compares fi.json with en.json and rejects
English files without a Finnish counterpart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 07:09:22 +02:00

116 lines
3.8 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Users\Domain\Enums\Locale;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;
uses(RefreshDatabase::class);
it('stores the language a signed-in user chooses and shows the pages in it', function () {
$user = User::factory()->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' => '[email protected]', 'locale' => 'fi']);
$this->post(route('locale.update'), ['locale' => 'en'])->assertSessionHas('locale_chosen', true);
Livewire::test('users::login')
->set('email', '[email protected]')
->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' => '[email protected]', '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', '[email protected]')
->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';
});
});