Projet

Général

Profil

Paste
Télécharger (13,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_group / tests / field_group.display.test @ 27370441

1
<?php
2

    
3
/**
4
 * @file
5
 * Test file for fieldgroup display.
6
 */
7

    
8
/**
9
 * Group display tests
10
 */
11
class GroupDisplayTestCase extends DrupalWebTestCase {
12

    
13
  protected $node;
14

    
15
  public static function getInfo() {
16
    return array(
17
      'name' => 'Display tests',
18
      'description' => 'Test the field group display.',
19
      'group' => 'Field Group',
20
    );
21
  }
22

    
23
  function setUp() {
24

    
25
    parent::setUp('field_test', 'field_group', 'field_group_test');
26

    
27
    $node = new stdClass();
28
    $node->type = 'article';
29
    $node->title = $this->randomName();
30
    $node->status = 1;
31

    
32
    // Create test fields.
33
    $test_fields = array('field_test', 'field_test_2', 'field_no_access');
34
    foreach ($test_fields as $field_name) {
35

    
36
      $field = array(
37
        'field_name' => $field_name,
38
        'type' => 'test_field',
39
        'cardinality' => 1,
40
      );
41
      $instance = array(
42
        'field_name' => $field_name,
43
        'entity_type' => 'node',
44
        'bundle' => 'article',
45
        'label' => $this->randomName(),
46
        'display' => array(
47
          'default' => array(
48
            'type' => 'field_test_default',
49
            'settings' => array(
50
              'test_formatter_setting' => $this->randomName(),
51
            ),
52
          ),
53
          'teaser' => array(
54
            'type' => 'field_test_default',
55
            'settings' => array(
56
              'test_formatter_setting' => $this->randomName(),
57
            ),
58
          ),
59
        ),
60
      );
61
      field_create_field($field);
62
      field_create_instance($instance);
63

    
64
      $node->{$field_name}[LANGUAGE_NONE][0]['value'] = mt_rand(1, 127);
65
    }
66

    
67
    node_save($node);
68
    $this->node = $node;
69
  }
70

    
71
  /**
72
   * Create a new group.
73
   * @param array $data
74
   *   Data for the field group.
75
   */
76
  function createGroup($mode, array $data) {
77

    
78
    $group_name = 'group_' . drupal_strtolower($this->randomName(8));
79
    $identifier = $group_name . '|node|article|' . $mode;
80

    
81
    $field_group = new stdClass;
82
    $field_group->disabled = FALSE;
83
    $field_group->api_version = 1;
84
    $field_group->identifier = $identifier;
85
    $field_group->group_name = $group_name;
86
    $field_group->entity_type = 'node';
87
    $field_group->bundle = 'article';
88
    $field_group->mode = $mode;
89
    $field_group->parent_name = '';
90
    $field_group->children = $data['children'];
91
    $field_group->data = $data;
92
    drupal_write_record('field_group', $field_group);
93
    ctools_export_crud_enable('field_group', $field_group->identifier);
94

    
95
    return $field_group;
96
  }
97

    
98
  /**
99
   * Test if an empty  formatter.
100
   */
101
  function testFieldAccess() {
102

    
103
    $data = array(
104
      'label' => 'Wrapper',
105
      'weight' => '1',
106
      'children' => array(
107
        0 => 'field_no_access',
108
      ),
109
      'format_type' => 'div',
110
      'format_settings' => array(
111
        'label' => 'Link',
112
        'instance_settings' => array(
113
          'required_fields' => 0,
114
          'id' => 'wrapper-id',
115
          'classes' => 'test-class',
116
          'description' => '',
117
          'show_label' => FALSE,
118
          'label_element' => 'h3',
119
          'effect' => 'blink',
120
          'speed' => 'fast',
121
        ),
122
        'formatter' => 'open',
123
      ),
124
    );
125
    $group = $this->createGroup('default', $data);
126

    
127
    $groups = field_group_info_groups('node', 'article', 'default', TRUE);
128
    $this->drupalGet('node/' . $this->node->nid);
129

    
130
    // Test if group is not shown.
131
    $this->assertNoFieldByXPath("//div[contains(@id, 'wrapper-id')]", NULL, t('Div that contains fields with no access is not shown.'));
132
  }
133

    
134
  /**
135
   * Test the div formatter.
136
   */
137
  function testDiv() {
138

    
139
    $data = array(
140
      'label' => 'Wrapper',
141
      'weight' => '1',
142
      'children' => array(
143
        0 => 'field_test',
144
      ),
145
      'format_type' => 'div',
146
      'format_settings' => array(
147
        'label' => 'Link',
148
        'instance_settings' => array(
149
          'required_fields' => 0,
150
          'id' => 'wrapper-id',
151
          'classes' => 'test-class',
152
          'description' => '',
153
          'show_label' => FALSE,
154
          'label_element' => 'h3',
155
          'effect' => 'blink',
156
          'speed' => 'fast',
157
        ),
158
        'formatter' => 'open',
159
      ),
160
    );
161
    $group = $this->createGroup('default', $data);
162

    
163
    $groups = field_group_info_groups('node', 'article', 'default', TRUE);
164
    $this->drupalGet('node/' . $this->node->nid);
165

    
166
    // Test group ids and classes.
167
    $this->assertFieldByXPath("//div[contains(@id, 'wrapper-id')]", NULL, t('Wrapper id set on wrapper div'));
168
    $this->assertFieldByXPath("//div[contains(@class, 'test-class')]", NULL, t('Test class set on wrapper div') . 'class="' . $group->group_name . ' test-class');
169

    
170
    // Test group label.
171
    $this->assertNoRaw('<h3><span>' . $data['label'] . '</span></h3>', t('Label is not shown'));
172

    
173
    // Set show label to true.
174
    $group->data['format_settings']['instance_settings']['show_label'] = TRUE;
175

    
176
    drupal_write_record('field_group', $group, array('identifier'));
177
    $groups = field_group_info_groups('node', 'article', 'default', TRUE);
178
    $this->drupalGet('node/' . $this->node->nid);
179
    $this->assertRaw('<h3><span>' . $data['label'] . '</span></h3>', t('Label is shown'));
180

    
181
    // Change to collapsible
182
    $group->data['format_settings']['formatter'] = 'collapsible';
183
    drupal_write_record('field_group', $group, array('identifier'));
184
    $groups = field_group_info_groups('node', 'article', 'default', TRUE);
185
    $this->drupalGet('node/' . $this->node->nid);
186
    $this->assertFieldByXPath("//div[contains(@class, 'speed-fast')]", NULL, t('Speed class is set'));
187
    $this->assertFieldByXPath("//div[contains(@class, 'effect-blink')]", NULL, t('Effect class is set'));
188
  }
189

    
190
  /**
191
   * Test the horizontal tabs formatter.
192
   */
193
  function testHorizontalTabs() {
194

    
195
    $data = array(
196
      'label' => 'Tab 1',
197
      'weight' => '1',
198
      'children' => array(
199
        0 => 'field_test',
200
      ),
201
      'format_type' => 'htab',
202
      'format_settings' => array(
203
        'label' => 'Tab 1',
204
        'instance_settings' => array(
205
          'classes' => 'test-class',
206
          'description' => '',
207
        ),
208
        'formatter' => 'open',
209
      ),
210
    );
211
    $first_tab = $this->createGroup('default', $data);
212

    
213
    $data = array(
214
      'label' => 'Tab 2',
215
      'weight' => '1',
216
      'children' => array(
217
        0 => 'field_test_2',
218
      ),
219
      'format_type' => 'htab',
220
      'format_settings' => array(
221
        'label' => 'Tab 1',
222
        'instance_settings' => array(
223
          'classes' => 'test-class-2',
224
          'description' => 'description of second tab',
225
        ),
226
        'formatter' => 'closed',
227
      ),
228
    );
229
    $second_tab = $this->createGroup('default', $data);
230

    
231
    $data = array(
232
      'label' => 'Tabs',
233
      'weight' => '1',
234
      'children' => array(
235
        0 => $first_tab->group_name,
236
        1 => $second_tab->group_name,
237
      ),
238
      'format_type' => 'htabs',
239
      'format_settings' => array(
240
        'label' => 'Tab 1',
241
        'instance_settings' => array(
242
          'classes' => 'test-class-wrapper',
243
        ),
244
      ),
245
    );
246
    $tabs = $this->createGroup('default', $data);
247

    
248
    $groups = field_group_info_groups('node', 'article', 'default', TRUE);
249

    
250
    $this->drupalGet('node/' . $this->node->nid);
251

    
252
    // Test properties.
253
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
254
    $this->assertFieldByXPath("//fieldset[contains(@class, 'test-class-2')]", NULL, t('Test class set on second tab'));
255
    $this->assertRaw('<div class="fieldset-description">description of second tab</div>', t('Description of tab is shown'));
256
    $this->assertRaw('class="collapsible collapsed test-class-2', t('Second tab is default collapsed'));
257

    
258
    // Test if correctly nested
259
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@class, 'test-class')]", NULL, 'First tab is displayed as child of the wrapper.');
260
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@class, 'test-class-2')]", NULL, 'Second tab is displayed as child of the wrapper.');
261

    
262
  }
