Projet

Général

Profil

Paste
Télécharger (7,15 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / tests / WebformConditionalsTestCase.test @ 01f36513

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

    
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 $input_values
53
   * @param $operator
54
   *   The operator that will be used to check the source component, such as
55
   *   "equal" or "starts_with". See _webform_conditional_operator_info().
56
   * @param $conditional_values
57
   *   The string match value that will be entered into the source component's
58
   *   conditional configuration. If passed in as an array, multiple rules
59
   *   will be added to the conditional.
60
   * @param $should_match
61
   *   Boolean value indicating if the source values will cause the conditional
62
   *   operation to trigger or not. If TRUE, the submission should succeed.
63
   *   If FALSE, validation errors are expected to be triggered. The input
64
   *   $value will be compared against the "sample values" input provided by
65
   *   webformComponents().
66
   */
67
  private function webformTestConditionalComponent($component, $input_values, $operator, $conditional_values, $should_match) {
68
    // Create the Webform test node and add a same-page conditional followed
69
    // by a second-page conditional. Insert page breaks between all components.
70
    $input_string = (is_array($input_values) ? print_r($input_values, 1) : $input_values);
71
    $match_string = (is_array($conditional_values) ? print_r($conditional_values, 1) : $conditional_values);
72
    $conditional_string = $should_match ? 'should' : 'should not';
73
    $settings = array(
74
      'title' => 'Test conditional webform: ' . $component['type'] . ' "' . $input_string . '"' . $conditional_string . ' be ' . $operator . ' "' . $match_string . '"',
75
      'type' => 'webform',
76
      'webform' => webform_node_defaults(),
77
    );
78

    
79
    $components = array();
80
    $components[] = $component;
81

    
82
    $test_components = $this->webformComponents();
83
    $textfield = $test_components['textfield']['component'];
84

    
85
    // Add a test textfield on the first page.
86
    $textfield['weight'] = '199';
87
    $textfield['form_key'] = $this->randomName();
88
    $textfield['required'] = '1';
89
    $components[] = $textfield;
90

    
91
    // Then add a page break and another textfield on the second page.
92
    $components[] = array(
93
      'type' => 'pagebreak',
94
      'form_key' => 'pagebreak_' . $this->randomName(),
95
      'pid' => 0,
96
      'name' => 'Page break',
97
      'weight' => '200',
98
    );
99
    $textfield['form_key'] = $this->randomName();
100
    $textfield['weight'] = '201';
101
    $components[] = $textfield;
102

    
103
    $settings['webform']['components'] = $components;
104
    $node = $this->drupalCreateNode($settings);
105
    // Needed to get a complete object.
106
    $node = node_load($node->nid);
107

    
108
    // We now have a new test node. First try checking same-page conditionals.
109
    $rules = array();
110
    $conditional_values = is_array($conditional_values) ? $conditional_values : array($conditional_values);
111
    foreach ($conditional_values as $conditional_value) {
112
      $rules[] = array(
113
        'source_type' => 'component',
114
        // The first component in the form is always the source.
115
        'source' => 1,
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 set individually.
128
          'target' => NULL,
129
          'target_type' => 'component',
130
          'invert' => '1',
131
          'argument' => NULL,
132
        ),
133
      ),
134
      'weight' => 0,
135
    );
136

    
137
    // The same-page textfield test.
138
    $conditional['actions'][0]['target'] = 2;
139
    $node->webform['conditionals'] = array($conditional);
140
    node_save($node);
141

    
142
    // Submit our test data.
143
    $edit = $this->webformPost(array($component['form_key'] => $input_values));
144
    $this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
145

    
146
    // Ensure we reached the second page for matched conditionals.
147
    $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));
148
    if ($should_match) {
149
      $this->assertRaw('&lt; Previous Page', $message, t('Webform'));
150
    }
151
    // Or that we did not reach the second page for mismatched conditionals.
152
    else {
153
      $this->assertNoRaw('&lt; Previous Page', $message, t('Webform'));
154
    }
155

    
156
    // Adjust the conditionals to make them separate-page conditionals.
157
    // The separate-page textfield test.
158
    $conditional['actions'][0]['target'] = 3;
159
    $node->webform['conditionals'] = array($conditional);
160
    $node->webform['components'][2]['required'] = '0';
161
    node_save($node);
162

    
163
    // Re-submit the form again, this time checking for the field on the
164
    // second page.
165
    $this->drupalPost('node/' . $node->nid, $edit, 'Next Page >', array(), array(), 'webform-client-form-' . $node->nid);
166
    $string_match = 'name="submitted[' . $textfield['form_key'] . ']"';
167

    
168
    // Ensure that the field is properly hidden based on a match.
169
    $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));
170
    if ($should_match) {
171
      $this->assertNoRaw($string_match, $message, t('Webform'));
172
    }
173
    // Or that the field is still present on a mismatch.
174
    else {
175
      $this->assertRaw($string_match, $message, t('Webform'));
176
    }
177
  }
178

    
179
}