You are here

protected function ResourceTestBase::getExpectedCollectionResponse in Drupal 10

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

array|null $included_paths: (optional) Any include paths that should be appended to the expected response.

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()

File

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

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, array $included_paths = NULL, $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'] = [];
  }
  $cacheability = static::getExpectedCollectionCacheability($this->account, $collection, NULL, $filtered);
  $cacheability
    ->setCacheMaxAge($merged_response
    ->getCacheableMetadata()
    ->getCacheMaxAge());
  $collection_response = new CacheableResourceResponse($merged_document);
  $collection_response
    ->addCacheableDependency($cacheability);
  if (is_null($included_paths)) {
    return $collection_response;
  }
  $related_responses = array_reduce($collection, function ($related_responses, EntityInterface $entity) use ($included_paths, $request_options, $self_link) {
    if (!$entity
      ->access('view', $this->account) && !$entity
      ->access('view label', $this->account)) {
      return $related_responses;
    }
    $expected_related_responses = $this
      ->getExpectedRelatedResponses($included_paths, $request_options, $entity);
    if (empty($related_responses)) {
      return $expected_related_responses;
    }
    foreach ($included_paths as $included_path) {
      $both_responses = [
        $related_responses[$included_path],
        $expected_related_responses[$included_path],
      ];
      $related_responses[$included_path] = static::toCollectionResourceResponse($both_responses, $self_link, TRUE);
    }
    return $related_responses;
  }, []);
  return static::decorateExpectedResponseForIncludedFields($collection_response, $related_responses);
}