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