You are here

protected function EntityUnitTestBase::createUser in Drupal 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.

File

core/modules/system/src/Tests/Entity/EntityUnitTestBase.php, line 108

Class

EntityUnitTestBase
Defines an abstract test base for entity unit tests.

Namespace

Drupal\system\Tests\Entity

Code

protected function createUser($values = [], $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;
}