Community & Support

SimpleTest -> add user to a role

Hi..
Is it possible to add an user to a specific role? I only found the possibility to add permissions to an user:

$this->drupalCreateUser(array('access content', 'create page content'));

Do i have to add all permissions of a role or is it possible to add the role directly? For example: drupalUserRole('moderator');

Comments

In what kind of situation do

In what kind of situation do you need to change the role?

Just thinking that the task could possibly be accomplished through some of the modules available. (Searching for "role" in the title field, over at Drupalmodules.com gave a few suggestions: Auto assign role, Auto assign role, Role assign, Role delegation et.c.)

Regards

Samuel Lampa
RIL Partner AB

In Simpletest Drupal 6,

In Simpletest Drupal 6, visiting the user creation page in simpletest and filling it out results in an "illegal choice" problem, probably because role id 1 is selected by default but grayed out, confusing simpletest. The following code will create an account for a given role (in this case role #4) (it is drupalCreateUser(), slightly modified)

<?php
    $rid
= 4;

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

   
$account = user_save('', $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'];
?>

Use Case

Do you have to somehow create the role you're trying to assign to first? The role wouldn't exist in the SimpleTest context, right?

I do have a use case where I have hook_form_alter doing different things based on user role, so to test that I would need to assign my SimpleTest users to the appropriate roles, load that form, and see what I get.

If someone knows how to create these roles and then assign SimpleTest users to them, I would like to know how as well!

UPDATE: found it. you can use

<?php
$rid
= $this->drupalCreateRole(array('access content', 'access user profiles', 'create application content', 'create page content'), 'role name'); 
?>

To create a role with a specific name and then assign to the user you will create.

The role will only exist if

The role will only exist if it has been created by a module you're including for testing, for example:

1. create a role on a website
2. use features, and export your role as part of a module
3. in that module you just created, include a simpletest which enables the module (and thus the role)
4. now, the role will exist within simpletest.

I don't really see why you would want to assign a user to a role in simpletest outside of this scenario: just use simpletest's standard method of creating a user with a subset of permissions directly, not using roles at all.

Cheers,

Albert.

@ Albert hi, i created a

@ Albert

hi, i created a content type and now writing unit tests for its add and edit, i am unable to determine appropriate permissions for its node add, since when I go to the permissions page (if my content-type name is 'proposal'), my permission name would be like 'Proposal: Create new content'. but providing this doesnot work, so i am looking for a way to assign all permissions at once so as to run my first unit test with success.
Second solution would be to assign a role of 'administrator' to my user but unfortunately I think when the unit tests are run; there exist only two roles (authenticated and anonymous - am I right?), so i find no way to assign a role of administrator to my unit test user also. thats why i would need all the permissions to my user.
I hope you got the situation I am into.

FIrdous