MessageCommandTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. /**
  5. * Tests adding messages via AJAX command.
  6. *
  7. * @group Ajax
  8. */
  9. class MessageCommandTest extends WebDriverTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected static $modules = ['ajax_test'];
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $defaultTheme = 'stark';
  18. /**
  19. * Test AJAX MessageCommand use in a form.
  20. */
  21. public function testMessageCommand() {
  22. $page = $this->getSession()->getPage();
  23. $assert_session = $this->assertSession();
  24. $this->drupalGet('ajax-test/message');
  25. $page->pressButton('Make Message In Default Location');
  26. $this->waitForMessageVisible('I am a message in the default location.');
  27. $this->assertAnnounceContains('I am a message in the default location.');
  28. $assert_session->elementsCount('css', '.messages__wrapper .messages', 1);
  29. $page->pressButton('Make Message In Alternate Location');
  30. $this->waitForMessageVisible('I am a message in an alternate location.', '#alternate-message-container');
  31. $assert_session->pageTextContains('I am a message in the default location.');
  32. $this->assertAnnounceContains('I am a message in an alternate location.');
  33. $assert_session->elementsCount('css', '.messages__wrapper .messages', 1);
  34. $assert_session->elementsCount('css', '#alternate-message-container .messages', 1);
  35. $page->pressButton('Make Warning Message');
  36. $this->waitForMessageVisible('I am a warning message in the default location.', NULL, 'warning');
  37. $assert_session->pageTextNotContains('I am a message in the default location.');
  38. $assert_session->elementsCount('css', '.messages__wrapper .messages', 1);
  39. $assert_session->elementsCount('css', '#alternate-message-container .messages', 1);
  40. $this->drupalGet('ajax-test/message');
  41. // Test that by default, previous messages in a location are removed.
  42. for ($i = 0; $i < 6; $i++) {
  43. $page->pressButton('Make Message In Default Location');
  44. $this->waitForMessageVisible('I am a message in the default location.');
  45. $assert_session->elementsCount('css', '.messages__wrapper .messages', 1);
  46. $page->pressButton('Make Warning Message');
  47. $this->waitForMessageVisible('I am a warning message in the default location.', NULL, 'warning');
  48. // Test that setting MessageCommand::$option['announce'] => '' supresses
  49. // screen reader announcement.
  50. $this->assertAnnounceNotContains('I am a warning message in the default location.');
  51. $this->waitForMessageRemoved('I am a message in the default location.');
  52. $assert_session->elementsCount('css', '.messages__wrapper .messages', 1);
  53. }
  54. // Test that if MessageCommand::clearPrevious is FALSE, messages will not
  55. // be cleared.
  56. $this->drupalGet('ajax-test/message');
  57. for ($i = 1; $i < 7; $i++) {
  58. $page->pressButton('Make Message In Alternate Location');
  59. $expected_count = $page->waitFor(10, function () use ($i, $page) {
  60. return count($page->findAll('css', '#alternate-message-container .messages')) === $i;
  61. });
  62. $this->assertTrue($expected_count);
  63. $this->assertAnnounceContains('I am a message in an alternate location.');
  64. }
  65. }
  66. /**
  67. * Asserts that a message of the expected type appears.
  68. *
  69. * @param string $message
  70. * The expected message.
  71. * @param string $selector
  72. * The selector for the element in which to check for the expected message.
  73. * @param string $type
  74. * The expected type.
  75. */
  76. protected function waitForMessageVisible($message, $selector = '[data-drupal-messages]', $type = 'status') {
  77. $this->assertNotEmpty($this->assertSession()->waitForElementVisible('css', $selector . ' .messages--' . $type . ':contains("' . $message . '")'));
  78. }
  79. /**
  80. * Asserts that a message of the expected type is removed.
  81. *
  82. * @param string $message
  83. * The expected message.
  84. * @param string $selector
  85. * The selector for the element in which to check for the expected message.
  86. * @param string $type
  87. * The expected type.
  88. */
  89. protected function waitForMessageRemoved($message, $selector = '[data-drupal-messages]', $type = 'status') {
  90. $this->assertNotEmpty($this->assertSession()->waitForElementRemoved('css', $selector . ' .messages--' . $type . ':contains("' . $message . '")'));
  91. }
  92. /**
  93. * Checks for inclusion of text in #drupal-live-announce.
  94. *
  95. * @param string $expected_message
  96. * The text expected to be present in #drupal-live-announce.
  97. */
  98. protected function assertAnnounceContains($expected_message) {
  99. $assert_session = $this->assertSession();
  100. $this->assertNotEmpty($assert_session->waitForElement('css', "#drupal-live-announce:contains('$expected_message')"));
  101. }
  102. /**
  103. * Checks for absence of the given text from #drupal-live-announce.
  104. *
  105. * @param string $expected_message
  106. * The text expected to be absent from #drupal-live-announce.
  107. */
  108. protected function assertAnnounceNotContains($expected_message) {
  109. $assert_session = $this->assertSession();
  110. $this->assertEmpty($assert_session->waitForElement('css', "#drupal-live-announce:contains('$expected_message')", 1000));
  111. }
  112. }