form_example.test

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

Test file for form_example module.

Classes

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