Files
Toiminnanohjaus/tests/Browser/UsersAccessibilityTest.php
T
petriandClaude Opus 5.5 246e806c1c Add structured specification and show set-password links to admins
The customer answered the open questions in määrittely.txt. Collect the
answers into docs/maarittely.md: roles and permission table, money
visibility, customer job fields and enum keys, Kanban, tasks, time
entries, expenses, pricing, Fennoa billing export, data model, revised
phases with phase 1 in detail, and remaining open questions. CLAUDE.md
now points to it.

Apply the answers that change the Users module:
- Passwords need 12+ characters with mixed case, a number and a symbol,
  with a Finnish message for each rule.
- When an admin creates a user or asks for a new link, the set-password
  link is emailed and also shown to the admin to copy, even if the email
  fails. The link lives only in the page state. users:create-admin
  prints it too. Self-service "forgot password" stays as it was.
- The copy field uses x-input + x-button with a status message, because
  x-clipboard has an unlabelled input and a changing button name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 17:22:43 +02:00

121 lines
4.6 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Pest\Browser\Api\AwaitableWebpage;
use Pest\Browser\Api\PendingAwaitablePage;
uses(RefreshDatabase::class);
$devicesAndSchemes = [
'desktop, light' => ['desktop', 'light'],
'desktop, dark' => ['desktop', 'dark'],
'mobile, light' => ['mobile', 'light'],
'mobile, dark' => ['mobile', 'dark'],
];
/**
* @param PendingAwaitablePage|AwaitableWebpage $page
*/
function onDeviceAndScheme(mixed $page, string $device, string $colorScheme): mixed
{
$page = $device === 'mobile' ? $page->on()->mobile() : $page->on()->desktop();
return $colorScheme === 'dark' ? $page->inDarkMode() : $page->inLightMode();
}
it('has no accessibility issues on the guest pages', function (string $routeName, string $device, string $colorScheme) {
$url = $routeName === 'password.reset'
? route($routeName, ['token' => 'token', 'email' => '[email protected]'])
: route($routeName);
onDeviceAndScheme(visit($url), $device, $colorScheme)
->assertNoJavaScriptErrors()
->assertNoAccessibilityIssues(level: 2);
})->with([
'login' => 'login',
'forgot password' => 'password.request',
'reset password' => 'password.reset',
])->with($devicesAndSchemes);
it('has no accessibility issues on the user admin pages', function (string $page, string $device, string $colorScheme) {
$admin = User::factory()->admin()->create();
$intern = User::factory()->intern('2027-05-31')->create();
$this->actingAs($admin);
$url = match ($page) {
'list' => route('users.index'),
'create' => route('users.create'),
'edit intern' => route('users.edit', $intern->id),
};
onDeviceAndScheme(visit($url), $device, $colorScheme)
->assertNoJavaScriptErrors()
->assertNoAccessibilityIssues(level: 2);
})->with(['list', 'create', 'edit intern'])->with($devicesAndSchemes);
it('shows a new set-password link to copy without accessibility issues', function (string $device, string $colorScheme) {
$this->actingAs(User::factory()->admin()->create());
$user = User::factory()->create();
onDeviceAndScheme(visit(route('users.edit', $user->id)), $device, $colorScheme)
->press('Luo salasanan asetuslinkki')
->assertSee('Uusi linkki luotu')
->assertSourceHas('/reset-password/')
->assertNoJavaScriptErrors()
->assertNoAccessibilityIssues(level: 2);
})->with($devicesAndSchemes);
it('shows a login error without accessibility issues', function (string $colorScheme) {
$page = onDeviceAndScheme(visit(route('login')), 'desktop', $colorScheme);
$page->fill('[wire\\:model="email"]', '[email protected]')
->fill('[wire\\:model="password"]', 'väärä-salasana')
->press('Kirjaudu')
->assertSee('Kirjautuminen ei onnistunut.')
->assertNoAccessibilityIssues(level: 2);
})->with(['light', 'dark']);
it('sends a user who is deactivated mid-session to the login page with the reason', function () {
$user = User::factory()->create();
$this->actingAs($user);
// visit() is lazy: load the form before the account is deactivated.
$page = visit(route('customers.create'))->assertSee('Uusi asiakas');
$user->update(['is_active' => false]);
$page->fill('[wire\\:model="form.name"]', 'Mainos Oy')
->press('Tallenna')
->assertPathIs('/login')
->assertSee('Käyttöoikeutesi on päättynyt. Ota yhteyttä pääkäyttäjään.')
->assertNoAccessibilityIssues(level: 2);
});
it('shows and hides the password with a named toggle button', function () {
$toggle = '[aria-label="Näytä salasana"]';
visit(route('login'))
->assertAttribute('[wire\\:model="password"]', 'type', 'password')
->assertAttribute($toggle, 'aria-pressed', 'false')
->click($toggle)
->assertAttribute('[wire\\:model="password"]', 'type', 'text')
->assertAttribute($toggle, 'aria-pressed', 'true')
->assertAttribute($toggle, 'aria-label', 'Näytä salasana');
});
it('logs in with the keyboard alone', function () {
User::factory()->create(['email' => '[email protected]']);
visit(route('login'))
->keys('html > body', 'Tab')
->keys('a[href="#main-content"]', 'Enter')
->keys('#main-content', 'Tab')
->assertScript('document.activeElement.getAttribute("wire:model")', 'email')
->type('[wire\\:model="email"]', '[email protected]')
->type('[wire\\:model="password"]', 'password')
->keys('[wire\\:model="password"]', 'Enter')
->assertPathIs('/')
->assertSee('Kirjaudu ulos');
});