mediatribe.net -- Drupal and Web Development

Notice: this post was last updated 5 years 41 weeks ago so it might be outdated. Please be cautious before implementing any of suggestions herein.

Drupal 7 Simpletest: in your tests, creating users with specifc roles

When creating automated tests in Drupal 7, you can use $this->drupalCreateUser() to create a user with specific permissions, but what if you want to create a user with a specific role? Here's how to set up a user with the role "administrator", for example within your setUp() method:

<?php
   
// create a new user with some permissions you need; then log in.
   
$perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
   
$perms = array_keys($perms[array_search('administrator', user_roles())]);
   
$admin = $this->drupalCreateUser($perms);
?>

In fact this creates a user

In fact this creates a user with the same permissions as a given role at the time the user is created. With this technique, your user will not have the role itself

Hello! I wrote a method that

Hello!
I wrote a method that creates an admin user for Testing:

<?php
public function drupalCreateAdminUser(array $permissions = array()) {
   
$roles = user_roles();
   
$index = array_search('administrator', $roles);
   
$user = $this->drupalCreateUser($permissions);
   
$user->roles[$index] = 'administrator';
    return
user_save($user);
}
?>

You may use it like that:

<?php
$user
= $this->drupalCreateAdminUser();
$this->drupalLogin($user);
?>

Here is an example of a

Here is an example of a function which only takes the role you want for the user you're creating.

<?php
/**
* 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);
}
?>

<?php  /**   * Creates an

<?php
 
/**
   * Creates an admin user with 'administrator' role, with a limited number of permissions or all the admin permissions.
   * @param array $permissions
   * @return user object
   */
 
public function drupalCreateAdminUser(array $permissions = array()) {
   
$roles = user_roles();
   
$index = array_search('administrator', $roles);
    if (empty(
$permissions)) {
     
// Get all the permissions for the 'administrator' role.
     
$permissions = user_role_permissions(array($index => 'administrator'));
    }
   
$user = $this->drupalCreateUser($permissions);
   
$user->roles[$index] = 'administrator';
    return
user_save($user);
  }
?>

This version actually gives

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().

<?php
/**
* Create a user with a given set of roles.
*
* @param array $roles
* Array of role names to assign to the user. Note that the user always has
* the "authenticated users" role.
*
* @return object|false
* A fully loaded user object with pass_raw property, or FALSE if account
* creation fails.
*/
protected function drupalCreateUserWithRole(array $roles = []) {
// Create an array like [$rid => $rid] with the roles in $role.
static $user_roles;
if (!isset($user_roles)) {
$user_roles = user_roles();
}
$rids = [];
foreach ($roles as $role) {
$rid = array_search($role, $user_roles);
$rids[$rid] = $rid;
}

// Create a user assigned to that role.
$edit = array();
$edit['name'] = $this->randomName();
$edit['mail'] = $edit['name'] . '@example.com';
$edit['pass'] = user_password();
$edit['status'] = 1;
if ($rids) {
$edit['roles'] = $rids;
}

$account = user_save(drupal_anonymous_user(), $edit);

$this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
if (empty($account->uid)) {
return FALSE;
}

// Add the raw password so that we can log in as this user.
$account->pass_raw = $edit['pass'];
return $account;
}

Very nice. Thanks for

Very nice. Thanks for sharing, this makes testing integration of features easier.