Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Handling Users and Permissions in SimpleTest

Video loading...

Transcript language code: 

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:03
    Handling Users and Permissions in SimpleTest
  • 0:05
    with Joe Shindelar
  • 0:08
  • 0:09
    So now that we know how to navigate a site
  • 0:11
    and submit forms via SimpleTest, we're
  • 0:13
    going to pretty quickly discover that we also need to be logged
  • 0:17
    in as an authorized user in order to perform a lot of tasks in Drupal.
  • 0:22
    Since our tests all run in the context of a fresh Drupal
  • 0:26
    installation, there are not, by default, any users for our site,
  • 0:30
    so we'll need to create new users, assign them permissions,

Handling Users and Permissions in SimpleTest


Typically our application is going to require testing of functionality that is restricted to only users with a certain set of permissions. Like creating a new node for example. In order for the DrupalWebTestCase browser to test these pages we need to first log in to our site. In order to log in we need to have a username and password that we can use to login. In this lesson we'll look at using DrupalWebTestCase::drupalCreateUser() and DrupalWebTestCase::drupalLogin() in order to gain authenticated access to our application and then access and submit a restricted form.

Here's an example of creating a user with the 'administer site configuration` permissions:

$new_user = $this->drupalCreateUser(array('administer site configuration'));

And then logging in as that user:

$new_user = $this->drupalLogin($new_user);

After that the SimpleTest browser will be logged in as that user and all requests that it makes will be authenticated. If you want to return to an anonymous user session you can use the DrupalWebTestCase::drupalLogout() method.

For more information about these helper methods check out the API Functions documentation in the handbook.

Log in or sign up to download companion files.