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