simpletest_example.test

  1. examples
    1. 6 simpletest_example/simpletest_example.test
    2. 7 simpletest_example/simpletest_example.test
    3. 8 simpletest_example/simpletest_example.test

An example of simpletest tests to accompany the tutorial at http://drupal.org/node/890654.

Classes

NameDescription
SimpletestExampleTestCaseThe 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…
SimpletestUnitTestExampleTestCaseAlthough 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.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * An example of simpletest tests to accompany the tutorial at
  5. * http://drupal.org/node/890654.
  6. */
  7. /**
  8. * The SimpletestExampleTestCase is a functional test case, meaning that it
  9. * actually exercises a particular sequence of actions through the web UI.
  10. * The majority of core test cases are done this way, but the Simpletest suite
  11. * also provides unit tests as demonstrated in the unit test case example later
  12. * in this file.
  13. *
  14. * Functional test cases are far slower to execute than unit test cases because
  15. * they require a complete Drupal install to be done for each test.
  16. *
  17. * @see DrupalWebTestCase
  18. * @see SimpletestUnitTestExampleTestCase
  19. */
  20. class SimpletestExampleTestCase extends DrupalWebTestCase {
  21. protected $privileged_user;
  22. public static function getInfo() {
  23. return array(
  24. 'name' => 'Simpletest Example',
  25. 'description' => 'Ensure that the simpletest_example content type provided functions properly.',
  26. 'group' => 'Examples',
  27. );
  28. }
  29. public function setUp() {
  30. parent::setUp('simpletest_example'); // Enable any modules required for the test
  31. // Create and log in our user. The user has the arbitrary privilege
  32. // 'extra special edit any simpletest_example' which the code uses
  33. // to grant access.
  34. $this->privileged_user = $this->drupalCreateUser(array('create simpletest_example content', 'extra special edit any simpletest_example'));
  35. $this->drupalLogin($this->privileged_user);
  36. }
  37. // Create a simpletest_example node using the node form
  38. public function testSimpleTestExampleCreate() {
  39. // Create node to edit.
  40. $edit = array();
  41. $edit['title'] = $this->randomName(8);
  42. $edit["body[und][0][value]"] = $this->randomName(16);
  43. $this->drupalPost('node/add/simpletest-example', $edit, t('Save'));
  44. $this->assertText(t('Simpletest Example Node Type @title has been created.', array('@title' => $edit['title'])));
  45. }
  46. // Create a simpletest_example node and then see if our user can edit it
  47. public function testSimpleTestExampleEdit() {
  48. $settings = array(
  49. 'type' => 'simpletest_example',
  50. 'title' => $this->randomName(32),
  51. 'body' => array(LANGUAGE_NONE => array(array($this->randomName(64)))),
  52. );
  53. $node = $this->drupalCreateNode($settings);
  54. // For debugging, we might output the node structure with $this->verbose()
  55. $this->verbose('Node created: ' . var_export($node, TRUE));
  56. // It would only be output if the testing settings had 'verbose' set.
  57. // We'll run this test normally, but not on the testbot, as it would
  58. // indicate that the examples module was failing tests.
  59. if (!$this->runningOnTestbot()) {
  60. // The debug() statement will output information into the test results.
  61. // It can also be used in Drupal 7 anywhere in code and will come out
  62. // as a drupal_set_message().
  63. debug('We are not running on the PIFR testing server, so will go ahead and catch the failure.');
  64. $this->drupalGet("node/{$node->nid}/edit");
  65. // Make sure we don't get a 401 unauthorized response:
  66. $this->assertResponse(200, t('User is allowed to edit the content.'));
  67. // Looking for title text in the page to determine whether we were
  68. // successful opening edit form.
  69. $this->assertText(t("@title", array('@title' => $settings['title'])), "Found title in edit form");
  70. }
  71. }
  72. /**
  73. * Detect if we're running on PIFR testbot; skip intentional failure in that
  74. * case. It happens that on the testbot the site under test is in a directory
  75. * named 'checkout' or 'site_under_test'.
  76. *
  77. * @return boolean
  78. * TRUE if running on testbot.
  79. */
  80. public function runningOnTestbot() {
  81. return (file_exists("../checkout") || file_exists("../site_under_test"));
  82. }
  83. }
  84. /**
  85. * Although most core test cases are based on DrupalWebTestCase and are
  86. * functional tests (exercising the web UI) we also have DrupalUnitTestCase,
  87. * which executes much faster because a Drupal install does not have to be
  88. * one. No environment is provided to a test case based on DrupalUnitTestCase;
  89. * it must be entirely self-contained.
  90. *
  91. * @see DrupalUnitTestCase
  92. */
  93. class SimpletestUnitTestExampleTestCase extends DrupalUnitTestCase {
  94. public static function getInfo() {
  95. return array(
  96. 'name' => 'Simpletest Example unit tests',
  97. 'description' => 'Test that simpletest_example_empty_mysql_date works properly.',
  98. 'group' => 'Examples',
  99. );
  100. }
  101. function setUp() {
  102. drupal_load('module', 'simpletest_example');
  103. parent::setUp();
  104. }
  105. /**
  106. * Call simpletest_example_empty_mysql_date and check that it returns correct
  107. * result.
  108. *
  109. * Note that no environment is provided; we're just testing the correct
  110. * behavior of a function when passed specific arguments.
  111. */
  112. public function testSimpletestUnitTestExampleFunction() {
  113. $result = simpletest_example_empty_mysql_date(NULL);
  114. $message = t('A NULL value should return TRUE.');
  115. $this->assertTrue($result, $message);
  116. $result = simpletest_example_empty_mysql_date('');
  117. $message = t('An empty string should return TRUE.');
  118. $this->assertTrue($result, $message);
  119. $result = simpletest_example_empty_mysql_date('0000-00-00');
  120. $message = t('An "empty" MySQL DATE should return TRUE.');
  121. $this->assertTrue($result, $message);
  122. $result = simpletest_example_empty_mysql_date(date('Y-m-d'));
  123. $message = t('A valid date should return FALSE.');
  124. $this->assertFalse($result, $message);
  125. }
  126. }
Login or register to post comments