Logging In As Superuser In Drupal 6 SimpleTest

Sometimes the most complex part of using SimpleTest is making sure that everything is working as it should before you start your tests. This means setting up the correct modules and creating the correct content types in advance. Because SimpleTest works by issuing CURL requests to pages the most reliable way of doing certain tasks is to use the administration forms as you normally would. Taking an example, if you want to enable a block from a module you should submit the block form, adding the blocks you need to the various different regions this like.

// Enable the different blocks
$blocks = array(
    'my_awesome_block' => 'header'
);

$this->drupalPost('admin/build/block', $blocks, 'Save blocks');

However, if you haven't given the current user 'administer blocks' access, or have forgotten to log them in, then you will get an access denied error when you try to do this.

This is fine when only one form is in question. The problem occurs when when testing complex modules that rely on quite a few other system elements being enabled. Adding a large list of permissions is largely unnecessary and probably irrelevant to what you are trying to test. The best solution to this is to log in as the Drupal superuser and bypass all of the setup permissions issues. This might seem straightforward to do, but when SimpleTest installs Drupal (as it does at the start of every test) it will create a placeholder for the superuser and not activate it. Therefore, to login as the superuser you need to activate the user and reload them so you can log in using that account.

The SimpleTest method drupalLogin() is used to log a user into the system, and this takes a user object as a single parameter. You will also need to add an attribute called 'pass_raw' to the user object before you try to login with it. This is the password in raw text form which SimpleTest will use on the login form.

To log in with the superuser use the following code.

public function setUp() {
    // Enable any modules required for the test.
    parent::setUp('views', 'views_ui', 'views_export', 'profile', 'taxonomy');
    
    // Update the master user so we can use it, setting the username and password fields
    db_query("UPDATE {users} SET name = 'master', pass = MD5('wibble'), status = 1 WHERE uid = 1");
    
    // Load the master user
    $admin_user = user_load(1);
    
    // Give it a pass_raw value so SimpleTest can login with it
    $admin_user->pass_raw = 'wibble';
    
    // Login
    $this->drupalLogin($admin_user);
    
    // run through some complex tasks here...
}

This step is especially important if you are trying to import a view as it turns out that only the superusers can import views. Even if you enable all of the correct modules and give your created user all of the correct permissions you will still get access denied messages if you try to access the import view page.

Before you start your tests it is probably a good idea to log out of the superuser account. This can be done with the drupalLogout() method in the following way.

$this->drupalLogout();

Comments

First of all, thanks for the blog post, it really helped me!

Just one thing I'd like to note though; I did manage to successfully access the Views import page with a regular user with all permissions using the following code:

// Create a user with ALL available permissions and log in...
$permissions = module_invoke_all('perm');
$full_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($full_user);
// load the value of a "view export" (stored in a file in my case)...
$view_import = file_get_contents('PATH_TO_FILE_CONTAINING_EXPORT');
// check if you can access the import page (with "verbose message")
$this->drupalGet('admin/build/views/import');
// import the view by submitting the import form
$this->drupalPost('admin/build/views/import', array('view' => $view_import), 'Import');
// now you're on the "edit View" page, and still need to save it!
$this->drupalPost(null, array(), 'Save');

Hope this helps someone...

Permalink

Hi Ben! Thanks for that snippet. Very interesting :)

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
4 + 15 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.