function UserCreationTrait::createUser

Same name in other branches
  1. 8.9.x core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createUser()
  2. 10 core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createUser()
  3. 11.x 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.

1061 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
AccessTest::setUp in core/modules/views/tests/src/Functional/Plugin/AccessTest.php
Sets up the test.
AccessTestBase::setUp in core/modules/user/tests/src/Functional/Views/AccessTestBase.php
Sets up the test.
ActionFormAjaxTest::setUp in core/modules/action/tests/src/FunctionalJavascript/ActionFormAjaxTest.php

... See full list

File

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

Class

UserCreationTrait
Provides test methods for user creation and authentication.

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.