Created
September 8, 2012 17:03
-
-
Save dmp1ce/3677218 to your computer and use it in GitHub Desktop.
Creates a user with a given role using the Drupal simpletest framework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates a user with the give role. | |
**/ | |
public function drupalCreateUserWithRole($role) { | |
// Get all of the roles in the system. | |
$roles = user_roles(); | |
// Find the index for the role we want to assign to the user. | |
$index = array_search($role, $roles); | |
// Get the permissions for the role. | |
$permissions = user_role_permissions(array(array_search($role, $roles) => $role)); | |
// Create the user with the permissions. | |
$user = $this->drupalCreateUser(array_keys($permissions[$index])); | |
// Assign the role. | |
$user->roles[$index] = $role; | |
// Return the user we have created. | |
return user_save($user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This version actually gives the user the role instead of giving them the same permissions as the role. Most of this is identical to drupalCreateUser().