function UserCreationTrait::createUser
Same name in other branches
- 9 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()
- 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.
1178 calls to UserCreationTrait::createUser()
- AccessAnnouncementTest::testAnnounceFirstLogin in core/
modules/ announcements_feed/ tests/ src/ FunctionalJavascript/ AccessAnnouncementTest.php - Test of viewing announcements by a user with appropriate permission.
- AccessAnnouncementTest::testAnnounceWithoutPermission in core/
modules/ announcements_feed/ tests/ src/ FunctionalJavascript/ AccessAnnouncementTest.php - Testing announce icon without announce permission.
- 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.
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;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.