function DrupalWebTestCase::drupalCreateRole
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 ID of newly created role, or FALSE if role creation failed.
3 calls to DrupalWebTestCase::drupalCreateRole()
- DrupalWebTestCase::drupalCreateUser in modules/
simpletest/ drupal_web_test_case.php - Create a user with a given set of permissions.
- UserRolesAssignmentTestCase::testAssignAndRemoveRole in modules/
user/ user.test - Tests that a user can be assigned a role and that the role can be removed again.
- UserRolesAssignmentTestCase::testCreateUserWithRole in modules/
user/ user.test - Tests that when creating a user the role can be assigned. And that it can be removed again.
File
-
modules/
simpletest/ drupal_web_test_case.php, line 1272
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;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.