Unverified Commit 99b6843d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3268932 by danflanagan8, mondrake, dww, xjm, longwave, alexpott,...

Issue #3268932 by danflanagan8, mondrake, dww, xjm, longwave, alexpott, larowlan: Add methods to assert status messages to WebAssert

(cherry picked from commit e5e74fb9)
parent 6932e25e
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ class ModerationActionsTest extends BrowserTestBase {
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
@@ -81,16 +81,14 @@ public function testNodeStatusActions($action, $bundle, $warning_appears, $start

    if ($warning_appears) {
      if ($action == 'node_publish_action') {
        $this->assertSession()
          ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly published.');
        $this->assertSession()->statusMessageContains(node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly published.', 'warning');
      }
      else {
        $this->assertSession()
          ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly unpublished.');
        $this->assertSession()->statusMessageContains(node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly unpublished.', 'warning');
      }
    }
    else {
      $this->assertSession()->elementNotExists('css', '.messages--warning');
      $this->assertSession()->statusMessageNotExists('warning');
    }

    // Ensure after the action has run, the node matches the expected status.
+5 −6
Original line number Diff line number Diff line
@@ -108,17 +108,16 @@ public function testUploadFileExceedingMaximumFileSize() {
    $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($invalid_file));

    // An error message should appear informing the user that the file exceeded
    // the maximum file size.
    $this->assertSession()->waitForElement('css', '.messages--error');
    // The error message includes the actual file size limit which depends on
    // the current environment, so we check for a part of the message.
    $this->assertSession()->pageTextContains('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size');
    // the maximum file size. The error message includes the actual file size
    // limit which depends on the current environment, so we check for a part
    // of the message.
    $this->assertSession()->statusMessageContainsAfterWait('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size', 'error');

    // Now upload a valid file and check that the error message disappears.
    $valid_file = $this->generateFile('not_exceeding_post_max_size', 8, 8);
    $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($valid_file));
    $this->assertSession()->waitForElement('named', ['id_or_name', 'field_file_0_remove_button']);
    $this->assertSession()->elementNotExists('css', '.messages--error');
    $this->assertSession()->statusMessageNotExistsAfterWait('error');
  }

}
+21 −0
Original line number Diff line number Diff line
@@ -146,6 +146,27 @@ public function messengerServiceTest() {
    return [];
  }

  /**
   * Sets messages for testing the WebAssert methods related to messages.
   *
   * @return array
   *   Empty array, we just need the messages.
   */
  public function statusMessagesForAssertions(): array {
    // Add a simple message of each type.
    $this->messenger->addMessage('My Status Message', 'status');
    $this->messenger->addMessage('My Error Message', 'error');
    $this->messenger->addMessage('My Warning Message', 'warning');

    // Add messages with special characters and/or markup.
    $this->messenger->addStatus('This has " in the middle');
    $this->messenger->addStatus('This has \' in the middle');
    $this->messenger->addStatus('<em>This<span>markup will be</span> escaped</em>.');
    $this->messenger->addStatus('Peaches & cream');

    return [];
  }

  /**
   * Controller to return $_GET['destination'] for testing.
   *
+8 −0
Original line number Diff line number Diff line
@@ -21,6 +21,14 @@ system_test.messenger_service:
  requirements:
    _access: 'TRUE'

system_test.status_messages_for_assertions:
  path: '/system-test/status-messages-for-assertions'
  defaults:
    _title: 'Set various message to test status message assertion methods'
    _controller: '\Drupal\system_test\Controller\SystemTestController::statusMessagesForAssertions'
  requirements:
    _access: 'TRUE'

system_test.main_content_fallback:
  path: '/system-test/main-content-fallback'
  defaults:
+88 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\AssertionFailedError;

/**
 * Tests the Messenger service.
@@ -61,4 +62,91 @@ public function testDrupalMessengerService() {
    $assert->pageTextContains('system_test_preinstall_module called');
  }

  /**
   * Tests assertion methods in WebAssert related to status messages.
   */
  public function testStatusMessageAssertions(): void {
    $this->drupalGet(Url::fromRoute('system_test.status_messages_for_assertions'));

    // Use the simple messages to test basic functionality.
    // Test WebAssert::statusMessagesExists().
    $this->assertSession()->statusMessageExists();
    $this->assertSession()->statusMessageExists('status');
    $this->assertSession()->statusMessageExists('error');
    $this->assertSession()->statusMessageExists('warning');

    // WebAssert::statusMessageContains().
    $this->assertSession()->statusMessageContains('My Status Message');
    $this->assertSession()->statusMessageContains('My Error Message');
    $this->assertSession()->statusMessageContains('My Warning Message');
    // Test partial match.
    $this->assertSession()->statusMessageContains('My Status');
    // Test with second arg.
    $this->assertSession()->statusMessageContains('My Status Message', 'status');
    $this->assertSession()->statusMessageContains('My Error Message', 'error');
    $this->assertSession()->statusMessageContains('My Warning Message', 'warning');

    // Test WebAssert::statusMessageNotContains().
    $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');

    // Check that special characters get handled correctly.
    $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 &amp; cream');

    // Go to a new route that only has messages of type 'status'.
    $this->drupalGet(Url::fromRoute('system_test.messenger_service'));
    // Test WebAssert::statusMessageNotExists().
    $this->assertSession()->statusMessageNotExists('error');
    $this->assertSession()->statusMessageNotExists('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.
    $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.');

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

}
Loading