ElementsLabelsTest.php 7.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Form\ElementsLabelsTest.
 */

namespace Drupal\system\Tests\Form;

use Drupal\simpletest\WebTestBase;

/**
13
14
15
 * Tests form element labels, required markers and associated output.
 *
 * @group Form
16
17
18
 */
class ElementsLabelsTest extends WebTestBase {

19
20
21
22
23
24
25
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('form_test');

26
27
28
29
30
31
32
33
34
35
  /**
   * Test form elements, labels, title attibutes and required marks output
   * correctly and have the correct label option class if needed.
   */
  function testFormLabels() {
    $this->drupalGet('form_test/form-labels');

    // Check that the checkbox/radio processing is not interfering with
    // basic placement.
    $elements = $this->xpath('//input[@id="edit-form-checkboxes-test-third-checkbox"]/following-sibling::label[@for="edit-form-checkboxes-test-third-checkbox" and @class="option"]');
36
    $this->assertTrue(isset($elements[0]), 'Label follows field and label option class correct for regular checkboxes.');
37
38
39

    // Make sure the label is rendered for checkboxes.
    $elements = $this->xpath('//input[@id="edit-form-checkboxes-test-0"]/following-sibling::label[@for="edit-form-checkboxes-test-0" and @class="option"]');
40
    $this->assertTrue(isset($elements[0]), 'Label 0 found checkbox.');
41
42

    $elements = $this->xpath('//input[@id="edit-form-radios-test-second-radio"]/following-sibling::label[@for="edit-form-radios-test-second-radio" and @class="option"]');
43
    $this->assertTrue(isset($elements[0]), 'Label follows field and label option class correct for regular radios.');
44
45
46

    // Make sure the label is rendered for radios.
    $elements = $this->xpath('//input[@id="edit-form-radios-test-0"]/following-sibling::label[@for="edit-form-radios-test-0" and @class="option"]');
47
    $this->assertTrue(isset($elements[0]), 'Label 0 found radios.');
48
49
50
51

    // Exercise various defaults for checkboxes and modifications to ensure
    // appropriate override and correct behavior.
    $elements = $this->xpath('//input[@id="edit-form-checkbox-test"]/following-sibling::label[@for="edit-form-checkbox-test" and @class="option"]');
52
    $this->assertTrue(isset($elements[0]), 'Label follows field and label option class correct for a checkbox by default.');
53
54
55

    // Exercise various defaults for textboxes and modifications to ensure
    // appropriate override and correct behavior.
56
    $elements = $this->xpath('//label[@for="edit-form-textfield-test-title-and-required" and @class="form-required"]/following-sibling::input[@id="edit-form-textfield-test-title-and-required"]');
57
    $this->assertTrue(isset($elements[0]), 'Label precedes textfield, with required marker inside label.');
58

59
    $elements = $this->xpath('//input[@id="edit-form-textfield-test-no-title-required"]/preceding-sibling::label[@for="edit-form-textfield-test-no-title-required" and @class="form-required"]');
60
    $this->assertTrue(isset($elements[0]), 'Label tag with required marker precedes required textfield with no title.');
61

62
63
    $elements = $this->xpath('//input[@id="edit-form-textfield-test-title-invisible"]/preceding-sibling::label[@for="edit-form-textfield-test-title-invisible" and @class="visually-hidden"]');
    $this->assertTrue(isset($elements[0]), 'Label preceding field and label class is visually-hidden.');
64

65
    $elements = $this->xpath('//input[@id="edit-form-textfield-test-title"]/preceding-sibling::span[@class="form-required"]');
66
    $this->assertFalse(isset($elements[0]), 'No required marker on non-required field.');
67
68

    $elements = $this->xpath('//input[@id="edit-form-textfield-test-title-after"]/following-sibling::label[@for="edit-form-textfield-test-title-after" and @class="option"]');
69
    $this->assertTrue(isset($elements[0]), 'Label after field and label option class correct for text field.');
70
71

    $elements = $this->xpath('//label[@for="edit-form-textfield-test-title-no-show"]');
72
    $this->assertFalse(isset($elements[0]), 'No label tag when title set not to display.');
73

74
75
76
    $elements = $this->xpath('//div[contains(@class, "form-item-form-textfield-test-title-invisible") and contains(@class, "form-no-label")]');
    $this->assertTrue(isset($elements[0]), 'Field class is form-no-label when there is no label.');

77
78
    // Check #field_prefix and #field_suffix placement.
    $elements = $this->xpath('//span[@class="field-prefix"]/following-sibling::div[@id="edit-form-radios-test"]');
79
    $this->assertTrue(isset($elements[0]), 'Properly placed the #field_prefix element after the label and before the field.');
80
81

    $elements = $this->xpath('//span[@class="field-suffix"]/preceding-sibling::div[@id="edit-form-radios-test"]');
82
    $this->assertTrue(isset($elements[0]), 'Properly places the #field_suffix element immediately after the form field.');
83
84
85

    // Check #prefix and #suffix placement.
    $elements = $this->xpath('//div[@id="form-test-textfield-title-prefix"]/following-sibling::div[contains(@class, \'form-item-form-textfield-test-title\')]');
86
    $this->assertTrue(isset($elements[0]), 'Properly places the #prefix element before the form item.');
87
88

    $elements = $this->xpath('//div[@id="form-test-textfield-title-suffix"]/preceding-sibling::div[contains(@class, \'form-item-form-textfield-test-title\')]');
89
    $this->assertTrue(isset($elements[0]), 'Properly places the #suffix element before the form item.');
90
91
92
93
94
95
96

    // Check title attribute for radios and checkboxes.
    $elements = $this->xpath('//div[@id="edit-form-checkboxes-title-attribute"]');
    $this->assertEqual($elements[0]['title'], 'Checkboxes test' . ' (' . t('Required') . ')', 'Title attribute found.');
    $elements = $this->xpath('//div[@id="edit-form-radios-title-attribute"]');
    $this->assertEqual($elements[0]['title'], 'Radios test' . ' (' . t('Required') . ')', 'Title attribute found.');
  }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

  /**
   * Tests different display options for form element descriptions.
   */
  function testFormDescriptions() {
    $this->drupalGet('form_test/form-descriptions');

    // Check #description placement with #description_display='after'.
    $field_id = 'edit-form-textfield-test-description-after';
    $description_id = $field_id . '--description';
    $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[@id="' . $description_id . '"]');
    $this->assertTrue(isset($elements[0]), t('Properly places the #description element after the form item.'));

    // Check #description placement with #description_display='before'.
    $field_id = 'edit-form-textfield-test-description-before';
    $description_id = $field_id . '--description';
    $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/preceding-sibling::div[@id="' . $description_id . '"]');
    $this->assertTrue(isset($elements[0]), t('Properly places the #description element before the form item.'));

    // Check if the class is 'visually-hidden' on the form element description
    // for the option with #description_display='invisible' and also check that
    // the description is placed after the form element.
    $field_id = 'edit-form-textfield-test-description-invisible';
    $description_id = $field_id . '--description';
    $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[contains(@class, "visually-hidden")]');
    $this->assertTrue(isset($elements[0]), t('Properly renders the #description element visually-hidden.'));
  }

125
}