You are here

protected function UserCreationTrait::createRole in Drupal 8

Same name and namespace in other branches
  1. 9 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.

17 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
CKEditorIntegrationTest::testPreviewUsesDefaultThemeAndIsClientCacheable in core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
The CKEditor Widget must load a preview generated using the default theme.
ConfigTranslationListUiTest::doUserRoleListTest in core/modules/config_translation/tests/src/Functional/ConfigTranslationListUiTest.php
Tests the role listing for the translate operation.
HandlerFieldRoleTest::testRole in core/modules/user/tests/src/Functional/Views/HandlerFieldRoleTest.php

... See full list

File

core/modules/user/tests/src/Traits/UserCreationTrait.php, line 248

Class

UserCreationTrait
Provides methods to create additional test users and switch the currently logged in one.

Namespace

Drupal\Tests\user\Traits

Code

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 = strtolower($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
    ->assertIdentical($result, SAVED_NEW, new FormattableMarkup('Created role ID @rid with name @name.', [
    '@name' => var_export($role
      ->label(), TRUE),
    '@rid' => var_export($role
      ->id(), TRUE),
  ]), 'Role');
  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);
      if (!$missing_permissions) {
        $this
          ->pass(new FormattableMarkup('Created permissions: @perms', [
          '@perms' => implode(', ', $permissions),
        ]), 'Role');
      }
      else {
        $this
          ->fail(new FormattableMarkup('Failed to create permissions: @perms', [
          '@perms' => implode(', ', $missing_permissions),
        ]), 'Role');
      }
    }
    return $role
      ->id();
  }
  else {
    return FALSE;
  }
}