function UserCreationTrait::createUser
Same name and namespace in other branches
- 11.x core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createUser()
- 10 core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createUser()
- 8.9.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.
231 calls to UserCreationTrait::createUser()
- AdminPathEntityConverterLanguageTest::setUp in core/
modules/ language/ tests/ src/ Functional/ AdminPathEntityConverterLanguageTest.php - BlockContentTestBase::setUp in core/
modules/ block_content/ tests/ src/ Functional/ Views/ BlockContentTestBase.php - Sets up the test.
- BlockHookOperationTest::setUp in core/
modules/ block/ tests/ src/ Functional/ BlockHookOperationTest.php - BlockTest::testThemeAdminLink in core/
modules/ block/ tests/ src/ Functional/ BlockTest.php - Tests that a link exists to block layout from the appearance form.
- BooleanFieldTest::setUp in core/
modules/ field/ tests/ src/ Functional/ Boolean/ BooleanFieldTest.php
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\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, 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.