You are here

protected function DrupalWebTestCase::drupalCreateRole in SimpleTest 6.2

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

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.
  db_query("INSERT INTO {role} (name) VALUES ('%s')", $name);
  $role = db_fetch_object(db_query("SELECT * FROM {role} WHERE name = '%s'", $name));
  $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)) {
    db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $role->rid, implode(', ', $permissions));
    $count = count(explode(', ', db_result(db_query("SELECT perm FROM {permission} WHERE rid = %d", $role->rid))));
    $this
      ->assertTrue($count == count($permissions), t('Created permissions: @perms', array(
      '@perms' => implode(', ', $permissions),
    )), t('Role'));
    return $role->rid;
  }
  else {
    return FALSE;
  }
}