3 namespace Drupal\system\Tests\Form;
5 use Drupal\simpletest\WebTestBase;
8 * Tests building and processing of core form elements.
12 class ElementTest extends WebTestBase {
19 public static $modules = ['form_test'];
22 * Tests placeholder text for elements that support placeholders.
24 public function testPlaceHolderText() {
25 $this->drupalGet('form-test/placeholder-text');
26 $expected = 'placeholder-text';
27 // Test to make sure non-textarea elements have the proper placeholder text.
28 foreach (['textfield', 'tel', 'url', 'password', 'email', 'number'] as $type) {
29 $element = $this->xpath('//input[@id=:id and @placeholder=:expected]', [
30 ':id' => 'edit-' . $type,
31 ':expected' => $expected,
33 $this->assertTrue(!empty($element), format_string('Placeholder text placed in @type.', ['@type' => $type]));
36 // Test to make sure textarea has the proper placeholder text.
37 $element = $this->xpath('//textarea[@id=:id and @placeholder=:expected]', [
38 ':id' => 'edit-textarea',
39 ':expected' => $expected,
41 $this->assertTrue(!empty($element), 'Placeholder text placed in textarea.');
45 * Tests expansion of #options for #type checkboxes and radios.
47 public function testOptions() {
48 $this->drupalGet('form-test/checkboxes-radios');
50 // Verify that all options appear in their defined order.
51 foreach (['checkbox', 'radio'] as $type) {
52 $elements = $this->xpath('//input[@type=:type]', [':type' => $type]);
53 $expected_values = ['0', 'foo', '1', 'bar', '>'];
54 foreach ($elements as $element) {
55 $expected = array_shift($expected_values);
56 $this->assertIdentical((string) $element['value'], $expected);
60 // Verify that the choices are admin filtered as expected.
61 $this->assertRaw("<em>Special Char</em>alert('checkboxes');");
62 $this->assertRaw("<em>Special Char</em>alert('radios');");
63 $this->assertRaw('<em>Bar - checkboxes</em>');
64 $this->assertRaw('<em>Bar - radios</em>');
66 // Enable customized option sub-elements.
67 $this->drupalGet('form-test/checkboxes-radios/customize');
69 // Verify that all options appear in their defined order, taking a custom
70 // #weight into account.
71 foreach (['checkbox', 'radio'] as $type) {
72 $elements = $this->xpath('//input[@type=:type]', [':type' => $type]);
73 $expected_values = ['0', 'foo', 'bar', '>', '1'];
74 foreach ($elements as $element) {
75 $expected = array_shift($expected_values);
76 $this->assertIdentical((string) $element['value'], $expected);
79 // Verify that custom #description properties are output.
80 foreach (['checkboxes', 'radios'] as $type) {
81 $elements = $this->xpath('//input[@id=:id]/following-sibling::div[@class=:class]', [
82 ':id' => 'edit-' . $type . '-foo',
83 ':class' => 'description',
85 $this->assertTrue(count($elements), format_string('Custom %type option description found.', [
92 * Tests wrapper ids for checkboxes and radios.
94 public function testWrapperIds() {
95 $this->drupalGet('form-test/checkboxes-radios');
97 // Verify that wrapper id is different from element id.
98 foreach (['checkboxes', 'radios'] as $type) {
99 $element_ids = $this->xpath('//div[@id=:id]', [':id' => 'edit-' . $type]);
100 $wrapper_ids = $this->xpath('//fieldset[@id=:id]', [':id' => 'edit-' . $type . '--wrapper']);
101 $this->assertTrue(count($element_ids) == 1, format_string('A single element id found for type %type', ['%type' => $type]));
102 $this->assertTrue(count($wrapper_ids) == 1, format_string('A single wrapper id found for type %type', ['%type' => $type]));
107 * Tests button classes.
109 public function testButtonClasses() {
110 $this->drupalGet('form-test/button-class');
111 // Just contains(@class, "button") won't do because then
112 // "button--foo" would contain "button". Instead, check
113 // " button ". Make sure it matches in the beginning and the end too
114 // by adding a space before and after.
115 $this->assertEqual(2, count($this->xpath('//*[contains(concat(" ", @class, " "), " button ")]')));
116 $this->assertEqual(1, count($this->xpath('//*[contains(concat(" ", @class, " "), " button--foo ")]')));
117 $this->assertEqual(1, count($this->xpath('//*[contains(concat(" ", @class, " "), " button--danger ")]')));
121 * Tests the #group property.
123 public function testGroupElements() {
124 $this->drupalGet('form-test/group-details');
125 $elements = $this->xpath('//div[@class="details-wrapper"]//div[@class="details-wrapper"]//label');
126 $this->assertTrue(count($elements) == 1);
127 $this->drupalGet('form-test/group-container');
128 $elements = $this->xpath('//div[@id="edit-container"]//div[@class="details-wrapper"]//label');
129 $this->assertTrue(count($elements) == 1);
130 $this->drupalGet('form-test/group-fieldset');
131 $elements = $this->xpath('//fieldset[@id="edit-fieldset"]//div[@id="edit-meta"]//label');
132 $this->assertTrue(count($elements) == 1);
133 $this->drupalGet('form-test/group-vertical-tabs');
134 $elements = $this->xpath('//div[@data-vertical-tabs-panes]//details[@id="edit-meta"]//label');
135 $this->assertTrue(count($elements) == 1);
136 $elements = $this->xpath('//div[@data-vertical-tabs-panes]//details[@id="edit-meta-2"]//label');
137 $this->assertTrue(count($elements) == 1);
141 * Tests the #required property on details and fieldset elements.
143 public function testRequiredFieldsetsAndDetails() {
144 $this->drupalGet('form-test/group-details');
145 $this->assertFalse($this->cssSelect('summary.form-required'));
146 $this->drupalGet('form-test/group-details/1');
147 $this->assertTrue($this->cssSelect('summary.form-required'));
148 $this->drupalGet('form-test/group-fieldset');
149 $this->assertFalse($this->cssSelect('span.form-required'));
150 $this->drupalGet('form-test/group-fieldset/1');
151 $this->assertTrue($this->cssSelect('span.form-required'));
155 * Tests a form with a autocomplete setting..
157 public function testFormAutocomplete() {
158 $this->drupalGet('form-test/autocomplete');
160 $result = $this->xpath('//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
161 $this->assertEqual(count($result), 0, 'Ensure that the user does not have access to the autocompletion');
162 $result = $this->xpath('//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
163 $this->assertEqual(count($result), 0, 'Ensure that the user does not have access to the autocompletion');
165 $user = $this->drupalCreateUser(['access autocomplete test']);
166 $this->drupalLogin($user);
167 $this->drupalGet('form-test/autocomplete');
169 // Make sure that the autocomplete library is added.
170 $this->assertRaw('core/misc/autocomplete.js');
172 $result = $this->xpath('//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
173 $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
174 $result = $this->xpath('//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
175 $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
179 * Tests form element error messages.
181 public function testFormElementErrors() {
182 $this->drupalPostForm('form_test/details-form', [], 'Submit');
183 $this->assertText('I am an error on the details element.');