form_example.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // #11: Confirmation form.
  143. // Try to submit without a name.
  144. $url = 'examples/form_example/tutorial/11';
  145. $this->drupalPost($url, array(), t('Submit'));
  146. $this->assertText('Name field is required.');
  147. // Verify that we can enter a name and get the confirmation form.
  148. $this->drupalPost(
  149. $url,
  150. array('name' => t('name 1')), t('Submit')
  151. );
  152. $this->assertText(t('Is this really your name?'));
  153. $this->assertFieldById('edit-name', 'name 1');
  154. // Check the 'yes' button.
  155. $confirmation_text = t("Confirmation form submission recieved. According to your submission your name is '@name'", array('@name' => 'name 1'));
  156. $url = 'examples/form_example/tutorial/11/confirm/name%201';
  157. $this->drupalPost($url, array(), t('This is my name'));
  158. $this->assertText($confirmation_text);
  159. // Check the 'no' button.
  160. $this->drupalGet($url);
  161. $this->clickLink(t('Nope, not my name'));
  162. $this->assertNoText($confirmation_text);
  163. }
  164. /**
  165. * Test Wizard tutorial.
  166. *
  167. * @TODO improve this using drupal_form_submit
  168. */
  169. public function testWizard() {
  170. // Check if the wizard is there.
  171. $this->drupalGet('examples/form_example/wizard');
  172. $this->assertText(t('Extensible wizard example'));
  173. $first_name = $this->randomName(8);
  174. $last_name = $this->randomName(8);
  175. $city = $this->randomName(8);
  176. $aunts_name = $this->randomName(8);
  177. // Submit the first step of the wizard.
  178. $options = array(
  179. 'first_name' => $first_name,
  180. 'last_name' => $last_name,
  181. );
  182. $this->drupalPost('examples/form_example/wizard', $options, t('Next'));
  183. // A label city is created, and two buttons appear, Previous and Next.
  184. $this->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));
  185. // Go back to the beginning and verify that the value is there.
  186. $this->drupalPost(NULL, array(), t('Previous'));
  187. $this->assertFieldByName('first_name', $first_name);
  188. $this->assertFieldByName('last_name', $last_name);
  189. // Go next. We should keep our values.
  190. $this->drupalPost(NULL, array(), t('Next'));
  191. $this->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));
  192. // Try "San Francisco".
  193. $this->drupalPost(NULL, array('city' => 'San Francisco'), t('Next'));
  194. $this->assertText(t('You were warned not to enter "San Francisco"'));
  195. // Try the real city.
  196. $this->drupalPost(NULL, array('city' => $city), t('Next'));
  197. // Enter the Aunt's name, but then the previous button.
  198. $this->drupalPost(NULL, array('aunts_name' => $aunts_name), t('Previous'));
  199. $this->assertFieldByName('city', $city);
  200. // Go to first step and re-check all fields.
  201. $this->drupalPost(NULL, array(), t('Previous'));
  202. $this->assertFieldByName('first_name', $first_name);
  203. $this->assertFieldByName('last_name', $last_name);
  204. // Re-check second step.
  205. $this->drupalPost(NULL, array(), t('Next'));
  206. $this->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));
  207. $this->assertFieldByName('city', $city);
  208. // Re-check third step.
  209. $this->drupalPost(NULL, array(), t('Next'));
  210. $this->assertFieldByName('aunts_name', $aunts_name);
  211. // Press finish and check for correct values.
  212. $this->drupalPost(NULL, array(), t('Finish'));
  213. $this->assertRaw(t('[first_name] =&gt; @first_name', array('@first_name' => $first_name)));
  214. $this->assertRaw(t('[last_name] =&gt; @last_name', array('@last_name' => $last_name)));
  215. $this->assertRaw(t('[city] =&gt; @city', array('@city' => $city)));
  216. $this->assertRaw(t('[aunts_name] =&gt; @aunts_name', array('@aunts_name' => $aunts_name)));
  217. }
  218. /**
  219. * Test the element_example form for correct behavior.
  220. */
  221. public function testElementExample() {
  222. // Make one basic POST with a set of values and check for correct responses.
  223. $edit = array(
  224. 'a_form_example_textfield' => $this->randomName(),
  225. 'a_form_example_checkbox' => TRUE,
  226. 'a_form_example_element_discrete[areacode]' => sprintf('%03d', rand(0, 999)),
  227. 'a_form_example_element_discrete[prefix]' => sprintf('%03d', rand(0, 999)),
  228. 'a_form_example_element_discrete[extension]' => sprintf('%04d', rand(0, 9999)),
  229. 'a_form_example_element_combined[areacode]' => sprintf('%03d', rand(0, 999)),
  230. 'a_form_example_element_combined[prefix]' => sprintf('%03d', rand(0, 999)),
  231. 'a_form_example_element_combined[extension]' => sprintf('%04d', rand(0, 9999)),
  232. );
  233. $this->drupalPost('examples/form_example/element_example', $edit, t('Submit'));
  234. $this->assertText(t('a_form_example_textfield has value @value', array('@value' => $edit['a_form_example_textfield'])));
  235. $this->assertText(t('a_form_example_checkbox has value 1'));
  236. $this->assertPattern(t('/areacode.*!areacode/', array('!areacode' => $edit['a_form_example_element_discrete[areacode]'])));
  237. $this->assertPattern(t('/prefix.*!prefix/', array('!prefix' => $edit['a_form_example_element_discrete[prefix]'])));
  238. $this->assertPattern(t('/extension.*!extension/', array('!extension' => $edit['a_form_example_element_discrete[extension]'])));
  239. $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]'])));
  240. // Now flip the checkbox and check for correct behavior.
  241. $edit['a_form_example_checkbox'] = FALSE;
  242. $this->drupalPost('examples/form_example/element_example', $edit, t('Submit'));
  243. $this->assertText(t('a_form_example_checkbox has value 0'));
  244. }
  245. }