You are here

public function ContextTest::testSetContextValueCacheableDependency in Drupal 8

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

@covers ::setContextValue

File

core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php, line 103
Contains \Drupal\Tests\Core\Plugin\Context\ContextTest.

Class

ContextTest
@coversDefaultClass \Drupal\Core\Plugin\Context\Context @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());
}