You are here

protected static function ResourceTestBase::decorateExpectedResponseForIncludedFields in JSON:API 8

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

Decorates the expected response with included data and cache metadata.

This adds the expected includes to the expected document and also builds the expected cacheability for those includes. It does so based of responses from the related routes for individual relationships.

Parameters

\Drupal\jsonapi\ResourceResponse $expected_response: The expected ResourceResponse.

\Drupal\jsonapi\ResourceResponse[] $related_responses: The related ResourceResponses, keyed by relationship field names.

Return value

\Drupal\jsonapi\ResourceResponse The decorated ResourceResponse.

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

File

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

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected static function decorateExpectedResponseForIncludedFields(ResourceResponse $expected_response, array $related_responses) {
  $expected_document = $expected_response
    ->getResponseData();
  $expected_cacheability = $expected_response
    ->getCacheableMetadata();
  foreach ($related_responses as $related_response) {
    $related_document = $related_response
      ->getResponseData();
    $expected_cacheability
      ->addCacheableDependency($related_response
      ->getCacheableMetadata());
    if (!empty($related_document['errors'])) {

      // If any of the related response documents had top-level errors, we
      // should later expect the document to have 'meta' errors too.
      foreach ($related_document['errors'] as $error) {

        // @todo remove this when inaccessible relationships are able to raise errors in https://www.drupal.org/project/jsonapi/issues/2956084.
        if (strpos($error['detail'], 'The current user is not allowed to view this relationship.') !== 0) {
          unset($error['source']['pointer']);
          $expected_document['meta']['errors'][] = $error;
        }
      }
    }
    elseif (isset($related_document['data'])) {
      $related_data = $related_document['data'];
      $related_resources = static::isResourceIdentifier($related_data) ? [
        $related_data,
      ] : $related_data;
      foreach ($related_resources as $related_resource) {
        if (empty($expected_document['included']) || !static::collectionHasResourceIdentifier($related_resource, $expected_document['included'])) {
          $expected_document['included'][] = $related_resource;
        }
      }
    }
  }
  return (new ResourceResponse($expected_document))
    ->addCacheableDependency($expected_cacheability);
}