WebformSubmissionTestCase.test 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Webform module submission tests.
  4. */
  5. class WebformSubmissionTestCase extends WebformTestCase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => t('Webform submission'),
  12. 'description' => t('Submits a sample webform and checks the database integrity.'),
  13. 'group' => t('Webform'),
  14. );
  15. }
  16. /**
  17. * Test sending a submission and check database integrity.
  18. */
  19. public function testWebformSubmission() {
  20. $this->drupalLogin($this->webform_users['admin']);
  21. $this->webformReset();
  22. $this->webformSubmissionExecute('sample');
  23. $loggedInUser = $this->loggedInUser;
  24. $this->drupalLogout();
  25. // Test webform_get_submission_count().
  26. $this->webformSubmissionExecute('sample');
  27. $count = webform_get_submission_count($this->webformForm()->nid);
  28. $this->assertIdentical((int) $count, 2, 'webform_get_submission_count() counts 2 total submissions.');
  29. $count = webform_get_submission_count($this->webformForm()->nid, $loggedInUser->uid);
  30. $this->assertIdentical((int) $count, 1, 'webform_get_submission_count() counts 1 submission from loggedInUser.');
  31. // Counting the anonymous submission doesn't work because
  32. // $_SESSION['webform_submission'] is not populated in testing.
  33. // Test _webform_submission_prepare_mail().
  34. $node = node_load($this->webformForm()->nid);
  35. $submission = webform_get_submissions($node->nid);
  36. $submission = array_pop($submission);
  37. $email = array(
  38. 'status' => TRUE,
  39. 'html' => FALSE,
  40. 'template' => 'default',
  41. 'from_address' => 'Test From',
  42. 'from_name' => 'from@example.com',
  43. 'subject' => 'Test Subject',
  44. 'email' => 'to@example.com',
  45. );
  46. variable_set('webform_email_replyto', TRUE);
  47. variable_set('webform_email_address_format', 'long');
  48. variable_set('webform_default_from_name', 'Default "From" Name');
  49. variable_set('webform_default_from_address', 'default-from@example.com');
  50. $prepared_email = _webform_submission_prepare_mail($node, $submission, $email);
  51. $this->assertIdentical($prepared_email['mail_params']['email']['from'], '"from@example.com via Default \'From\' Name" <default-from@example.com>', 'From address is correctly set in _webform_submission_prepare_mail().');
  52. }
  53. /**
  54. * Test a submission that uses default values, and check database integrity.
  55. */
  56. public function testWebformSubmissionDefault() {
  57. $this->drupalLogin($this->webform_users['admin']);
  58. $this->webformReset();
  59. $this->webformSubmissionExecute('default');
  60. $this->drupalLogout();
  61. }
  62. /**
  63. * Test validation errors on each component that has specialized validation.
  64. */
  65. public function testWebformSubmissionValidate() {
  66. $this->drupalLogin($this->webform_users['admin']);
  67. $this->webformReset();
  68. $this->webformSubmissionValidateExecute();
  69. $this->drupalLogout();
  70. }
  71. /**
  72. * Test that required fields with no default value can't be submitted as-is.
  73. */
  74. public function testWebformSubmissionRequiredComponents() {
  75. $this->drupalLogin($this->webform_users['admin']);
  76. $this->webformReset();
  77. // Create the Webform test node, and set all components to be required
  78. // with no default value.
  79. $node = $this->webformForm();
  80. $node = node_load($node->nid);
  81. foreach ($node->webform['components'] as &$component) {
  82. $component['value'] = '';
  83. $component['required'] = '1';
  84. }
  85. node_save($node);
  86. // Submit the webform with no data. We should get a message that all the
  87. // components are required. The exceptions are hidden fields, which can't be
  88. // made required, and date fields, which default to the current date when no
  89. // default value is provided; therefore, we don't expect a message for
  90. // those.
  91. $this->drupalPost('node/' . $node->nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  92. foreach ($node->webform['components'] as $component) {
  93. if ($component['type'] != 'hidden' && $component['type'] != 'date') {
  94. $this->assertText(t('!name field is required.', array('!name' => $component['name'])));
  95. }
  96. }
  97. $this->drupalLogout();
  98. }
  99. /**
  100. * Test length validation.
  101. */
  102. public function testWebformSubmissionComponentLength() {
  103. $this->drupalLogin($this->webform_users['admin']);
  104. $this->webformReset();
  105. // Create the Webform test node.
  106. $node = $this->webformForm();
  107. $node = node_load($node->nid);
  108. // Get the cid of the textfield component.
  109. foreach ($node->webform['components'] as &$component) {
  110. if ($component['form_key'] === 'textfield') {
  111. $textfield_cid = $component['cid'];
  112. break;
  113. }
  114. }
  115. // Set length validation rules.
  116. $node->webform['components'][$textfield_cid]['extra']['maxlength'] = 5;
  117. $node->webform['components'][$textfield_cid]['extra']['minlength'] = 4;
  118. node_save($node);
  119. // Text value that is too long.
  120. $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '123456'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  121. $this->assertRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => $node->webform['components'][$textfield_cid]['name'], '%max' => 5, '%length' => 6)));
  122. // Text value that is too short.
  123. $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '123'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  124. $this->assertRaw(t('!name cannot be shorter than %min characters but is currently %length characters long.', array('!name' => $node->webform['components'][$textfield_cid]['name'], '%min' => 4, '%length' => 3)));
  125. // Test value that meets validation rules has no error message.
  126. $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '12345'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  127. $this->assertNoPattern('/ cannot be (longer|shorter) than /');
  128. }
  129. /**
  130. * Execute the submission test.
  131. *
  132. * @param string $value_type
  133. * The values to be submitted to the webform. Either "sample" or "default".
  134. */
  135. public function webformSubmissionExecute($value_type = 'sample') {
  136. $path = drupal_get_path('module', 'webform');
  137. module_load_include('inc', 'webform', 'includes/webform.submissions');
  138. // Create a new Webform test node.
  139. $node = $this->webformForm();
  140. $submission_values = $value_type == 'sample' ? $this->webformPost() : array();
  141. // Visit the node page with the "foo=bar" query, to test
  142. // [current-page:query:?] default values.
  143. $this->drupalGet('node/' . $node->nid, array('query' => array('foo' => 'bar')));
  144. $this->assertText($node->title, t('Webform node created and accessible at !url', array('!url' => 'node/' . $node->nid)), t('Webform'));
  145. // Submit our test data.
  146. $this->drupalPost(NULL, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  147. // Confirm that the submission has been created.
  148. $this->assertText($node->webform['confirmation'], t('Confirmation message "@confirmation" received.', array('@confirmation' => $node->webform['confirmation'])), t('Webform'));
  149. // Get the SID of the new submission.
  150. $matches = array();
  151. preg_match('/sid=([0-9]+)/', $this->getUrl(), $matches);
  152. $sid = $matches[1];
  153. // Pull in the database submission and check the values.
  154. drupal_static_reset('webform_get_submission');
  155. $actual_submission = webform_get_submission($node->nid, $sid);
  156. $component_info = $this->webformComponents();
  157. foreach ($node->webform['components'] as $cid => $component) {
  158. $stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
  159. $actual_value = $actual_submission->data[$cid];
  160. $result = $this->assertEqual($stable_value, $actual_value, t('Component @form_key data integrity check when using @type values.', array('@form_key' => $component['form_key'], '@type' => $value_type)), t('Webform'));
  161. if (!$result || $result === 'fail') {
  162. $this->fail(t("Expected !expected\n\nRecieved !recieved", array('!expected' => print_r($stable_value, TRUE), '!recieved' => print_r($actual_value, TRUE))), t('Webform'));
  163. }
  164. }
  165. }
  166. /**
  167. * Execute a validation check for a single component.
  168. */
  169. public function webformSubmissionValidateExecute() {
  170. $path = drupal_get_path('module', 'webform');
  171. module_load_include('inc', 'webform', 'includes/webform.submissions');
  172. // Create a new Webform test node.
  173. $node = $this->webformForm();
  174. // Visit the node page.
  175. $this->drupalGet('node/' . $node->nid);
  176. foreach ($this->webformComponents() as $key => $component_info) {
  177. if (isset($component_info['error values'])) {
  178. foreach ($component_info['error values'] as $value => $error_message) {
  179. $submission_values = array();
  180. $submission_values["submitted[$key]"] = $value;
  181. // Submit our test data.
  182. $this->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  183. // Confirm that the validation error occurred and the submission did
  184. // not save.
  185. $this->assertRaw($error_message, t('Validation message properly thrown: "%message".', array('%message' => $error_message)), t('Webform'));
  186. $this->assertFalse(preg_match('/sid=([0-9]+)/', $this->getUrl()), t('Submission not saved.'));
  187. }
  188. }
  189. }
  190. }
  191. }