103 lines
3.4 KiB
PHP
103 lines
3.4 KiB
PHP
<?php
|
|
|
|
use App\Models\Permission;
|
|
use App\Models\Role;
|
|
|
|
beforeEach(function () {
|
|
$this->artisan('db:seed');
|
|
});
|
|
|
|
it('lists the permission matrix', function () {
|
|
browserLogin();
|
|
|
|
visit('/permissions')
|
|
->assertNoJavaScriptErrors()
|
|
->assertSee('Permissions')
|
|
->assertSee('Add Permission')
|
|
->assertSee('Save Permissions')
|
|
// A column header per role, and a row per permission.
|
|
->assertSee('Admin')
|
|
->assertSee('Manage Users')
|
|
->assertSee('Manage Roles');
|
|
});
|
|
|
|
it('creates a permission', function () {
|
|
browserLogin();
|
|
|
|
visit('/permissions/create')
|
|
->assertSee('Create New Permission')
|
|
->assertSee('Permission Details')
|
|
->fill('#name', 'reports.view')
|
|
->fill('#display_name', 'View Reports')
|
|
->fill('#description', 'Grants access to the reports.')
|
|
->click('Create Permission')
|
|
->assertSee('Permission created successfully.');
|
|
|
|
expect(Permission::where('name', 'reports.view')->exists())->toBeTrue();
|
|
});
|
|
|
|
it('reflects the permissions a role already holds', function () {
|
|
browserLogin();
|
|
|
|
$role = Role::factory()->create(['display_name' => 'Auditor']);
|
|
$permission = Permission::factory()->create(['display_name' => 'Audit Everything']);
|
|
$role->permissions()->attach($permission->id);
|
|
|
|
// The checkbox is a Reka CheckboxRoot, which renders a button carrying
|
|
// aria-checked rather than a native input.
|
|
visit('/permissions')
|
|
->assertSee('Audit Everything')
|
|
->assertAttribute("#cb-{$role->id}-{$permission->id}", 'aria-checked', 'true');
|
|
});
|
|
|
|
it('centres each checkbox in its column', function () {
|
|
browserLogin();
|
|
|
|
$role = Role::factory()->create(['display_name' => 'Auditor']);
|
|
$permission = Permission::factory()->create(['display_name' => 'Audit Everything']);
|
|
|
|
// Checkbox renders a flex box, which the cell's text-center cannot reach,
|
|
// so the centring is measured rather than assumed.
|
|
$centred = <<<JS
|
|
function () {
|
|
const box = document.querySelector('#cb-{$role->id}-{$permission->id}');
|
|
const cell = box.closest('td').getBoundingClientRect();
|
|
const self = box.getBoundingClientRect();
|
|
|
|
return Math.abs((self.left + self.right) / 2 - (cell.left + cell.right) / 2) < 1;
|
|
}
|
|
JS;
|
|
|
|
visit('/permissions')
|
|
->assertSee('Audit Everything')
|
|
->assertScript($centred);
|
|
});
|
|
|
|
it('saves a change to the matrix', function () {
|
|
browserLogin();
|
|
|
|
$role = Role::factory()->create(['display_name' => 'Auditor']);
|
|
$permission = Permission::factory()->create(['display_name' => 'Audit Everything']);
|
|
|
|
visit('/permissions')
|
|
->assertAttribute("#cb-{$role->id}-{$permission->id}", 'aria-checked', 'false')
|
|
->click("#cb-{$role->id}-{$permission->id}")
|
|
->click('Save Permissions')
|
|
->assertSee('Permissions saved successfully.');
|
|
|
|
expect($role->permissions()->pluck('id')->all())->toContain($permission->id);
|
|
});
|
|
|
|
it('asks for confirmation before deleting a permission', function () {
|
|
browserLogin();
|
|
|
|
Permission::factory()->create(['display_name' => 'Throwaway Permission']);
|
|
|
|
// The seeded permissions are not removable, so only this one renders the
|
|
// delete control.
|
|
visit('/permissions')
|
|
->assertSee('Throwaway Permission')
|
|
->click('[aria-label="Delete Permission"]')
|
|
->assertSee('Are you sure that you want to delete this permission?');
|
|
});
|