function EntityUnitTest::testCacheContexts

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php \Drupal\Tests\Core\Entity\EntityUnitTest::testCacheContexts()
  2. 10 core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php \Drupal\Tests\Core\Entity\EntityUnitTest::testCacheContexts()
  3. 11.x 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());
}

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