DrupalMessengerServiceTest.php in Drupal 10
File
core/modules/system/tests/src/Functional/Bootstrap/DrupalMessengerServiceTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\Bootstrap;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\AssertionFailedError;
class DrupalMessengerServiceTest extends BrowserTestBase {
protected static $modules = [
'system_test',
];
protected $defaultTheme = 'stark';
public function testDrupalMessengerService() {
$this
->drupalGet(Url::fromRoute('system_test.messenger_service'));
$this
->assertSession()
->pageTextNotContains('First message (removed).');
$this
->assertSession()
->responseContains('Second message with <em>markup!</em> (not removed).');
$this
->assertSession()
->pageTextMatchesCount(1, '/Non Duplicated message/');
$this
->assertSession()
->pageTextMatchesCount(3, '/Duplicated message/');
$this
->assertSession()
->responseContains('Markup with <em>markup!</em>');
$this
->assertSession()
->pageTextMatchesCount(1, '/Markup with markup!/');
$this
->assertSession()
->responseContains('Markup2 with <em>markup!</em>');
$this
->assertSession()
->pageTextMatchesCount(1, '$Non duplicate Markup / string.$');
$this
->assertSession()
->pageTextMatchesCount(2, '$Duplicate Markup / string.$');
$this
->assertSession()
->assertEscaped('<em>This<span>markup will be</span> escaped</em>.');
$assert = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$edit = [];
$edit["modules[help][enable]"] = TRUE;
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$assert
->pageTextContains('Help has been enabled');
$assert
->pageTextContains('system_test_preinstall_module called');
}
public function testStatusMessageAssertions() : void {
$this
->drupalGet(Url::fromRoute('system_test.status_messages_for_assertions'));
$this
->assertSession()
->statusMessageExists();
$this
->assertSession()
->statusMessageExists('status');
$this
->assertSession()
->statusMessageExists('error');
$this
->assertSession()
->statusMessageExists('warning');
$this
->assertSession()
->statusMessageContains('My Status Message');
$this
->assertSession()
->statusMessageContains('My Error Message');
$this
->assertSession()
->statusMessageContains('My Warning Message');
$this
->assertSession()
->statusMessageContains('My Status');
$this
->assertSession()
->statusMessageContains('My Status Message', 'status');
$this
->assertSession()
->statusMessageContains('My Error Message', 'error');
$this
->assertSession()
->statusMessageContains('My Warning Message', 'warning');
$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');
$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');
$this
->drupalGet(Url::fromRoute('system_test.messenger_service'));
$this
->assertSession()
->statusMessageNotExists('error');
$this
->assertSession()
->statusMessageNotExists('warning');
$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.');
$this
->expectException(\InvalidArgumentException::class);
$this
->assertSession()
->statusMessageExists('not a valid type');
}
}