You are here

public function MessageTest::testGetText in Message 8

Tests for getText.

@covers ::getText

File

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

Class

MessageTest
Kernel tests for the Message entity.

Namespace

Drupal\Tests\message\Kernel

Code

public function testGetText() {

  // Test with missing message template.

  /** @var \Drupal\message\Entity\Message $message */
  $message = $this->entityTypeManager
    ->getStorage('message')
    ->create([
    'template' => 'no_exists',
  ]);
  $this
    ->assertEmpty($message
    ->getText());

  // Non-existent delta.

  /** @var \Drupal\message\Entity\Message $message */
  $message = $this->entityTypeManager
    ->getStorage('message')
    ->create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  $this
    ->assertEmpty($message
    ->getText(Language::LANGCODE_NOT_SPECIFIED, 123));

  // Verify token clearing disabled.
  $this->messageTemplate
    ->setSettings([
    'token options' => [
      'token replace' => TRUE,
      'clear' => FALSE,
    ],
  ]);
  $this->messageTemplate
    ->set('text', [
    [
      'value' => 'foo [fake:token] and [message:author:name]',
      'format' => filter_default_format(),
    ],
  ]);
  $this->messageTemplate
    ->save();

  /** @var \Drupal\message\Entity\Message $message */
  $message = $this->entityTypeManager
    ->getStorage('message')
    ->create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  $text = $message
    ->getText();
  $this
    ->assertEquals(1, count($text));
  $this
    ->assertEquals('<p>foo [fake:token] and [message:author:name]</p>' . "\n", $text[0]);

  // Verify token clearing enabled.
  $this->messageTemplate
    ->setSettings([
    'token options' => [
      'token replace' => TRUE,
      'clear' => TRUE,
    ],
  ]);
  $this->messageTemplate
    ->save();

  /** @var \Drupal\message\Entity\Message $message */
  $message = $this->entityTypeManager
    ->getStorage('message')
    ->create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  $text = $message
    ->getText();
  $this
    ->assertEquals(1, count($text));
  $this
    ->assertEquals('<p>foo  and </p>' . "\n", $text[0]);

  // Verify token replacement.
  $account = $this
    ->createUser();
  $message
    ->setOwner($account);
  $message
    ->save();
  $text = $message
    ->getText();
  $this
    ->assertEquals(1, count($text));
  $this
    ->assertEquals('<p>foo  and ' . $account
    ->getAccountName() . "</p>\n", $text[0]);

  // Disable token processing.
  $this->messageTemplate
    ->setSettings([
    'token options' => [
      'token replace' => FALSE,
      'clear' => TRUE,
    ],
  ]);
  $this->messageTemplate
    ->save();
  $text = $message
    ->getText();
  $this
    ->assertEquals(1, count($text));
  $this
    ->assertEquals('<p>foo [fake:token] and [message:author:name]</p>' . "\n", $text[0]);
}