function NodeTest::getExpectedCacheContexts

The expected cache contexts for the GET/HEAD response of the test entity.

Parameters

array|null $sparse_fieldset: If a sparse fieldset is being requested, limit the expected cache contexts for this entity's fields to just these fields.

Return value

string[] A set of cache contexts.

Overrides ResourceTestBase::getExpectedCacheContexts

1 call to NodeTest::getExpectedCacheContexts()
NodeTest::testGetIndividual in core/modules/jsonapi/tests/src/Functional/NodeTest.php
Tests GETting an individual resource, plus edge cases to ensure good DX.

File

core/modules/jsonapi/tests/src/Functional/NodeTest.php, line 422

Class

NodeTest
JSON:API integration test for the "Node" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedCacheContexts(?array $sparse_fieldset = NULL) {
  // \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testRevisions()
  // loads different revisions via query parameters, we do our best
  // here to react to those directly, or indirectly.
  $cache_contexts = parent::getExpectedCacheContexts($sparse_fieldset);
  // This is bubbled up by
  // \Drupal\node\NodeAccessControlHandler::checkAccess() directly.
  if ($this->entity
    ->isPublished()) {
    return $cache_contexts;
  }
  if (!\Drupal::currentUser()->isAuthenticated()) {
    return Cache::mergeContexts($cache_contexts, [
      'user.roles:authenticated',
    ]);
  }
  if (\Drupal::currentUser()->hasPermission('view own unpublished content')) {
    return Cache::mergeContexts($cache_contexts, [
      'user',
    ]);
  }
  return $cache_contexts;
}

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