You are here

protected static function ResourceTestBase::getExpectedCollectionCacheability in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedCollectionCacheability()
  2. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedCollectionCacheability()

Computes the cacheability for a given entity collection.

Parameters

\Drupal\Core\Session\AccountInterface $account: An account for which cacheability should be computed (cacheability is dependent on access).

\Drupal\Core\Entity\EntityInterface[] $collection: The entities for which cacheability should be computed.

array $sparse_fieldset: (optional) If a sparse fieldset is being requested, limit the expected cacheability for the collection entities' fields to just those in the fieldset. NULL means all fields.

bool $filtered: Whether the collection is filtered or not.

Return value

\Drupal\Core\Cache\CacheableMetadata The expected cacheability for the given entity collection.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 559

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected static function getExpectedCollectionCacheability(AccountInterface $account, array $collection, array $sparse_fieldset = NULL, $filtered = FALSE) {
  $cacheability = array_reduce($collection, function (CacheableMetadata $cacheability, EntityInterface $entity) use ($sparse_fieldset, $account) {
    $access_result = static::entityAccess($entity, 'view', $account);
    if (!$access_result
      ->isAllowed()) {
      $access_result = static::entityAccess($entity, 'view label', $account)
        ->addCacheableDependency($access_result);
    }
    $cacheability
      ->addCacheableDependency($access_result);
    if ($access_result
      ->isAllowed()) {
      $cacheability
        ->addCacheableDependency($entity);
      if ($entity instanceof FieldableEntityInterface) {
        foreach ($entity as $field_name => $field_item_list) {

          /** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */
          if (is_null($sparse_fieldset) || in_array($field_name, $sparse_fieldset)) {
            $field_access = static::entityFieldAccess($entity, $field_name, 'view', $account);
            $cacheability
              ->addCacheableDependency($field_access);
            if ($field_access
              ->isAllowed()) {
              foreach ($field_item_list as $field_item) {

                /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
                foreach (TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item) as $property) {
                  $cacheability
                    ->addCacheableDependency(CacheableMetadata::createFromObject($property));
                }
              }
            }
          }
        }
      }
    }
    return $cacheability;
  }, new CacheableMetadata());
  $entity_type = reset($collection)
    ->getEntityType();
  $cacheability
    ->addCacheTags([
    'http_response',
  ]);
  $cacheability
    ->addCacheTags($entity_type
    ->getListCacheTags());
  $cache_contexts = [
    // Cache contexts for JSON:API URL query parameters.
    'url.query_args:fields',
    'url.query_args:filter',
    'url.query_args:include',
    'url.query_args:page',
    'url.query_args:sort',
    // Drupal defaults.
    'url.site',
  ];

  // If the entity type is revisionable, add a resource version cache context.
  $cache_contexts = Cache::mergeContexts($cache_contexts, $entity_type
    ->isRevisionable() ? [
    'url.query_args:resourceVersion',
  ] : []);
  $cacheability
    ->addCacheContexts($cache_contexts);
  return $cacheability;
}