simpletest_example.test 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * @ingroup simpletest_example
  21. */
  22. class SimpleTestExampleTestCase extends DrupalWebTestCase {
  23. protected $privilegedUser;
  24. /**
  25. * Give display information to the SimpleTest system.
  26. *
  27. * getInfo() returns a keyed array of information for SimpleTest to show.
  28. *
  29. * It's a good idea to organize your tests consistently using the 'group'
  30. * key.
  31. */
  32. public static function getInfo() {
  33. return array(
  34. 'name' => 'SimpleTest Example',
  35. 'description' => 'Ensure that the simpletest_example content type provided functions properly.',
  36. 'group' => 'Examples',
  37. );
  38. }
  39. /**
  40. * Set up the test environment.
  41. *
  42. * This method is called once per test method, before the test is executed.
  43. * It gives you a chance to control the setup of the test environment.
  44. *
  45. * If you need a different test environment, then you should create another
  46. * test class which overloads DrupalWebTestCase::setUp() differently.
  47. *
  48. * @see DrupalWebTestCase::setUp()
  49. */
  50. public function setUp() {
  51. // We call parent::setUp() with the list of modules we want to enable.
  52. // This can be an array or just a list of arguments.
  53. parent::setUp('simpletest_example');
  54. // Create and log in our user. The user has the arbitrary privilege
  55. // 'extra special edit any simpletest_example' which is provided by
  56. // our module to grant access.
  57. $this->privilegedUser = $this->drupalCreateUser(array('create simpletest_example content', 'extra special edit any simpletest_example'));
  58. $this->drupalLogin($this->privilegedUser);
  59. }
  60. /**
  61. * Create a simpletest_example node using the node form.
  62. */
  63. public function testSimpleTestExampleCreate() {
  64. // Create node to edit.
  65. $edit = array();
  66. $edit['title'] = $this->randomName(8);
  67. $edit["body[und][0][value]"] = $this->randomName(16);
  68. $this->drupalPost('node/add/simpletest-example', $edit, t('Save'));
  69. $this->assertText(t('SimpleTest Example Node Type @title has been created.', array('@title' => $edit['title'])));
  70. }
  71. /**
  72. * Create a simpletest_example node and then see if our user can edit it.
  73. */
  74. public function testSimpleTestExampleEdit() {
  75. $settings = array(
  76. 'type' => 'simpletest_example',
  77. 'title' => $this->randomName(32),
  78. 'body' => array(LANGUAGE_NONE => array(array($this->randomName(64)))),
  79. );
  80. $node = $this->drupalCreateNode($settings);
  81. // For debugging, we might output the node structure with $this->verbose()
  82. // It would only be output if the testing settings had 'verbose' set.
  83. $this->verbose('Node created: ' . var_export($node, TRUE));
  84. // We'll run this test normally, but not on the testbot, as it would
  85. // indicate that the examples module was failing tests.
  86. if (!$this->runningOnTestbot()) {
  87. // The debug() statement will output information into the test results.
  88. // It can also be used in Drupal 7 anywhere in code and will come out
  89. // as a drupal_set_message().
  90. debug('We are not running on the PIFR testing server, so will go ahead and catch the failure.');
  91. $this->drupalGet("node/{$node->nid}/edit");
  92. // Make sure we don't get a 401 unauthorized response:
  93. $this->assertResponse(200, 'User is allowed to edit the content.');
  94. // Looking for title text in the page to determine whether we were
  95. // successful opening edit form.
  96. $this->assertText(t("@title", array('@title' => $settings['title'])), "Found title in edit form");
  97. }
  98. }
  99. /**
  100. * Detect if we're running on PIFR testbot.
  101. *
  102. * Skip intentional failure in that case. It happens that on the testbot the
  103. * site under test is in a directory named 'checkout' or 'site_under_test'.
  104. *
  105. * @return bool
  106. * TRUE if running on testbot.
  107. */
  108. public function runningOnTestbot() {
  109. // @todo: Add this line back once the testbot variable is available.
  110. // https://www.drupal.org/node/2565181
  111. // return env('DRUPALCI');
  112. return TRUE;
  113. }
  114. }
  115. /**
  116. * Although most core test cases are based on DrupalWebTestCase and are
  117. * functional tests (exercising the web UI) we also have DrupalUnitTestCase,
  118. * which executes much faster because a Drupal install does not have to be
  119. * one. No environment is provided to a test case based on DrupalUnitTestCase;
  120. * it must be entirely self-contained.
  121. *
  122. * @see DrupalUnitTestCase
  123. *
  124. * @ingroup simpletest_example
  125. */
  126. class SimpleTestUnitTestExampleTestCase extends DrupalUnitTestCase {
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public static function getInfo() {
  131. return array(
  132. 'name' => 'SimpleTest Example unit tests',
  133. 'description' => 'Test that simpletest_example_empty_mysql_date works properly.',
  134. 'group' => 'Examples',
  135. );
  136. }
  137. /**
  138. * Set up the test environment.
  139. *
  140. * Note that we use drupal_load() instead of passing our module dependency
  141. * to parent::setUp(). That's because we're using DrupalUnitTestCase, and
  142. * thus we don't want to install the module, only load it's code.
  143. *
  144. * Also, DrupalUnitTestCase can't actually install modules. This is by
  145. * design.
  146. */
  147. public function setUp() {
  148. drupal_load('module', 'simpletest_example');
  149. parent::setUp();
  150. }
  151. /**
  152. * Test simpletest_example_empty_mysql_date().
  153. *
  154. * Note that no environment is provided; we're just testing the correct
  155. * behavior of a function when passed specific arguments.
  156. */
  157. public function testSimpleTestUnitTestExampleFunction() {
  158. $result = simpletest_example_empty_mysql_date(NULL);
  159. // Note that test assertion messages should never be translated, so
  160. // this string is not wrapped in t().
  161. $message = 'A NULL value should return TRUE.';
  162. $this->assertTrue($result, $message);
  163. $result = simpletest_example_empty_mysql_date('');
  164. $message = 'An empty string should return TRUE.';
  165. $this->assertTrue($result, $message);
  166. $result = simpletest_example_empty_mysql_date('0000-00-00');
  167. $message = 'An "empty" MySQL DATE should return TRUE.';
  168. $this->assertTrue($result, $message);
  169. $result = simpletest_example_empty_mysql_date(date('Y-m-d'));
  170. $message = 'A valid date should return FALSE.';
  171. $this->assertFalse($result, $message);
  172. }
  173. }
  174. /**
  175. * SimpleTestExampleMockModuleTestCase allows us to demonstrate how you can
  176. * use a mock module to aid in functional testing in Drupal.
  177. *
  178. * If you have some functionality that's not intrinsic to the code under test,
  179. * you can add a special mock module that only gets installed during test
  180. * time. This allows you to implement APIs created by your module, or otherwise
  181. * exercise the code in question.
  182. *
  183. * This test case class is very similar to SimpleTestExampleTestCase. The main
  184. * difference is that we enable the simpletest_example_test module in the
  185. * setUp() method. Then we can test for behaviors provided by that module.
  186. *
  187. * @see SimpleTestExampleTestCase
  188. *
  189. * @ingroup simpletest_example
  190. */
  191. class SimpleTestExampleMockModuleTestCase extends DrupalWebTestCase {
  192. /**
  193. * Give display information to the SimpleTest system.
  194. *
  195. * getInfo() returns a keyed array of information for SimpleTest to show.
  196. *
  197. * It's a good idea to organize your tests consistently using the 'group'
  198. * key.
  199. */
  200. public static function getInfo() {
  201. return array(
  202. 'name' => 'SimpleTest Mock Module Example',
  203. 'description' => "Ensure that we can modify SimpleTest Example's content types.",
  204. 'group' => 'Examples',
  205. );
  206. }
  207. /**
  208. * Set up the test environment.
  209. *
  210. * Note that we're enabling both the simpletest_example and
  211. * simpletest_example_test modules.
  212. */
  213. public function setUp() {
  214. // We call parent::setUp() with the list of modules we want to enable.
  215. parent::setUp('simpletest_example', 'simpletest_example_test');
  216. }
  217. /**
  218. * Test modifications made by our mock module.
  219. *
  220. * We create a simpletest_example node and then see if our submodule
  221. * operated on it.
  222. */
  223. public function testSimpleTestExampleMockModule() {
  224. // Create a user.
  225. $test_user = $this->drupalCreateUser(array('access content'));
  226. // Log them in.
  227. $this->drupalLogin($test_user);
  228. // Set up some content.
  229. $settings = array(
  230. 'type' => 'simpletest_example',
  231. 'title' => $this->randomName(32),
  232. 'body' => array(LANGUAGE_NONE => array(array($this->randomName(64)))),
  233. );
  234. // Create the content node.
  235. $node = $this->drupalCreateNode($settings);
  236. // View the node.
  237. $this->drupalGet("node/{$node->nid}");
  238. // Check that our module did it's thing.
  239. $this->assertText(t('The test module did its thing.'), "Found evidence of test module.");
  240. }
  241. }