You are here

protected function UserCreationTrait::createUser in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createUser()
  2. 9 core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\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.

array $values: (optional) An array of initial user field values.

Return value

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

Throws

\Drupal\Core\Entity\EntityStorageException If the user creation fails.

140 calls to UserCreationTrait::createUser()
AccessDeniedTest::setUp in core/modules/system/tests/src/Functional/System/AccessDeniedTest.php
AccessPermissionTest::setUp in core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
AccessTestBase::setUp in core/modules/user/tests/src/Functional/Views/AccessTestBase.php
AdminTest::setUp in core/modules/system/tests/src/Functional/System/AdminTest.php
AjaxPageStateTest::setUp in core/modules/system/tests/src/Functional/Render/AjaxPageStateTest.php

... See full list

File

core/modules/user/tests/src/Traits/UserCreationTrait.php, line 160

Class

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

Namespace

Drupal\Tests\user\Traits

Code

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

  // 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 = $values;
  if ($name) {
    $edit['name'] = $name;
  }
  elseif (!isset($values['name'])) {
    $edit['name'] = $this
      ->randomMachineName();
  }
  $edit += [
    'mail' => $edit['name'] . '@example.com',
    'pass' => \Drupal::service('password_generator')
      ->generate(),
    'status' => 1,
  ];
  if ($rid) {
    $edit['roles'] = [
      $rid,
    ];
  }
  if ($admin) {
    $edit['roles'][] = $this
      ->createAdminRole();
  }
  $account = User::create($edit);
  $account
    ->save();
  $valid_user = $account
    ->id() !== NULL;
  $this
    ->assertTrue($valid_user, new FormattableMarkup('User created with name %name and pass %pass', [
    '%name' => $edit['name'],
    '%pass' => $edit['pass'],
  ]), 'User login');
  if (!$valid_user) {
    return FALSE;
  }

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

  // Support BrowserTestBase as well.
  $account->passRaw = $account->pass_raw;
  return $account;
}