You are here

public function TokenTest::testReplaceWithHookTokensAlterWithBubbleableMetadata 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::testReplaceWithHookTokensAlterWithBubbleableMetadata()

@covers ::replace @covers ::replace

File

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

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testReplaceWithHookTokensAlterWithBubbleableMetadata() {
  $this->moduleHandler
    ->expects($this
    ->any())
    ->method('invokeAll')
    ->willReturn([]);
  $this->moduleHandler
    ->expects($this
    ->any())
    ->method('alter')
    ->willReturnCallback(function ($hook_name, array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
    $replacements['[node:title]'] = 'hello world';
    $bubbleable_metadata
      ->addCacheContexts([
      'custom_context',
    ]);
    $bubbleable_metadata
      ->addCacheTags([
      'node:1',
    ]);
    $bubbleable_metadata
      ->setCacheMaxAge(10);
  });
  $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());
}