function ResourceResponseTestTrait::getExpectedIncludedResourceResponse

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

Gets an array of expected ResourceResponses for the given include paths.

Parameters

array $include_paths: The list of relationship include paths for which to get expected data.

array $request_options: Request options to apply.

Return value

\Drupal\jsonapi\ResourceResponse The expected ResourceResponse.

See also

\GuzzleHttp\ClientInterface::request()

1 call to ResourceResponseTestTrait::getExpectedIncludedResourceResponse()
ResourceTestBase::doTestIncluded in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests included resources.

File

core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php, line 124

Class

ResourceResponseTestTrait
Utility methods for handling resource responses.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedIncludedResourceResponse(array $include_paths, array $request_options) {
    $resource_type = $this->resourceType;
    $resource_data = array_reduce($include_paths, function ($data, $path) use ($request_options, $resource_type) {
        $field_names = explode('.', $path);
        
        /** @var \Drupal\Core\Entity\EntityInterface $entity */
        $entity = $this->entity;
        $collected_responses = [];
        foreach ($field_names as $public_field_name) {
            $resource_type = $this->container
                ->get('jsonapi.resource_type.repository')
                ->get($entity->getEntityTypeId(), $entity->bundle());
            $field_name = $resource_type->getInternalName($public_field_name);
            $field_access = static::entityFieldAccess($entity, $field_name, 'view', $this->account);
            if (!$field_access->isAllowed()) {
                if (!$entity->access('view') && $entity->access('view label') && $field_access instanceof AccessResultReasonInterface && empty($field_access->getReason())) {
                    $field_access->setReason("The user only has authorization for the 'view label' operation.");
                }
                $via_link = Url::fromRoute(sprintf('jsonapi.%s.%s.related', $entity->getEntityTypeId() . '--' . $entity->bundle(), $public_field_name), [
                    'entity' => $entity->uuid(),
                ]);
                $collected_responses[] = static::getAccessDeniedResponse($entity, $field_access, $via_link, $field_name, 'The current user is not allowed to view this relationship.', $field_name);
                break;
            }
            if ($target_entity = $entity->{$field_name}->entity) {
                $target_access = static::entityAccess($target_entity, 'view', $this->account);
                if (!$target_access->isAllowed()) {
                    $target_access = static::entityAccess($target_entity, 'view label', $this->account)
                        ->addCacheableDependency($target_access);
                }
                if (!$target_access->isAllowed()) {
                    $resource_identifier = static::toResourceIdentifier($target_entity);
                    if (!static::collectionHasResourceIdentifier($resource_identifier, $data['already_checked'])) {
                        $data['already_checked'][] = $resource_identifier;
                        $via_link = Url::fromRoute(sprintf('jsonapi.%s.individual', $resource_identifier['type']), [
                            'entity' => $resource_identifier['id'],
                        ]);
                        $collected_responses[] = static::getAccessDeniedResponse($entity, $target_access, $via_link, NULL, NULL, '/data');
                    }
                    break;
                }
            }
            $psr_responses = $this->getResponses([
                static::getRelatedLink(static::toResourceIdentifier($entity), $public_field_name),
            ], $request_options);
            $collected_responses[] = static::toCollectionResourceResponse(static::toResourceResponses($psr_responses), NULL, TRUE);
            $entity = $entity->{$field_name}->entity;
        }
        if (!empty($collected_responses)) {
            $data['responses'][$path] = static::toCollectionResourceResponse($collected_responses, NULL, TRUE);
        }
        return $data;
    }, [
        'responses' => [],
        'already_checked' => [],
    ]);
    $individual_document = $this->getExpectedDocument();
    $expected_base_url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
        'entity' => $this->entity
            ->uuid(),
    ])
        ->setAbsolute();
    $include_url = clone $expected_base_url;
    $query = [
        'include' => implode(',', $include_paths),
    ];
    $include_url->setOption('query', $query);
    $individual_document['links']['self']['href'] = $include_url->toString();
    // The test entity reference field should always be present.
    if (!isset($individual_document['data']['relationships']['field_jsonapi_test_entity_ref'])) {
        if (static::$resourceTypeIsVersionable) {
            assert($this->entity instanceof RevisionableInterface);
            $version_identifier = 'id:' . $this->entity
                ->getRevisionId();
            $version_query_string = '?resourceVersion=' . urlencode($version_identifier);
        }
        else {
            $version_query_string = '';
        }
        $individual_document['data']['relationships']['field_jsonapi_test_entity_ref'] = [
            'data' => [],
            'links' => [
                'related' => [
                    'href' => $expected_base_url->toString() . '/field_jsonapi_test_entity_ref' . $version_query_string,
                ],
                'self' => [
                    'href' => $expected_base_url->toString() . '/relationships/field_jsonapi_test_entity_ref' . $version_query_string,
                ],
            ],
        ];
    }
    $basic_cacheability = (new CacheableMetadata())->addCacheTags($this->getExpectedCacheTags())
        ->addCacheContexts($this->getExpectedCacheContexts());
    return static::decorateExpectedResponseForIncludedFields(new CacheableResourceResponse($individual_document), $resource_data['responses'])->addCacheableDependency($basic_cacheability);
}

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