You are here

public function EntityUnitTest::testCacheContexts in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php \Drupal\Tests\Core\Entity\EntityUnitTest::testCacheContexts()
  2. 9 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 587

Class

EntityUnitTest
@coversDefaultClass \Drupal\Core\Entity\EntityBase @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
    ->assertEqualsCanonicalizing([], $this->entity
    ->getCacheContexts());

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