5 * Webform module conditional tests.
8 include_once(dirname(__FILE__) . '/webform.test');
10 class WebformConditionalsTestCase extends WebformTestCase {
12 * Implements getInfo().
14 public static function getInfo() {
16 'name' => t('Webform conditionals'),
17 'description' => t('Generates webforms to test conditional showing and hiding of fields.'),
18 'group' => t('Webform'),
25 function setUp($added_modules = array()) {
26 parent::setUp($added_modules);
30 * Implements tearDown().
37 * Test that required fields with no default value can't be submitted as-is.
39 function testWebformConditionals() {
40 $this->drupalLogin($this->webform_users['admin']);
41 $this->webformReset();
43 $test_components = $this->testWebformComponents();
45 'match conditional values' => TRUE,
46 'mismatch conditional values' => FALSE,
48 // Test each component, processing all 'match conditional values' for TRUE
49 // and all 'mismatch conditional values for FALSE.
50 foreach ($test_components as $key => $component_info) {
51 foreach ($test_specs as $values_key => $result) {
52 if (isset($component_info[$values_key])) {
53 foreach ($component_info[$values_key] as $operator => $match_value) {
54 $this->webformTestConditionalComponent($component_info['component'], $component_info['sample values'], $operator, $match_value, $result);
60 $this->drupalLogout();
64 * Assembles a test node for checking if conditional properties are respected.
67 * The sample component that should be tested as the source of a conditional
70 * The operator that will be used to check the source component, such as
71 * "equal" or "starts_with". See _webform_conditional_operator_info().
72 * @param $conditional_values
73 * The string match value that will be entered into the source component's
74 * conditional configuration. If passed in as an array, multiple rules
75 * will be added to the conditional.
76 * @param $should_match
77 * Boolean value indicating if the source values will cause the conditional
78 * operation to trigger or not. If TRUE, the submission should succeed.
79 * If FALSE, validation errors are expected to be triggered. The input
80 * $value will be compared against the "sample values" input provided by
81 * testWebformComponents().
83 * None. This function executes its own assert statements to show results.
85 private function webformTestConditionalComponent($component, $input_values, $operator, $conditional_values, $should_match) {
86 // Create the Webform test node and add a same-page conditional followed
87 // by a second-page conditional. Insert page breaks between all components.
88 $input_string = (is_array($input_values) ? print_r($input_values, 1) : $input_values);
89 $match_string = (is_array($conditional_values) ? print_r($conditional_values, 1) : $conditional_values);
90 $conditional_string = $should_match ? 'should' : 'should not';
92 'title' => 'Test conditional webform: ' . $component['type'] . ' "' . $input_string . '"' . $conditional_string . ' be ' . $operator . ' "' . $match_string . '"',
94 'webform' => webform_node_defaults(),
97 $components = array();
98 $components[] = $component;
100 $test_components = $this->testWebformComponents();
101 $textfield = $test_components['textfield']['component'];
103 // Add a test textfield on the first page.
104 $textfield['weight'] = '199';
105 $textfield['form_key'] = $this->randomName();
106 $textfield['required'] = '1';
107 $components[] = $textfield;
109 // Then add a page break and another textfield on the second page.
110 $components[] = array(
111 'type' => 'pagebreak',
112 'form_key' => 'pagebreak_' . $this->randomName(),
114 'name' => 'Page break',
117 $textfield['form_key'] = $this->randomName();
118 $textfield['weight'] = '201';
119 $components[] = $textfield;
121 $settings['webform']['components'] = $components;
122 $node = $this->drupalCreateNode($settings);
123 $node = node_load($node->nid); // Needed to get a complete object.
125 // We now have a new test node. First try checking same-page conditionals.
127 $conditional_values = is_array($conditional_values) ? $conditional_values : array($conditional_values);
128 foreach ($conditional_values as $conditional_value) {
130 'source_type' => 'component',
131 'source' => 1, // The first component in the form is always the source.
132 'operator' => $operator,
133 'value' => $conditional_value,
136 $conditional = array(
143 'target' => NULL, // Target set individually.
144 'target_type' => 'component',
152 $conditional['actions'][0]['target'] = 2; // The same-page textfield test.
153 $node->webform['conditionals'] = array($conditional);
156 // Submit our test data.
157 $edit = $this->testWebformPost(array($component['form_key'] => $input_values));
158 $this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
160 // Ensure we reached the second page for matched conditionals.
161 $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));
163 $this->assertRaw('< Previous Page', $message, t('Webform'));
165 // Or that we did not reach the second page for mismatched conditionals.
167 $this->assertNoRaw('< Previous Page', $message, t('Webform'));
170 // Adjust the conditionals to make them separate-page conditionals.
171 $conditional['actions'][0]['target'] = 3; // The separate-page textfield test.
172 $node->webform['conditionals'] = array($conditional);
173 $node->webform['components'][2]['required'] = '0';
176 // Re-submit the form again, this time checking for the field on the
178 $this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
179 $string_match = 'name="submitted[' . $textfield['form_key'] . ']"';
181 // Ensure that the field is properly hidden based on a match.
182 $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));
184 $this->assertNoRaw($string_match, $message, t('Webform'));
186 // Or that the field is still present on a mismatch.
188 $this->assertRaw($string_match, $message, t('Webform'));