form_example.test

You are here

  1. 7 form_example/form_example.test
  2. 6 form_example/form_example.test

Test file for form_example module.

Classes

Namesort descending Description
FormExampleTestCase Default test case for the form_example module.

File

form_example/form_example.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Test file for form_example module.
  5. */
  6. /**
  7. * Default test case for the form_example module.
  8. *
  9. * @ingroup form_example
  10. */
  11. class FormExampleTestCase extends DrupalWebTestCase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Form Example',
  18. 'description' => 'Various tests on the form_example module.' ,
  19. 'group' => 'Examples',
  20. );
  21. }
  22. /**
  23. * Enable modules.
  24. */
  25. public function setUp() {
  26. parent::setUp('form_example');
  27. }
  28. /**
  29. * Test each tutorial.
  30. */
  31. public function testTutorials() {
  32. // Tutorial #1
  33. $this->drupalGet('examples/form_example/tutorial');
  34. $this->assertText(t('#9'));
  35. // #2
  36. $this->drupalPost('examples/form_example/tutorial/2', array('name' => t('name')), t('Submit'));
  37. // #4
  38. $this->drupalPost('examples/form_example/tutorial/4',
  39. array('first' => t('firstname'), 'last' => t('lastname')), t('Submit'));
  40. $this->drupalPost('examples/form_example/tutorial/4', array(), t('Submit'));
  41. $this->assertText(t('First name field is required'));
  42. $this->assertText(t('Last name field is required'));
  43. // #5
  44. $this->drupalPost('examples/form_example/tutorial/5',
  45. array('first' => t('firstname'), 'last' => t('lastname')), t('Submit'));
  46. $this->assertText(t('Please enter your first name'));
  47. $this->drupalPost('examples/form_example/tutorial/4', array(), t('Submit'));
  48. $this->assertText(t('First name field is required'));
  49. $this->assertText(t('Last name field is required'));
  50. // #6
  51. $this->drupalPost(
  52. 'examples/form_example/tutorial/6',
  53. array(
  54. 'first' => t('firstname'),
  55. 'last' => t('lastname'),
  56. 'year_of_birth' => 1955,
  57. ),
  58. t('Submit'));
  59. $this->assertNoText(t('Enter a year between 1900 and 2000'));
  60. $this->drupalPost(
  61. 'examples/form_example/tutorial/6',
  62. array(
  63. 'first' => t('firstname'),
  64. 'last' => t('lastname'),
  65. 'year_of_birth' => 1855,
  66. ),
  67. t('Submit')
  68. );
  69. $this->assertText(t('Enter a year between 1900 and 2000'));
  70. // #7
  71. $this->drupalPost(
  72. 'examples/form_example/tutorial/7',
  73. array(
  74. 'first' => t('firstname'),
  75. 'last' => t('lastname'),
  76. 'year_of_birth' => 1955,
  77. ),
  78. t('Submit')
  79. );
  80. $this->assertText(t('The form has been submitted. name="firstname lastname", year of birth=1955'));
  81. $this->drupalPost(
  82. 'examples/form_example/tutorial/7',
  83. array(
  84. 'first' => t('firstname'),
  85. 'last' => t('lastname'),
  86. 'year_of_birth' => 1855,
  87. ),
  88. t('Submit')
  89. );
  90. $this->assertText(t('Enter a year between 1900 and 2000'));
  91. // Test tutorial #8.
  92. $this->drupalPost(
  93. 'examples/form_example/tutorial/8',
  94. array(
  95. 'first' => t('firstname'),
  96. 'last' => t('lastname'),
  97. 'year_of_birth' => 1955,
  98. ),
  99. t('Next >>')
  100. );
  101. $this->drupalPost(NULL, array('color' => t('green')), t('<< Back'));
  102. $this->drupalPost(NULL, array(), t('Next >>'));
  103. $this->drupalPost(NULL, array('color' => t('red')), t('Submit'));
  104. $this->assertText(t('The form has been submitted. name="firstname lastname", year of birth=1955'));
  105. $this->assertText(t('And the favorite color is red'));
  106. // #9
  107. $url = 'examples/form_example/tutorial/9';
  108. for ($i = 1; $i <= 4; $i++) {
  109. if ($i > 1) {
  110. // Later steps of multistep form take NULL.
  111. $url = NULL;
  112. }
  113. $this->drupalPost(
  114. $url,
  115. array(
  116. "name[$i][first]" => "firstname $i",
  117. "name[$i][last]" => "lastname $i",
  118. "name[$i][year_of_birth]" => 1950 + $i,
  119. ),
  120. t('Add another name')
  121. );
  122. $this->assertText(t('Name #@num', array('@num' => $i + 1)));
  123. }
  124. // Now remove the last name added (#5).
  125. $this->drupalPost(NULL, array(), t('Remove latest name'));
  126. $this->assertNoText("Name #5");
  127. $this->drupalPost(NULL, array(), t('Submit'));
  128. $this->assertText('Form 9 has been submitted');
  129. for ($i = 1; $i <= 4; $i++) {
  130. $this->assertText(t('@num: firstname @num lastname @num (@year)', array('@num' => $i, '@year' => 1950 + $i)));
  131. }
  132. // #10
  133. $url = 'examples/form_example/tutorial/10';
  134. $this->drupalPost($url, array(), t('Submit'));
  135. $this->assertText(t('No file was uploaded.'));
  136. // Get sample images.
  137. $images = $this->drupalGetTestFiles('image');
  138. foreach ($images as $image) {
  139. $this->drupalPost($url, array('files[file]' => drupal_realpath($image->uri)), t('Submit'));
  140. $this->assertText(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $image->filename)));
  141. }
  142. }
  143. /**
  144. * Test Wizard tutorial.
  145. *
  146. * @TODO improve this using drupal_form_submit
  147. */
  148. public function testWizard() {
  149. // Check if the wizard is there.
  150. $this->drupalGet('examples/form_example/wizard');
  151. $this->assertText(t('Extensible wizard example'));
  152. $first_name = $this->randomName(8);
  153. $last_name = $this->randomName(8);
  154. $city = $this->randomName(8);
  155. $aunts_name = $this->randomName(8);
  156. // Submit the first step of the wizard.
  157. $options = array(
  158. 'first_name' => $first_name,
  159. 'last_name' => $last_name,
  160. );
  161. $this->drupalPost('examples/form_example/wizard', $options, t('Next'));
  162. // A label city is created, and two buttons appear, Preivous and Next.
  163. $this->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));
  164. // Go back to the beginning and verify that the value is there.
  165. $this->drupalPost(NULL, array(), t('Previous'));
  166. $this->assertFieldByName('first_name', $first_name);
  167. $this->assertFieldByName('last_name', $last_name);
  168. // Go next. We should keep our values.
  169. $this->drupalPost(NULL, array(), t('Next'));
  170. $this->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));
  171. // Try "San Francisco".
  172. $this->drupalPost(NULL, array('city' => 'San Francisco'), t('Next'));
  173. $this->assertText(t('You were warned not to enter "San Francisco"'));
  174. // Try the real city.
  175. $this->drupalPost(NULL, array('city' => $city), t('Next'));
  176. // Enter the Aunt's name, but then the previous button.
  177. $this->drupalPost(NULL, array('aunts_name' => $aunts_name), t('Previous'));
  178. $this->assertFieldByName('city', $city);
  179. // Now go forward and then press finish; check for correct values.
  180. $this->drupalPost(NULL, array(), t('Next'));
  181. $this->drupalPost(NULL, array('aunts_name' => $aunts_name), t('Finish'));
  182. $this->assertRaw(t('[first_name] =&gt; @first_name', array('@first_name' => $first_name)));
  183. $this->assertRaw(t('[last_name] =&gt; @last_name', array('@last_name' => $last_name)));
  184. $this->assertRaw(t('[city] =&gt; @city', array('@city' => $city)));
  185. $this->assertRaw(t('[aunts_name] =&gt; @aunts_name', array('@aunts_name' => $aunts_name)));
  186. }
  187. /**
  188. * Test the element_example form for correct behavior.
  189. */
  190. public function testElementExample() {
  191. // Make one basic POST with a set of values and check for correct responses.
  192. $edit = array(
  193. 'a_form_example_textfield' => $this->randomName(),
  194. 'a_form_example_checkbox' => TRUE,
  195. 'a_form_example_element_discrete[areacode]' => sprintf('%03d', rand(0, 999)),
  196. 'a_form_example_element_discrete[prefix]' => sprintf('%03d', rand(0, 999)),
  197. 'a_form_example_element_discrete[extension]' => sprintf('%04d', rand(0, 9999)),
  198. 'a_form_example_element_combined[areacode]' => sprintf('%03d', rand(0, 999)),
  199. 'a_form_example_element_combined[prefix]' => sprintf('%03d', rand(0, 999)),
  200. 'a_form_example_element_combined[extension]' => sprintf('%04d', rand(0, 9999)),
  201. );
  202. $this->drupalPost('examples/form_example/element_example', $edit, t('Submit'));
  203. $this->assertText(t('a_form_example_textfield has value @value', array('@value' => $edit['a_form_example_textfield'])));
  204. $this->assertText(t('a_form_example_checkbox has value 1'));
  205. $this->assertPattern(t('/areacode.*!areacode/', array('!areacode' => $edit['a_form_example_element_discrete[areacode]'])));
  206. $this->assertPattern(t('/prefix.*!prefix/', array('!prefix' => $edit['a_form_example_element_discrete[prefix]'])));
  207. $this->assertPattern(t('/extension.*!extension/', array('!extension' => $edit['a_form_example_element_discrete[extension]'])));
  208. $this->assertText(t('a_form_example_element_combined has value @value', array('@value' => $edit['a_form_example_element_combined[areacode]'] . $edit['a_form_example_element_combined[prefix]'] . $edit['a_form_example_element_combined[extension]'])));
  209. // Now flip the checkbox and check for correct behavior.
  210. $edit['a_form_example_checkbox'] = FALSE;
  211. $this->drupalPost('examples/form_example/element_example', $edit, t('Submit'));
  212. $this->assertText(t('a_form_example_checkbox has value 0'));
  213. }
  214. }