annotate core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
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@17 26 public static $modules = ['test_page_test', 'form_test', 'system_test', 'node'];
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@17 117 // Submit the form using the button label.
Chris@0 118 $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
Chris@0 119 $this->assertSame($this->getSession()->getPage()->getContent(), $result);
Chris@0 120 $value = $config_factory->get('form_test.object')->get('bananas');
Chris@0 121 $this->assertSame('red', $value);
Chris@0 122
Chris@0 123 $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
Chris@0 124 $value = $config_factory->get('form_test.object')->get('bananas');
Chris@0 125 $this->assertSame('', $value);
Chris@0 126
Chris@17 127 // Submit the form using the button id.
Chris@17 128 $edit = ['bananas' => 'blue'];
Chris@17 129 $result = $this->drupalPostForm('form-test/object-builder', $edit, 'edit-submit');
Chris@17 130 $this->assertSame($this->getSession()->getPage()->getContent(), $result);
Chris@17 131 $value = $config_factory->get('form_test.object')->get('bananas');
Chris@17 132 $this->assertSame('blue', $value);
Chris@17 133
Chris@17 134 // Submit the form using the button name.
Chris@17 135 $edit = ['bananas' => 'purple'];
Chris@17 136 $result = $this->drupalPostForm('form-test/object-builder', $edit, 'op');
Chris@17 137 $this->assertSame($this->getSession()->getPage()->getContent(), $result);
Chris@17 138 $value = $config_factory->get('form_test.object')->get('bananas');
Chris@17 139 $this->assertSame('purple', $value);
Chris@17 140
Chris@0 141 // Test drupalPostForm() with no-html response.
Chris@0 142 $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
Chris@0 143 $this->assertTrue(1000, $values['beer']);
Chris@17 144
Chris@17 145 // Test drupalPostForm() with form by HTML id.
Chris@17 146 $this->drupalCreateContentType(['type' => 'page']);
Chris@17 147 $this->drupalLogin($this->drupalCreateUser(['create page content']));
Chris@17 148 $this->drupalGet('form-test/two-instances-of-same-form');
Chris@17 149 $this->getSession()->getPage()->fillField('edit-title-0-value', 'form1');
Chris@17 150 $this->getSession()->getPage()->fillField('edit-title-0-value--2', 'form2');
Chris@17 151 $this->drupalPostForm(NULL, [], 'Save', [], 'node-page-form--2');
Chris@17 152 $this->assertSession()->pageTextContains('Page form2 has been created.');
Chris@0 153 }
Chris@0 154
Chris@0 155 /**
Chris@0 156 * Tests clickLink() functionality.
Chris@0 157 */
Chris@0 158 public function testClickLink() {
Chris@0 159 $this->drupalGet('test-page');
Chris@0 160 $this->clickLink('Visually identical test links');
Chris@0 161 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
Chris@0 162 $this->drupalGet('test-page');
Chris@0 163 $this->clickLink('Visually identical test links', 0);
Chris@0 164 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
Chris@0 165 $this->drupalGet('test-page');
Chris@0 166 $this->clickLink('Visually identical test links', 1);
Chris@0 167 $this->assertContains('user/register', $this->getSession()->getCurrentUrl());
Chris@0 168 }
Chris@0 169
Chris@0 170 public function testError() {
Chris@0 171 $this->setExpectedException('\Exception', 'User notice: foo');
Chris@0 172 $this->drupalGet('test-error');
Chris@0 173 }
Chris@0 174
Chris@0 175 /**
Chris@0 176 * Tests linkExists() with pipe character (|) in locator.
Chris@0 177 *
Chris@0 178 * @see \Drupal\Tests\WebAssert::linkExists()
Chris@0 179 */
Chris@0 180 public function testPipeCharInLocator() {
Chris@0 181 $this->drupalGet('test-pipe-char');
Chris@0 182 $this->assertSession()->linkExists('foo|bar|baz');
Chris@0 183 }
Chris@0 184
Chris@0 185 /**
Chris@0 186 * Tests linkExistsExact() functionality.
Chris@0 187 *
Chris@0 188 * @see \Drupal\Tests\WebAssert::linkExistsExact()
Chris@0 189 */
Chris@0 190 public function testLinkExistsExact() {
Chris@0 191 $this->drupalGet('test-pipe-char');
Chris@0 192 $this->assertSession()->linkExistsExact('foo|bar|baz');
Chris@0 193 }
Chris@0 194
Chris@0 195 /**
Chris@0 196 * Tests linkExistsExact() functionality fail.
Chris@0 197 *
Chris@0 198 * @see \Drupal\Tests\WebAssert::linkExistsExact()
Chris@0 199 */
Chris@0 200 public function testInvalidLinkExistsExact() {
Chris@0 201 $this->drupalGet('test-pipe-char');
Chris@0 202 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar found');
Chris@0 203 $this->assertSession()->linkExistsExact('foo|bar');
Chris@0 204 }
Chris@0 205
Chris@0 206 /**
Chris@0 207 * Tests linkNotExistsExact() functionality.
Chris@0 208 *
Chris@0 209 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
Chris@0 210 */
Chris@0 211 public function testLinkNotExistsExact() {
Chris@0 212 $this->drupalGet('test-pipe-char');
Chris@0 213 $this->assertSession()->linkNotExistsExact('foo|bar');
Chris@0 214 }
Chris@0 215
Chris@0 216 /**
Chris@0 217 * Tests linkNotExistsExact() functionality fail.
Chris@0 218 *
Chris@0 219 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
Chris@0 220 */
Chris@0 221 public function testInvalidLinkNotExistsExact() {
Chris@0 222 $this->drupalGet('test-pipe-char');
Chris@0 223 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar|baz not found');
Chris@0 224 $this->assertSession()->linkNotExistsExact('foo|bar|baz');
Chris@0 225 }
Chris@0 226
Chris@0 227 /**
Chris@0 228 * Tests legacy text asserts.
Chris@0 229 */
Chris@14 230 public function testTextAsserts() {
Chris@0 231 $this->drupalGet('test-encoded');
Chris@0 232 $dangerous = 'Bad html <script>alert(123);</script>';
Chris@0 233 $sanitized = Html::escape($dangerous);
Chris@0 234 $this->assertNoText($dangerous);
Chris@0 235 $this->assertText($sanitized);
Chris@0 236
Chris@0 237 // Test getRawContent().
Chris@17 238 $this->assertSame($this->getSession()->getPage()->getContent(), $this->getSession()->getPage()->getContent());
Chris@0 239 }
Chris@0 240
Chris@0 241 /**
Chris@0 242 * Tests legacy field asserts which use xpath directly.
Chris@0 243 */
Chris@14 244 public function testXpathAsserts() {
Chris@0 245 $this->drupalGet('test-field-xpath');
Chris@0 246 $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
Chris@0 247 $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
Chris@0 248 $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
Chris@0 249
Chris@0 250 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name');
Chris@0 251 $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
Chris@0 252 $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
Chris@0 253 $this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
Chris@0 254
Chris@0 255 $this->assertNoFieldByXPath('//notexisting');
Chris@0 256 $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
Chris@0 257
Chris@0 258 // Test that the assertion fails correctly.
Chris@0 259 try {
Chris@0 260 $this->assertFieldByXPath("//input[@id = 'notexisting']");
Chris@0 261 $this->fail('The "notexisting" field was found.');
Chris@0 262 }
Chris@0 263 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 264 $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.');
Chris@0 265 }
Chris@0 266
Chris@0 267 try {
Chris@0 268 $this->assertNoFieldByXPath("//input[@id = 'edit-name']");
Chris@0 269 $this->fail('The "edit-name" field was not found.');
Chris@0 270 }
Chris@0 271 catch (ExpectationException $e) {
Chris@0 272 $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
Chris@0 273 }
Chris@0 274
Chris@0 275 try {
Chris@0 276 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'not the value');
Chris@0 277 $this->fail('The "edit-name" field is found with the value "not the value".');
Chris@0 278 }
Chris@0 279 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 280 $this->pass('The "edit-name" field is not found with the value "not the value".');
Chris@0 281 }
Chris@0 282 }
Chris@0 283
Chris@0 284 /**
Chris@0 285 * Tests legacy field asserts using textfields.
Chris@0 286 */
Chris@14 287 public function testFieldAssertsForTextfields() {
Chris@0 288 $this->drupalGet('test-field-xpath');
Chris@0 289
Chris@0 290 // *** 1. assertNoField().
Chris@0 291 $this->assertNoField('invalid_name_and_id');
Chris@0 292
Chris@0 293 // Test that the assertion fails correctly when searching by name.
Chris@0 294 try {
Chris@0 295 $this->assertNoField('name');
Chris@0 296 $this->fail('The "name" field was not found based on name.');
Chris@0 297 }
Chris@0 298 catch (ExpectationException $e) {
Chris@0 299 $this->pass('assertNoField correctly failed. The "name" field was found by name.');
Chris@0 300 }
Chris@0 301
Chris@0 302 // Test that the assertion fails correctly when searching by id.
Chris@0 303 try {
Chris@0 304 $this->assertNoField('edit-name');
Chris@0 305 $this->fail('The "name" field was not found based on id.');
Chris@0 306 }
Chris@0 307 catch (ExpectationException $e) {
Chris@0 308 $this->pass('assertNoField correctly failed. The "name" field was found by id.');
Chris@0 309 }
Chris@0 310
Chris@0 311 // *** 2. assertField().
Chris@0 312 $this->assertField('name');
Chris@0 313 $this->assertField('edit-name');
Chris@0 314
Chris@0 315 // Test that the assertion fails correctly if the field does not exist.
Chris@0 316 try {
Chris@0 317 $this->assertField('invalid_name_and_id');
Chris@0 318 $this->fail('The "invalid_name_and_id" field was found.');
Chris@0 319 }
Chris@0 320 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 321 $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
Chris@0 322 }
Chris@0 323
Chris@0 324 // *** 3. assertNoFieldById().
Chris@0 325 $this->assertNoFieldById('name');
Chris@0 326 $this->assertNoFieldById('name', 'not the value');
Chris@0 327 $this->assertNoFieldById('notexisting');
Chris@0 328 $this->assertNoFieldById('notexisting', NULL);
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->assertNoFieldById('edit-description');
Chris@0 333 $this->fail('The "description" field, with no value was not found.');
Chris@0 334 }
Chris@0 335 catch (ExpectationException $e) {
Chris@0 336 $this->pass('The "description" field, with no value was found.');
Chris@0 337 }
Chris@0 338
Chris@0 339 // Test that the assertion fails correctly if a NULL value is passed in.
Chris@0 340 try {
Chris@0 341 $this->assertNoFieldById('edit-name', NULL);
Chris@0 342 $this->fail('The "name" field was not found.');
Chris@0 343 }
Chris@0 344 catch (ExpectationException $e) {
Chris@0 345 $this->pass('The "name" field was found.');
Chris@0 346 }
Chris@0 347
Chris@0 348 // *** 4. assertFieldById().
Chris@0 349 $this->assertFieldById('edit-name', NULL);
Chris@0 350 $this->assertFieldById('edit-name', 'Test name');
Chris@0 351 $this->assertFieldById('edit-description', NULL);
Chris@0 352 $this->assertFieldById('edit-description');
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->assertFieldById('edit-name');
Chris@0 357 $this->fail('The "edit-name" field with no value was found.');
Chris@0 358 }
Chris@0 359 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 360 $this->pass('The "edit-name" field with no value was not found.');
Chris@0 361 }
Chris@0 362
Chris@0 363 // Test that the assertion fails correctly if the wrong value is passed in.
Chris@0 364 try {
Chris@0 365 $this->assertFieldById('edit-name', 'not the value');
Chris@0 366 $this->fail('The "name" field was found, using the wrong value.');
Chris@0 367 }
Chris@0 368 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 369 $this->pass('The "name" field was not found, using the wrong value.');
Chris@0 370 }
Chris@0 371
Chris@0 372 // *** 5. assertNoFieldByName().
Chris@0 373 $this->assertNoFieldByName('name');
Chris@0 374 $this->assertNoFieldByName('name', 'not the value');
Chris@0 375 $this->assertNoFieldByName('notexisting');
Chris@0 376 $this->assertNoFieldByName('notexisting', NULL);
Chris@0 377
Chris@0 378 // Test that the assertion fails correctly if no value is passed in.
Chris@0 379 try {
Chris@0 380 $this->assertNoFieldByName('description');
Chris@0 381 $this->fail('The "description" field, with no value was not found.');
Chris@0 382 }
Chris@0 383 catch (ExpectationException $e) {
Chris@0 384 $this->pass('The "description" field, with no value was found.');
Chris@0 385 }
Chris@0 386
Chris@0 387 // Test that the assertion fails correctly if a NULL value is passed in.
Chris@0 388 try {
Chris@0 389 $this->assertNoFieldByName('name', NULL);
Chris@0 390 $this->fail('The "name" field was not found.');
Chris@0 391 }
Chris@0 392 catch (ExpectationException $e) {
Chris@0 393 $this->pass('The "name" field was found.');
Chris@0 394 }
Chris@0 395
Chris@0 396 // *** 6. assertFieldByName().
Chris@0 397 $this->assertFieldByName('name');
Chris@0 398 $this->assertFieldByName('name', NULL);
Chris@0 399 $this->assertFieldByName('name', 'Test name');
Chris@0 400 $this->assertFieldByName('description');
Chris@0 401 $this->assertFieldByName('description', '');
Chris@0 402 $this->assertFieldByName('description', NULL);
Chris@0 403
Chris@0 404 // Test that the assertion fails correctly if given the wrong name.
Chris@0 405 try {
Chris@0 406 $this->assertFieldByName('non-existing-name');
Chris@0 407 $this->fail('The "non-existing-name" field was found.');
Chris@0 408 }
Chris@0 409 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 410 $this->pass('The "non-existing-name" field was not found');
Chris@0 411 }
Chris@0 412
Chris@0 413 // Test that the assertion fails correctly if given the wrong value.
Chris@0 414 try {
Chris@0 415 $this->assertFieldByName('name', 'not the value');
Chris@0 416 $this->fail('The "name" field with incorrect value was found.');
Chris@0 417 }
Chris@0 418 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 419 $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
Chris@0 420 }
Chris@0 421
Chris@0 422 // Test that text areas can contain new lines.
Chris@0 423 $this->assertFieldsByValue($this->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline");
Chris@0 424 }
Chris@0 425
Chris@0 426 /**
Chris@0 427 * Tests legacy field asserts for options field type.
Chris@0 428 */
Chris@14 429 public function testFieldAssertsForOptions() {
Chris@0 430 $this->drupalGet('test-field-xpath');
Chris@0 431
Chris@0 432 // Option field type.
Chris@0 433 $this->assertOptionByText('options', 'one');
Chris@0 434 try {
Chris@0 435 $this->assertOptionByText('options', 'four');
Chris@0 436 $this->fail('The select option "four" was found.');
Chris@0 437 }
Chris@0 438 catch (ExpectationException $e) {
Chris@0 439 $this->pass($e->getMessage());
Chris@0 440 }
Chris@0 441
Chris@0 442 $this->assertOption('options', 1);
Chris@0 443 try {
Chris@0 444 $this->assertOption('options', 4);
Chris@0 445 $this->fail('The select option "4" was found.');
Chris@0 446 }
Chris@0 447 catch (ExpectationException $e) {
Chris@0 448 $this->pass($e->getMessage());
Chris@0 449 }
Chris@0 450
Chris@0 451 $this->assertNoOption('options', 'non-existing');
Chris@0 452 try {
Chris@0 453 $this->assertNoOption('options', 'one');
Chris@0 454 $this->fail('The select option "one" was not found.');
Chris@0 455 }
Chris@0 456 catch (ExpectationException $e) {
Chris@0 457 $this->pass($e->getMessage());
Chris@0 458 }
Chris@0 459
Chris@0 460 $this->assertOptionSelected('options', 2);
Chris@0 461 try {
Chris@0 462 $this->assertOptionSelected('options', 4);
Chris@0 463 $this->fail('The select option "4" was selected.');
Chris@0 464 }
Chris@0 465 catch (ExpectationException $e) {
Chris@0 466 $this->pass($e->getMessage());
Chris@0 467 }
Chris@0 468
Chris@0 469 try {
Chris@0 470 $this->assertOptionSelected('options', 1);
Chris@0 471 $this->fail('The select option "1" was selected.');
Chris@0 472 }
Chris@0 473 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 474 $this->pass($e->getMessage());
Chris@0 475 }
Chris@0 476
Chris@0 477 }
Chris@0 478
Chris@0 479 /**
Chris@0 480 * Tests legacy field asserts for button field type.
Chris@0 481 */
Chris@14 482 public function testFieldAssertsForButton() {
Chris@0 483 $this->drupalGet('test-field-xpath');
Chris@0 484
Chris@0 485 $this->assertFieldById('edit-save', NULL);
Chris@0 486 // Test that the assertion fails correctly if the field value is passed in
Chris@0 487 // rather than the id.
Chris@0 488 try {
Chris@0 489 $this->assertFieldById('Save', NULL);
Chris@0 490 $this->fail('The field with id of "Save" was found.');
Chris@0 491 }
Chris@0 492 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
Chris@0 493 $this->pass($e->getMessage());
Chris@0 494 }
Chris@0 495
Chris@0 496 $this->assertNoFieldById('Save', NULL);
Chris@0 497 // Test that the assertion fails correctly if the id of an actual field is
Chris@0 498 // passed in.
Chris@0 499 try {
Chris@0 500 $this->assertNoFieldById('edit-save', NULL);
Chris@0 501 $this->fail('The field with id of "edit-save" was not found.');
Chris@0 502 }
Chris@0 503 catch (ExpectationException $e) {
Chris@0 504 $this->pass($e->getMessage());
Chris@0 505 }
Chris@0 506
Chris@0 507 // Test that multiple fields with the same name are validated correctly.
Chris@0 508 $this->assertFieldByName('duplicate_button', 'Duplicate button 1');
Chris@0 509 $this->assertFieldByName('duplicate_button', 'Duplicate button 2');
Chris@0 510 $this->assertNoFieldByName('duplicate_button', 'Rabbit');
Chris@0 511
Chris@0 512 try {
Chris@0 513 $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2');
Chris@0 514 $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.');
Chris@0 515 }
Chris@0 516 catch (ExpectationException $e) {
Chris@0 517 $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.');
Chris@0 518 }
Chris@0 519 }
Chris@0 520
Chris@0 521 /**
Chris@0 522 * Tests legacy field asserts for checkbox field type.
Chris@0 523 */
Chris@14 524 public function testFieldAssertsForCheckbox() {
Chris@0 525 $this->drupalGet('test-field-xpath');
Chris@0 526
Chris@0 527 // Part 1 - Test by name.
Chris@0 528 // Test that checkboxes are found/not found correctly by name, when using
Chris@0 529 // TRUE or FALSE to match their 'checked' state.
Chris@0 530 $this->assertFieldByName('checkbox_enabled', TRUE);
Chris@0 531 $this->assertFieldByName('checkbox_disabled', FALSE);
Chris@0 532 $this->assertNoFieldByName('checkbox_enabled', FALSE);
Chris@0 533 $this->assertNoFieldByName('checkbox_disabled', TRUE);
Chris@0 534
Chris@0 535 // Test that checkboxes are found by name when using NULL to ignore the
Chris@0 536 // 'checked' state.
Chris@0 537 $this->assertFieldByName('checkbox_enabled', NULL);
Chris@0 538 $this->assertFieldByName('checkbox_disabled', NULL);
Chris@0 539
Chris@0 540 // Test that checkboxes are found by name when passing no second parameter.
Chris@0 541 $this->assertFieldByName('checkbox_enabled');
Chris@0 542 $this->assertFieldByName('checkbox_disabled');
Chris@0 543
Chris@0 544 // Test that we have legacy support.
Chris@0 545 $this->assertFieldByName('checkbox_enabled', '1');
Chris@0 546 $this->assertFieldByName('checkbox_disabled', '');
Chris@0 547
Chris@0 548 // Test that the assertion fails correctly when using NULL to ignore state.
Chris@0 549 try {
Chris@0 550 $this->assertNoFieldByName('checkbox_enabled', NULL);
Chris@0 551 $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
Chris@0 552 }
Chris@0 553 catch (ExpectationException $e) {
Chris@0 554 $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
Chris@0 555 }
Chris@0 556
Chris@0 557 // Part 2 - Test by ID.
Chris@0 558 // Test that checkboxes are found/not found correctly by ID, when using
Chris@0 559 // TRUE or FALSE to match their 'checked' state.
Chris@0 560 $this->assertFieldById('edit-checkbox-enabled', TRUE);
Chris@0 561 $this->assertFieldById('edit-checkbox-disabled', FALSE);
Chris@0 562 $this->assertNoFieldById('edit-checkbox-enabled', FALSE);
Chris@0 563 $this->assertNoFieldById('edit-checkbox-disabled', TRUE);
Chris@0 564
Chris@0 565 // Test that checkboxes are found by ID, when using NULL to ignore the
Chris@0 566 // 'checked' state.
Chris@0 567 $this->assertFieldById('edit-checkbox-enabled', NULL);
Chris@0 568 $this->assertFieldById('edit-checkbox-disabled', NULL);
Chris@0 569
Chris@0 570 // Test that checkboxes are found by ID when passing no second parameter.
Chris@0 571 $this->assertFieldById('edit-checkbox-enabled');
Chris@0 572 $this->assertFieldById('edit-checkbox-disabled');
Chris@0 573
Chris@0 574 // Test that we have legacy support.
Chris@0 575 $this->assertFieldById('edit-checkbox-enabled', '1');
Chris@0 576 $this->assertFieldById('edit-checkbox-disabled', '');
Chris@0 577
Chris@0 578 // Test that the assertion fails correctly when using NULL to ignore state.
Chris@0 579 try {
Chris@0 580 $this->assertNoFieldById('edit-checkbox-disabled', NULL);
Chris@0 581 $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
Chris@0 582 }
Chris@0 583 catch (ExpectationException $e) {
Chris@0 584 $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
Chris@0 585 }
Chris@0 586
Chris@0 587 // Part 3 - Test the specific 'checked' assertions.
Chris@0 588 $this->assertFieldChecked('edit-checkbox-enabled');
Chris@0 589 $this->assertNoFieldChecked('edit-checkbox-disabled');
Chris@0 590
Chris@17 591 // Test that the assertion fails correctly with non-existent field id.
Chris@0 592 try {
Chris@0 593 $this->assertNoFieldChecked('incorrect_checkbox_id');
Chris@0 594 $this->fail('The "incorrect_checkbox_id" field was found');
Chris@0 595 }
Chris@0 596 catch (ExpectationException $e) {
Chris@0 597 $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.');
Chris@0 598 }
Chris@0 599
Chris@0 600 // Test that the assertion fails correctly for a checkbox that is checked.
Chris@0 601 try {
Chris@0 602 $this->assertNoFieldChecked('edit-checkbox-enabled');
Chris@0 603 $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
Chris@0 604 }
Chris@0 605 catch (ExpectationException $e) {
Chris@0 606 $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.');
Chris@0 607 }
Chris@0 608
Chris@0 609 // Test that the assertion fails correctly for a checkbox that is not
Chris@0 610 // checked.
Chris@0 611 try {
Chris@0 612 $this->assertFieldChecked('edit-checkbox-disabled');
Chris@0 613 $this->fail('The "edit-checkbox-disabled" field was found and checked.');
Chris@0 614 }
Chris@0 615 catch (ExpectationException $e) {
Chris@0 616 $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.');
Chris@0 617 }
Chris@0 618 }
Chris@0 619
Chris@0 620 /**
Chris@0 621 * Tests the ::cronRun() method.
Chris@0 622 */
Chris@0 623 public function testCronRun() {
Chris@0 624 $last_cron_time = \Drupal::state()->get('system.cron_last');
Chris@0 625 $this->cronRun();
Chris@0 626 $this->assertSession()->statusCodeEquals(204);
Chris@0 627 $next_cron_time = \Drupal::state()->get('system.cron_last');
Chris@0 628
Chris@0 629 $this->assertGreaterThan($last_cron_time, $next_cron_time);
Chris@0 630 }
Chris@0 631
Chris@0 632 /**
Chris@0 633 * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
Chris@0 634 */
Chris@0 635 public function testInstall() {
Chris@0 636 $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
Chris@0 637 $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
Chris@0 638 }
Chris@0 639
Chris@0 640 /**
Chris@0 641 * Tests the assumption that local time is in 'Australia/Sydney'.
Chris@0 642 */
Chris@0 643 public function testLocalTimeZone() {
Chris@18 644 $expected = 'Australia/Sydney';
Chris@0 645 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
Chris@18 646 $this->assertEquals($expected, date_default_timezone_get());
Chris@0 647
Chris@0 648 // The 'Australia/Sydney' time zone is also set in
Chris@0 649 // FunctionalTestSetupTrait::initConfig().
Chris@0 650 $config_factory = $this->container->get('config.factory');
Chris@0 651 $value = $config_factory->get('system.date')->get('timezone.default');
Chris@18 652 $this->assertEquals($expected, $value);
Chris@18 653
Chris@18 654 // Test that users have the correct time zone set.
Chris@18 655 $this->assertEquals($expected, $this->rootUser->getTimeZone());
Chris@18 656 $admin_user = $this->drupalCreateUser(['administer site configuration']);
Chris@18 657 $this->assertEquals($expected, $admin_user->getTimeZone());
Chris@0 658 }
Chris@0 659
Chris@0 660 /**
Chris@0 661 * Tests the ::checkForMetaRefresh() method.
Chris@0 662 */
Chris@0 663 public function testCheckForMetaRefresh() {
Chris@0 664 // Disable following redirects in the client.
Chris@0 665 $this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
Chris@0 666 // Set the maximumMetaRefreshCount to zero to make sure the redirect doesn't
Chris@0 667 // happen when doing a drupalGet.
Chris@0 668 $this->maximumMetaRefreshCount = 0;
Chris@0 669 $this->drupalGet('test-meta-refresh');
Chris@0 670 $this->assertNotEmpty($this->cssSelect('meta[http-equiv="refresh"]'));
Chris@0 671 // Allow one redirect to happen.
Chris@0 672 $this->maximumMetaRefreshCount = 1;
Chris@0 673 $this->checkForMetaRefresh();
Chris@0 674 // Check that we are now on the test page.
Chris@0 675 $this->assertSession()->pageTextContains('Test page text.');
Chris@0 676 }
Chris@0 677
Chris@12 678 public function testGetDefaultDriveInstance() {
Chris@12 679 putenv('MINK_DRIVER_ARGS=' . json_encode([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]]));
Chris@12 680 $this->getDefaultDriverInstance();
Chris@12 681 $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs);
Chris@12 682 }
Chris@12 683
Chris@17 684 /**
Chris@17 685 * Ensures we can't access modules we shouldn't be able to after install.
Chris@17 686 */
Chris@17 687 public function testProfileModules() {
Chris@17 688 $this->setExpectedException(\InvalidArgumentException::class, 'The module demo_umami_content does not exist.');
Chris@17 689 $this->assertFileExists('core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.info.yml');
Chris@17 690 \Drupal::service('extension.list.module')->getPathname('demo_umami_content');
Chris@17 691 }
Chris@17 692
Chris@18 693 /**
Chris@18 694 * Test the protections provided by .htkey.
Chris@18 695 */
Chris@18 696 public function testHtkey() {
Chris@18 697 // Remove the Simpletest private key file so we can test the protection
Chris@18 698 // against requests that forge a valid testing user agent to gain access
Chris@18 699 // to the installer.
Chris@18 700 // @see drupal_valid_test_ua()
Chris@18 701 // Not using File API; a potential error must trigger a PHP warning.
Chris@18 702 $install_url = Url::fromUri('base:core/install.php', ['external' => TRUE, 'absolute' => TRUE])->toString();
Chris@18 703 $this->drupalGet($install_url);
Chris@18 704 $this->assertSession()->statusCodeEquals(200);
Chris@18 705 unlink($this->siteDirectory . '/.htkey');
Chris@18 706 $this->drupalGet($install_url);
Chris@18 707 $this->assertSession()->statusCodeEquals(403);
Chris@18 708 }
Chris@18 709
Chris@0 710 }