You are here

protected function UserCreationTrait::createUser in Zircon Profile 8

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

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.

string $name: The user name.

bool $admin: (optional) Whether the user should be an administrator with all the available permissions.

Return value

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

602 calls to UserCreationTrait::createUser()
AccessDeniedTest::setUp in core/modules/system/src/Tests/System/AccessDeniedTest.php
Sets up a Drupal site for running functional and integration tests.
AccessTest::setUp in core/modules/views/src/Tests/Plugin/AccessTest.php
Sets up a Drupal site for running functional and integration tests.
AccessTestBase::setUp in core/modules/user/src/Tests/Views/AccessTestBase.php
Sets up a Drupal site for running functional and integration tests.
ActionUninstallTest::testActionUninstall in core/modules/action/src/Tests/ActionUninstallTest.php
Tests Action uninstall.
AdminPathEntityConverterLanguageTest::setUp in core/modules/language/src/Tests/AdminPathEntityConverterLanguageTest.php
Sets up a Drupal site for running functional and integration tests.

... See full list

File

core/modules/simpletest/src/UserCreationTrait.php, line 51
Contains \Drupal\simpletest\UserCreationTrait.

Class

UserCreationTrait
Provides methods to create additional test users and switch the currently logged in one.

Namespace

Drupal\simpletest

Code

protected function createUser(array $permissions = array(), $name = NULL, $admin = FALSE) {

  // Create a role with the given permission set, if any.
  $rid = FALSE;
  if ($permissions) {
    $rid = $this
      ->createRole($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,
    );
  }
  if ($admin) {
    $edit['roles'][] = $this
      ->createAdminRole();
  }
  $account = User::create($edit);
  $account
    ->save();
  $this
    ->assertTrue($account
    ->id(), SafeMarkup::format('User created with name %name and pass %pass', array(
    '%name' => $edit['name'],
    '%pass' => $edit['pass'],
  )), 'User login');
  if (!$account
    ->id()) {
    return FALSE;
  }

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