You are here

protected function ResourceTestBase::getExpectedGetRelationshipResponse 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::getExpectedGetRelationshipResponse()
  2. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipResponse()

Gets an expected ResourceResponse for the given relationship.

Parameters

string $relationship_field_name: The relationship for which to get an expected response.

\Drupal\Core\Entity\EntityInterface|null $entity: (optional) The entity for which to get expected relationship response.

Return value

\Drupal\jsonapi\CacheableResourceResponse The expected ResourceResponse.

File

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

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedGetRelationshipResponse($relationship_field_name, EntityInterface $entity = NULL) {
  $entity = $entity ?: $this->entity;
  $access = AccessResult::neutral()
    ->addCacheContexts($entity
    ->getEntityType()
    ->isRevisionable() ? [
    'url.query_args:resourceVersion',
  ] : []);
  $access = $access
    ->orIf(static::entityFieldAccess($entity, $this->resourceType
    ->getInternalName($relationship_field_name), 'view', $this->account));
  if (!$access
    ->isAllowed()) {
    $via_link = Url::fromRoute(sprintf('jsonapi.%s.%s.relationship.get', static::$resourceTypeName, $relationship_field_name), [
      'entity' => $entity
        ->uuid(),
    ]);
    return static::getAccessDeniedResponse($this->entity, $access, $via_link, $relationship_field_name, 'The current user is not allowed to view this relationship.', FALSE);
  }
  $expected_document = $this
    ->getExpectedGetRelationshipDocument($relationship_field_name, $entity);
  $expected_cacheability = (new CacheableMetadata())
    ->addCacheTags([
    'http_response',
  ])
    ->addCacheContexts([
    'url.site',
    'url.query_args:include',
    'url.query_args:fields',
  ])
    ->addCacheableDependency($entity)
    ->addCacheableDependency($access);
  $status_code = $expected_document['errors'][0]['status'] ?? 200;
  $resource_response = new CacheableResourceResponse($expected_document, $status_code);
  $resource_response
    ->addCacheableDependency($expected_cacheability);
  return $resource_response;
}