You are here

public function MessageTest::testGetTextArgumentProcessing in Message 8

Tests for getText argument handling.

@covers ::getText

File

tests/src/Kernel/MessageTest.php, line 158

Class

MessageTest
Kernel tests for the Message entity.

Namespace

Drupal\Tests\message\Kernel

Code

public function testGetTextArgumentProcessing() {
  $this->messageTemplate
    ->setSettings([
    'token options' => [
      'token replace' => FALSE,
      'clear' => TRUE,
    ],
  ]);
  $this->messageTemplate
    ->set('text', [
    [
      'value' => '@foo @replace and @no_replace',
      'format' => filter_default_format(),
    ],
    [
      'value' => 'some @foo other @replace',
      'format' => filter_default_format(),
    ],
  ]);
  $this->messageTemplate
    ->save();

  /** @var \Drupal\message\Entity\Message $message */
  $message = $this->entityTypeManager
    ->getStorage('message')
    ->create([
    'template' => $this->messageTemplate
      ->id(),
    'arguments' => [
      [
        '@foo' => 'bar',
        '@replace' => [
          'pass message' => TRUE,
          'arguments' => [
            // When pass message is false, we'll use this text.
            'bar_replacement',
          ],
          'callback' => [
            static::class,
            'argumentCallback',
          ],
        ],
      ],
    ],
  ]);
  $message
    ->save();
  $text = $message
    ->getText();
  $this
    ->assertEquals(2, count($text));
  $this
    ->assertEquals('<p>bar bar_replacement_' . $message
    ->id() . ' and @no_replace</p>' . "\n", $text[0]);
  $this
    ->assertEquals('<p>some bar other bar_replacement_' . $message
    ->id() . "</p>\n", $text[1]);

  // Do not pass the message.

  /** @var \Drupal\message\Entity\Message $message */
  $message = $this->entityTypeManager
    ->getStorage('message')
    ->create([
    'template' => $this->messageTemplate
      ->id(),
    'arguments' => [
      [
        '@foo' => 'bar',
        '@replace' => [
          'pass message' => FALSE,
          'arguments' => [
            // When pass message is false, we'll use this text.
            'bar_replacement',
          ],
          'callback' => [
            static::class,
            'argumentCallback',
          ],
        ],
      ],
    ],
  ]);
  $message
    ->save();
  $text = $message
    ->getText();
  $this
    ->assertEquals(2, count($text));
  $this
    ->assertEquals('<p>bar bar_replacement and @no_replace</p>' . "\n", $text[0]);
  $this
    ->assertEquals('<p>some bar other bar_replacement' . "</p>\n", $text[1]);
}