- examples
An example of simpletest tests to accompany the tutorial at http://drupal.org/node/890654.
Classes
| Name | Description |
|---|---|
| SimpletestExampleTestCase | The SimpletestExampleTestCase is a functional test case, meaning that it actually exercises a particular sequence of actions through the web UI. The majority of core test cases are done this way, but the Simpletest suite also provides unit tests as… |
| SimpletestUnitTestExampleTestCase | Although most core test cases are based on DrupalWebTestCase and are functional tests (exercising the web UI) we also have DrupalUnitTestCase, which executes much faster because a Drupal install does not have to be one. No environment is provided to a… |
File
simpletest_example/simpletest_example.testView source
- <?php
-
- /**
- * @file
- * An example of simpletest tests to accompany the tutorial at
- * http://drupal.org/node/890654.
- */
-
- /**
- * The SimpletestExampleTestCase is a functional test case, meaning that it
- * actually exercises a particular sequence of actions through the web UI.
- * The majority of core test cases are done this way, but the Simpletest suite
- * also provides unit tests as demonstrated in the unit test case example later
- * in this file.
- *
- * Functional test cases are far slower to execute than unit test cases because
- * they require a complete Drupal install to be done for each test.
- *
- * @see DrupalWebTestCase
- * @see SimpletestUnitTestExampleTestCase
- */
- class SimpletestExampleTestCase extends DrupalWebTestCase {
- protected $privileged_user;
-
- public static function getInfo() {
- return array(
- 'name' => 'Simpletest Example',
- 'description' => 'Ensure that the simpletest_example content type provided functions properly.',
- 'group' => 'Examples',
- );
- }
-
- public function setUp() {
- parent::setUp('simpletest_example'); // Enable any modules required for the test
- // Create and log in our user. The user has the arbitrary privilege
- // 'extra special edit any simpletest_example' which the code uses
- // to grant access.
- $this->privileged_user = $this->drupalCreateUser(array('create simpletest_example content', 'extra special edit any simpletest_example'));
- $this->drupalLogin($this->privileged_user);
- }
-
- // Create a simpletest_example node using the node form
- public function testSimpleTestExampleCreate() {
- // Create node to edit.
- $edit = array();
- $edit['title'] = $this->randomName(8);
- $edit["body[und][0][value]"] = $this->randomName(16);
- $this->drupalPost('node/add/simpletest-example', $edit, t('Save'));
- $this->assertText(t('Simpletest Example Node Type @title has been created.', array('@title' => $edit['title'])));
- }
-
- // Create a simpletest_example node and then see if our user can edit it
- public function testSimpleTestExampleEdit() {
- $settings = array(
- 'type' => 'simpletest_example',
- 'title' => $this->randomName(32),
- 'body' => array(LANGUAGE_NONE => array(array($this->randomName(64)))),
- );
- $node = $this->drupalCreateNode($settings);
-
- // For debugging, we might output the node structure with $this->verbose()
- $this->verbose('Node created: ' . var_export($node, TRUE));
- // It would only be output if the testing settings had 'verbose' set.
-
- // We'll run this test normally, but not on the testbot, as it would
- // indicate that the examples module was failing tests.
- if (!$this->runningOnTestbot()) {
- // The debug() statement will output information into the test results.
- // It can also be used in Drupal 7 anywhere in code and will come out
- // as a drupal_set_message().
- debug('We are not running on the PIFR testing server, so will go ahead and catch the failure.');
- $this->drupalGet("node/{$node->nid}/edit");
- // Make sure we don't get a 401 unauthorized response:
- $this->assertResponse(200, t('User is allowed to edit the content.'));
-
- // Looking for title text in the page to determine whether we were
- // successful opening edit form.
- $this->assertText(t("@title", array('@title' => $settings['title'])), "Found title in edit form");
- }
- }
-
- /**
- * Detect if we're running on PIFR testbot; skip intentional failure in that
- * case. It happens that on the testbot the site under test is in a directory
- * named 'checkout' or 'site_under_test'.
- *
- * @return boolean
- * TRUE if running on testbot.
- */
- public function runningOnTestbot() {
- return (file_exists("../checkout") || file_exists("../site_under_test"));
- }
- }
-
-
- /**
- * Although most core test cases are based on DrupalWebTestCase and are
- * functional tests (exercising the web UI) we also have DrupalUnitTestCase,
- * which executes much faster because a Drupal install does not have to be
- * one. No environment is provided to a test case based on DrupalUnitTestCase;
- * it must be entirely self-contained.
- *
- * @see DrupalUnitTestCase
- */
- class SimpletestUnitTestExampleTestCase extends DrupalUnitTestCase {
-
- public static function getInfo() {
- return array(
- 'name' => 'Simpletest Example unit tests',
- 'description' => 'Test that simpletest_example_empty_mysql_date works properly.',
- 'group' => 'Examples',
- );
- }
-
- function setUp() {
- drupal_load('module', 'simpletest_example');
- parent::setUp();
- }
-
- /**
- * Call simpletest_example_empty_mysql_date and check that it returns correct
- * result.
- *
- * Note that no environment is provided; we're just testing the correct
- * behavior of a function when passed specific arguments.
- */
- public function testSimpletestUnitTestExampleFunction() {
- $result = simpletest_example_empty_mysql_date(NULL);
- $message = t('A NULL value should return TRUE.');
- $this->assertTrue($result, $message);
-
- $result = simpletest_example_empty_mysql_date('');
- $message = t('An empty string should return TRUE.');
- $this->assertTrue($result, $message);
-
- $result = simpletest_example_empty_mysql_date('0000-00-00');
- $message = t('An "empty" MySQL DATE should return TRUE.');
- $this->assertTrue($result, $message);
-
- $result = simpletest_example_empty_mysql_date(date('Y-m-d'));
- $message = t('A valid date should return FALSE.');
- $this->assertFalse($result, $message);
- }
- }
-