Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\FunctionalTests;
|
Chris@0
|
4
|
Chris@0
|
5 use Behat\Mink\Exception\ExpectationException;
|
Chris@0
|
6 use Drupal\Component\Serialization\Json;
|
Chris@0
|
7 use Drupal\Component\Utility\Html;
|
Chris@0
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
10 use Drupal\Tests\Traits\Core\CronRunTrait;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Tests BrowserTestBase functionality.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group browsertestbase
|
Chris@0
|
16 */
|
Chris@0
|
17 class BrowserTestBaseTest extends BrowserTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 use CronRunTrait;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Modules to enable.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var array
|
Chris@0
|
25 */
|
Chris@0
|
26 public static $modules = ['test_page_test', 'form_test', 'system_test'];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Tests basic page test.
|
Chris@0
|
30 */
|
Chris@0
|
31 public function testGoTo() {
|
Chris@0
|
32 $account = $this->drupalCreateUser();
|
Chris@0
|
33 $this->drupalLogin($account);
|
Chris@0
|
34
|
Chris@0
|
35 // Visit a Drupal page that requires login.
|
Chris@0
|
36 $this->drupalGet('test-page');
|
Chris@0
|
37 $this->assertSession()->statusCodeEquals(200);
|
Chris@0
|
38
|
Chris@0
|
39 // Test page contains some text.
|
Chris@0
|
40 $this->assertSession()->pageTextContains('Test page text.');
|
Chris@0
|
41
|
Chris@0
|
42 // Check that returned plain text is correct.
|
Chris@0
|
43 $text = $this->getTextContent();
|
Chris@0
|
44 $this->assertContains('Test page text.', $text);
|
Chris@0
|
45 $this->assertNotContains('</html>', $text);
|
Chris@0
|
46
|
Chris@0
|
47 // Response includes cache tags that we can assert.
|
Chris@0
|
48 $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');
|
Chris@0
|
49
|
Chris@0
|
50 // Test that we can read the JS settings.
|
Chris@0
|
51 $js_settings = $this->getDrupalSettings();
|
Chris@0
|
52 $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
|
Chris@0
|
53
|
Chris@0
|
54 // Test drupalGet with a url object.
|
Chris@0
|
55 $url = Url::fromRoute('test_page_test.render_title');
|
Chris@0
|
56 $this->drupalGet($url);
|
Chris@0
|
57 $this->assertSession()->statusCodeEquals(200);
|
Chris@0
|
58
|
Chris@0
|
59 // Test page contains some text.
|
Chris@0
|
60 $this->assertSession()->pageTextContains('Hello Drupal');
|
Chris@0
|
61
|
Chris@0
|
62 // Test that setting headers with drupalGet() works.
|
Chris@0
|
63 $this->drupalGet('system-test/header', [], [
|
Chris@0
|
64 'Test-Header' => 'header value',
|
Chris@0
|
65 ]);
|
Chris@0
|
66 $returned_header = $this->getSession()->getResponseHeader('Test-Header');
|
Chris@0
|
67 $this->assertSame('header value', $returned_header);
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Tests basic form functionality.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function testForm() {
|
Chris@0
|
74 // Ensure the proper response code for a _form route.
|
Chris@0
|
75 $this->drupalGet('form-test/object-builder');
|
Chris@0
|
76 $this->assertSession()->statusCodeEquals(200);
|
Chris@0
|
77
|
Chris@0
|
78 // Ensure the form and text field exist.
|
Chris@0
|
79 $this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
|
Chris@0
|
80 $this->assertSession()->fieldExists('bananas');
|
Chris@0
|
81
|
Chris@0
|
82 // Check that the hidden field exists and has a specific value.
|
Chris@0
|
83 $this->assertSession()->hiddenFieldExists('strawberry');
|
Chris@0
|
84 $this->assertSession()->hiddenFieldExists('red');
|
Chris@0
|
85 $this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield');
|
Chris@0
|
86 $this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown');
|
Chris@0
|
87 $this->assertSession()->hiddenFieldValueEquals('strawberry', 'red');
|
Chris@0
|
88
|
Chris@0
|
89 // Check that a hidden field does not exist.
|
Chris@0
|
90 $this->assertSession()->hiddenFieldNotExists('bananas');
|
Chris@0
|
91 $this->assertSession()->hiddenFieldNotExists('pineapple');
|
Chris@0
|
92
|
Chris@0
|
93 $edit = ['bananas' => 'green'];
|
Chris@0
|
94 $this->submitForm($edit, 'Save', 'form-test-form-test-object');
|
Chris@0
|
95
|
Chris@0
|
96 $config_factory = $this->container->get('config.factory');
|
Chris@0
|
97 $value = $config_factory->get('form_test.object')->get('bananas');
|
Chris@0
|
98 $this->assertSame('green', $value);
|
Chris@0
|
99
|
Chris@0
|
100 // Test drupalPostForm().
|
Chris@0
|
101 $edit = ['bananas' => 'red'];
|
Chris@0
|
102 $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
|
Chris@0
|
103 $this->assertSame($this->getSession()->getPage()->getContent(), $result);
|
Chris@0
|
104 $value = $config_factory->get('form_test.object')->get('bananas');
|
Chris@0
|
105 $this->assertSame('red', $value);
|
Chris@0
|
106
|
Chris@0
|
107 $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
|
Chris@0
|
108 $value = $config_factory->get('form_test.object')->get('bananas');
|
Chris@0
|
109 $this->assertSame('', $value);
|
Chris@0
|
110
|
Chris@0
|
111 // Test drupalPostForm() with no-html response.
|
Chris@0
|
112 $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
|
Chris@0
|
113 $this->assertTrue(1000, $values['beer']);
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Tests clickLink() functionality.
|
Chris@0
|
118 */
|
Chris@0
|
119 public function testClickLink() {
|
Chris@0
|
120 $this->drupalGet('test-page');
|
Chris@0
|
121 $this->clickLink('Visually identical test links');
|
Chris@0
|
122 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
|
Chris@0
|
123 $this->drupalGet('test-page');
|
Chris@0
|
124 $this->clickLink('Visually identical test links', 0);
|
Chris@0
|
125 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
|
Chris@0
|
126 $this->drupalGet('test-page');
|
Chris@0
|
127 $this->clickLink('Visually identical test links', 1);
|
Chris@0
|
128 $this->assertContains('user/register', $this->getSession()->getCurrentUrl());
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 public function testError() {
|
Chris@0
|
132 $this->setExpectedException('\Exception', 'User notice: foo');
|
Chris@0
|
133 $this->drupalGet('test-error');
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Tests linkExists() with pipe character (|) in locator.
|
Chris@0
|
138 *
|
Chris@0
|
139 * @see \Drupal\Tests\WebAssert::linkExists()
|
Chris@0
|
140 */
|
Chris@0
|
141 public function testPipeCharInLocator() {
|
Chris@0
|
142 $this->drupalGet('test-pipe-char');
|
Chris@0
|
143 $this->assertSession()->linkExists('foo|bar|baz');
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * Tests linkExistsExact() functionality.
|
Chris@0
|
148 *
|
Chris@0
|
149 * @see \Drupal\Tests\WebAssert::linkExistsExact()
|
Chris@0
|
150 */
|
Chris@0
|
151 public function testLinkExistsExact() {
|
Chris@0
|
152 $this->drupalGet('test-pipe-char');
|
Chris@0
|
153 $this->assertSession()->linkExistsExact('foo|bar|baz');
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * Tests linkExistsExact() functionality fail.
|
Chris@0
|
158 *
|
Chris@0
|
159 * @see \Drupal\Tests\WebAssert::linkExistsExact()
|
Chris@0
|
160 */
|
Chris@0
|
161 public function testInvalidLinkExistsExact() {
|
Chris@0
|
162 $this->drupalGet('test-pipe-char');
|
Chris@0
|
163 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar found');
|
Chris@0
|
164 $this->assertSession()->linkExistsExact('foo|bar');
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * Tests linkNotExistsExact() functionality.
|
Chris@0
|
169 *
|
Chris@0
|
170 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
|
Chris@0
|
171 */
|
Chris@0
|
172 public function testLinkNotExistsExact() {
|
Chris@0
|
173 $this->drupalGet('test-pipe-char');
|
Chris@0
|
174 $this->assertSession()->linkNotExistsExact('foo|bar');
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * Tests linkNotExistsExact() functionality fail.
|
Chris@0
|
179 *
|
Chris@0
|
180 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
|
Chris@0
|
181 */
|
Chris@0
|
182 public function testInvalidLinkNotExistsExact() {
|
Chris@0
|
183 $this->drupalGet('test-pipe-char');
|
Chris@0
|
184 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar|baz not found');
|
Chris@0
|
185 $this->assertSession()->linkNotExistsExact('foo|bar|baz');
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * Tests legacy text asserts.
|
Chris@0
|
190 */
|
Chris@14
|
191 public function testTextAsserts() {
|
Chris@0
|
192 $this->drupalGet('test-encoded');
|
Chris@0
|
193 $dangerous = 'Bad html <script>alert(123);</script>';
|
Chris@0
|
194 $sanitized = Html::escape($dangerous);
|
Chris@0
|
195 $this->assertNoText($dangerous);
|
Chris@0
|
196 $this->assertText($sanitized);
|
Chris@0
|
197
|
Chris@0
|
198 // Test getRawContent().
|
Chris@0
|
199 $this->assertSame($this->getSession()->getPage()->getContent(), $this->getRawContent());
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 /**
|
Chris@0
|
203 * Tests legacy field asserts which use xpath directly.
|
Chris@0
|
204 */
|
Chris@14
|
205 public function testXpathAsserts() {
|
Chris@0
|
206 $this->drupalGet('test-field-xpath');
|
Chris@0
|
207 $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
|
Chris@0
|
208 $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
|
Chris@0
|
209 $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
|
Chris@0
|
210
|
Chris@0
|
211 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name');
|
Chris@0
|
212 $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
|
Chris@0
|
213 $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
|
Chris@0
|
214 $this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
|
Chris@0
|
215
|
Chris@0
|
216 $this->assertNoFieldByXPath('//notexisting');
|
Chris@0
|
217 $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
|
Chris@0
|
218
|
Chris@0
|
219 // Test that the assertion fails correctly.
|
Chris@0
|
220 try {
|
Chris@0
|
221 $this->assertFieldByXPath("//input[@id = 'notexisting']");
|
Chris@0
|
222 $this->fail('The "notexisting" field was found.');
|
Chris@0
|
223 }
|
Chris@0
|
224 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
225 $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.');
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 try {
|
Chris@0
|
229 $this->assertNoFieldByXPath("//input[@id = 'edit-name']");
|
Chris@0
|
230 $this->fail('The "edit-name" field was not found.');
|
Chris@0
|
231 }
|
Chris@0
|
232 catch (ExpectationException $e) {
|
Chris@0
|
233 $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 try {
|
Chris@0
|
237 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'not the value');
|
Chris@0
|
238 $this->fail('The "edit-name" field is found with the value "not the value".');
|
Chris@0
|
239 }
|
Chris@0
|
240 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
241 $this->pass('The "edit-name" field is not found with the value "not the value".');
|
Chris@0
|
242 }
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * Tests legacy field asserts using textfields.
|
Chris@0
|
247 */
|
Chris@14
|
248 public function testFieldAssertsForTextfields() {
|
Chris@0
|
249 $this->drupalGet('test-field-xpath');
|
Chris@0
|
250
|
Chris@0
|
251 // *** 1. assertNoField().
|
Chris@0
|
252 $this->assertNoField('invalid_name_and_id');
|
Chris@0
|
253
|
Chris@0
|
254 // Test that the assertion fails correctly when searching by name.
|
Chris@0
|
255 try {
|
Chris@0
|
256 $this->assertNoField('name');
|
Chris@0
|
257 $this->fail('The "name" field was not found based on name.');
|
Chris@0
|
258 }
|
Chris@0
|
259 catch (ExpectationException $e) {
|
Chris@0
|
260 $this->pass('assertNoField correctly failed. The "name" field was found by name.');
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 // Test that the assertion fails correctly when searching by id.
|
Chris@0
|
264 try {
|
Chris@0
|
265 $this->assertNoField('edit-name');
|
Chris@0
|
266 $this->fail('The "name" field was not found based on id.');
|
Chris@0
|
267 }
|
Chris@0
|
268 catch (ExpectationException $e) {
|
Chris@0
|
269 $this->pass('assertNoField correctly failed. The "name" field was found by id.');
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@0
|
272 // *** 2. assertField().
|
Chris@0
|
273 $this->assertField('name');
|
Chris@0
|
274 $this->assertField('edit-name');
|
Chris@0
|
275
|
Chris@0
|
276 // Test that the assertion fails correctly if the field does not exist.
|
Chris@0
|
277 try {
|
Chris@0
|
278 $this->assertField('invalid_name_and_id');
|
Chris@0
|
279 $this->fail('The "invalid_name_and_id" field was found.');
|
Chris@0
|
280 }
|
Chris@0
|
281 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
282 $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 // *** 3. assertNoFieldById().
|
Chris@0
|
286 $this->assertNoFieldById('name');
|
Chris@0
|
287 $this->assertNoFieldById('name', 'not the value');
|
Chris@0
|
288 $this->assertNoFieldById('notexisting');
|
Chris@0
|
289 $this->assertNoFieldById('notexisting', NULL);
|
Chris@0
|
290
|
Chris@0
|
291 // Test that the assertion fails correctly if no value is passed in.
|
Chris@0
|
292 try {
|
Chris@0
|
293 $this->assertNoFieldById('edit-description');
|
Chris@0
|
294 $this->fail('The "description" field, with no value was not found.');
|
Chris@0
|
295 }
|
Chris@0
|
296 catch (ExpectationException $e) {
|
Chris@0
|
297 $this->pass('The "description" field, with no value was found.');
|
Chris@0
|
298 }
|
Chris@0
|
299
|
Chris@0
|
300 // Test that the assertion fails correctly if a NULL value is passed in.
|
Chris@0
|
301 try {
|
Chris@0
|
302 $this->assertNoFieldById('edit-name', NULL);
|
Chris@0
|
303 $this->fail('The "name" field was not found.');
|
Chris@0
|
304 }
|
Chris@0
|
305 catch (ExpectationException $e) {
|
Chris@0
|
306 $this->pass('The "name" field was found.');
|
Chris@0
|
307 }
|
Chris@0
|
308
|
Chris@0
|
309 // *** 4. assertFieldById().
|
Chris@0
|
310 $this->assertFieldById('edit-name', NULL);
|
Chris@0
|
311 $this->assertFieldById('edit-name', 'Test name');
|
Chris@0
|
312 $this->assertFieldById('edit-description', NULL);
|
Chris@0
|
313 $this->assertFieldById('edit-description');
|
Chris@0
|
314
|
Chris@0
|
315 // Test that the assertion fails correctly if no value is passed in.
|
Chris@0
|
316 try {
|
Chris@0
|
317 $this->assertFieldById('edit-name');
|
Chris@0
|
318 $this->fail('The "edit-name" field with no value was found.');
|
Chris@0
|
319 }
|
Chris@0
|
320 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
321 $this->pass('The "edit-name" field with no value was not found.');
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 // Test that the assertion fails correctly if the wrong value is passed in.
|
Chris@0
|
325 try {
|
Chris@0
|
326 $this->assertFieldById('edit-name', 'not the value');
|
Chris@0
|
327 $this->fail('The "name" field was found, using the wrong value.');
|
Chris@0
|
328 }
|
Chris@0
|
329 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
330 $this->pass('The "name" field was not found, using the wrong value.');
|
Chris@0
|
331 }
|
Chris@0
|
332
|
Chris@0
|
333 // *** 5. assertNoFieldByName().
|
Chris@0
|
334 $this->assertNoFieldByName('name');
|
Chris@0
|
335 $this->assertNoFieldByName('name', 'not the value');
|
Chris@0
|
336 $this->assertNoFieldByName('notexisting');
|
Chris@0
|
337 $this->assertNoFieldByName('notexisting', NULL);
|
Chris@0
|
338
|
Chris@0
|
339 // Test that the assertion fails correctly if no value is passed in.
|
Chris@0
|
340 try {
|
Chris@0
|
341 $this->assertNoFieldByName('description');
|
Chris@0
|
342 $this->fail('The "description" field, with no value was not found.');
|
Chris@0
|
343 }
|
Chris@0
|
344 catch (ExpectationException $e) {
|
Chris@0
|
345 $this->pass('The "description" field, with no value was found.');
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 // Test that the assertion fails correctly if a NULL value is passed in.
|
Chris@0
|
349 try {
|
Chris@0
|
350 $this->assertNoFieldByName('name', NULL);
|
Chris@0
|
351 $this->fail('The "name" field was not found.');
|
Chris@0
|
352 }
|
Chris@0
|
353 catch (ExpectationException $e) {
|
Chris@0
|
354 $this->pass('The "name" field was found.');
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 // *** 6. assertFieldByName().
|
Chris@0
|
358 $this->assertFieldByName('name');
|
Chris@0
|
359 $this->assertFieldByName('name', NULL);
|
Chris@0
|
360 $this->assertFieldByName('name', 'Test name');
|
Chris@0
|
361 $this->assertFieldByName('description');
|
Chris@0
|
362 $this->assertFieldByName('description', '');
|
Chris@0
|
363 $this->assertFieldByName('description', NULL);
|
Chris@0
|
364
|
Chris@0
|
365 // Test that the assertion fails correctly if given the wrong name.
|
Chris@0
|
366 try {
|
Chris@0
|
367 $this->assertFieldByName('non-existing-name');
|
Chris@0
|
368 $this->fail('The "non-existing-name" field was found.');
|
Chris@0
|
369 }
|
Chris@0
|
370 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
371 $this->pass('The "non-existing-name" field was not found');
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 // Test that the assertion fails correctly if given the wrong value.
|
Chris@0
|
375 try {
|
Chris@0
|
376 $this->assertFieldByName('name', 'not the value');
|
Chris@0
|
377 $this->fail('The "name" field with incorrect value was found.');
|
Chris@0
|
378 }
|
Chris@0
|
379 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
380 $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@0
|
383 // Test that text areas can contain new lines.
|
Chris@0
|
384 $this->assertFieldsByValue($this->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline");
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 /**
|
Chris@0
|
388 * Tests legacy field asserts for options field type.
|
Chris@0
|
389 */
|
Chris@14
|
390 public function testFieldAssertsForOptions() {
|
Chris@0
|
391 $this->drupalGet('test-field-xpath');
|
Chris@0
|
392
|
Chris@0
|
393 // Option field type.
|
Chris@0
|
394 $this->assertOptionByText('options', 'one');
|
Chris@0
|
395 try {
|
Chris@0
|
396 $this->assertOptionByText('options', 'four');
|
Chris@0
|
397 $this->fail('The select option "four" was found.');
|
Chris@0
|
398 }
|
Chris@0
|
399 catch (ExpectationException $e) {
|
Chris@0
|
400 $this->pass($e->getMessage());
|
Chris@0
|
401 }
|
Chris@0
|
402
|
Chris@0
|
403 $this->assertOption('options', 1);
|
Chris@0
|
404 try {
|
Chris@0
|
405 $this->assertOption('options', 4);
|
Chris@0
|
406 $this->fail('The select option "4" was found.');
|
Chris@0
|
407 }
|
Chris@0
|
408 catch (ExpectationException $e) {
|
Chris@0
|
409 $this->pass($e->getMessage());
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@0
|
412 $this->assertNoOption('options', 'non-existing');
|
Chris@0
|
413 try {
|
Chris@0
|
414 $this->assertNoOption('options', 'one');
|
Chris@0
|
415 $this->fail('The select option "one" was not found.');
|
Chris@0
|
416 }
|
Chris@0
|
417 catch (ExpectationException $e) {
|
Chris@0
|
418 $this->pass($e->getMessage());
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 $this->assertOptionSelected('options', 2);
|
Chris@0
|
422 try {
|
Chris@0
|
423 $this->assertOptionSelected('options', 4);
|
Chris@0
|
424 $this->fail('The select option "4" was selected.');
|
Chris@0
|
425 }
|
Chris@0
|
426 catch (ExpectationException $e) {
|
Chris@0
|
427 $this->pass($e->getMessage());
|
Chris@0
|
428 }
|
Chris@0
|
429
|
Chris@0
|
430 try {
|
Chris@0
|
431 $this->assertOptionSelected('options', 1);
|
Chris@0
|
432 $this->fail('The select option "1" was selected.');
|
Chris@0
|
433 }
|
Chris@0
|
434 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
435 $this->pass($e->getMessage());
|
Chris@0
|
436 }
|
Chris@0
|
437
|
Chris@0
|
438 // Test \Drupal\FunctionalTests\AssertLegacyTrait::getAllOptions.
|
Chris@0
|
439 $this->drupalGet('/form-test/select');
|
Chris@0
|
440 $this->assertCount(6, $this->getAllOptions($this->cssSelect('select[name="opt_groups"]')[0]));
|
Chris@0
|
441 }
|
Chris@0
|
442
|
Chris@0
|
443 /**
|
Chris@0
|
444 * Tests legacy field asserts for button field type.
|
Chris@0
|
445 */
|
Chris@14
|
446 public function testFieldAssertsForButton() {
|
Chris@0
|
447 $this->drupalGet('test-field-xpath');
|
Chris@0
|
448
|
Chris@0
|
449 $this->assertFieldById('edit-save', NULL);
|
Chris@0
|
450 // Test that the assertion fails correctly if the field value is passed in
|
Chris@0
|
451 // rather than the id.
|
Chris@0
|
452 try {
|
Chris@0
|
453 $this->assertFieldById('Save', NULL);
|
Chris@0
|
454 $this->fail('The field with id of "Save" was found.');
|
Chris@0
|
455 }
|
Chris@0
|
456 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
457 $this->pass($e->getMessage());
|
Chris@0
|
458 }
|
Chris@0
|
459
|
Chris@0
|
460 $this->assertNoFieldById('Save', NULL);
|
Chris@0
|
461 // Test that the assertion fails correctly if the id of an actual field is
|
Chris@0
|
462 // passed in.
|
Chris@0
|
463 try {
|
Chris@0
|
464 $this->assertNoFieldById('edit-save', NULL);
|
Chris@0
|
465 $this->fail('The field with id of "edit-save" was not found.');
|
Chris@0
|
466 }
|
Chris@0
|
467 catch (ExpectationException $e) {
|
Chris@0
|
468 $this->pass($e->getMessage());
|
Chris@0
|
469 }
|
Chris@0
|
470
|
Chris@0
|
471 // Test that multiple fields with the same name are validated correctly.
|
Chris@0
|
472 $this->assertFieldByName('duplicate_button', 'Duplicate button 1');
|
Chris@0
|
473 $this->assertFieldByName('duplicate_button', 'Duplicate button 2');
|
Chris@0
|
474 $this->assertNoFieldByName('duplicate_button', 'Rabbit');
|
Chris@0
|
475
|
Chris@0
|
476 try {
|
Chris@0
|
477 $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2');
|
Chris@0
|
478 $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.');
|
Chris@0
|
479 }
|
Chris@0
|
480 catch (ExpectationException $e) {
|
Chris@0
|
481 $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.');
|
Chris@0
|
482 }
|
Chris@0
|
483 }
|
Chris@0
|
484
|
Chris@0
|
485 /**
|
Chris@0
|
486 * Tests legacy field asserts for checkbox field type.
|
Chris@0
|
487 */
|
Chris@14
|
488 public function testFieldAssertsForCheckbox() {
|
Chris@0
|
489 $this->drupalGet('test-field-xpath');
|
Chris@0
|
490
|
Chris@0
|
491 // Part 1 - Test by name.
|
Chris@0
|
492 // Test that checkboxes are found/not found correctly by name, when using
|
Chris@0
|
493 // TRUE or FALSE to match their 'checked' state.
|
Chris@0
|
494 $this->assertFieldByName('checkbox_enabled', TRUE);
|
Chris@0
|
495 $this->assertFieldByName('checkbox_disabled', FALSE);
|
Chris@0
|
496 $this->assertNoFieldByName('checkbox_enabled', FALSE);
|
Chris@0
|
497 $this->assertNoFieldByName('checkbox_disabled', TRUE);
|
Chris@0
|
498
|
Chris@0
|
499 // Test that checkboxes are found by name when using NULL to ignore the
|
Chris@0
|
500 // 'checked' state.
|
Chris@0
|
501 $this->assertFieldByName('checkbox_enabled', NULL);
|
Chris@0
|
502 $this->assertFieldByName('checkbox_disabled', NULL);
|
Chris@0
|
503
|
Chris@0
|
504 // Test that checkboxes are found by name when passing no second parameter.
|
Chris@0
|
505 $this->assertFieldByName('checkbox_enabled');
|
Chris@0
|
506 $this->assertFieldByName('checkbox_disabled');
|
Chris@0
|
507
|
Chris@0
|
508 // Test that we have legacy support.
|
Chris@0
|
509 $this->assertFieldByName('checkbox_enabled', '1');
|
Chris@0
|
510 $this->assertFieldByName('checkbox_disabled', '');
|
Chris@0
|
511
|
Chris@0
|
512 // Test that the assertion fails correctly when using NULL to ignore state.
|
Chris@0
|
513 try {
|
Chris@0
|
514 $this->assertNoFieldByName('checkbox_enabled', NULL);
|
Chris@0
|
515 $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
|
Chris@0
|
516 }
|
Chris@0
|
517 catch (ExpectationException $e) {
|
Chris@0
|
518 $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
|
Chris@0
|
519 }
|
Chris@0
|
520
|
Chris@0
|
521 // Part 2 - Test by ID.
|
Chris@0
|
522 // Test that checkboxes are found/not found correctly by ID, when using
|
Chris@0
|
523 // TRUE or FALSE to match their 'checked' state.
|
Chris@0
|
524 $this->assertFieldById('edit-checkbox-enabled', TRUE);
|
Chris@0
|
525 $this->assertFieldById('edit-checkbox-disabled', FALSE);
|
Chris@0
|
526 $this->assertNoFieldById('edit-checkbox-enabled', FALSE);
|
Chris@0
|
527 $this->assertNoFieldById('edit-checkbox-disabled', TRUE);
|
Chris@0
|
528
|
Chris@0
|
529 // Test that checkboxes are found by ID, when using NULL to ignore the
|
Chris@0
|
530 // 'checked' state.
|
Chris@0
|
531 $this->assertFieldById('edit-checkbox-enabled', NULL);
|
Chris@0
|
532 $this->assertFieldById('edit-checkbox-disabled', NULL);
|
Chris@0
|
533
|
Chris@0
|
534 // Test that checkboxes are found by ID when passing no second parameter.
|
Chris@0
|
535 $this->assertFieldById('edit-checkbox-enabled');
|
Chris@0
|
536 $this->assertFieldById('edit-checkbox-disabled');
|
Chris@0
|
537
|
Chris@0
|
538 // Test that we have legacy support.
|
Chris@0
|
539 $this->assertFieldById('edit-checkbox-enabled', '1');
|
Chris@0
|
540 $this->assertFieldById('edit-checkbox-disabled', '');
|
Chris@0
|
541
|
Chris@0
|
542 // Test that the assertion fails correctly when using NULL to ignore state.
|
Chris@0
|
543 try {
|
Chris@0
|
544 $this->assertNoFieldById('edit-checkbox-disabled', NULL);
|
Chris@0
|
545 $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
|
Chris@0
|
546 }
|
Chris@0
|
547 catch (ExpectationException $e) {
|
Chris@0
|
548 $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
|
Chris@0
|
549 }
|
Chris@0
|
550
|
Chris@0
|
551 // Part 3 - Test the specific 'checked' assertions.
|
Chris@0
|
552 $this->assertFieldChecked('edit-checkbox-enabled');
|
Chris@0
|
553 $this->assertNoFieldChecked('edit-checkbox-disabled');
|
Chris@0
|
554
|
Chris@0
|
555 // Test that the assertion fails correctly with non-existant field id.
|
Chris@0
|
556 try {
|
Chris@0
|
557 $this->assertNoFieldChecked('incorrect_checkbox_id');
|
Chris@0
|
558 $this->fail('The "incorrect_checkbox_id" field was found');
|
Chris@0
|
559 }
|
Chris@0
|
560 catch (ExpectationException $e) {
|
Chris@0
|
561 $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.');
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 // Test that the assertion fails correctly for a checkbox that is checked.
|
Chris@0
|
565 try {
|
Chris@0
|
566 $this->assertNoFieldChecked('edit-checkbox-enabled');
|
Chris@0
|
567 $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
|
Chris@0
|
568 }
|
Chris@0
|
569 catch (ExpectationException $e) {
|
Chris@0
|
570 $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.');
|
Chris@0
|
571 }
|
Chris@0
|
572
|
Chris@0
|
573 // Test that the assertion fails correctly for a checkbox that is not
|
Chris@0
|
574 // checked.
|
Chris@0
|
575 try {
|
Chris@0
|
576 $this->assertFieldChecked('edit-checkbox-disabled');
|
Chris@0
|
577 $this->fail('The "edit-checkbox-disabled" field was found and checked.');
|
Chris@0
|
578 }
|
Chris@0
|
579 catch (ExpectationException $e) {
|
Chris@0
|
580 $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.');
|
Chris@0
|
581 }
|
Chris@0
|
582 }
|
Chris@0
|
583
|
Chris@0
|
584 /**
|
Chris@0
|
585 * Tests the ::cronRun() method.
|
Chris@0
|
586 */
|
Chris@0
|
587 public function testCronRun() {
|
Chris@0
|
588 $last_cron_time = \Drupal::state()->get('system.cron_last');
|
Chris@0
|
589 $this->cronRun();
|
Chris@0
|
590 $this->assertSession()->statusCodeEquals(204);
|
Chris@0
|
591 $next_cron_time = \Drupal::state()->get('system.cron_last');
|
Chris@0
|
592
|
Chris@0
|
593 $this->assertGreaterThan($last_cron_time, $next_cron_time);
|
Chris@0
|
594 }
|
Chris@0
|
595
|
Chris@0
|
596 /**
|
Chris@0
|
597 * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
|
Chris@0
|
598 */
|
Chris@0
|
599 public function testInstall() {
|
Chris@0
|
600 $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
|
Chris@0
|
601 $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
|
Chris@0
|
602 }
|
Chris@0
|
603
|
Chris@0
|
604 /**
|
Chris@0
|
605 * Tests the assumption that local time is in 'Australia/Sydney'.
|
Chris@0
|
606 */
|
Chris@0
|
607 public function testLocalTimeZone() {
|
Chris@0
|
608 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
|
Chris@0
|
609 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
|
Chris@0
|
610
|
Chris@0
|
611 // The 'Australia/Sydney' time zone is also set in
|
Chris@0
|
612 // FunctionalTestSetupTrait::initConfig().
|
Chris@0
|
613 $config_factory = $this->container->get('config.factory');
|
Chris@0
|
614 $value = $config_factory->get('system.date')->get('timezone.default');
|
Chris@0
|
615 $this->assertEquals('Australia/Sydney', $value);
|
Chris@0
|
616 }
|
Chris@0
|
617
|
Chris@0
|
618 /**
|
Chris@0
|
619 * Tests the ::checkForMetaRefresh() method.
|
Chris@0
|
620 */
|
Chris@0
|
621 public function testCheckForMetaRefresh() {
|
Chris@0
|
622 // Disable following redirects in the client.
|
Chris@0
|
623 $this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
|
Chris@0
|
624 // Set the maximumMetaRefreshCount to zero to make sure the redirect doesn't
|
Chris@0
|
625 // happen when doing a drupalGet.
|
Chris@0
|
626 $this->maximumMetaRefreshCount = 0;
|
Chris@0
|
627 $this->drupalGet('test-meta-refresh');
|
Chris@0
|
628 $this->assertNotEmpty($this->cssSelect('meta[http-equiv="refresh"]'));
|
Chris@0
|
629 // Allow one redirect to happen.
|
Chris@0
|
630 $this->maximumMetaRefreshCount = 1;
|
Chris@0
|
631 $this->checkForMetaRefresh();
|
Chris@0
|
632 // Check that we are now on the test page.
|
Chris@0
|
633 $this->assertSession()->pageTextContains('Test page text.');
|
Chris@0
|
634 }
|
Chris@0
|
635
|
Chris@12
|
636 public function testGetDefaultDriveInstance() {
|
Chris@12
|
637 putenv('MINK_DRIVER_ARGS=' . json_encode([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]]));
|
Chris@12
|
638 $this->getDefaultDriverInstance();
|
Chris@12
|
639 $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs);
|
Chris@12
|
640 }
|
Chris@12
|
641
|
Chris@0
|
642 }
|