You are here

protected function KernelTestBase::createUser in Forms Steps 8

Creates a user.

Parameters

array $values: (optional) The values used to create the entity.

array $permissions: (optional) Array of permission names to assign to user.

Return value

\Drupal\user\Entity\User The created user entity.

Throws

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Kernel/KernelTestBase.php, line 81

Class

KernelTestBase
Defines an abstract test base for kernel tests.

Namespace

Drupal\Tests\forms_steps\Kernel

Code

protected function createUser(array $values = [], array $permissions = []) {
  if ($permissions) {

    // Create a new role and apply permissions to it.
    $role = Role::create([
      'id' => strtolower($this
        ->randomMachineName(8)),
      'label' => $this
        ->randomMachineName(8),
    ]);
    $role
      ->save();
    user_role_grant_permissions($role
      ->id(), $permissions);
    $values['roles'][] = $role
      ->id();
  }
  $account = User::create($values + [
    'name' => $this
      ->randomMachineName(),
    'status' => 1,
  ]);
  $account
    ->enforceIsNew();
  $account
    ->save();
  return $account;
}