File: D:/HostingSpaces/slenders/slenders.nl/tests/Browser/AuthorisationTest.php
<?php declare(strict_types=1);
namespace Tests\Browser;
use App\Komma\Kms\QualityAssurance\ExtraDuskTools;
use App\Komma\Users\Models\KmsUserRole;
use App\Komma\Users\Models\KmsUser;
use Laravel\Dusk\Browser;
use Tests\Browser\Pages\KmsUsersSectionTestPage;
use Tests\DuskTestCase;
class AuthorisationTest extends DuskTestCase
{
/**
* @test
* @group Authorisation
* @throws \Throwable
* @see ExtraDuskTools
*/
public function testUserSectionAuthorisationForAdmins()
{
$newUser = factory(KmsUser::class)->make();
$adminUser = KmsUser::where('email', '=',\UserTableSeeder::getAdminDefaultCredentials()['email'])->first();
//Get the roles that should be available for admins, and roles that should not
[$availableAdminRoles, $unavailableAdminRoles] = collect(KmsUserRole::getAsArray())->partition(function(int $role) use ($adminUser) {
return $adminUser->isAtLeast($role);
});
//Test that the Admin user can create another user.
$this->browse(function (Browser $browser) use($adminUser, $newUser, $availableAdminRoles, $unavailableAdminRoles) {
$browser->loginAs($adminUser, 'kms')
->visit(new KmsUsersSectionTestPage()) //When the user visits this page it means that is Authorized to view it. See App\Komma\Base\Policy::index(). Triggered by the controller's index method.
->assertVisible('@add_button') //When the users sees this button it means that he is Authorized to see it. See App\Komma\Base\Policy::index(). Triggered by the call in entities/index.blade.php
->click('@add_button')
->assertVisible('@save_button')
->pause(1000)
->scrollToElement('[dusk="Select-role"]') //See extraDuskTools
->pause(500)
//Validate that the user can select the correct roles
->click('@Select-role');
//Check that all the available admin roles are visible. The ones that the user is Authorized to see.
foreach ($availableAdminRoles as $availableAdminRole)
{
$browser->assertSee(__('kms/roles.'.$availableAdminRole));
}
//Check that the unavailable roles are not visible. The ones that the user is Not Authorized to see.
foreach ($unavailableAdminRoles as $unavailableAdminRole)
{
/** @var KmsUserRole $unavailableAdminRole **/
$browser->assertDontSee(__('kms/roles.'.$unavailableAdminRole));
}
$browser->assertSeeIn('@Select-role-2', __('kms/roles.'.$adminUser->role))
->click('@Select-role-2')
->type('@TextField-username', $newUser->username)
->type('@TextField-first_name', $newUser->first_name)
->type('@TextField-last_name', $newUser->last_name)
->type('@TextField-email', $newUser->email)
->type('@Password-password-1', 'Test123')
->type('@Password-password-2', 'Test123')
->click('@save_button')
->assertSee(__('kms/global.saved'));
$newUser = KmsUser::where('email', '=', $newUser->email)->first();
$this->assertEquals(KmsUserRole::Admin, $newUser->role); //The new user must be an admin at this point
$browser->assertSee(__('kms/kms_users.section.subtitle'))
->type('@entity_search_input', $newUser->first_name.' '.$newUser->last_name)
->assertSeeIn('@search-result-counter', '1'); //The adminUser which we used to login must now see the just created other admin user. Because the list must show users that have the same role level or lower.
});
}
}