function DrupalMessengerServiceTest::testStatusMessageAssertions
Same name in other branches
- 10 core/modules/system/tests/src/Functional/Bootstrap/DrupalMessengerServiceTest.php \Drupal\Tests\system\Functional\Bootstrap\DrupalMessengerServiceTest::testStatusMessageAssertions()
- 11.x core/modules/system/tests/src/Functional/Bootstrap/DrupalMessengerServiceTest.php \Drupal\Tests\system\Functional\Bootstrap\DrupalMessengerServiceTest::testStatusMessageAssertions()
Tests assertion methods in WebAssert related to status messages.
File
-
core/
modules/ system/ tests/ src/ Functional/ Bootstrap/ DrupalMessengerServiceTest.php, line 68
Class
- DrupalMessengerServiceTest
- Tests the Messenger service.
Namespace
Drupal\Tests\system\Functional\BootstrapCode
public function testStatusMessageAssertions() : void {
$this->drupalGet(Url::fromRoute('system_test.status_messages_for_assertions'));
// Use the simple messages to test basic functionality.
// Test WebAssert::statusMessagesExists().
$this->assertSession()
->statusMessageExists();
$this->assertSession()
->statusMessageExists('status');
$this->assertSession()
->statusMessageExists('error');
$this->assertSession()
->statusMessageExists('warning');
// WebAssert::statusMessageContains().
$this->assertSession()
->statusMessageContains('My Status Message');
$this->assertSession()
->statusMessageContains('My Error Message');
$this->assertSession()
->statusMessageContains('My Warning Message');
// Test partial match.
$this->assertSession()
->statusMessageContains('My Status');
// Test with second arg.
$this->assertSession()
->statusMessageContains('My Status Message', 'status');
$this->assertSession()
->statusMessageContains('My Error Message', 'error');
$this->assertSession()
->statusMessageContains('My Warning Message', 'warning');
// Test WebAssert::statusMessageNotContains().
$this->assertSession()
->statusMessageNotContains('My Status Message is fake');
$this->assertSession()
->statusMessageNotContains('My Status Message is fake', 'status');
$this->assertSession()
->statusMessageNotContains('My Error Message', 'status');
$this->assertSession()
->statusMessageNotContains('My Status Message', 'error');
// Check that special characters get handled correctly.
$this->assertSession()
->statusMessageContains('This has " in the middle');
$this->assertSession()
->statusMessageContains('This has \' in the middle');
$this->assertSession()
->statusMessageContains('<em>This<span>markup will be</span> escaped</em>');
$this->assertSession()
->statusMessageContains('Peaches & cream');
$this->assertSession()
->statusMessageNotContains('Peaches & cream');
// Go to a new route that only has messages of type 'status'.
$this->drupalGet(Url::fromRoute('system_test.messenger_service'));
// Test WebAssert::statusMessageNotExists().
$this->assertSession()
->statusMessageNotExists('error');
$this->assertSession()
->statusMessageNotExists('warning');
// Perform a few assertions that should fail. We can only call
// TestCase::expectException() once per test, so we make a few
// try/catch blocks.
$expected_failure_occurred = FALSE;
try {
$this->assertSession()
->statusMessageContains('This message is not real');
} catch (AssertionFailedError $e) {
$expected_failure_occurred = TRUE;
}
$this->assertTrue($expected_failure_occurred, 'WebAssert::statusMessageContains() did not fail when it should have failed.');
$expected_failure_occurred = FALSE;
try {
$this->assertSession()
->statusMessageNotContains('markup');
} catch (AssertionFailedError $e) {
$expected_failure_occurred = TRUE;
}
$this->assertTrue($expected_failure_occurred, 'WebAssert::statusMessageNotContains() did not fail when it should have failed.');
$expected_failure_occurred = FALSE;
try {
$this->assertSession()
->statusMessageExists('error');
} catch (AssertionFailedError $e) {
$expected_failure_occurred = TRUE;
}
$this->assertTrue($expected_failure_occurred, 'WebAssert::statusMessageExists() did not fail when it should have failed.');
$expected_failure_occurred = FALSE;
try {
$this->assertSession()
->statusMessageNotExists();
} catch (AssertionFailedError $e) {
$expected_failure_occurred = TRUE;
}
$this->assertTrue($expected_failure_occurred, 'WebAssert::statusMessageNotExists() did not fail when it should have failed.');
// Tests passing a bad status type.
$this->expectException(\InvalidArgumentException::class);
$this->assertSession()
->statusMessageExists('not a valid type');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.