You are here

public function MessageTokenTest::testHardCodedTokens in Message 8

Test the hard coded tokens.

File

tests/src/Kernel/MessageTokenTest.php, line 94

Class

MessageTokenTest
Test the Message and tokens integration.

Namespace

Drupal\Tests\message\Kernel

Code

public function testHardCodedTokens() {
  $random_text = $this
    ->randomString();
  $token_messages = [
    'some text @{message:author} ' . $random_text,
    'some text %{message:author} ' . $random_text,
    'some text @{wrong:token} ' . $random_text,
  ];

  // The plain_text filter replaces line breaks, so those should be here too.
  $replaced_messages = [
    '<p>some text ' . Html::escape($this->user
      ->label() . ' ' . $random_text) . "</p>\n",
    '<p>some text <em class="placeholder">' . Html::escape($this->user
      ->label()) . '</em> ' . Html::escape($random_text) . "</p>\n",
    '<p>some text @{wrong:token} ' . Html::escape($random_text) . "</p>\n",
  ];

  // Create the message template.
  $message_template = $this
    ->createMessageTemplate('dummy_message', 'Dummy message', '', $token_messages);

  // Assert the arguments.
  $original_message = Message::create([
    'template' => $message_template
      ->id(),
    'uid' => $this->user
      ->id(),
  ]);
  $this
    ->assertTrue($original_message
    ->getArguments() == FALSE, 'No message arguments exist prior to saving the message.');
  $original_message
    ->save();

  // Make very, very sure the message arguments are not coming from the
  // object save created.
  $this->entityTypeManager
    ->getStorage('message')
    ->resetCache();
  $message = Message::load($original_message
    ->id());
  $this
    ->assertNotSame($message, $original_message);
  $arguments = $message
    ->getArguments();
  $this
    ->assertEquals(count($arguments), 2, 'Correct number of arguments added after saving the message.');

  // Assert message is rendered as expected.
  $this
    ->assertEquals($replaced_messages, $message
    ->getText(), 'The text rendered as expected.');
}