You are here

public function EntityUnitTest::testCacheContexts in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php \Drupal\Tests\Core\Entity\EntityUnitTest::testCacheContexts()

@covers ::getCacheContexts @covers ::addCacheContexts

File

core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php, line 515
Contains \Drupal\Tests\Core\Entity\EntityUnitTest.

Class

EntityUnitTest
@coversDefaultClass \Drupal\Core\Entity\Entity @group Entity @group Access

Namespace

Drupal\Tests\Core\Entity

Code

public function testCacheContexts() {
  $cache_contexts_manager = $this
    ->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
    ->disableOriginalConstructor()
    ->getMock();
  $cache_contexts_manager
    ->method('assertValidTokens')
    ->willReturn(TRUE);
  $container = new ContainerBuilder();
  $container
    ->set('cache_contexts_manager', $cache_contexts_manager);
  \Drupal::setContainer($container);

  // There are no cache contexts by default.
  $this
    ->assertEquals([], $this->entity
    ->getCacheContexts());

  // Add an additional cache context.
  $this->entity
    ->addCacheContexts([
    'user',
  ]);
  $this
    ->assertEquals([
    'user',
  ], $this->entity
    ->getCacheContexts());
}