You are here

protected static function ResourceResponseTestTrait::toCollectionResourceResponse in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php \Drupal\Tests\jsonapi\Functional\ResourceResponseTestTrait::toCollectionResourceResponse()

Merges individual responses into a collection response.

Here, a collection response refers to a response with multiple resource objects. Not necessarily to a response to a collection route. In both cases, the document should indistinguishable.

Parameters

\Drupal\jsonapi\ResourceResponse[] $responses: An array or ResourceResponses to be merged.

string|null $self_link: The self link for the merged document if one should be set.

bool $is_multiple: Whether the responses are for a multiple cardinality field. This cannot be deduced from the number of responses, because a multiple cardinality field may have only one value.

Return value

\Drupal\jsonapi\CacheableResourceResponse The merged ResourceResponse.

3 calls to ResourceResponseTestTrait::toCollectionResourceResponse()
ResourceResponseTestTrait::getExpectedIncludedResourceResponse in core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php
Gets an array of expected ResourceResponses for the given include paths.
ResourceTestBase::getExpectedCollectionResponse in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Returns a JSON:API collection document for the expected entities.
ResourceTestBase::getExpectedRelatedResponse in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Builds an expected related ResourceResponse for the given field.

File

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

Class

ResourceResponseTestTrait
Utility methods for handling resource responses.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected static function toCollectionResourceResponse(array $responses, $self_link, $is_multiple) {
  assert(count($responses) > 0);
  $merged_document = [];
  $merged_cacheability = new CacheableMetadata();
  foreach ($responses as $response) {
    $response_document = $response
      ->getResponseData();

    // If any of the response documents had top-level errors, we should later
    // expect the merged document to have all errors as omitted links under
    // the 'meta.omitted' member.
    if (!empty($response_document['errors'])) {
      static::addOmittedObject($merged_document, static::errorsToOmittedObject($response_document['errors']));
    }
    if (!empty($response_document['meta']['omitted'])) {
      static::addOmittedObject($merged_document, $response_document['meta']['omitted']);
    }
    elseif (isset($response_document['data'])) {
      $response_data = $response_document['data'];
      if (!isset($merged_document['data'])) {
        $merged_document['data'] = static::isResourceIdentifier($response_data) && $is_multiple ? [
          $response_data,
        ] : $response_data;
      }
      else {
        $response_resources = static::isResourceIdentifier($response_data) ? [
          $response_data,
        ] : $response_data;
        foreach ($response_resources as $response_resource) {
          $merged_document['data'][] = $response_resource;
        }
      }
    }
    $merged_cacheability
      ->addCacheableDependency($response
      ->getCacheableMetadata());
  }
  $merged_document['jsonapi'] = [
    'meta' => [
      'links' => [
        'self' => [
          'href' => 'http://jsonapi.org/format/1.0/',
        ],
      ],
    ],
    'version' => '1.0',
  ];

  // Until we can reasonably know what caused an error, we shouldn't include
  // 'self' links in error documents. For example, a 404 shouldn't have a
  // 'self' link because HATEOAS links shouldn't point to resources which do
  // not exist.
  if (isset($merged_document['errors'])) {
    unset($merged_document['links']);
  }
  else {
    if (!isset($merged_document['data'])) {
      $merged_document['data'] = $is_multiple ? [] : NULL;
    }
    $merged_document['links'] = [
      'self' => [
        'href' => $self_link,
      ],
    ];
  }

  // All collections should be 200, without regard for the status of the
  // individual resources in those collections, which means any '4xx-response'
  // cache tags on the individual responses should also be omitted.
  $merged_cacheability
    ->setCacheTags(array_diff($merged_cacheability
    ->getCacheTags(), [
    '4xx-response',
  ]));
  return (new CacheableResourceResponse($merged_document, 200))
    ->addCacheableDependency($merged_cacheability);
}