You are here

public function TokenTest::testReplaceWithHookTokensWithBubbleableMetadata in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testReplaceWithHookTokensWithBubbleableMetadata()

@covers ::replace

File

core/tests/Drupal/Tests/Core/Utility/TokenTest.php, line 186
Contains \Drupal\Tests\Core\Utility\TokenTest.

Class

TokenTest
@coversDefaultClass \Drupal\Core\Utility\Token @group Utility

Namespace

Drupal\Tests\Core\Utility

Code

public function testReplaceWithHookTokensWithBubbleableMetadata() {
  $this->moduleHandler
    ->expects($this
    ->any())
    ->method('invokeAll')
    ->willReturnCallback(function ($hook_name, $args) {
    $cacheable_metadata = $args[4];
    $cacheable_metadata
      ->addCacheContexts([
      'custom_context',
    ]);
    $cacheable_metadata
      ->addCacheTags([
      'node:1',
    ]);
    $cacheable_metadata
      ->setCacheMaxAge(10);
    return [
      '[node:title]' => 'hello world',
    ];
  });
  $node = $this
    ->prophesize('Drupal\\node\\NodeInterface');
  $node
    ->getCacheContexts()
    ->willReturn([]);
  $node
    ->getCacheTags()
    ->willReturn([]);
  $node
    ->getCacheMaxAge()
    ->willReturn(14);
  $node = $node
    ->reveal();
  $bubbleable_metadata = new BubbleableMetadata();
  $bubbleable_metadata
    ->setCacheContexts([
    'current_user',
  ]);
  $bubbleable_metadata
    ->setCacheMaxAge(12);
  $result = $this->token
    ->replace('[node:title]', [
    'node' => $node,
  ], [], $bubbleable_metadata);
  $this
    ->assertEquals('hello world', $result);
  $this
    ->assertEquals([
    'node:1',
  ], $bubbleable_metadata
    ->getCacheTags());
  $this
    ->assertEquals([
    'current_user',
    'custom_context',
  ], $bubbleable_metadata
    ->getCacheContexts());
  $this
    ->assertEquals(10, $bubbleable_metadata
    ->getCacheMaxAge());
}