function ContextTest::testSetContextValueCacheableDependency

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest::testSetContextValueCacheableDependency()
  2. 10 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest::testSetContextValueCacheableDependency()
  3. 11.x 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 103

Class

ContextTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Plugin%21Context%21Context.php/class/Context/8.9.x" title="A Drupal specific context wrapper class." class="local">\Drupal\Core\Plugin\Context\Context</a> @group Plugin

Namespace

Drupal\Tests\Core\Plugin\Context

Code

public function testSetContextValueCacheableDependency() {
    $container = new Container();
    $cache_context_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\CacheContextsManager')
        ->disableOriginalConstructor()
        ->setMethods([
        '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.