1 user.test UserRolesAssignmentTestCase::testCreateUserWithRole()

Tests that when creating a user the role can be assigned. And that it can be removed again.

File

core/modules/user/tests/user.test, line 2697
Tests for user.module.

Class

UserRolesAssignmentTestCase
Test role assignment.

Code

function testCreateUserWithRole() {
  $role_name = $this->backdropCreateRole(array('administer content types'));
  // Create a new user and add the role at the same time.
  $edit = array(
    'name' => $this->randomName(),
    'mail' => $this->randomName() . '@example.com',
    'pass' => $pass = $this->randomString(),
    'notify' => FALSE,
    "roles[$role_name]" => $role_name,
  );
  $this->backdropPost('admin/people/create', $edit, t('Create new account'));
  $this->assertText(t('Created a new user account for !name.', array('!name' => $edit['name'])));
  // Get the newly added user.
  $account = user_load_by_name($edit['name']);

  $this->backdropGet('user/' . $account->uid . '/edit');
  $this->assertFieldChecked('edit-roles-' . strtolower($role_name), 'Role is assigned.');
  $this->userLoadAndCheckRoleAssigned($account, $role_name);

  // Remove the role again.
  $this->backdropPost('user/' . $account->uid . '/edit', array("roles[$role_name]" => FALSE), t('Save'));
  $this->assertText(t('The changes have been saved.'));
  $this->backdropGet('user/' . $account->uid . '/edit');
  $this->assertNoFieldChecked('edit-roles-' . strtolower($role_name), 'Role is removed from user.');
  $this->userLoadAndCheckRoleAssigned($account, $role_name, FALSE);
}