class JsMessageWhitespaceTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageWhitespaceTest.php \Drupal\FunctionalJavascriptTests\Core\JsMessageWhitespaceTest

Tests that message.js handles whitespace in empty message wrappers.

Attributes

#[Group('Javascript')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of JsMessageWhitespaceTest

File

core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageWhitespaceTest.php, line 14

Namespace

Drupal\FunctionalJavascriptTests\Core
View source
class JsMessageWhitespaceTest extends WebDriverTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'js_message_test',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * Tests that message.js handles whitespace in empty message wrappers.
   *
   * This test verifies that getting the message wrapper does not fail when
   * themes have a new line or something in their template.
   */
  public function testMessageClearWithWhitespace() : void {
    $this->drupalGet('js_message_test_link');
    $this->assertSession()
      ->pageTextContains('JsMessageLinks');
    // Simulate real-world scenario: theme template outputs whitespace
    // in the messages wrapper but no actual message elements.
    $this->getSession()
      ->executeScript("\n      var wrapper = document.querySelector('[data-drupal-messages]');\n      if (wrapper) {\n        // Clear existing content\n        wrapper.innerHTML = '';\n        // Add whitespace like a theme template might (newlines, spaces, tabs)\n        wrapper.innerHTML = '\\n  \\t\\n  \\n';\n      }\n    ");
    // Drupal.Message should work without errors when the wrapper contains only
    // whitespace.
    $this->getSession()
      ->executeScript("\n      try {\n        var msg = new Drupal.Message();\n        msg.add('Test message', {type: 'status'});\n        msg.clear();\n        // Mark success if no errors thrown\n        document.body.setAttribute('data-message-test-success', 'true');\n      } catch (e) {\n        // Mark failure if any errors thrown\n        document.body.setAttribute('data-message-test-error', e.toString());\n      }\n    ");
    // Verify the test completed successfully without JavaScript errors.
    $error = $this->getSession()
      ->evaluateScript("\n      return document.body.getAttribute('data-message-test-error');\n    ");
    $this->assertNull($error);
    // Verify the wrapper still exists after the operations.
    $this->assertSession()
      ->elementExists('css', '[data-drupal-messages]');
  }

}

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