263

    
264
  /**
265
   * Test the vertical tabs formatter.
266
   */
267
  function testVerticalTabs() {
268

    
269
    $data = array(
270
      'label' => 'Tab 1',
271
      'weight' => '1',
272
      'children' => array(
273
        0 => 'field_test',
274
      ),
275
      'format_type' => 'tab',
276
      'format_settings' => array(
277
        'label' => 'Tab 1',
278
        'instance_settings' => array(
279
          'classes' => 'test-class',
280
          'description' => '',
281
        ),
282
        'formatter' => 'open',
283
      ),
284
    );
285
    $first_tab = $this->createGroup('default', $data);
286
    $first_tab_id = 'edit-' . $first_tab->group_name;
287

    
288
    $data = array(
289
      'label' => 'Tab 2',
290
      'weight' => '1',
291
      'children' => array(
292
        0 => 'field_test_2',
293
      ),
294
      'format_type' => 'tab',
295
      'format_settings' => array(
296
        'label' => 'Tab 1',
297
        'instance_settings' => array(
298
          'classes' => 'test-class-2',
299
          'description' => 'description of second tab',
300
        ),
301
        'formatter' => 'closed',
302
      ),
303
    );
304
    $second_tab = $this->createGroup('default', $data);
305
    $second_tab_id = 'edit-' . $second_tab->group_name;
306

    
307
    $data = array(
308
      'label' => 'Tabs',
309
      'weight' => '1',
310
      'children' => array(
311
        0 => $first_tab->group_name,
312
        1 => $second_tab->group_name,
313
      ),
314
      'format_type' => 'tabs',
315
      'format_settings' => array(
316
        'label' => 'Tab 1',
317
        'instance_settings' => array(
318
          'classes' => 'test-class-wrapper',
319
        ),
320
      ),
321
    );
322
    $tabs = $this->createGroup('default', $data);
323

    
324
    $groups = field_group_info_groups('node', 'article', 'default', TRUE);
325

    
326
    $this->drupalGet('node/' . $this->node->nid);
327

    
328
    // Test properties.
329
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
330
    $this->assertFieldByXPath("//fieldset[contains(@class, 'test-class-2')]", NULL, t('Test class set on second tab'));
331
    $this->assertRaw('<div class="fieldset-description">description of second tab</div>', t('Description of tab is shown'));
332
    $this->assertRaw('class="collapsible collapsed test-class-2', t('Second tab is default collapsed'));
333

    
334
    // Test if correctly nested
335
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$first_tab_id')]", NULL, 'First tab is displayed as child of the wrapper.');
336
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$second_tab_id')]", NULL, 'Second tab is displayed as child of the wrapper.');
337
  }
