You are here

public function HookTokensTest::testNodeSummaryTokenFromFieldTextReplacement in Node Summary Token From P Tags 1.0.x

Same name and namespace in other branches
  1. 1.x tests/src/HookTokensTest.php \Drupal\Tests\node_summary_token_from_p\HookTokensTest::testNodeSummaryTokenFromFieldTextReplacement()

Test if the [node:summary] token is filled from the field_text field.

File

tests/src/HookTokensTest.php, line 155

Class

HookTokensTest

Namespace

Drupal\Tests\node_summary_token_from_p

Code

public function testNodeSummaryTokenFromFieldTextReplacement() {

  // Create a user and a node.
  $account = $this
    ->createUser();

  /* @var $node \Drupal\node\NodeInterface */
  $node = Node::create([
    'type' => 'article_wo_body',
    'tnid' => 0,
    'uid' => $account
      ->id(),
    'title' => 'Title',
    'field_text' => [
      [
        'value' => '<p>This is sentence 1. This is sentence 2?</p><p>This is sentence 3!</p><p>This is sentence 4.</p>',
        'format' => 'html',
      ],
    ],
  ]);
  $node
    ->save();

  // Generate and test tokens.
  $tests = [];
  $tests['[node:summary]'] = 'This is sentence 1. This is sentence 2? This is sentence 3!';
  $base_bubbleable_metadata = BubbleableMetadata::createFromObject($node);
  $metadata_tests = [];
  $metadata_tests['[node:summary]'] = $base_bubbleable_metadata;

  // Test to make sure that we generated something for each token.
  $this
    ->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
  foreach ($tests as $input => $expected) {
    $bubbleable_metadata = new BubbleableMetadata();
    $output = $this->tokenService
      ->replace($input, [
      'node' => $node,
    ], [
      'langcode' => $this->interfaceLanguage
        ->getId(),
    ], $bubbleable_metadata);
    $this
      ->assertEqual($output, $expected, new FormattableMarkup('Node token %token replaced.', [
      '%token' => $input,
    ]));
    $this
      ->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
  }
}