annotate core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @ 16:c2387f117808

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