function UserCreationTrait::createRole
Same name in other branches
- 9 core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createRole()
- 8.9.x core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createRole()
- 10 core/modules/user/tests/src/Traits/UserCreationTrait.php \Drupal\Tests\user\Traits\UserCreationTrait::createRole()
Creates a role with specified permissions.
Parameters
array $permissions: Array of permission names to assign to role.
string $rid: (optional) The role ID (machine name). Defaults to a random name.
string $name: (optional) The label for the role. Defaults to a random string.
int $weight: (optional) The weight for the role. Defaults to NULL which sets the weight to maximum + 1.
Return value
string Role ID of newly created role, or FALSE if role creation failed.
25 calls to UserCreationTrait::createRole()
- AccessTest::setUp in core/
modules/ views/ tests/ src/ Functional/ Plugin/ AccessTest.php - AccessTestBase::setUp in core/
modules/ user/ tests/ src/ Functional/ Views/ AccessTestBase.php - Sets up the test.
- ConfigTranslationListUiTest::doUserRoleListTest in core/
modules/ config_translation/ tests/ src/ Functional/ ConfigTranslationListUiTest.php - Tests the role listing for the translate operation.
- ContextualFiltersStringTest::setUp in core/
modules/ views/ tests/ src/ Functional/ Plugin/ ContextualFiltersStringTest.php - EntityCloneConfigActionTest::setUp in core/
tests/ Drupal/ KernelTests/ Core/ Recipe/ EntityCloneConfigActionTest.php
File
-
core/
modules/ user/ tests/ src/ Traits/ UserCreationTrait.php, line 240
Class
- UserCreationTrait
- Provides test methods for user creation and authentication.
Namespace
Drupal\Tests\user\TraitsCode
protected function createRole(array $permissions, $rid = NULL, $name = NULL, $weight = NULL) {
// Generate a random, lowercase machine name if none was passed.
if (!isset($rid)) {
$rid = $this->randomMachineName(8);
}
// Generate a random label.
if (!isset($name)) {
// In the role UI role names are trimmed and random string can start or
// end with a space.
$name = trim($this->randomString(8));
}
// Check the all the permissions strings are valid.
if (!$this->checkPermissions($permissions)) {
return FALSE;
}
// Create new role.
$role = Role::create([
'id' => $rid,
'label' => $name,
]);
if (isset($weight)) {
$role->set('weight', $weight);
}
$result = $role->save();
$this->assertSame(SAVED_NEW, $result, "Created role ID {$role->id()} with name {$role->label()}.");
if ($result === SAVED_NEW) {
// Grant the specified permissions to the role, if any.
if (!empty($permissions)) {
$this->grantPermissions($role, $permissions);
$assigned_permissions = Role::load($role->id())
->getPermissions();
$missing_permissions = array_diff($permissions, $assigned_permissions);
$this->assertEmpty($missing_permissions);
}
return $role->id();
}
else {
return FALSE;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.