Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Sep 8, 2024
1 parent bbfc8d6 commit 9f27837
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions tests/Cms/Users/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,107 @@ public static function passwordProvider(): array
];
}

/**
* @covers ::roles
*/
public function testRoles(): void
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'roles' => [
['name' => 'admin'],
['name' => 'editor'],
['name' => 'guest']
],
'users' => [
[
'email' => 'admin@getkirby.com',
'role' => 'admin'
],
[
'email' => 'editor@getkirby.com',
'role' => 'editor'
]
],
]);

// last admin has only admin role as option
$user = $app->user('admin@getkirby.com');
$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['admin'], $roles);

// normal user should not have admin as option
$user = $app->user('editor@getkirby.com');
$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['editor', 'guest'], $roles);

// only if current user is admin, normal user can also have admin option
$app->impersonate('admin@getkirby.com');
$user = $app->user('editor@getkirby.com');
$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['admin', 'editor', 'guest'], $roles);
}

/**
* @covers ::roles
*/
public function testRolesFilteredForPurpose(): void
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'blueprints' => [
'users/admin' => [
'name' => 'admin',
],
'users/editor' => [
'name' => 'editor',
],
'users/client' => [
'name' => 'client',
'options' => [
'create' => [
'editor' => false
]
]
],
'users/guest' => [
'name' => 'guest',
'options' => [
'changeRole' => [
'editor' => false
]
]
]
],
'users' => [
[
'email' => 'admin@getkirby.com',
'role' => 'admin'
],
[
'email' => 'editor@getkirby.com',
'role' => 'editor'
]
],
]);

$app->impersonate('editor@getkirby.com');
$user = $app->user('editor@getkirby.com');

$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['client', 'editor', 'guest'], $roles);

$roles = $user->roles('create')->values(fn ($role) => $role->id());
$this->assertSame(['editor', 'guest'], $roles);

$roles = $user->roles('change')->values(fn ($role) => $role->id());
$this->assertSame(['client', 'editor'], $roles);
}

public function testSecret()
{
$app = new App([
Expand Down

0 comments on commit 9f27837

Please sign in to comment.