function AccessPolicyProcessorTest::testCacheContexts

Same name in other branches
  1. 10 core/tests/Drupal/Tests/Core/Session/AccessPolicyProcessorTest.php \Drupal\Tests\Core\Session\AccessPolicyProcessorTest::testCacheContexts()

Tests that only the cache contexts for policies that apply are added.

File

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

Class

AccessPolicyProcessorTest
Tests the AccessPolicyProcessor service.

Namespace

Drupal\Tests\Core\Session

Code

public function testCacheContexts() : void {
    // BazAccessPolicy and BarAlterAccessPolicy shouldn't add any contexts.
    $initial_cacheability = (new CacheableMetadata())->addCacheContexts([
        'foo',
        'bar',
    ]);
    $final_cacheability = (new CacheableMetadata())->addCacheContexts([
        'foo',
        'bar',
    ])
        ->addCacheTags([
        'access_policies',
    ]);
    $variation_cache = $this->prophesize(VariationCacheInterface::class);
    $variation_cache->get(Argument::cetera())
        ->willReturn(FALSE);
    $variation_cache->set([
        'access_policies',
        'anything',
    ], Argument::any(), $final_cacheability, $initial_cacheability)
        ->shouldBeCalled();
    $cache_static = $this->prophesize(CacheBackendInterface::class);
    $cache_static->get('access_policies:access_policy_processor:contexts:anything')
        ->willReturn(FALSE);
    $cache_static->set('access_policies:access_policy_processor:contexts:anything', [
        'foo',
        'bar',
    ])
        ->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.