API functions

Last modified: October 14, 2009 - 20:44

The internal browser

The DrupalWebTestCase features an internal browser that can be used to navigate on your test site. Please read the basic documentation of the WebTestCase class for more information.

function $this->drupalGet($path, $options = array())

This function does a get request to a Drupal page.
The $path indicates a page that can be visited; $options contains additional data which may be passed to the url() function in order to determine the url to visit. The content will be loaded and saved into $this->_content (as well as returned), where it can be retrieved by using the function $this->drupalGetContent().

function $this->drupalPost($path, $edit, $submit, $reporting = TRUE)

This function does a post request on a Drupal page.
The $path indicates a page containing a form that will be filled with $edit data. Then the button indicated by $submit will be clicked (submit caption will be translated by this method).

It also does assertion that the requests were successful and form fields could be set.

Example:

<?php
$name
= $this->randomName();
$mail = "$name@example.com";
$edit = array('name' => $name,
             
'mail' => $mail);
$this->drupalPost('user/register', $edit, 'Create new account');
?>

Note: this function used to be called drupalPostRequest()

With multi-step forms, it is possible to post each subsequent step by passing NULL as the $path.

function $this->clickLink($label, $index = 0)

Follows a link on the current page by name. Will click the first link found with this link text by default, or a later one if an $index is given.
An assertion is done about the availability of the link and the URL it points to. Also gives some output including current and requested URL.

Example:

<?php
$this
->clickLink(t('Log out'));
?>

function $this->drupalCreateUser($permissions = NULL)

This function creates a user and returns the user object with an additional value pass_raw containing the non-hashed password.
It also creates a role with the specified $permissions that is assigned to the returned user.
The $permissions are specified as an array of strings. If it is omitted or NULL, the default permissions for a registered user will be used:
array('access comments', 'access content', 'post comments', 'post comments without approval')
An assertion for success is done as well as clean-up on the user and role tables.

function $this->drupalLogin($user = NULL)

This function logs a user into your site via the internal browser. You can just hand it a $user object (required is a pass_raw value).
If the argument is omitted this function will create a user and role with the standard permissions mentioned above.

After the user is logged in, you can now navigate with the internal browser.

Example:

<?php
// Prepare a user to do the stuff
$user = $this->drupalCreateUser(array('access content', 'create page content'));
$this->drupalLogin($user);

// Now do something with the users
$this->drupalGet('node/' . $node->nid));
?>

This method also does several assertions about the login process from the browsers perspective.

function $this->_drupalCreateRole($permissions = NULL)

This function is rarely useful. The $permissions parameter behaves exactly like in drupalCreateUser.
The return value is a role-id integer or FALSE on failure.
A success assertion is done, as well as decent clean-up of the role and permission tables.

function $this->randomString($number = 8)

Returns a string with $number ASCII characters of codes 32 to 126 prefixed by a prefix unique to the test run. The first character will not be a number.

function $this->randomName($number = 8)

Returns a string with $number alphanumerical character(s) prefixed by a prefix unique to the test run. The first character will not be a number.

function $this->drupalCreateNode($settings)

Create a new node. The default values for nodes are set for you. You can selectively override these (or append new data to these) using the $settings associative array that is passed into this function.

Example:

<?php
$settings
= array(
 
'type' => 'my_special_node_type', // This replaces the default type
 
'my_special_field' => 'glark', // This appends a new field.
);
$node = $this->drupalCreateNode($settings);

$this->assertEqual($node->type, 'my_spcial_node_type'); // We set this.
$this->assertEqual($node->comment, 2); // This is default.
?>

Default settings are as follows:

<?php
   
// Populate defaults array
   
$defaults = array(
     
'body'      => $this->randomName(32),
     
'title'     => $this->randomName(8),
     
'comment'   => 2,
     
'changed'   => time(),
     
'format'    => FILTER_FORMAT_DEFAULT,
     
'moderate'  => 0,
     
'promote'   => 0,
     
'revision'  => 1,
     
'log'       => '',
     
'status'    => 1,
     
'sticky'    => 0,
     
'type'      => 'page',
     
'revisions' => NULL,
     
'taxonomy'  => NULL,
    );
   
$defaults['teaser'] = $defaults['body'];
   
// If we already have a node, we use the original node's created time, and this
   
if (isset($defaults['created'])) {
     
$defaults['date'] = format_date($defaults['created'], 'custom', 'Y-m-d H:i:s O');
    }
    if (empty(
$settings['uid'])) {
      global
$user;
     
$defaults['uid'] = $user->uid;
    }
?>

function $this->cronRun()

This function runs the cron functionality of the Drupal installed by SimpleTest. Never call drupal_run_cron directly. Not available in older versions of SimpleTest!

$this->randomString() renamed to $this->randomName()

evoltech - June 11, 2009 - 07:32

in simpletest version 6.x-2.8 it seems that randomString does not exist. It appears that randomName() is the function to use.

hackbloc.org : exploit code not people

 
 

Drupal is a registered trademark of Dries Buytaert.