1.20.x backdrop_web_test_case.php protected BackdropWebTestCase::backdropCreateUser(array $permissions = array())

Create a user with a given set of permissions.

Parameters

array $permissions: Array of permission names to assign to user. Note that the user always has the default permissions derived from the "authenticated users" role.

Return value

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

File

modules/simpletest/backdrop_web_test_case.php, line 1321

Class

BackdropWebTestCase
Test case for typical Backdrop tests.

Code

protected function backdropCreateUser(array $permissions = array()) {
  // Create a role with the given permission set, if any.
  $role_name = FALSE;
  if ($permissions) {
    $role_name = $this->backdropCreateRole($permissions);
    if (!$role_name) {
      return FALSE;
    }
  }

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

  $account = entity_create('user', $edit);
  $account->save();

  $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;
}