338

    
339
  /**
340
   * Test the accordion formatter.
341
   */
342
  function testAccordion() {
343

    
344
    $data = array(
345
      'label' => 'Accordion item 1',
346
      'weight' => '1',
347
      'children' => array(
348
        0 => 'field_test',
349
      ),
350
      'format_type' => 'accordion-item',
351
      'format_settings' => array(
352
        'label' => 'Accordion item 1',
353
        'instance_settings' => array(
354
          'classes' => 'test-class',
355
        ),
356
        'formatter' => 'closed',
357
      ),
358
    );
359
    $first_item = $this->createGroup('default', $data);
360
    $first_item_id = 'node_article_full_' . $first_item->group_name;
361

    
362
    $data = array(
363
      'label' => 'Accordion item 2',
364
      'weight' => '1',
365
      'children' => array(
366
        0 => 'field_test_2',
367
      ),
368
      'format_type' => 'accordion-item',
369
      'format_settings' => array(
370
        'label' => 'Tab 2',
371
        'instance_settings' => array(
372
          'classes' => 'test-class-2',
373
        ),
374
        'formatter' => 'open',
375
      ),
376
    );
377
    $second_item = $this->createGroup('default', $data);
378
    $second_item_id = 'node_article_full_' . $second_item->group_name;
379

    
380
    $data = array(
381
      'label' => 'Accordion',
382
      'weight' => '1',
383
      'children' => array(
384
        0 => $first_item->group_name,
385
        1 => $second_item->group_name,
386
      ),
387
      'format_type' => 'accordion',
388
      'format_settings' => array(
389
        'label' => 'Tab 1',
390
        'instance_settings' => array(
391
          'classes' => 'test-class-wrapper',
392
          'effect' => 'bounceslide'
393
        ),
394
      ),
395
    );
396
    $accordion = $this->createGroup('default', $data);
397

    
398
    $groups = field_group_info_groups('node', 'article', 'default', TRUE);
399

    
400
    $this->drupalGet('node/' . $this->node->nid);
401

    
402
    // Test properties.
403
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
404
    $this->assertFieldByXPath("//div[contains(@class, 'effect-bounceslide')]", NULL, t('Correct effect is set on the accordion'));
405
    $this->assertFieldByXPath("//div[contains(@class, 'test-class')]", NULL, t('Accordion item with test-class is shown'));
406
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-2')]", NULL, t('Accordion item with test-class-2 is shown'));
407
    $this->assertFieldByXPath("//h3[contains(@class, 'field-group-accordion-active')]", NULL, t('Accordion item 2 was set active'));
408

    
409
    // Test if correctly nested
410
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//div[contains(@class, 'test-class')]", NULL, 'First item is displayed as child of the wrapper.');
411
    $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//div[contains(@class, 'test-class-2')]", NULL, 'Second item is displayed as child of the wrapper.');
412
  }
413

    
414
}