function TokenTest::testReplaceWithBubbleableMetadataObject

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testReplaceWithBubbleableMetadataObject()
  2. 8.9.x core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testReplaceWithBubbleableMetadataObject()
  3. 11.x core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testReplaceWithBubbleableMetadataObject()

@covers ::replace

File

core/tests/Drupal/Tests/Core/Utility/TokenTest.php, line 156

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testReplaceWithBubbleableMetadataObject() : void {
    $this->moduleHandler
        ->expects($this->any())
        ->method('invokeAll')
        ->willReturn([
        '[node:title]' => 'hello world',
    ]);
    $bubbleable_metadata = new BubbleableMetadata();
    $bubbleable_metadata->setCacheContexts([
        'current_user',
    ]);
    $bubbleable_metadata->setCacheMaxAge(12);
    $node = $this->prophesize('Drupal\\node\\NodeInterface');
    $node->getCacheTags()
        ->willReturn([
        'node:1',
    ]);
    $node->getCacheContexts()
        ->willReturn([
        'custom_context',
    ]);
    $node->getCacheMaxAge()
        ->willReturn(10);
    $node = $node->reveal();
    $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.