You are here

protected static function ResourceTestBase::decorateExpectedResponseForIncludedFields in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::decorateExpectedResponseForIncludedFields()
  2. 10 core/modules/jsonapi/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::getExpectedCollectionResponse in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Returns a JSON:API collection document for the expected entities.

File

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

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());
    $expected_cacheability
      ->setCacheTags(array_values(array_diff($expected_cacheability
      ->getCacheTags(), [
      '4xx-response',
    ])));

    // If any of the related response documents had omitted items or errors,
    // we should later expect the document to have omitted items as well.
    if (!empty($related_document['errors'])) {
      static::addOmittedObject($expected_document, static::errorsToOmittedObject($related_document['errors']));
    }
    if (!empty($related_document['meta']['omitted'])) {
      static::addOmittedObject($expected_document, $related_document['meta']['omitted']);
    }
    if (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);
}