Skip navigation
Help

DrupalWebTestCase::drupalCreateUser

7 drupal_web_test_case.php protected DrupalWebTestCase::drupalCreateUser($permissions = array('access comments', 'access content', 'post comments', 'skip comment approval'))

Create a user with a given set of permissions. The permissions correspond to the names given on the privileges page.

Parameters

$permissions: Array of permission names to assign to user.

Return value

A fully loaded user object with pass_raw property, or FALSE if account creation fails.

256 calls to DrupalWebTestCase::drupalCreateUser()

File

drupal/modules/simpletest/drupal_web_test_case.php, line 1057

Code

protected function drupalCreateUser($permissions = array('access comments', 'access content', 'post comments', 'skip comment approval')) {
  // Create a role with the given permission set.
  if (!($rid = $this->drupalCreateRole($permissions))) {
    return FALSE;
  }

  // Create a user assigned to that role.
  $edit = array();
  $edit['name']   = $this->randomName();
  $edit['mail']   = $edit['name'] . '@example.com';
  $edit['roles']  = array($rid => $rid);
  $edit['pass']   = user_password();
  $edit['status'] = 1;

  $account = user_save(drupal_anonymous_user(), $edit);

  $this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
  if (empty($account->uid)) {
    return FALSE;
  }

  // Add the raw password so that we can log in as this user.
  $account->pass_raw = $edit['pass'];
  return $account;
}