function AccessPolicyProcessorTest::testCaching

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

Tests if the caches are called correctly.

@dataProvider cachingProvider

File

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

Class

AccessPolicyProcessorTest
Tests the AccessPolicyProcessor service.

Namespace

Drupal\Tests\Core\Session

Code

public function testCaching(bool $db_cache_hit, bool $static_cache_hit) : void {
    if ($static_cache_hit) {
        $this->assertFalse($db_cache_hit, 'DB cache should never be checked when there is a static hit.');
    }
    $account = $this->prophesize(AccountInterface::class)
        ->reveal();
    $scope = 'bar';
    $bar_access_policy = new BarAccessPolicy();
    $bar_permissions = $bar_access_policy->calculatePermissions($account, $scope);
    $bar_permissions->addCacheTags([
        'access_policies',
    ]);
    $none_refinable_bar_permissions = new CalculatedPermissions($bar_permissions);
    $cache_static = $this->prophesize(VariationCacheInterface::class);
    $cache_db = $this->prophesize(VariationCacheInterface::class);
    if (!$static_cache_hit) {
        if (!$db_cache_hit) {
            $cache_db->get(Argument::cetera())
                ->willReturn(FALSE);
            $cache_db->set(Argument::any(), $bar_permissions, Argument::cetera())
                ->shouldBeCalled();
        }
        else {
            $cache_item = new CacheItem($bar_permissions);
            $cache_db->get(Argument::cetera())
                ->willReturn($cache_item);
            $cache_db->set()
                ->shouldNotBeCalled();
        }
        $cache_static->get(Argument::cetera())
            ->willReturn(FALSE);
        $cache_static->set(Argument::any(), $none_refinable_bar_permissions, Argument::cetera())
            ->shouldBeCalled();
    }
    else {
        $cache_item = new CacheItem($none_refinable_bar_permissions);
        $cache_static->get(Argument::cetera())
            ->willReturn($cache_item);
        $cache_static->set()
            ->shouldNotBeCalled();
    }
    $cache_static = $cache_static->reveal();
    $cache_db = $cache_db->reveal();
    $processor = $this->setUpAccessPolicyProcessor($cache_db, $cache_static);
    $processor->addAccessPolicy($bar_access_policy);
    $permissions = $processor->processAccessPolicies($account, $scope);
    $this->assertEquals($none_refinable_bar_permissions, $permissions, 'Cached permission matches calculated.');
}

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