function ResourceTestBase::getExpectedRelatedResponse

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedRelatedResponse()
  2. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedRelatedResponse()
  3. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedRelatedResponse()

Builds an expected related ResourceResponse for the given field.

Parameters

string $relationship_field_name: The relationship field name for which to build an expected ResourceResponse.

array $request_options: Request options to apply.

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to get expected related resources.

Return value

\Drupal\jsonapi\ResourceResponse An expected ResourceResponse.

See also

\GuzzleHttp\ClientInterface::request()

2 calls to ResourceTestBase::getExpectedRelatedResponse()
ResourceTestBase::getExpectedRelatedResponses in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Builds an array of expected related ResourceResponses, keyed by field name.
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 1898

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedRelatedResponse($relationship_field_name, array $request_options, EntityInterface $entity) {
    // Get the relationships responses which contain resource identifiers for
    // every related resource.
    $base_resource_identifier = static::toResourceIdentifier($entity);
    $internal_name = $this->resourceType
        ->getInternalName($relationship_field_name);
    $access = AccessResult::neutral()->addCacheContexts($entity->getEntityType()
        ->isRevisionable() ? [
        'url.query_args:resourceVersion',
    ] : []);
    $access = $access->orIf(static::entityFieldAccess($entity, $internal_name, 'view', $this->account));
    if (!$access->isAllowed()) {
        $detail = 'The current user is not allowed to view this relationship.';
        if (!$entity->access('view') && $entity->access('view label') && $access instanceof AccessResultReasonInterface && empty($access->getReason())) {
            $access->setReason("The user only has authorization for the 'view label' operation.");
        }
        $via_link = Url::fromRoute(sprintf('jsonapi.%s.%s.related', $base_resource_identifier['type'], $relationship_field_name), [
            'entity' => $base_resource_identifier['id'],
        ]);
        $related_response = static::getAccessDeniedResponse($entity, $access, $via_link, $relationship_field_name, $detail, FALSE);
    }
    else {
        $self_link = static::getRelatedLink($base_resource_identifier, $relationship_field_name);
        $relationship_response = $this->getExpectedGetRelationshipResponse($relationship_field_name, $entity);
        $relationship_document = $relationship_response->getResponseData();
        // The relationships may be empty, in which case we shouldn't attempt to
        // fetch the individual identified resources.
        if (empty($relationship_document['data'])) {
            $cache_contexts = Cache::mergeContexts([
                // Cache contexts for JSON:API URL query parameters.
'url.query_args:fields',
                'url.query_args:include',
                // Drupal defaults.
'url.site',
            ], $this->entity
                ->getEntityType()
                ->isRevisionable() ? [
                'url.query_args:resourceVersion',
            ] : []);
            $cacheability = (new CacheableMetadata())->addCacheContexts($cache_contexts)
                ->addCacheTags([
                'http_response',
            ]);
            if (isset($relationship_document['errors'])) {
                $related_response = $relationship_response;
            }
            else {
                $cardinality = is_null($relationship_document['data']) ? 1 : -1;
                $related_response = (new CacheableResourceResponse(static::getEmptyCollectionResponse($cardinality, $self_link)->getResponseData()))
                    ->addCacheableDependency($cacheability);
            }
        }
        else {
            $is_to_one_relationship = static::isResourceIdentifier($relationship_document['data']);
            $resource_identifiers = $is_to_one_relationship ? [
                $relationship_document['data'],
            ] : $relationship_document['data'];
            // Remove any relationships to 'virtual' resources.
            $resource_identifiers = array_filter($resource_identifiers, function ($resource_identifier) {
                return $resource_identifier['id'] !== 'virtual';
            });
            if (!empty($resource_identifiers)) {
                $individual_responses = static::toResourceResponses($this->getResponses(static::getResourceLinks($resource_identifiers), $request_options));
                $related_response = static::toCollectionResourceResponse($individual_responses, $self_link, !$is_to_one_relationship);
            }
            else {
                $cardinality = $is_to_one_relationship ? 1 : -1;
                $related_response = static::getEmptyCollectionResponse($cardinality, $self_link);
            }
        }
        $related_response->addCacheableDependency($relationship_response->getCacheableMetadata());
    }
    return $related_response;
}

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