function MessageCommandTest::testMessageCommand

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/Ajax/MessageCommandTest.php \Drupal\FunctionalJavascriptTests\Ajax\MessageCommandTest::testMessageCommand()
  2. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/MessageCommandTest.php \Drupal\FunctionalJavascriptTests\Ajax\MessageCommandTest::testMessageCommand()
  3. 10 core/tests/Drupal/FunctionalJavascriptTests/Ajax/MessageCommandTest.php \Drupal\FunctionalJavascriptTests\Ajax\MessageCommandTest::testMessageCommand()

Tests AJAX MessageCommand use in a form.

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/MessageCommandTest.php, line 30

Class

MessageCommandTest
Tests adding messages via AJAX command.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

public function testMessageCommand() : void {
    $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');
    // Test that by default, previous messages in a location are removed.
    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');
        // Test that setting MessageCommand::$option['announce'] => '' suppresses
        // screen reader announcement.
        $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);
    }
    // Test that if MessageCommand::clearPrevious is FALSE, messages will not
    // be cleared.
    $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.');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.