submission.test 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Webform module submission tests.
  5. */
  6. include_once(dirname(__FILE__) . '/webform.test');
  7. class WebformSubmissionTestCase extends WebformTestCase {
  8. /**
  9. * Implements getInfo().
  10. */
  11. public static function getInfo() {
  12. return array(
  13. 'name' => t('Webform submission'),
  14. 'description' => t('Submits a sample webform and checks the database integrity.'),
  15. 'group' => t('Webform'),
  16. );
  17. }
  18. /**
  19. * Implements setUp().
  20. */
  21. function setUp() {
  22. parent::setUp();
  23. }
  24. /**
  25. * Implements tearDown().
  26. */
  27. function tearDown() {
  28. parent::tearDown();
  29. }
  30. /**
  31. * Test sending a submission and check database integrity.
  32. */
  33. function testWebformSubmission() {
  34. $this->drupalLogin($this->webform_users['admin']);
  35. $this->webformReset();
  36. $this->webformSubmissionExecute('sample');
  37. $this->drupalLogout();
  38. }
  39. /**
  40. * Test a submission that uses default values, and check database integrity.
  41. */
  42. function testWebformSubmissionDefault() {
  43. $this->drupalLogin($this->webform_users['admin']);
  44. $this->webformReset();
  45. $this->webformSubmissionExecute('default');
  46. $this->drupalLogout();
  47. }
  48. /**
  49. * Test validation errors on each component that has specialized validation.
  50. */
  51. function testWebformSubmissionValidate() {
  52. $this->drupalLogin($this->webform_users['admin']);
  53. $this->webformReset();
  54. $this->webformSubmissionValidateExecute();
  55. $this->drupalLogout();
  56. }
  57. /**
  58. * Test that required fields with no default value can't be submitted as-is.
  59. */
  60. function testWebformSubmissionRequiredComponents() {
  61. $this->drupalLogin($this->webform_users['admin']);
  62. $this->webformReset();
  63. // Create the Webform test node, and set all components to be mandatory
  64. // with no default value.
  65. $node = $this->testWebformForm();
  66. $node = node_load($node->nid);
  67. foreach ($node->webform['components'] as &$component) {
  68. $component['value'] = '';
  69. $component['mandatory'] = '1';
  70. }
  71. node_save($node);
  72. // Submit the webform with no data. We should get a message that all the
  73. // components are required. (The exceptions are hidden fields, which can't
  74. // be made mandatory, and date fields, which default to the current date
  75. // when no default value is provided; therefore, we don't expect a message
  76. // for those.)
  77. $this->drupalPost('node/' . $node->nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  78. foreach ($node->webform['components'] as $component) {
  79. if ($component['type'] != 'hidden' && $component['type'] != 'date') {
  80. $this->assertText(t('!name field is required.', array('!name' => $component['name'])));
  81. }
  82. }
  83. $this->drupalLogout();
  84. }
  85. /**
  86. * Execute the submission test.
  87. *
  88. * @param $value_type
  89. * The values to be submitted to the webform. Either "sample" or "default".
  90. */
  91. function webformSubmissionExecute($value_type = 'sample') {
  92. $path = drupal_get_path('module', 'webform');
  93. module_load_include('inc', 'webform', 'includes/webform.submissions');
  94. // Create a new Webform test node.
  95. $node = $this->testWebformForm();
  96. $submission_values = $value_type == 'sample' ? $this->testWebformPost() : array();
  97. // Visit the node page with the "foo=bar" query, to test %get[] default values.
  98. $this->drupalGet('node/' . $node->nid, array('query' => array('foo' => 'bar')));
  99. $this->assertText($node->body[LANGUAGE_NONE][0]['value'], t('Webform node created and accessible at !url', array('!url' => 'node/' . $node->nid)), t('Webform'));
  100. // Submit our test data.
  101. $this->drupalPost(NULL, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  102. // Confirm that the submission has been created.
  103. $this->assertText($node->webform['confirmation'], t('Confirmation message "@confirmation" received.', array('@confirmation' => $node->webform['confirmation'])), t('Webform'));
  104. // Get the SID of the new submission.
  105. $matches = array();
  106. preg_match('/sid=([0-9]+)/', $this->getUrl(), $matches);
  107. $sid = $matches[1];
  108. // Pull in the database submission and check the values.
  109. $actual_submission = webform_get_submission($node->nid, $sid, TRUE);
  110. $component_info = $this->testWebformComponents();
  111. foreach ($node->webform['components'] as $cid => $component) {
  112. $stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
  113. $actual_value = $actual_submission->data[$cid]['value'];
  114. $result = $this->assertEqual($stable_value, $actual_value, t('Component @form_key data integrity check', array('@form_key' => $component['form_key'])), t('Webform'));
  115. if (!$result || $result === 'fail') {
  116. $this->fail(t('Expected !expected', array('!expected' => print_r($stable_value, TRUE))) . "\n\n" . t('Recieved !recieved', array('!recieved' => print_r($actual_value, TRUE))), t('Webform'));
  117. }
  118. }
  119. }
  120. /**
  121. * Execute a validation check for a single component.
  122. *
  123. * @param $value_type
  124. * The values to be submitted to the webform. Either "sample" or "default".
  125. */
  126. function webformSubmissionValidateExecute() {
  127. $path = drupal_get_path('module', 'webform');
  128. module_load_include('inc', 'webform', 'includes/webform.submissions');
  129. // Create a new Webform test node.
  130. $node = $this->testWebformForm();
  131. // Visit the node page.
  132. $this->drupalGet('node/' . $node->nid);
  133. foreach ($this->testWebformComponents() as $key => $component_info) {
  134. if (isset($component_info['error values'])) {
  135. foreach ($component_info['error values'] as $value => $error_message) {
  136. $submission_values = array();
  137. $submission_values["submitted[$key]"] = $value;
  138. // Submit our test data.
  139. $this->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
  140. // Confirm that the validation error occurred and the submission did not save.
  141. $this->assertRaw($error_message, t('Validation message properly thrown: "%message".', array('%message' => $error_message)), t('Webform'));
  142. $this->assertFalse(preg_match('/sid=([0-9]+)/', $this->getUrl()), t('Submission not saved.'));
  143. }
  144. }
  145. }
  146. }
  147. }