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