You are here

public function HookTokensTest::testCaching in Node Summary Token From P Tags 1.x

File

tests/src/HookTokensTest.php, line 204

Class

HookTokensTest

Namespace

Drupal\Tests\node_summary_token_from_p

Code

public function testCaching() {

  // 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();
  $cid = 'node_summary_token_from_p:' . $node
    ->id();
  $this
    ->assertFalse($this->cache
    ->get($cid));
  $expected_cached_value = 'This is sentence 1. This is sentence 2? This is sentence 3!';
  $bubbleable_metadata = new BubbleableMetadata();
  $output = $this->tokenService
    ->replace('[node:summary]', [
    'node' => $node,
  ], [
    'langcode' => $this->interfaceLanguage
      ->getId(),
  ], $bubbleable_metadata);
  $this
    ->assertEquals($expected_cached_value, $output);
  $cached = $this->cache
    ->get($cid);
  $this
    ->assertNotFalse($cached);
  $this
    ->assertEquals($expected_cached_value, $cached->data);
  $node
    ->set('field_text', [
    'value' => '<p>Changed.</p><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();
  $this
    ->assertEquals($expected_cached_value, $output);
  $expected_cached_value = 'Changed. This is sentence 1. This is sentence 2?';
  $bubbleable_metadata = new BubbleableMetadata();
  $output = $this->tokenService
    ->replace('[node:summary]', [
    'node' => $node,
  ], [
    'langcode' => $this->interfaceLanguage
      ->getId(),
  ], $bubbleable_metadata);
  $this
    ->assertEquals($expected_cached_value, $output);
  $cached = $this->cache
    ->get($cid);
  $this
    ->assertNotFalse($cached);
  $this
    ->assertEquals($expected_cached_value, $cached->data);
}