View source
<?php
namespace Drupal\FunctionalJavascriptTests\Ajax;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use PHPUnit\Framework\ExpectationFailedException;
class MessageCommandTest extends WebDriverTestBase {
protected static $modules = [
'ajax_test',
];
protected $defaultTheme = 'stark';
public function testMessageCommand() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('ajax-test/message');
$page
->pressButton('Make Message In Default Location');
$this
->waitForMessageVisible('I am a message in the default location.');
$this
->assertAnnounceContains('I am a message in the default location.');
$assert_session
->elementsCount('css', '.messages__wrapper .messages', 1);
$page
->pressButton('Make Message In Alternate Location');
$this
->waitForMessageVisible('I am a message in an alternate location.', '#alternate-message-container');
$assert_session
->pageTextContains('I am a message in the default location.');
$this
->assertAnnounceContains('I am a message in an alternate location.');
$assert_session
->elementsCount('css', '.messages__wrapper .messages', 1);
$assert_session
->elementsCount('css', '#alternate-message-container .messages', 1);
$page
->pressButton('Make Warning Message');
$this
->waitForMessageVisible('I am a warning message in the default location.', NULL, 'warning');
$assert_session
->pageTextNotContains('I am a message in the default location.');
$assert_session
->elementsCount('css', '.messages__wrapper .messages', 1);
$assert_session
->elementsCount('css', '#alternate-message-container .messages', 1);
$this
->drupalGet('ajax-test/message');
for ($i = 0; $i < 6; $i++) {
$page
->pressButton('Make Message In Default Location');
$this
->waitForMessageVisible('I am a message in the default location.');
$assert_session
->elementsCount('css', '.messages__wrapper .messages', 1);
$page
->pressButton('Make Warning Message');
$this
->waitForMessageVisible('I am a warning message in the default location.', NULL, 'warning');
$this
->assertAnnounceNotContains('I am a warning message in the default location.');
$this
->waitForMessageRemoved('I am a message in the default location.');
$assert_session
->elementsCount('css', '.messages__wrapper .messages', 1);
}
$this
->drupalGet('ajax-test/message');
for ($i = 1; $i < 7; $i++) {
$page
->pressButton('Make Message In Alternate Location');
$expected_count = $page
->waitFor(10, function () use ($i, $page) {
return count($page
->findAll('css', '#alternate-message-container .messages')) === $i;
});
$this
->assertTrue($expected_count);
$this
->assertAnnounceContains('I am a message in an alternate location.');
}
}
public function testJsStatusMessageAssertions() : void {
$page = $this
->getSession()
->getPage();
$this
->drupalGet('ajax-test/message');
$page
->pressButton('Make Message In Default Location');
$this
->assertSession()
->statusMessageContainsAfterWait('I am a message in the default location.');
$page
->pressButton('Make Message In Alternate Location');
$this
->assertSession()
->statusMessageContainsAfterWait('I am a message in an alternate location.', 'status');
$page
->pressButton('Make Warning Message');
$this
->assertSession()
->statusMessageContainsAfterWait('I am a warning message in the default location.', 'warning');
$this
->drupalGet('ajax-test/message');
$page
->pressButton('Make Message In Default Location');
$this
->assertSession()
->statusMessageNotContainsAfterWait('This is not a real message');
$page
->pressButton('Make Message In Alternate Location');
$this
->assertSession()
->statusMessageNotContainsAfterWait('I am a message in an alternate location.', 'warning');
$page
->pressButton('Make Warning Message');
$this
->assertSession()
->statusMessageContainsAfterWait('I am a warning');
$this
->drupalGet('ajax-test/message');
$page
->pressButton('Make Message In Default Location');
$this
->assertSession()
->statusMessageExistsAfterWait();
$page
->pressButton('Make Message In Alternate Location');
$this
->assertSession()
->statusMessageNotExistsAfterWait('error');
$page
->pressButton('Make Warning Message');
$this
->assertSession()
->statusMessageExistsAfterWait('warning');
$expected_failure_occurred = FALSE;
try {
$this
->assertSession()
->statusMessageContainsAfterWait('Not a real message', NULL, 1000);
} catch (ExpectationFailedException $e) {
$expected_failure_occurred = TRUE;
}
$this
->assertTrue($expected_failure_occurred, 'JsWebAssert::statusMessageContainsAfterWait() did not fail when it should have failed.');
$expected_failure_occurred = FALSE;
try {
$this
->assertSession()
->statusMessageNotContainsAfterWait('I am a warning', NULL, 1000);
} catch (ExpectationFailedException $e) {
$expected_failure_occurred = TRUE;
}
$this
->assertTrue($expected_failure_occurred, 'JsWebAssert::statusMessageNotContainsAfterWait() did not fail when it should have failed.');
$expected_failure_occurred = FALSE;
try {
$this
->assertSession()
->statusMessageExistsAfterWait('error', 1000);
} catch (ExpectationFailedException $e) {
$expected_failure_occurred = TRUE;
}
$this
->assertTrue($expected_failure_occurred, 'JsWebAssert::statusMessageExistsAfterWait() did not fail when it should have failed.');
$expected_failure_occurred = FALSE;
try {
$this
->assertSession()
->statusMessageNotExistsAfterWait('warning', 1000);
} catch (ExpectationFailedException $e) {
$expected_failure_occurred = TRUE;
}
$this
->assertTrue($expected_failure_occurred, 'JsWebAssert::statusMessageNotExistsAfterWait() did not fail when it should have failed.');
$this
->expectException(\InvalidArgumentException::class);
$this
->assertSession()
->statusMessageExistsAfterWait('not a valid type');
}
protected function waitForMessageVisible($message, $selector = '[data-drupal-messages]', $type = 'status') {
$this
->assertNotEmpty($this
->assertSession()
->waitForElementVisible('css', $selector . ' .messages--' . $type . ':contains("' . $message . '")'));
}
protected function waitForMessageRemoved($message, $selector = '[data-drupal-messages]', $type = 'status') {
$this
->assertNotEmpty($this
->assertSession()
->waitForElementRemoved('css', $selector . ' .messages--' . $type . ':contains("' . $message . '")'));
}
protected function assertAnnounceContains(string $expected_message) : void {
$assert_session = $this
->assertSession();
$this
->assertNotEmpty($assert_session
->waitForElement('css', "#drupal-live-announce:contains('{$expected_message}')"));
}
protected function assertAnnounceNotContains(string $expected_message) : void {
$assert_session = $this
->assertSession();
$this
->assertEmpty($assert_session
->waitForElement('css', "#drupal-live-announce:contains('{$expected_message}')", 1000));
}
}