1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Webform module conditional tests.
|
6
|
*/
|
7
|
class WebformConditionalsTestCase extends WebformTestCase {
|
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
|
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
|
|
46
|
/**
|
47
|
* Assembles a test node for checking if conditional properties are respected.
|
48
|
*
|
49
|
* @param $component
|
50
|
* The sample component that should be tested as the source of a conditional
|
51
|
* operator.
|
52
|
* @param $operator
|
53
|
* The operator that will be used to check the source component, such as
|
54
|
* "equal" or "starts_with". See _webform_conditional_operator_info().
|
55
|
* @param $conditional_values
|
56
|
* The string match value that will be entered into the source component's
|
57
|
* conditional configuration. If passed in as an array, multiple rules
|
58
|
* will be added to the conditional.
|
59
|
* @param $should_match
|
60
|
* Boolean value indicating if the source values will cause the conditional
|
61
|
* operation to trigger or not. If TRUE, the submission should succeed.
|
62
|
* If FALSE, validation errors are expected to be triggered. The input
|
63
|
* $value will be compared against the "sample values" input provided by
|
64
|
* webformComponents().
|
65
|
*
|
66
|
* @return void
|
67
|
* This function executes its own assert statements to show results.
|
68
|
*/
|
69
|
private function webformTestConditionalComponent($component, $input_values, $operator, $conditional_values, $should_match) {
|
70
|
// Create the Webform test node and add a same-page conditional followed
|
71
|
// by a second-page conditional. Insert page breaks between all components.
|
72
|
$input_string = (is_array($input_values) ? print_r($input_values, 1) : $input_values);
|
73
|
$match_string = (is_array($conditional_values) ? print_r($conditional_values, 1) : $conditional_values);
|
74
|
$conditional_string = $should_match ? 'should' : 'should not';
|
75
|
$settings = array(
|
76
|
'title' => 'Test conditional webform: ' . $component['type'] . ' "' . $input_string . '"' . $conditional_string . ' be ' . $operator . ' "' . $match_string . '"',
|
77
|
'type' => 'webform',
|
78
|
'webform' => webform_node_defaults(),
|
79
|
);
|
80
|
|
81
|
$components = array();
|
82
|
$components[] = $component;
|
83
|
|
84
|
$test_components = $this->webformComponents();
|
85
|
$textfield = $test_components['textfield']['component'];
|
86
|
|
87
|
// Add a test textfield on the first page.
|
88
|
$textfield['weight'] = '199';
|
89
|
$textfield['form_key'] = $this->randomName();
|
90
|
$textfield['required'] = '1';
|
91
|
$components[] = $textfield;
|
92
|
|
93
|
// Then add a page break and another textfield on the second page.
|
94
|
$components[] = array(
|
95
|
'type' => 'pagebreak',
|
96
|
'form_key' => 'pagebreak_' . $this->randomName(),
|
97
|
'pid' => 0,
|
98
|
'name' => 'Page break',
|
99
|
'weight' => '200',
|
100
|
);
|
101
|
$textfield['form_key'] = $this->randomName();
|
102
|
$textfield['weight'] = '201';
|
103
|
$components[] = $textfield;
|
104
|
|
105
|
$settings['webform']['components'] = $components;
|
106
|
$node = $this->drupalCreateNode($settings);
|
107
|
$node = node_load($node->nid); // Needed to get a complete object.
|
108
|
|
109
|
// We now have a new test node. First try checking same-page conditionals.
|
110
|
$rules = array();
|
111
|
$conditional_values = is_array($conditional_values) ? $conditional_values : array($conditional_values);
|
112
|
foreach ($conditional_values as $conditional_value) {
|
113
|
$rules[] = array(
|
114
|
'source_type' => 'component',
|
115
|
'source' => 1, // The first component in the form is always the source.
|
116
|
'operator' => $operator,
|
117
|
'value' => $conditional_value,
|
118
|
);
|
119
|
}
|
120
|
$conditional = array(
|
121
|
'rgid' => 0,
|
122
|
'rules' => $rules,
|
123
|
'andor' => 'and',
|
124
|
'actions' => array(
|
125
|
0 => array(
|
126
|
'action' => 'show',
|
127
|
'target' => NULL, // Target set individually.
|
128
|
'target_type' => 'component',
|
129
|
'invert' => '1',
|
130
|
'argument' => NULL,
|
131
|
),
|
132
|
),
|
133
|
'weight' => 0,
|
134
|
);
|
135
|
|
136
|
$conditional['actions'][0]['target'] = 2; // The same-page textfield test.
|
137
|
$node->webform['conditionals'] = array($conditional);
|
138
|
node_save($node);
|
139
|
|
140
|
// Submit our test data.
|
141
|
$edit = $this->webformPost(array($component['form_key'] => $input_values));
|
142
|
$this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
|
143
|
|
144
|
// Ensure we reached the second page for matched conditionals.
|
145
|
$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));
|
146
|
if ($should_match) {
|
147
|
$this->assertRaw('< Previous Page', $message, t('Webform'));
|
148
|
}
|
149
|
// Or that we did not reach the second page for mismatched conditionals.
|
150
|
else {
|
151
|
$this->assertNoRaw('< Previous Page', $message, t('Webform'));
|
152
|
}
|
153
|
|
154
|
// Adjust the conditionals to make them separate-page conditionals.
|
155
|
$conditional['actions'][0]['target'] = 3; // The separate-page textfield test.
|
156
|
$node->webform['conditionals'] = array($conditional);
|
157
|
$node->webform['components'][2]['required'] = '0';
|
158
|
node_save($node);
|
159
|
|
160
|
// Re-submit the form again, this time checking for the field on the
|
161
|
// second page.
|
162
|
$this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
|
163
|
$string_match = 'name="submitted[' . $textfield['form_key'] . ']"';
|
164
|
|
165
|
// Ensure that the field is properly hidden based on a match.
|
166
|
$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));
|
167
|
if ($should_match) {
|
168
|
$this->assertNoRaw($string_match, $message, t('Webform'));
|
169
|
}
|
170
|
// Or that the field is still present on a mismatch.
|
171
|
else {
|
172
|
$this->assertRaw($string_match, $message, t('Webform'));
|
173
|
}
|
174
|
}
|
175
|
}
|