1.20.x backdrop_web_test_case.php protected BackdropWebTestCase::backdropCreateRole(array $permissions, $name = NULL)

Creates a role with specified permissions.

Parameters

$permissions: Array of permission names to assign to role.

$name: (optional) String for the name of the role. Defaults to a random string.

Return value

Role name of newly created role, or FALSE if role creation failed.:

File

modules/simpletest/backdrop_web_test_case.php, line 1364

Class

BackdropWebTestCase
Test case for typical Backdrop tests.

Code

protected function backdropCreateRole(array $permissions, $name = NULL) {
  // Generate random name if it was not passed.
  if (!$name) {
    $name = $this->randomName();
  }

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

  // Create new role.
  $role = new stdClass();
  $role->name = $name;
  $role->label = $name;
  user_role_save($role);
  user_role_grant_permissions($role->name, $permissions);
  $role = user_role_load($role->name);
  $this->assertTrue(isset($role->name), t('Created role of name: @name', array('@name' => $name)), t('Role'));
  if ($role && !empty($role->name)) {
    $this->assertTrue(count($role->permissions) == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role'));
    return $role->name;
  }
  else {
    return FALSE;
  }
}