JsMessageWhitespaceTest.php

Same filename and directory in other branches
  1. 11.x core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageWhitespaceTest.php

Namespace

Drupal\FunctionalJavascriptTests\Core

File

core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageWhitespaceTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\FunctionalJavascriptTests\Core;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests that message.js handles whitespace in empty message wrappers.
 */
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]');
  }

}

Classes

Title Deprecated Summary
JsMessageWhitespaceTest Tests that message.js handles whitespace in empty message wrappers.

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