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