function AccessPolicyProcessorTest::testCacheContextCaching

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Session/AccessPolicyProcessorTest.php \Drupal\Tests\Core\Session\AccessPolicyProcessorTest::testCacheContextCaching()

Tests that the persistent cache contexts are added properly.

File

core/tests/Drupal/Tests/Core/Session/AccessPolicyProcessorTest.php, line 339

Class

AccessPolicyProcessorTest
Tests the AccessPolicyProcessor service.

Namespace

Drupal\Tests\Core\Session

Code

public function testCacheContextCaching() : void {
  $cache_entry = new \stdClass();
  $cache_entry->data = [
    'baz',
  ];
  $cache_static = $this->prophesize(CacheBackendInterface::class);
  $cache_static->get('access_policies:access_policy_processor:contexts:anything')
    ->willReturn($cache_entry);
  $cache_static->set('access_policies:access_policy_processor:contexts:anything', Argument::any())
    ->shouldNotBeCalled();
  // Hard-coded to "baz" because of the above cache entry.
  $initial_cacheability = (new CacheableMetadata())->addCacheContexts([
    'baz',
  ]);
  // Still adds in "foo" and "bar" in calculatePermissions(). Under normal
  // circumstances this would trigger an exception in VariationCache, but we
  // deliberately poison the cache in this test to see if it's called.
  $final_cacheability = (new CacheableMetadata())->addCacheContexts([
    'foo',
    'bar',
  ])
    ->addCacheTags([
    'access_policies',
  ]);
  $variation_cache = $this->prophesize(VariationCacheInterface::class);
  $variation_cache->get([
    'access_policies',
    'anything',
  ], $initial_cacheability)
    ->shouldBeCalled()
    ->willReturn(FALSE);
  $variation_cache->set([
    'access_policies',
    'anything',
  ], Argument::any(), $final_cacheability, $initial_cacheability)
    ->shouldBeCalled();
  $processor = $this->setUpAccessPolicyProcessor($variation_cache->reveal(), NULL, $cache_static->reveal());
  foreach ([
    new FooAccessPolicy(),
    new BarAccessPolicy(),
    new BazAccessPolicy(),
    new BarAlterAccessPolicy(),
  ] as $access_policy) {
    $processor->addAccessPolicy($access_policy);
  }
  $processor->processAccessPolicies($this->prophesize(AccountInterface::class)
    ->reveal(), 'anything');
}

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