View source
<?php
class WebformConditionalsTestCase extends WebformTestCase {
public static function getInfo() {
return array(
'name' => t('Webform conditionals'),
'description' => t('Generates webforms to test conditional showing and hiding of fields.'),
'group' => t('Webform'),
);
}
public function testWebformConditionals() {
$this
->drupalLogin($this->webform_users['admin']);
$this
->webformReset();
$test_components = $this
->webformComponents();
$test_specs = array(
'match conditional values' => TRUE,
'mismatch conditional values' => FALSE,
);
foreach ($test_components as $key => $component_info) {
foreach ($test_specs as $values_key => $result) {
if (isset($component_info[$values_key])) {
foreach ($component_info[$values_key] as $operator => $match_value) {
$this
->webformTestConditionalComponent($component_info['component'], $component_info['sample values'], $operator, $match_value, $result);
}
}
}
}
$this
->drupalLogout();
$node = (object) array(
'type' => 'webform',
'title' => 'Conditional test',
);
$default = array(
'type' => 'textfield',
'pid' => 0,
);
webform_component_defaults($default);
node_object_prepare($node);
$node->webform['components'][1] = array(
'cid' => 1,
'form_key' => 'c1',
) + $default;
$node->webform['components'][2] = array(
'cid' => 2,
'form_key' => 'c2',
) + $default;
$node->webform['conditionals'][0] = array(
'rgid' => 0,
'andor' => NULL,
'weight' => -1,
'rules' => array(
array(
'source_type' => 'component',
'source' => 1,
'operator' => 'not_empty',
),
),
'actions' => array(
array(
'target_type' => 'component',
'target' => 2,
'action' => 'show',
),
),
);
node_save($node);
unset($node->webform['components'][1]);
node_save($node);
$this
->assertEqual(array(), $node->webform['conditionals'], 'The whole conditional is deleted when all source components are deleted.');
}
private function webformTestConditionalComponent($component, $input_values, $operator, $conditional_values, $should_match) {
$input_string = is_array($input_values) ? print_r($input_values, 1) : $input_values;
$match_string = is_array($conditional_values) ? print_r($conditional_values, 1) : $conditional_values;
$conditional_string = $should_match ? 'should' : 'should not';
$settings = array(
'title' => 'Test conditional webform: ' . $component['type'] . ' "' . $input_string . '"' . $conditional_string . ' be ' . $operator . ' "' . $match_string . '"',
'type' => 'webform',
'webform' => webform_node_defaults(),
);
$components = array();
$components[] = $component;
$test_components = $this
->webformComponents();
$textfield = $test_components['textfield']['component'];
$textfield['weight'] = '199';
$textfield['form_key'] = $this
->randomName();
$textfield['required'] = '1';
$components[] = $textfield;
$components[] = array(
'type' => 'pagebreak',
'form_key' => 'pagebreak_' . $this
->randomName(),
'pid' => 0,
'name' => 'Page break',
'weight' => '200',
);
$textfield['form_key'] = $this
->randomName();
$textfield['weight'] = '201';
$components[] = $textfield;
$settings['webform']['components'] = $components;
$node = $this
->drupalCreateNode($settings);
$node = node_load($node->nid);
$rules = array();
$conditional_values = is_array($conditional_values) ? $conditional_values : array(
$conditional_values,
);
foreach ($conditional_values as $conditional_value) {
$rules[] = array(
'source_type' => 'component',
'source' => 1,
'operator' => $operator,
'value' => $conditional_value,
);
}
$conditional = array(
'rgid' => 0,
'rules' => $rules,
'andor' => 'and',
'actions' => array(
0 => array(
'action' => 'show',
'target' => NULL,
'target_type' => 'component',
'invert' => '1',
'argument' => NULL,
),
),
'weight' => 0,
);
$conditional['actions'][0]['target'] = 2;
$node->webform['conditionals'] = array(
$conditional,
);
node_save($node);
$edit = $this
->webformPost(array(
$component['form_key'] => $input_values,
));
$this
->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
$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,
));
if ($should_match) {
$this
->assertRaw('< Previous Page', $message, t('Webform'));
}
else {
$this
->assertNoRaw('< Previous Page', $message, t('Webform'));
}
$conditional['actions'][0]['target'] = 3;
$node->webform['conditionals'] = array(
$conditional,
);
$node->webform['components'][2]['required'] = '0';
node_save($node);
$this
->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
$string_match = 'name="submitted[' . $textfield['form_key'] . ']"';
$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,
));
if ($should_match) {
$this
->assertNoRaw($string_match, $message, t('Webform'));
}
else {
$this
->assertRaw($string_match, $message, t('Webform'));
}
}
}