Project

General

Profile

Paste
Download (6.96 KB) Statistics
| Branch: | Revision:

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

1
<?php
2

    
3
/**
4
 * Webform module component tests.
5
 */
6
class WebformComponentsTestCase extends WebformTestCase {
7

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

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

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

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

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

    
171
    node_save($node);
172

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

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

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

    
183
    // Check th elements have the scope attribute.
184
    $this->assertRaw('<th class="checkbox webform-grid-option" scope="col">', 'Grid <code>th</code> elements have @scope of "col".');
185

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

    
194
}