function TokenTest::testReplaceWithHookTokensWithBubbleableMetadata

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testReplaceWithHookTokensWithBubbleableMetadata()
  2. 10 core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testReplaceWithHookTokensWithBubbleableMetadata()
  3. 11.x 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 181

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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.