57da8daf278649a48d6b88d47c81700337845f80
[yaffs-website] / web / core / modules / system / tests / src / Functional / Form / ElementTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Form;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests building and processing of core form elements.
9  *
10  * @group Form
11  */
12 class ElementTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['form_test'];
20
21   /**
22    * Tests placeholder text for elements that support placeholders.
23    */
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,
32       ]);
33       $this->assertTrue(!empty($element), format_string('Placeholder text placed in @type.', ['@type' => $type]));
34     }
35
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,
40     ]);
41     $this->assertTrue(!empty($element), 'Placeholder text placed in textarea.');
42   }
43
44   /**
45    * Tests expansion of #options for #type checkboxes and radios.
46    */
47   public function testOptions() {
48     $this->drupalGet('form-test/checkboxes-radios');
49
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);
57       }
58     }
59
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>');
65
66     // Enable customized option sub-elements.
67     $this->drupalGet('form-test/checkboxes-radios/customize');
68
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);
77       }
78     }
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',
84       ]);
85       $this->assertTrue(count($elements), format_string('Custom %type option description found.', [
86         '%type' => $type,
87       ]));
88     }
89   }
90
91   /**
92    * Tests correct checked attribute for radios element.
93    */
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());
129   }
130
131   /**
132    * Tests wrapper ids for checkboxes and radios.
133    */
134   public function testWrapperIds() {
135     $this->drupalGet('form-test/checkboxes-radios');
136
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]));
143     }
144   }
145
146   /**
147    * Tests button classes.
148    */
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 ")]')));
158   }
159
160   /**
161    * Tests the #group property.
162    */
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);
178   }
179
180   /**
181    * Tests the #required property on details and fieldset elements.
182    */
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'));
192   }
193
194   /**
195    * Tests a form with a autocomplete setting..
196    */
197   public function testFormAutocomplete() {
198     $this->drupalGet('form-test/autocomplete');
199
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');
204
205     $user = $this->drupalCreateUser(['access autocomplete test']);
206     $this->drupalLogin($user);
207     $this->drupalGet('form-test/autocomplete');
208
209     // Make sure that the autocomplete library is added.
210     $this->assertRaw('core/misc/autocomplete.js');
211
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');
216   }
217
218   /**
219    * Tests form element error messages.
220    */
221   public function testFormElementErrors() {
222     $this->drupalPostForm('form_test/details-form', [], 'Submit');
223     $this->assertText('I am an error on the details element.');
224   }
225
226 }