public function RoleAssignPermissionTest::testRoleAssignRestrictedUser in RoleAssign 8
Tests that a restricted user can only (un)assign configured roles.
File
- tests/
src/ Functional/ RoleAssignPermissionTest.php, line 92
Class
- RoleAssignPermissionTest
- Tests that users can (un)assign roles based on the RoleAssign settings.
Namespace
Drupal\Tests\roleassign\FunctionalCode
public function testRoleAssignRestrictedUser() {
// Login as restricted user to test RoleAssign.
$this
->drupalLogin($this->restrictedUser);
// Load account edit page.
$this
->drupalGet('user/' . $this->testAccount
->id() . '/edit');
// Check that only assignable roles are displayed.
$this
->assertText(t('Assignable roles'));
$this
->assertNoFieldChecked('edit-roles-editor');
$this
->assertNoFieldChecked('edit-roles-webmaster');
$this
->assertNoField('edit-roles-siteadmin');
// Assign the role "editor" to the account.
$this
->drupalPostForm('user/' . $this->testAccount
->id() . '/edit', [
"roles[editor]" => "editor",
], t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertFieldChecked('edit-roles-editor', 'Role editor is assigned.');
$this
->assertNoFieldChecked('edit-roles-webmaster');
$this
->assertNoField('edit-roles-siteadmin');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, 'editor');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, RoleInterface::AUTHENTICATED_ID);
// Remove the role "editor" from the account.
$this
->drupalPostForm('user/' . $this->testAccount
->id() . '/edit', [
"roles[editor]" => FALSE,
], t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertNoFieldChecked('edit-roles-editor', 'Role editor is removed.');
$this
->assertNoFieldChecked('edit-roles-webmaster');
$this
->assertNoField('edit-roles-siteadmin');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, 'editor', FALSE);
$this
->userLoadAndCheckRoleAssigned($this->testAccount, RoleInterface::AUTHENTICATED_ID);
// Try to assign a restricted role programmatically to a new user.
$values = [
'name' => $this
->randomString(),
'roles' => [
'editor',
'siteadmin',
],
];
$code_account = User::create($values);
$code_account
->save();
// Check that user only gets editor role, but not siteadmin role.
$this
->assertTrue($code_account
->hasRole('editor'));
$this
->assertFalse($code_account
->hasRole('siteadmin'));
}