You are here

protected function BrowserTestBase::drupalCreateUser in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/BrowserTestBase.php \Drupal\simpletest\BrowserTestBase::drupalCreateUser()

Creates a user with a given set of permissions.

Parameters

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

string $name: (optional) The user name.

Return value

\Drupal\user\Entity\User|false A fully loaded user object with passRaw property, or FALSE if account creation fails.

1 call to BrowserTestBase::drupalCreateUser()
BrowserTestBaseTest::testGoTo in core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php
Tests basic page test.

File

core/modules/simpletest/src/BrowserTestBase.php, line 541
Contains \Drupal\simpletest\BrowserTestBase.

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\simpletest

Code

protected function drupalCreateUser(array $permissions = array(), $name = NULL) {

  // Create a role with the given permission set, if any.
  $rid = FALSE;
  if ($permissions) {
    $rid = $this
      ->drupalCreateRole($permissions);
    if (!$rid) {
      return FALSE;
    }
  }

  // Create a user assigned to that role.
  $edit = array();
  $edit['name'] = !empty($name) ? $name : $this
    ->randomMachineName();
  $edit['mail'] = $edit['name'] . '@example.com';
  $edit['pass'] = user_password();
  $edit['status'] = 1;
  if ($rid) {
    $edit['roles'] = array(
      $rid,
    );
  }
  $account = entity_create('user', $edit);
  $account
    ->save();
  $this
    ->assertNotNull($account
    ->id(), SafeMarkup::format('User created with name %name and pass %pass', array(
    '%name' => $edit['name'],
    '%pass' => $edit['pass'],
  )));
  if (!$account
    ->id()) {
    return FALSE;
  }

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