You are here

protected function DrupalWebTestCase::drupalCreateRole in SimpleTest 7.2

Same name and namespace in other branches
  1. 6.2 drupal_web_test_case.php \DrupalWebTestCase::drupalCreateRole()
  2. 7 drupal_web_test_case.php \DrupalWebTestCase::drupalCreateRole()

Internal helper function; Create 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 ID of newly created role, or FALSE if role creation failed.

1 call to DrupalWebTestCase::drupalCreateRole()
DrupalWebTestCase::drupalCreateUser in ./drupal_web_test_case.php
Create a user with a given set of permissions. The permissions correspond to the names given on the privileges page.

File

./drupal_web_test_case.php, line 1098
Provides DrupalTestCase, DrupalUnitTestCase, and DrupalWebTestCase classes.

Class

DrupalWebTestCase
Test case for typical Drupal tests.

Code

protected function drupalCreateRole(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;
  user_role_save($role);
  user_role_grant_permissions($role->rid, $permissions);
  $this
    ->assertTrue(isset($role->rid), t('Created role of name: @name, id: @rid', array(
    '@name' => $name,
    '@rid' => isset($role->rid) ? $role->rid : t('-n/a-'),
  )), t('Role'));
  if ($role && !empty($role->rid)) {
    $count = db_query('SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid', array(
      ':rid' => $role->rid,
    ))
      ->fetchField();
    $this
      ->assertTrue($count == count($permissions), t('Created permissions: @perms', array(
      '@perms' => implode(', ', $permissions),
    )), t('Role'));
    return $role->rid;
  }
  else {
    return FALSE;
  }
}