You are here

protected static function ResourceTestBase::getExpectedCollectionCacheability in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedCollectionCacheability()

Computes the cacheability for a given entity collection.

Parameters

\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.

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

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

Return value

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

5 calls to ResourceTestBase::getExpectedCollectionCacheability()
CommentTest::getExpectedCollectionCacheability in tests/src/Functional/CommentTest.php
Computes the cacheability for a given entity collection.
ContentLanguageSettingsTest::getExpectedCollectionCacheability in tests/src/Functional/ContentLanguageSettingsTest.php
Computes the cacheability for a given entity collection.
EntityTestTest::getExpectedCollectionCacheability in tests/src/Functional/EntityTestTest.php
Computes the cacheability for a given entity collection.
ResourceTestBase::getExpectedCollectionResponse in tests/src/Functional/ResourceTestBase.php
Returns a JSON API collection document for the expected entities.
ShortcutTest::getExpectedCollectionCacheability in tests/src/Functional/ShortcutTest.php
Computes the cacheability for a given entity collection.
5 methods override ResourceTestBase::getExpectedCollectionCacheability()
BlockTest::getExpectedCollectionCacheability in tests/src/Functional/BlockTest.php
Computes the cacheability for a given entity collection.
CommentTest::getExpectedCollectionCacheability in tests/src/Functional/CommentTest.php
Computes the cacheability for a given entity collection.
ContentLanguageSettingsTest::getExpectedCollectionCacheability in tests/src/Functional/ContentLanguageSettingsTest.php
Computes the cacheability for a given entity collection.
EntityTestTest::getExpectedCollectionCacheability in tests/src/Functional/EntityTestTest.php
Computes the cacheability for a given entity collection.
ShortcutTest::getExpectedCollectionCacheability in tests/src/Functional/ShortcutTest.php
Computes the cacheability for a given entity collection.

File

tests/src/Functional/ResourceTestBase.php, line 484

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected static function getExpectedCollectionCacheability(array $collection, array $sparse_fieldset = NULL, AccountInterface $account, $filtered = FALSE) {
  $cacheability = array_reduce($collection, function (CacheableMetadata $cacheability, EntityInterface $entity) use ($sparse_fieldset, $account) {
    $access_result = static::entityAccess($entity, 'view', $account);
    $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());
  $cacheability
    ->addCacheTags([
    'http_response',
  ]);
  $cacheability
    ->addCacheTags(reset($collection)
    ->getEntityType()
    ->getListCacheTags());
  $cacheability
    ->addCacheContexts([
    // 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',
  ]);
  return $cacheability;
}