Assertions

Last modified: January 23, 2010 - 23:24

Up to date documentation or additions may be found at the the api.drupal.org reference site (not necessarily formatted for browsing).

Method Description Example usage
<?php
$this
->assertTrue($result, $message = FALSE, $group = 'Other')
?>
Asserts that the variable $result resolves to TRUE.
<?php
$valid
= is_valid('foo');
$this->assertTrue($valid, t('Make sure "foo" is a valid variable.'));
?>
<?php
$this
->assertFalse($result, $message = '%s', $group = 'Other')
?>
Asserts that the variable $result resolves to FALSE.
<?php
$valid
= is_valid('bar');
$this->assertFalse($valid, t('Make sure "bar" is not a valid variable.'));
?>
<?php
$this
->assertNull($value, $message= '%s', $group= 'Other')
?>
Asserts that the variable $value resolves to NULL.
<?php
$result
= load_my_object(-1);
$this->assertNull($result, t('Make sure we get NULL when trying to load an invalid object.'));
?>
<?php
$this
->assertNotNull($value, $message= '%s', $group= 'Other')
?>
Asserts that the variable $value does NOT resolves to NULL.
<?php
$result
= load_my_object(1);
$this->assertNotNull($result, t('Make sure we get a result when trying to load a valid object.'));
?>
<?php
$this
->assertEqual($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is roughly equivalent (==) to $second.
<?php
menu_set_active_item
('node/3');
$arg = arg(1);
$this->assertEqual($arg, 3, t('Make sure arg(1) on the page node/3 resolves to 3.'));
?>
<?php
$this
->assertNotEqual($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is not roughly equivalent (!=) to $second.
<?php
menu_set_active_item
('node/3');
$arg = arg(0);
$this->assertNotEqual($arg, 3, t('Make sure arg(0) on the page node/3 does not resolve to 3.'));
?>
<?php
$this
->assertIdentical($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is absolutely identical (===) to $second.
<?php
$awesome
= get_awesome();
$this->assertIdentical($awesome, t('Drupal'), t('Make sure Drupal is awesome.'));
?>
<?php
$this
->assertNotIdentical($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is not absolutely identical (!==) to $second.
<?php
$awesome
= get_awesome();
$this->assertNotIdentical($awesome, t('Drupal\'s evil twin'), t('Make sure Drupal\'s evil twin is NOT awesome.'));
?>

Checking page results

<?php
$this
->assertPattern($pattern, $message= '%s', $group= 'Other')
?>
Asserts that the raw html content of the current page matches the regular expression $pattern.
<?php
$this
->assertPattern('|[Dd]rupal|', t('Make sure Drupal/drupal is on current page.'));
?>
<?php
$this
->assertNoPattern($pattern, $message= '%s', $group= 'Other')
?>
Asserts that the raw html content of the current page does not match the regular expression $pattern.
<?php
$this
->assertNoPattern('|[Ee]vil|', t('Make sure Evil/evil is not on current page.'));
?>
<?php
$this
->assertRaw($raw, $message="%s", $group= 'Other')
?>
Asserts that the html $raw appears in the raw html content of the current page in the SimpleTest browser.
<?php
$this
->drupalGet('user/register');
$this->assertRaw('<a href="'. url('user/password') .'">', t('Make sure a link appears to "Request new password" on the user register page.'));
?>
<?php
$this
->assertNoRaw($raw, $message="%s", $group= 'Other')
?>
Asserts that the html $raw does NOT appear in the raw html content of the current page in the SimpleTest browser.
<?php
$this
->drupalGet('node/1');
$this->assertNoRaw('<a href="'. url('user/password') .'">', t('Make sure a link appears to "Request new password" does not appear on the page node/1.'));
?>
<?php
$this
->assertText($text, $message="%s", $group= 'Other')
?>
Asserts that the text in $text appears in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('user/register');
$this->assertText(t('Request new password'), t('Make sure the text "Request new password" appears on the user register page.'));
?>
<?php
$this
->assertNoText($text, $message="%s", $group= 'Other')
?>
Asserts that the text in $text does NOT appear in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('node/1');
$this->assertNoText(t('Request new password'), t('Make sure the text "Request new password" does NOT appear on the page node/1.'));
?>
<?php
$this
->assertTitle($title, $message, $group= 'Other')
?>
Asserts that the title given in $title is the title of the current page in the SimpleTest browser.
<?php
$this
->drupalGet('tracker');
$this->assertTitle(t('Recent posts'), t('Make sure the title on the tracker page is "Recent posts".'));
?>
<?php
$this
->assertUniqueText($text, $message='', $group= 'Other')
?>
Asserts that the text in $text appears exactly once in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('user/register');
$this->assertUniqueText(t('Request new password'), t('Make sure the text "Request new password" appears exactly once on the user register page.'));
?>
<?php
$this
->assertNoUniqueText($text, $message='', $group= 'Other')
?>
Asserts that the text in $text appears more than once in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('user/register');
$this->assertNoUniqueText(t('password'), t('Make sure the text "password" appears more than once on the user register page.'));
?>
<?php
$this
->assertResponse($code, $message= '')
?>
Asserts that the http response code for the current page in the simpletest browser matches $code.
<?php
$this
->drupalGet('admin');
$this->assertResponse(403, t('Make sure access is denied on the administration page.'));
?>

Checking form element

<?php
assertFieldById
($id, $value = '', $message = '')
?>
Assert that a field exists in the current page with the given id and value.
<?php
$this
->assertFieldById('edit-path-alias', "home", "The alias field is set correctly");
?>
<?php
assertNoFieldById
($id, $value = '', $message = '')
?>
Assert that a field does not exists in the current page with the given id and value.
<?php
$this
->assertNoFieldById('edit-path-alias', "wrong-name", "The alias field is not incorrect");
?>
<?php
assertFieldByName
($id, $value = '', $message = '')
?>
Assert that a field exists in the current page with the given name and value.
<?php
$this
->assertFieldByName('path[alias]', "home", "The alias field is correct");
?>
<?php
assertNoFieldByName
($id, $value = '', $message = '')
?>
Assert that a field does not exists in the current page with the given name and value.
<?php
$this
->assertNoFieldByName('path[alias]', "wrong-name", "The alias field is not incorrect");
?>
<?php
assertFieldByXPath
($xpath, $value, $message, $group = 'Other')
?>
Assert that a field exists in the current page by the given XPath. draft, needs testing or better examples
<?php
$this
->assertFieldByXPath('/input[@type="checkbox" and position()=2]', TRUE, "The second checkbox is ticked");
$this->assertFieldByXPath('/input[@name="info[]" and @class="custom-class"]', TRUE, "There is an input field called 'info' and class 'custom' ");
?>
 
 

Drupal is a registered trademark of Dries Buytaert.