summaryrefslogtreecommitdiffstatshomepage
path: root/core/tests/Drupal/FunctionalJavascriptTests/Ajax/MessageCommandTest.php
blob: 69b49916163e1c9ee74bb12fa099ae76ab9c839e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

declare(strict_types=1);

namespace Drupal\FunctionalJavascriptTests\Ajax;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use PHPUnit\Framework\ExpectationFailedException;

/**
 * Tests adding messages via AJAX command.
 *
 * @group Ajax
 */
class MessageCommandTest extends WebDriverTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['ajax_test'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests AJAX MessageCommand use in a form.
   */
  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.');
    }
  }

  /**
   * Tests methods in JsWebAssert related to status messages.
   */
  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');

    // Reload and test some negative assertions.
    $this->drupalGet('ajax-test/message');

    $page->pressButton('Make Message In Default Location');
    // Use message that is not on page.
    $this->assertSession()->statusMessageNotContainsAfterWait('This is not a real message');

    $page->pressButton('Make Message In Alternate Location');
    // Use message that exists but has the wrong type.
    $this->assertSession()->statusMessageNotContainsAfterWait('I am a message in an alternate location.', 'warning');

    // Test partial match.
    $page->pressButton('Make Warning Message');
    $this->assertSession()->statusMessageContainsAfterWait('I am a warning');

    // One more reload to try with different arg combinations.
    $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');

    // Perform a few assertions that should fail. We can only call
    // TestCase::expectException() once per test, so we make a few
    // try/catch blocks. We pass a relatively short timeout because
    // it is a waste of time to wait 10 seconds in these assertions
    // that we fully expect to fail.
    $expected_failure_occurred = FALSE;
    try {
      $this->assertSession()->statusMessageContainsAfterWait('Not a real message', NULL, 1000);
    }
    catch (ExpectationFailedException) {
      $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) {
      $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) {
      $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) {
      $expected_failure_occurred = TRUE;
    }
    $this->assertTrue($expected_failure_occurred, 'JsWebAssert::statusMessageNotExistsAfterWait() did not fail when it should have failed.');

    // Tests passing a bad status type.
    $this->expectException(\InvalidArgumentException::class);
    $this->assertSession()->statusMessageExistsAfterWait('not a valid type');
  }

  /**
   * Asserts that a message of the expected type appears.
   *
   * @param string $message
   *   The expected message.
   * @param string $selector
   *   The selector for the element in which to check for the expected message.
   * @param string $type
   *   The expected type.
   */
  protected function waitForMessageVisible($message, $selector = '[data-drupal-messages]', $type = 'status'): void {
    $this->assertNotEmpty($this->assertSession()->waitForElementVisible('css', $selector . ' .messages--' . $type . ':contains("' . $message . '")'));
  }

  /**
   * Asserts that a message of the expected type is removed.
   *
   * @param string $message
   *   The expected message.
   * @param string $selector
   *   The selector for the element in which to check for the expected message.
   * @param string $type
   *   The expected type.
   */
  protected function waitForMessageRemoved($message, $selector = '[data-drupal-messages]', $type = 'status'): void {
    $this->assertNotEmpty($this->assertSession()->waitForElementRemoved('css', $selector . ' .messages--' . $type . ':contains("' . $message . '")'));
  }

  /**
   * Checks for inclusion of text in #drupal-live-announce.
   *
   * @param string $expected_message
   *   The text expected to be present in #drupal-live-announce.
   *
   * @internal
   */
  protected function assertAnnounceContains(string $expected_message): void {
    $assert_session = $this->assertSession();
    $this->assertNotEmpty($assert_session->waitForElement('css', "#drupal-live-announce:contains('$expected_message')"));
  }

  /**
   * Checks for absence of the given text from #drupal-live-announce.
   *
   * @param string $expected_message
   *   The text expected to be absent from #drupal-live-announce.
   *
   * @internal
   */
  protected function assertAnnounceNotContains(string $expected_message): void {
    $assert_session = $this->assertSession();
    $this->assertEmpty($assert_session->waitForElement('css', "#drupal-live-announce:contains('$expected_message')", 1000));
  }

}