Skip to content

Instantly share code, notes, and snippets.

@dmp1ce
Created September 8, 2012 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmp1ce/3677218 to your computer and use it in GitHub Desktop.
Save dmp1ce/3677218 to your computer and use it in GitHub Desktop.
Creates a user with a given role using the Drupal simpletest framework.
/**
* 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);
}
@irpro
Copy link

irpro commented Sep 22, 2015

Hi David
I cannot run this script , I have two problesm :
1 - Parse error: syntax error, unexpected 'public' (T_PUBLIC) in line 4
I remove 'public' and this skipped
2 - Fatal error: Call to undefined function user_roles() in line 6
I think this script needs includes and requires other files,is this true?

@lkmorlan
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment