function TokenTest::testReplaceWithHookTokensWithBubbleableMetadata

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

Class

TokenTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Utility%21Token.php/class/Token/11.x" title="Drupal placeholder/token replacement system." class="local">\Drupal\Core\Utility\Token</a> @group Utility

Namespace

Drupal\Tests\Core\Utility

Code

public function testReplaceWithHookTokensWithBubbleableMetadata() : void {
    $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.