Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform / tests / conditionals.test @ ebcc4118

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module conditional tests.
6
 */
7

    
8
include_once(dirname(__FILE__) . '/webform.test');
9

    
10
class WebformConditionalsTestCase extends WebformTestCase {
11
  /**
12
   * Implements getInfo().
13
   */
14
  public static function getInfo() {
15
    return array(
16
      'name' => t('Webform conditionals'),
17
      'description' => t('Generates webforms to test conditional showing and hiding of fields.'),
18
      'group' => t('Webform'),
19
    );
20
  }
21

    
22
  /**
23
   * Implements setUp().
24
   */
25
  function setUp($added_modules = array()) {
26
    parent::setUp($added_modules);
27
  }
28

    
29
  /**
30
   * Implements tearDown().
31
   */
32
  function tearDown() {
33
    parent::tearDown();
34
  }
35

    
36
  /**
37
   * Test that required fields with no default value can't be submitted as-is.
38
   */
39
  function testWebformConditionals() {
40
    $this->drupalLogin($this->webform_users['admin']);
41
    $this->webformReset();
42

    
43
    $test_components = $this->testWebformComponents();
44
    $test_specs = array(
45
      'match conditional values' => TRUE,
46
      'mismatch conditional values' => FALSE,
47
    );
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);
55
          }
56
        }
57
      }
58
    }
59

    
60
    $this->drupalLogout();
61
  }
62

    
63
  /**
64
   * Assembles a test node for checking if conditional properties are respected.
65
   *
66
   * @param $component
67
   *   The sample component that should be tested as the source of a conditional
68
   *   operator.
69
   * @param $operator
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().
82
   * @return
83
   *   None. This function executes its own assert statements to show results.
84
   */
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';
91
    $settings = array(
92
      'title' => 'Test conditional webform: ' . $component['type'] . ' "' . $input_string . '"' . $conditional_string . ' be ' . $operator . ' "' . $match_string . '"',
93
      'type' => 'webform',
94
      'webform' => webform_node_defaults(),
95
    );
96

    
97
    $components = array();
98
    $components[] = $component;
99

    
100
    $test_components = $this->testWebformComponents();
101
    $textfield = $test_components['textfield']['component'];
102

    
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;
108

    
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(),
113
      'pid' => 0,
114
      'name' => 'Page break',
115
      'weight' => '200',
116
    );
117
    $textfield['form_key'] = $this->randomName();
118
    $textfield['weight'] = '201';
119
    $components[] = $textfield;
120

    
121
    $settings['webform']['components'] = $components;
122
    $node = $this->drupalCreateNode($settings);
123
    $node = node_load($node->nid); // Needed to get a complete object.
124

    
125
    // We now have a new test node. First try checking same-page conditionals.
126
    $rules = array();
127
    $conditional_values = is_array($conditional_values) ? $conditional_values : array($conditional_values);
128
    foreach ($conditional_values as $conditional_value) {
129
      $rules[] = array(
130
        'source_type' => 'component',
131
        'source' => 1, // The first component in the form is always the source.
132
        'operator' => $operator,
133
        'value' => $conditional_value,
134
      );
135
    }
136
    $conditional = array(
137
      'rgid' => 0,
138
      'rules' => $rules,
139
      'andor' => 'and',
140
      'actions' => array(
141
        0 => array(
142
          'action' => 'show',
143
          'target' => NULL, // Target set individually.
144
          'target_type' => 'component',
145
          'invert' => '1',
146
          'argument' => NULL,
147
        ),
148
      ),
149
      'weight' => 0,
150
    );
151

    
152
    $conditional['actions'][0]['target'] = 2; // The same-page textfield test.
153
    $node->webform['conditionals'] = array($conditional);
154
    node_save($node);
155

    
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);
159

    
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));
162
    if ($should_match) {
163
      $this->assertRaw('&lt; Previous Page', $message, t('Webform'));
164
    }
165
    // Or that we did not reach the second page for mismatched conditionals.
166
    else {
167
      $this->assertNoRaw('&lt; Previous Page', $message, t('Webform'));
168
    }
169

    
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';
174
    node_save($node);
175

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

    
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));
183
    if ($should_match) {
184
      $this->assertNoRaw($string_match, $message, t('Webform'));
185
    }
186
    // Or that the field is still present on a mismatch.
187
    else {
188
      $this->assertRaw($string_match, $message, t('Webform'));
189
    }
190
  }
191
}