You are here

protected function ResourceTestBase::getExpectedCollectionResponse in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedCollectionResponse()

Returns a JSON API collection document for the expected entities.

Parameters

\Drupal\Core\Entity\EntityInterface[] $collection: The entities for the collection.

string $self_link: The self link for the collection response document.

array $request_options: Request options to apply.

bool $filtered: Whether the collection is filtered or not.

Return value

\Drupal\jsonapi\ResourceResponse A ResourceResponse for the expected entity collection.

See also

\GuzzleHttp\ClientInterface::request()

1 call to ResourceTestBase::getExpectedCollectionResponse()
ResourceTestBase::testCollection in tests/src/Functional/ResourceTestBase.php
Tests GETting a collection of resources.

File

tests/src/Functional/ResourceTestBase.php, line 1216

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedCollectionResponse(array $collection, $self_link, array $request_options, $filtered = FALSE) {
  $resource_identifiers = array_map([
    static::class,
    'toResourceIdentifier',
  ], $collection);
  $individual_responses = static::toResourceResponses($this
    ->getResponses(static::getResourceLinks($resource_identifiers), $request_options));
  $merged_response = static::toCollectionResourceResponse($individual_responses, $self_link, TRUE);
  $merged_document = $merged_response
    ->getResponseData();
  if (!isset($merged_document['data'])) {
    $merged_document['data'] = [];
  }

  // @todo remove this loop in https://www.drupal.org/project/jsonapi/issues/2853066.
  if (!empty($merged_document['meta']['errors'])) {
    foreach ($merged_document['meta']['errors'] as $index => $error) {
      $merged_document['meta']['errors'][$index]['source']['pointer'] = '/data';
    }
  }
  $cacheability = static::getExpectedCollectionCacheability($collection, NULL, $this->account, $filtered);
  $cacheability
    ->setCacheMaxAge($merged_response
    ->getCacheableMetadata()
    ->getCacheMaxAge());
  $collection_response = ResourceResponse::create($merged_document);
  $collection_response
    ->addCacheableDependency($cacheability);
  return $collection_response;
}