Projet

Général

Profil

Paste
Télécharger (6,78 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / tests / WebformComponentsTestCase.test @ 8c72e82a

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module component tests.
6
 */
7
class WebformComponentsTestCase extends WebformTestCase {
8
  /**
9
   * {@inheritdoc}
10
   */
11
  public static function getInfo() {
12
    return array(
13
      'name' => t('Webform components'),
14
      'description' => t('Add and remove components from a webform.'),
15
      'group' => t('Webform'),
16
    );
17
  }
18

    
19
  /**
20
   * Webform module component tests.
21
   */
22
  function testWebformComponents() {
23
    // Test webform_component_list().
24
    // Create a form consisting of three textfields separated by pagebreaks.
25
    $test_components = $this->webformComponents();
26
    $textfield = $test_components['textfield']['component'];
27

    
28
    // Page 1 textfield.
29
    $textfield['page_num'] = 1;
30
    $textfield['name'] = $this->randomName();
31
    $textfield['form_key'] = $this->randomName();
32
    $components[1] = $textfield;
33
    // Page 2 break.
34
    $components[2] = array(
35
      'type' => 'pagebreak',
36
      'form_key' => 'pagebreak_' . $this->randomName(),
37
      'pid' => 0,
38
      'name' => $this->randomName(),
39
      'page_num' => 2,
40
    );
41
    // Page 2 textfield.
42
    $textfield['name'] = $this->randomName();
43
    $textfield['form_key'] = $this->randomName();
44
    $textfield['page_num'] = 2;
45
    $components[3] = $textfield;
46
    // Page 3 break.
47
    $components[4] = array(
48
      'type' => 'pagebreak',
49
      'form_key' => 'pagebreak_' . $this->randomName(),
50
      'pid' => 0,
51
      // Name is the cid of another component. Should get a space added when it
52
      // is used as a key in the value returned from webform_component_list().
53
      'name' => '1',
54
      'page_num' => 3,
55
    );
56
    // Page 3 textfield.
57
    $textfield['name'] = $this->randomName();
58
    $textfield['form_key'] = $this->randomName();
59
    $textfield['page_num'] = 3;
60
    $components[5] = $textfield;
61
    // Page 4 break.
62
    $components[6] = array(
63
      'type' => 'pagebreak',
64
      'form_key' => 'pagebreak_' . $this->randomName(),
65
      'pid' => 0,
66
      // Name is the same as Page 3 break. Tests that name is made unique.
67
      'name' => '1',
68
      'page_num' => 4,
69
    );
70
    // Page 4 textfield.
71
    $textfield['name'] = $this->randomName();
72
    $textfield['form_key'] = $this->randomName();
73
    $textfield['page_num'] = 4;
74
    $components[7] = $textfield;
75

    
76
    // Generate a component list.
77
    $node = new stdClass();
78
    $node->webform['components'] = $components;
79
    $webform_component_list = webform_component_list($node, NULL, TRUE, TRUE);
80
    // Test return value.
81
    $test_list = array(
82
      1 => $components[1]['name'],
83
      $components[2]['name'] => array(2 => $components[2]['name'], 3 => $components[3]['name']),
84
      $components[4]['name'] . ' ' => array(4 => $components[4]['name'], 5 => $components[5]['name']),
85
      $components[6]['name'] . '_2' => array(6 => $components[6]['name'], 7 => $components[7]['name']),
86
    );
87
    $this->assertIdentical($webform_component_list, $test_list, 'webform_component_list() returns expected value.');
88

    
89
    // Test webform_component_parent_keys().
90
    $components = array(
91
      1 => array(
92
        'form_key' => $this->randomName(),
93
        'name' => $this->randomName(),
94
        'pid' => 0,
95
      ),
96
      2 => array(
97
        'form_key' => $this->randomName(),
98
        'name' => $this->randomName(),
99
        'pid' => 1,
100
      ),
101
      3 => array(
102
        'form_key' => $this->randomName(),
103
        'name' => $this->randomName(),
104
        'pid' => 2,
105
      ),
106
    );
107

    
108
    $node = new stdClass();
109
    $node->webform['components'] = $components;
110

    
111
    $parents = webform_component_parent_keys($node, $components[3]);
112
    $test_parents = array($components[1]['form_key'], $components[2]['form_key'], $components[3]['form_key']);
113
    $this->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected form_keys.');
114

    
115
    $parents = webform_component_parent_keys($node, $components[3], 'name');
116
    $test_parents = array($components[1]['name'], $components[2]['name'], $components[3]['name']);
117
    $this->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected names.');
118

    
119
    $parents = webform_component_parent_keys($node, $components[3], TRUE);
120
    $test_parents = array($components[1], $components[2], $components[3]);
121
    $this->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected component arrays.');
122

    
123

    
124
    // Test webform_get_cid().
125
    $settings = array(
126
      'title' => 'Test webform with multiple instances of a Form Key',
127
      'type' => 'webform',
128
    );
129
    $node = $this->drupalCreateNode($settings);
130

    
131
    // Add a new textarea component.
132
    $components = $this->webformComponents();
133
    $textarea = $components['textarea'];
134
    $textarea['type'] = 'textarea';
135
    $textarea['form_key'] = 'testing_key';
136
    $textarea['cid'] = 1;
137
    $textarea['pid'] = 0;
138
    $textarea = array_merge(webform_component_invoke('textarea', 'defaults'), $textarea);
139
    $node->webform['components'][1] = $textarea;
140

    
141
    // Add a new fieldset component.
142
    $fieldset = array(
143
      'cid' => 2,
144
      'pid' => 0,
145
      'form_key' => 'another_key',
146
      'name' => 'Fieldset',
147
      'type' => 'fieldset',
148
      'value' => '',
149
      'required' => '0',
150
      'weight' => '0',
151
    );
152
    $node->webform['components'][2] = $fieldset;
153

    
154
    // Add a new textfield component as child of the fieldset.
155
    $textfield = $components['textfield'];
156
    $textfield['type'] = 'textfield';
157
    $textfield['form_key'] = 'testing_key';
158
    $textfield['cid'] = 3;
159
    $textfield['pid'] = 2;
160
    $textfield = array_merge(webform_component_invoke('textarea', 'defaults'), $textfield);
161
    $node->webform['components'][3] = $textfield;
162

    
163
    // Add another textfield component.
164
    $textfield = $components['textfield'];
165
    $textfield['type'] = 'textfield';
166
    $textfield['form_key'] = 'dummy_key';
167
    $textfield['cid'] = 4;
168
    $textfield['pid'] = 0;
169
    $textfield = array_merge(webform_component_invoke('textarea', 'defaults'), $textfield);
170
    $node->webform['components'][4] = $textfield;
171

    
172
    node_save($node);
173

    
174
    // Test webform_get_cid() with providing a parent cid.
175
    $this->assertTrue(webform_get_cid($node, 'testing_key', 2) == 3, t('Returned expected Webform component id for a given form_key and parent (pid).'));
176

    
177
    // Test webform_get_cid() without providing a parent cid.
178
    $this->assertTrue(webform_get_cid($node, 'testing_key') == array(1, 3), t('Returned expected Webform component ids array for a given form_key.'));
179

    
180

    
181
    // Create and visit a new Webform test node.
182
    $node = $this->webformForm();
183
    $this->drupalGet('node/' . $node->nid);
184

    
185
    // Check that each label @for points to an element.
186
    $labels = $this->xpath('//label/@for');
187
    foreach ($labels as $label) {
188
      $for = $this->xpath("//*[@id=':id']", array(':id' => $label['for']));
189
      $this->assertTrue($for, 'Label with @for "' . $label['for'] . '" points to an element.');
190
    }
191
  }
192
}