1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Webform module conditional tests.
|
5
|
*/
|
6
|
class WebformConditionalsTestCase extends WebformTestCase {
|
7
|
|
8
|
/**
|
9
|
* {@inheritdoc}
|
10
|
*/
|
11
|
public static function getInfo() {
|
12
|
return array(
|
13
|
'name' => t('Webform conditionals'),
|
14
|
'description' => t('Generates webforms to test conditional showing and hiding of fields.'),
|
15
|
'group' => t('Webform'),
|
16
|
);
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Test that required fields with no default value can't be submitted as-is.
|
21
|
*/
|
22
|
public function testWebformConditionals() {
|
23
|
$this->drupalLogin($this->webform_users['admin']);
|
24
|
$this->webformReset();
|
25
|
|
26
|
$test_components = $this->webformComponents();
|
27
|
$test_specs = array(
|
28
|
'match conditional values' => TRUE,
|
29
|
'mismatch conditional values' => FALSE,
|
30
|
);
|
31
|
// Test each component, processing all 'match conditional values' for TRUE
|
32
|
// and all 'mismatch conditional values for FALSE.
|
33
|
foreach ($test_components as $key => $component_info) {
|
34
|
foreach ($test_specs as $values_key => $result) {
|
35
|
if (isset($component_info[$values_key])) {
|
36
|
foreach ($component_info[$values_key] as $operator => $match_value) {
|
37
|
$this->webformTestConditionalComponent($component_info['component'], $component_info['sample values'], $operator, $match_value, $result);
|
38
|
}
|
39
|
}
|
40
|
}
|
41
|
}
|
42
|
|
43
|
$this->drupalLogout();
|
44
|
|
45
|
// Test that the whole conditional is deleted when all source components are
|
46
|
// deleted. Inital setup: Two components. One source (c1) one target (c2).
|
47
|
$node = (object) array(
|
48
|
'type' => 'webform',
|
49
|
'title' => 'Conditional test',
|
50
|
);
|
51
|
$default = array(
|
52
|
'type' => 'textfield',
|
53
|
'pid' => 0,
|
54
|
);
|
55
|
webform_component_defaults($default);
|
56
|
node_object_prepare($node);
|
57
|
$node->webform['components'][1] = array(
|
58
|
'cid' => 1,
|
59
|
'form_key' => 'c1',
|
60
|
) + $default;
|
61
|
$node->webform['components'][2] = array(
|
62
|
'cid' => 2,
|
63
|
'form_key' => 'c2',
|
64
|
) + $default;
|
65
|
$node->webform['conditionals'][0] = array(
|
66
|
'rgid' => 0,
|
67
|
'andor' => NULL,
|
68
|
'weight' => -1,
|
69
|
'rules' => array(array(
|
70
|
'source_type' => 'component',
|
71
|
'source' => 1,
|
72
|
'operator' => 'not_empty',
|
73
|
),
|
74
|
),
|
75
|
'actions' => array(array(
|
76
|
'target_type' => 'component',
|
77
|
'target' => 2,
|
78
|
'action' => 'show',
|
79
|
),
|
80
|
),
|
81
|
);
|
82
|
node_save($node);
|
83
|
|
84
|
// Delete the source.
|
85
|
unset($node->webform['components'][1]);
|
86
|
node_save($node);
|
87
|
|
88
|
$this->assertEqual(array(), $node->webform['conditionals'], 'The whole conditional is deleted when all source components are deleted.');
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Assembles a test node for checking if conditional properties are respected.
|
93
|
*
|
94
|
* @param $component
|
95
|
* The sample component that should be tested as the source of a conditional
|
96
|
* operator.
|
97
|
* @param $input_values
|
98
|
* @param $operator
|
99
|
* The operator that will be used to check the source component, such as
|
100
|
* "equal" or "starts_with". See _webform_conditional_operator_info().
|
101
|
* @param $conditional_values
|
102
|
* The string match value that will be entered into the source component's
|
103
|
* conditional configuration. If passed in as an array, multiple rules
|
104
|
* will be added to the conditional.
|
105
|
* @param $should_match
|
106
|
* Boolean value indicating if the source values will cause the conditional
|
107
|
* operation to trigger or not. If TRUE, the submission should succeed.
|
108
|
* If FALSE, validation errors are expected to be triggered. The input
|
109
|
* $value will be compared against the "sample values" input provided by
|
110
|
* webformComponents().
|
111
|
*/
|
112
|
private function webformTestConditionalComponent($component, $input_values, $operator, $conditional_values, $should_match) {
|
113
|
// Create the Webform test node and add a same-page conditional followed
|
114
|
// by a second-page conditional. Insert page breaks between all components.
|
115
|
$input_string = (is_array($input_values) ? print_r($input_values, 1) : $input_values);
|
116
|
$match_string = (is_array($conditional_values) ? print_r($conditional_values, 1) : $conditional_values);
|
117
|
$conditional_string = $should_match ? 'should' : 'should not';
|
118
|
$settings = array(
|
119
|
'title' => 'Test conditional webform: ' . $component['type'] . ' "' . $input_string . '"' . $conditional_string . ' be ' . $operator . ' "' . $match_string . '"',
|
120
|
'type' => 'webform',
|
121
|
'webform' => webform_node_defaults(),
|
122
|
);
|
123
|
|
124
|
$components = array();
|
125
|
$components[] = $component;
|
126
|
|
127
|
$test_components = $this->webformComponents();
|
128
|
$textfield = $test_components['textfield']['component'];
|
129
|
|
130
|
// Add a test textfield on the first page.
|
131
|
$textfield['weight'] = '199';
|
132
|
$textfield['form_key'] = $this->randomName();
|
133
|
$textfield['required'] = '1';
|
134
|
$components[] = $textfield;
|
135
|
|
136
|
// Then add a page break and another textfield on the second page.
|
137
|
$components[] = array(
|
138
|
'type' => 'pagebreak',
|
139
|
'form_key' => 'pagebreak_' . $this->randomName(),
|
140
|
'pid' => 0,
|
141
|
'name' => 'Page break',
|
142
|
'weight' => '200',
|
143
|
);
|
144
|
$textfield['form_key'] = $this->randomName();
|
145
|
$textfield['weight'] = '201';
|
146
|
$components[] = $textfield;
|
147
|
|
148
|
$settings['webform']['components'] = $components;
|
149
|
$node = $this->drupalCreateNode($settings);
|
150
|
// Needed to get a complete object.
|
151
|
$node = node_load($node->nid);
|
152
|
|
153
|
// We now have a new test node. First try checking same-page conditionals.
|
154
|
$rules = array();
|
155
|
$conditional_values = is_array($conditional_values) ? $conditional_values : array($conditional_values);
|
156
|
foreach ($conditional_values as $conditional_value) {
|
157
|
$rules[] = array(
|
158
|
'source_type' => 'component',
|
159
|
// The first component in the form is always the source.
|
160
|
'source' => 1,
|
161
|
'operator' => $operator,
|
162
|
'value' => $conditional_value,
|
163
|
);
|
164
|
}
|
165
|
$conditional = array(
|
166
|
'rgid' => 0,
|
167
|
'rules' => $rules,
|
168
|
'andor' => 'and',
|
169
|
'actions' => array(
|
170
|
0 => array(
|
171
|
'action' => 'show',
|
172
|
// Target set individually.
|
173
|
'target' => NULL,
|
174
|
'target_type' => 'component',
|
175
|
'invert' => '1',
|
176
|
'argument' => NULL,
|
177
|
),
|
178
|
),
|
179
|
'weight' => 0,
|
180
|
);
|
181
|
|
182
|
// The same-page textfield test.
|
183
|
$conditional['actions'][0]['target'] = 2;
|
184
|
$node->webform['conditionals'] = array($conditional);
|
185
|
node_save($node);
|
186
|
|
187
|
// Submit our test data.
|
188
|
$edit = $this->webformPost(array($component['form_key'] => $input_values));
|
189
|
$this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
|
190
|
|
191
|
// Ensure we reached the second page for matched conditionals.
|
192
|
$message = t('Conditional same-page skipping of validation passed for "%form_key": %input_values @conditional_string be @operator %match_string', array('%form_key' => $component['form_key'], '%input_values' => $input_string, '@conditional_string' => $conditional_string, '@operator' => $operator, '%match_string' => $match_string));
|
193
|
if ($should_match) {
|
194
|
$this->assertRaw('< Previous Page', $message, t('Webform'));
|
195
|
}
|
196
|
// Or that we did not reach the second page for mismatched conditionals.
|
197
|
else {
|
198
|
$this->assertNoRaw('< Previous Page', $message, t('Webform'));
|
199
|
}
|
200
|
|
201
|
// Adjust the conditionals to make them separate-page conditionals.
|
202
|
// The separate-page textfield test.
|
203
|
$conditional['actions'][0]['target'] = 3;
|
204
|
$node->webform['conditionals'] = array($conditional);
|
205
|
$node->webform['components'][2]['required'] = '0';
|
206
|
node_save($node);
|
207
|
|
208
|
// Re-submit the form again, this time checking for the field on the
|
209
|
// second page.
|
210
|
$this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
|
211
|
$string_match = 'name="submitted[' . $textfield['form_key'] . ']"';
|
212
|
|
213
|
// Ensure that the field is properly hidden based on a match.
|
214
|
$message = t('Conditional separate-page skipping of validation passed for "%form_key": %input_values @conditional_string be @operator %match_string', array('%form_key' => $component['form_key'], '%input_values' => $input_string, '@conditional_string' => $conditional_string, '@operator' => $operator, '%match_string' => $match_string));
|
215
|
if ($should_match) {
|
216
|
$this->assertNoRaw($string_match, $message, t('Webform'));
|
217
|
}
|
218
|
// Or that the field is still present on a mismatch.
|
219
|
else {
|
220
|
$this->assertRaw($string_match, $message, t('Webform'));
|
221
|
}
|
222
|
}
|
223
|
|
224
|
}
|