You are here

protected function ResourceTestBase::getExpectedGetRelationshipResponse in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipResponse()
  2. 10 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\ResourceResponse The expected ResourceResponse.

3 calls to ResourceTestBase::getExpectedGetRelationshipResponse()
ResourceTestBase::doTestRelationshipGet in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Performs one round of relationship route testing.
ResourceTestBase::getExpectedRelatedResponse in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Builds an expected related ResourceResponse for the given field.
ResourceTestBase::testRevisions in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests individual and collection revisions.

File

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

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 = isset($expected_document['errors'][0]['status']) ? $expected_document['errors'][0]['status'] : 200;
  $resource_response = new ResourceResponse($expected_document, $status_code);
  $resource_response
    ->addCacheableDependency($expected_cacheability);
  return $resource_response;
}