public function RoleAssignTest::testRoleAssignUserForm in Role Delegation 8
Test that we can assign roles we have access to via the user edit form.
File
- tests/
src/ Functional/ RoleAssignTest.php, line 126
Class
- RoleAssignTest
- Functional tests for assigning roles.
Namespace
Drupal\Tests\role_delegation\FunctionalCode
public function testRoleAssignUserForm() {
$user_storage = \Drupal::entityTypeManager()
->getStorage('user');
$rid1 = $this
->drupalCreateRole([]);
$current_user = $this
->drupalCreateUser([
'administer users',
'assign all roles',
]);
$this
->drupalLogin($current_user);
// Go to the users roles edit page.
$account = $this
->drupalCreateUser();
$this
->drupalGet(sprintf('/user/%s/edit', $account
->id()));
// The form element field id and name.
$field_id = sprintf('edit-role-change-%s', $rid1);
$field_name = sprintf('role_change[%s]', $rid1);
// Ensure its disabled by default.
self::assertFalse($account
->hasPermission(sprintf('assign %s role', $rid1)), 'The target user does not have the role by default.');
$this
->assertSession()
->checkboxNotChecked($field_id);
// Assign the role and ensure its now checked and assigned.
$this
->submitForm([
$field_name => $rid1,
], 'Save');
$user_storage
->resetCache();
$account = $user_storage
->load($account
->id());
self::assertTrue($account
->hasRole($rid1), 'The target user has been granted the role.');
$this
->assertSession()
->checkboxChecked($field_id);
// Revoke the role.
$this
->submitForm([
$field_name => FALSE,
], 'Save');
$user_storage
->resetCache();
$account = $user_storage
->load($account
->id());
self::assertFalse($account
->hasRole($rid1), 'The target user has gotten the role revoked.');
$this
->assertSession()
->checkboxNotChecked($field_id);
}