function EntityCreateAccessCustomCidTest::testDefaultCid

Tests the entity access control handler caching with context.

@covers ::buildCreateAccessCid @dataProvider providerTestDefaultCid

Parameters

array $context: The context array for the test createAccess() check.

bool $in_cache: Whether there is already a cached createAccess() check for the cache ID.

bool $cacheable: If the test createAccess() check should be cacheable.

File

core/tests/Drupal/Tests/Core/Entity/Access/EntityCreateAccessCustomCidTest.php, line 132

Class

EntityCreateAccessCustomCidTest
Tests entity access control handler custom internal cache ID.

Namespace

Drupal\Tests\Core\Entity\Access

Code

public function testDefaultCid(array $context, bool $in_cache, bool $cacheable) : void {
    $bundle = $this->randomMachineName();
    $cid = "create:{$bundle}";
    $context['langcode'] = $this->langcode;
    $handler = new EntityAccessControlHandler($this->entityType);
    $handler->setModuleHandler($this->moduleHandler);
    $access_cache = $this->setUpAccessCache($handler, $in_cache, $cid);
    $cache = $access_cache->getValue($handler);
    // The cached value is AccessResult::allowed() but default result is
    // neutral() so createAccess returns TRUE for a cache hit, FALSE otherwise.
    $should_get_from_cache = $in_cache && $cacheable;
    $this->assertSame($should_get_from_cache, $handler->createAccess($bundle, $this->account, $context));
    $should_add_to_cache = $cacheable && !$in_cache;
    $cache_is_changed = $cache !== $access_cache->getValue($handler);
    $this->assertSame($should_add_to_cache, $cache_is_changed);
}

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