UserRolesAssignmentTestCase::testCreateUserWithRole

7 user.test UserRolesAssignmentTestCase::testCreateUserWithRole()
8 user.test UserRolesAssignmentTestCase::testCreateUserWithRole()

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

File

modules/user/user.test, line 2093
Tests for user.module.

Code

function testCreateUserWithRole() {
  $rid = $this->drupalCreateRole(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[pass1]' => $pass = $this->randomString(), 
    'pass[pass2]' => $pass, 
    "roles[$rid]" => $rid,
  );
  $this->drupalPost('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->drupalGet('user/' . $account->uid . '/edit');
  $this->assertFieldChecked('edit-roles-' . $rid, t('Role is assigned.'));
  $this->userLoadAndCheckRoleAssigned($account, $rid);

  // Remove the role again.
  $this->drupalPost('user/' . $account->uid . '/edit', array("roles[$rid]" => FALSE), t('Save'));
  $this->assertText(t('The changes have been saved.'));
  $this->assertNoFieldChecked('edit-roles-' . $rid, t('Role is removed from user.'));
  $this->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
}
Login or register to post comments