mediatribe.net -- Drupal and Web Development

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

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Image CAPTCHA
Enter the characters shown in the image.