Same name and namespace in other branches
- 8.9.x core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createUser()
- 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.
67 calls to UserCreationTrait::createUser()
- AccessPermissionTest::setUp in core/
modules/ user/ tests/ src/ Kernel/ Views/ AccessPermissionTest.php - AccessTest::testCreateAccess in core/
modules/ file/ tests/ src/ Kernel/ AccessTest.php - Tests create access is always denied even for user 1.
- AccessTest::testFileAccess in core/
modules/ file/ tests/ src/ Kernel/ AccessTest.php - Tests 'update' and 'delete' access to file entities.
- AccessTest::testFileCacheability in core/
modules/ file/ tests/ src/ Kernel/ AccessTest.php - Tests cacheability metadata.
- ArgumentDefaultTest::testPluginArgumentDefaultCurrentUser in core/
modules/ user/ tests/ src/ Kernel/ Views/ ArgumentDefaultTest.php - Tests the current user with argument default.
File
- core/
modules/ user/ tests/ src/ Traits/ UserCreationTrait.php, line 152
Class
- UserCreationTrait
- Provides test methods for user creation and authentication.
Namespace
Drupal\Tests\user\TraitsCode
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, "User created with name {$edit['name']} and pass {$edit['pass']}");
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;
}