function ContextTest::testSetContextValueCacheableDependency

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest::testSetContextValueCacheableDependency()
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest::testSetContextValueCacheableDependency()
  3. 10 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest::testSetContextValueCacheableDependency()

@covers ::setContextValue

File

core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php, line 98

Class

ContextTest
@coversDefaultClass \Drupal\Core\Plugin\Context\Context @group Plugin

Namespace

Drupal\Tests\Core\Plugin\Context

Code

public function testSetContextValueCacheableDependency() : void {
    $container = new Container();
    $cache_context_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
        ->disableOriginalConstructor()
        ->onlyMethods([
        'validateTokens',
    ])
        ->getMock();
    $container->set('cache_contexts_manager', $cache_context_manager);
    $cache_context_manager->expects($this->any())
        ->method('validateTokens')
        ->with([
        'route',
    ])
        ->willReturn([
        'route',
    ]);
    \Drupal::setContainer($container);
    $this->contextDefinition = $this->createMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
    $context = new Context($this->contextDefinition);
    $context->setTypedDataManager($this->typedDataManager);
    $cacheable_dependency = $this->createMock('Drupal\\Tests\\Core\\Plugin\\Context\\TypedDataCacheableDependencyInterface');
    $cacheable_dependency->expects($this->once())
        ->method('getCacheTags')
        ->willReturn([
        'node:1',
    ]);
    $cacheable_dependency->expects($this->once())
        ->method('getCacheContexts')
        ->willReturn([
        'route',
    ]);
    $cacheable_dependency->expects($this->once())
        ->method('getCacheMaxAge')
        ->willReturn(60);
    $context = Context::createFromContext($context, $cacheable_dependency);
    $this->assertSame($cacheable_dependency, $context->getContextData());
    $this->assertEquals([
        'node:1',
    ], $context->getCacheTags());
    $this->assertEquals([
        'route',
    ], $context->getCacheContexts());
    $this->assertEquals(60, $context->getCacheMaxAge());
}

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