protected function WebTestBase::drupalCreateRole

Internal helper function; Create 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.

Return value

string Role ID of newly created role, or FALSE if role creation failed.

5 calls to WebTestBase::drupalCreateRole()
AccessTest::setUp in drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/AccessTest.php
Sets up a Drupal site for running functional and integration tests.
AccessTestBase::setUp in drupal/core/modules/views/lib/Drupal/views/Tests/User/AccessTestBase.php
Sets up a Drupal site for running functional and integration tests.
UserRolesAssignmentTest::testAssignAndRemoveRole in drupal/core/modules/user/lib/Drupal/user/Tests/UserRolesAssignmentTest.php
Tests that a user can be assigned a role and that the role can be removed again.
UserRolesAssignmentTest::testCreateUserWithRole in drupal/core/modules/user/lib/Drupal/user/Tests/UserRolesAssignmentTest.php
Tests that when creating a user the role can be assigned. And that it can be removed again.
WebTestBase::drupalCreateUser in drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
Create a user with a given set of permissions.

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php, line 468
Definition of Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function drupalCreateRole(array $permissions, $rid = NULL, $name = NULL) {

  // Generate a random, lowercase machine name if none was passed.
  if (!isset($rid)) {
    $rid = strtolower($this
      ->randomName(8));
  }

  // Generate a random label.
  if (!isset($name)) {
    $name = $this
      ->randomString(8);
  }

  // Check the all the permissions strings are valid.
  if (!$this
    ->checkPermissions($permissions)) {
    return FALSE;
  }

  // Create new role.
  $role = new stdClass();
  $role->rid = $rid;
  $role->name = $name;
  $result = user_role_save($role);
  $this
    ->assertIdentical($result, SAVED_NEW, t('Created role ID @rid with name @name.', array(
    '@name' => var_export($role->name, TRUE),
    '@rid' => var_export($role->rid, TRUE),
  )), t('Role'));
  if ($result === SAVED_NEW) {

    // Grant the specified permissions to the role, if any.
    if (!empty($permissions)) {
      user_role_grant_permissions($role->rid, $permissions);
      $assigned_permissions = db_query('SELECT permission FROM {role_permission} WHERE rid = :rid', array(
        ':rid' => $role->rid,
      ))
        ->fetchCol();
      $missing_permissions = array_diff($permissions, $assigned_permissions);
      if (!$missing_permissions) {
        $this
          ->pass(t('Created permissions: @perms', array(
          '@perms' => implode(', ', $permissions),
        )), t('Role'));
      }
      else {
        $this
          ->fail(t('Failed to create permissions: @perms', array(
          '@perms' => implode(', ', $missing_permissions),
        )), t('Role'));
      }
    }
    return $role->rid;
  }
  else {
    return FALSE;
  }
}