Tips

Last modified: September 27, 2008 - 18:58

This is a list of some general tips for writing tests, feel free to pitch in and add your own!

Putting Data in edit[double][brackets] Fields

Calling drupalPost, we use only middle part of a field's name. And function begins with 'edit[' and ends with ']'. So, to put data we should use double][brackets in the result, we fill the desired field:

<?php
$this
->drupalPostRequest('form', array('double][brackets' => 'yupi') , t('Submit'));
?>

Selecting Multiple Fields

Sometimes we have to select multiple fields (e.g. Testing taxonomies), in which case:

<div class="form-item">
<label for="edit-taxonomy">ourfield:</label><br>
<select name="edit[taxonomy][]" multiple="multiple" size="3" id="edit-taxonomy"><option value="806" selected="selected">one</option><option value="808">three</option><option value="807">two</option></select>
</div>

We send an array with our selections:
<?php
$this
->drupalPost('form/multiplefields', array('taxonomy][' => array('one', 'three')), t('Submit'));
?>

Testing the Uploading Functionality

In the new version of Simpletest, there is an uploading feature. You simply have to put the full path of the file which you want to upload. When we run test we are in drupal's main directory, and if we have our file in modules/tests/ourfile then, to obtain real path we use:

<?php
$rpath
= realpath('modules/tests/ourfile');
$this->drupalPost('form/upload', array('file' => $rpath), t('Upload file'));
?>

Viewing Source during a Browser Test

Use $this->drupalGetContent() to output the source that the simpletest browser is receiving. This is useful for debugging during development.

Triggering your debugger (xdebug client) from a Browser Test

It can be quite hard to debug errors in tests. I like to edit calls to the drupalGet() and drupalPost() methods so that they have an array('query' => 'XDEBUG_SESSION_START=mw') as their $options parameter. Substitute your own IDE key for mw. This arrays assures that simpletest adds the XDEBUG querystring to its request and then up pops the debugger. Xdebug can be really hard to setup but is so very worth it.

An alternative is to setup a VirtualHost on your Apache with the following xdebug related lines. This causes all pages on this domain to trigger the debugger. Just use this domain when you want to debug and use a different domain when you are browsing or developing bug free :).

More xdebug remote debugging params should be set in your php.ini or in this VirtualHost.

  #customize this path to point to your Xdebug extension
  zend_extension=/Applications/Komodo IDE.app/Contents/SharedSupport/php/debugging/5.2/xdebug.so
   #customize this to match how you setup your xdebug client
  xdebug.idekey=mw
  xdebug.default_enable=1
  xdebug.remote_handler=dbgp
  php_admin_value xdebug.remote_enable 1
  #the following line triggers the xdebug client automatically for every request. useful for simpletest.
  php_admin_value xdebug.remote_autostart 1

 
 

Drupal is a registered trademark of Dries Buytaert.