You are here

public function SystemTestController::messengerServiceTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php \Drupal\system_test\Controller\SystemTestController::messengerServiceTest()

Tests setting messages and removing one before it is displayed.

Return value

string Empty string, we just test the setting of messages.

1 string reference to 'SystemTestController::messengerServiceTest'
system_test.routing.yml in core/modules/system/tests/modules/system_test/system_test.routing.yml
core/modules/system/tests/modules/system_test/system_test.routing.yml

File

core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php, line 111

Class

SystemTestController
Controller routines for system_test routes.

Namespace

Drupal\system_test\Controller

Code

public function messengerServiceTest() {

  // Set two messages.
  $this->messenger
    ->addStatus('First message (removed).');
  $this->messenger
    ->addStatus($this
    ->t('Second message with <em>markup!</em> (not removed).'));
  $messages = $this->messenger
    ->deleteByType('status');

  // Remove the first.
  unset($messages[0]);
  foreach ($messages as $message) {
    $this->messenger
      ->addStatus($message);
  }

  // Duplicate message check.
  $this->messenger
    ->addStatus('Non Duplicated message');
  $this->messenger
    ->addStatus('Non Duplicated message');
  $this->messenger
    ->addStatus('Duplicated message', TRUE);
  $this->messenger
    ->addStatus('Duplicated message', TRUE);

  // Add a Markup message.
  $this->messenger
    ->addStatus(Markup::create('Markup with <em>markup!</em>'));

  // Test duplicate Markup messages.
  $this->messenger
    ->addStatus(Markup::create('Markup with <em>markup!</em>'));

  // Ensure that multiple Markup messages work.
  $this->messenger
    ->addStatus(Markup::create('Markup2 with <em>markup!</em>'));

  // Test mixing of types.
  $this->messenger
    ->addStatus(Markup::create('Non duplicate Markup / string.'));
  $this->messenger
    ->addStatus('Non duplicate Markup / string.');
  $this->messenger
    ->addStatus(Markup::create('Duplicate Markup / string.'), TRUE);
  $this->messenger
    ->addStatus('Duplicate Markup / string.', TRUE);

  // Test auto-escape of non safe strings.
  $this->messenger
    ->addStatus('<em>This<span>markup will be</span> escaped</em>.');
  return [];
}