You are here

function UserRolesAssignmentTestCase::testCreateUserWithRole in Drupal 7

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

File

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

Class

UserRolesAssignmentTestCase
Test role assignment.

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, '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, 'Role is removed from user.');
  $this
    ->userLoadAndCheckRoleAssigned($account, $rid, FALSE);
}