3 namespace Drupal\Tests\system\Functional\Form;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests building and processing of core form elements.
12 class ElementTest extends BrowserTestBase {
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->getAttribute('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->getAttribute('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 correct checked attribute for radios element.
94 public function testRadiosChecked() {
95 // Verify that there is only one radio option checked.
96 $this->drupalGet('form-test/radios-checked');
97 $elements = $this->xpath('//input[@name="radios" and @checked]');
98 $this->assertCount(1, $elements);
99 $this->assertSame('0', $elements[0]->getValue());
100 $elements = $this->xpath('//input[@name="radios-string" and @checked]');
101 $this->assertCount(1, $elements);
102 $this->assertSame('bar', $elements[0]->getValue());
103 $elements = $this->xpath('//input[@name="radios-boolean-true" and @checked]');
104 $this->assertCount(1, $elements);
105 $this->assertSame('1', $elements[0]->getValue());
106 // A default value of FALSE indicates that nothing is set.
107 $elements = $this->xpath('//input[@name="radios-boolean-false" and @checked]');
108 $this->assertCount(0, $elements);
109 $elements = $this->xpath('//input[@name="radios-boolean-any" and @checked]');
110 $this->assertCount(1, $elements);
111 $this->assertSame('All', $elements[0]->getValue());
112 $elements = $this->xpath('//input[@name="radios-string-zero" and @checked]');
113 $this->assertCount(1, $elements);
114 $this->assertSame('0', $elements[0]->getValue());
115 $elements = $this->xpath('//input[@name="radios-int-non-zero" and @checked]');
116 $this->assertCount(1, $elements);
117 $this->assertSame('10', $elements[0]->getValue());
118 $elements = $this->xpath('//input[@name="radios-int-non-zero-as-string" and @checked]');
119 $this->assertCount(1, $elements);
120 $this->assertSame('100', $elements[0]->getValue());
121 $elements = $this->xpath('//input[@name="radios-empty-string" and @checked]');
122 $this->assertCount(1, $elements);
123 $this->assertSame('0', $elements[0]->getValue());
124 $elements = $this->xpath('//input[@name="radios-empty-array" and @checked]');
125 $this->assertCount(0, $elements);
126 $elements = $this->xpath('//input[@name="radios-key-FALSE" and @checked]');
127 $this->assertCount(1, $elements);
128 $this->assertSame('0', $elements[0]->getValue());
132 * Tests wrapper ids for checkboxes and radios.
134 public function testWrapperIds() {
135 $this->drupalGet('form-test/checkboxes-radios');
137 // Verify that wrapper id is different from element id.
138 foreach (['checkboxes', 'radios'] as $type) {
139 $element_ids = $this->xpath('//div[@id=:id]', [':id' => 'edit-' . $type]);
140 $wrapper_ids = $this->xpath('//fieldset[@id=:id]', [':id' => 'edit-' . $type . '--wrapper']);
141 $this->assertTrue(count($element_ids) == 1, format_string('A single element id found for type %type', ['%type' => $type]));
142 $this->assertTrue(count($wrapper_ids) == 1, format_string('A single wrapper id found for type %type', ['%type' => $type]));
147 * Tests button classes.
149 public function testButtonClasses() {
150 $this->drupalGet('form-test/button-class');
151 // Just contains(@class, "button") won't do because then
152 // "button--foo" would contain "button". Instead, check
153 // " button ". Make sure it matches in the beginning and the end too
154 // by adding a space before and after.
155 $this->assertEqual(2, count($this->xpath('//*[contains(concat(" ", @class, " "), " button ")]')));
156 $this->assertEqual(1, count($this->xpath('//*[contains(concat(" ", @class, " "), " button--foo ")]')));
157 $this->assertEqual(1, count($this->xpath('//*[contains(concat(" ", @class, " "), " button--danger ")]')));
161 * Tests the #group property.
163 public function testGroupElements() {
164 $this->drupalGet('form-test/group-details');
165 $elements = $this->xpath('//div[@class="details-wrapper"]//div[@class="details-wrapper"]//label');
166 $this->assertTrue(count($elements) == 1);
167 $this->drupalGet('form-test/group-container');
168 $elements = $this->xpath('//div[@id="edit-container"]//div[@class="details-wrapper"]//label');
169 $this->assertTrue(count($elements) == 1);
170 $this->drupalGet('form-test/group-fieldset');
171 $elements = $this->xpath('//fieldset[@id="edit-fieldset"]//div[@id="edit-meta"]//label');
172 $this->assertTrue(count($elements) == 1);
173 $this->drupalGet('form-test/group-vertical-tabs');
174 $elements = $this->xpath('//div[@data-vertical-tabs-panes]//details[@id="edit-meta"]//label');
175 $this->assertTrue(count($elements) == 1);
176 $elements = $this->xpath('//div[@data-vertical-tabs-panes]//details[@id="edit-meta-2"]//label');
177 $this->assertTrue(count($elements) == 1);
181 * Tests the #required property on details and fieldset elements.
183 public function testRequiredFieldsetsAndDetails() {
184 $this->drupalGet('form-test/group-details');
185 $this->assertFalse($this->cssSelect('summary.form-required'));
186 $this->drupalGet('form-test/group-details/1');
187 $this->assertTrue($this->cssSelect('summary.form-required'));
188 $this->drupalGet('form-test/group-fieldset');
189 $this->assertFalse($this->cssSelect('span.form-required'));
190 $this->drupalGet('form-test/group-fieldset/1');
191 $this->assertTrue($this->cssSelect('span.form-required'));
195 * Tests a form with a autocomplete setting..
197 public function testFormAutocomplete() {
198 $this->drupalGet('form-test/autocomplete');
200 $result = $this->xpath('//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
201 $this->assertEqual(count($result), 0, 'Ensure that the user does not have access to the autocompletion');
202 $result = $this->xpath('//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
203 $this->assertEqual(count($result), 0, 'Ensure that the user does not have access to the autocompletion');
205 $user = $this->drupalCreateUser(['access autocomplete test']);
206 $this->drupalLogin($user);
207 $this->drupalGet('form-test/autocomplete');
209 // Make sure that the autocomplete library is added.
210 $this->assertRaw('core/misc/autocomplete.js');
212 $result = $this->xpath('//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
213 $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
214 $result = $this->xpath('//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
215 $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
219 * Tests form element error messages.
221 public function testFormElementErrors() {
222 $this->drupalPostForm('form_test/details-form', [], 'Submit');
223 $this->assertText('I am an error on the details element.');