You are here

protected function ResourceTestBase::getExpectedRelatedResponses in JSON:API 8

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

Builds an array of expected related ResourceResponses, keyed by field name.

Parameters

array $relationship_field_names: The relationship field names for which to build expected ResourceResponses.

array $request_options: Request options to apply.

\Drupal\Core\Entity\EntityInterface|null $entity: (optional) The entity for which to get expected related resources.

Return value

mixed An array of expected ResourceResponses, keyed by thier relationship field name.

See also

\GuzzleHttp\ClientInterface::request()

3 calls to ResourceTestBase::getExpectedRelatedResponses()
ResourceTestBase::doTestRelated in tests/src/Functional/ResourceTestBase.php
Performs one round of related route testing.
ResourceTestBase::testCollection in tests/src/Functional/ResourceTestBase.php
Tests GETting a collection of resources.
TermTest::getExpectedRelatedResponses in tests/src/Functional/TermTest.php
Builds an array of expected related ResourceResponses, keyed by field name.
1 method overrides ResourceTestBase::getExpectedRelatedResponses()
TermTest::getExpectedRelatedResponses in tests/src/Functional/TermTest.php
Builds an array of expected related ResourceResponses, keyed by field name.

File

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

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedRelatedResponses(array $relationship_field_names, array $request_options, EntityInterface $entity = NULL) {
  $entity = $entity ?: $this->entity;

  // Get the relationships responses which contain resource identifiers for
  // every related resource.
  $relationship_responses = array_map(function ($relationship_field_name) use ($entity) {
    return $this
      ->getExpectedGetRelationshipResponse($relationship_field_name, $entity);
  }, array_combine($relationship_field_names, $relationship_field_names));
  $base_resource_identifier = static::toResourceIdentifier($entity);
  $expected_related_responses = [];
  foreach ($relationship_field_names as $relationship_field_name) {
    $access = static::entityFieldAccess($entity, $relationship_field_name, 'view', $this->account);
    if (!$access
      ->isAllowed()) {
      $detail = 'The current user is not allowed to view this relationship.';
      if ($access instanceof AccessResultReasonInterface && ($reason = $access
        ->getReason())) {
        $detail .= ' ' . $reason;
      }
      $related_response = (new ResourceResponse([
        'errors' => [
          [
            'status' => 403,
            'title' => 'Forbidden',
            'detail' => $detail,
            'links' => [
              'info' => HttpExceptionNormalizer::getInfoUrl(403),
            ],
            'code' => 0,
            'id' => '/' . $base_resource_identifier['type'] . '/' . $base_resource_identifier['id'],
            'source' => [
              'pointer' => $relationship_field_name,
            ],
          ],
        ],
      ], 403))
        ->addCacheableDependency($access);
    }
    else {
      $self_link = static::getRelatedLink($base_resource_identifier, $relationship_field_name);
      $relationship_response = $relationship_responses[$relationship_field_name];
      $relationship_document = $relationship_response
        ->getResponseData();

      // The relationships may be empty, in which case we shouldn't attempt to
      // fetch the individual identified resources.
      if (empty($relationship_document['data'])) {
        $related_response = isset($relationship_document['errors']) ? $relationship_response : new ResourceResponse([
          // Empty to-one relationships should be NULL and empty to-many
          // relationships should be an empty array.
          'data' => is_null($relationship_document['data']) ? NULL : [],
          'jsonapi' => [
            'meta' => [
              'links' => [
                'self' => 'http://jsonapi.org/format/1.0/',
              ],
            ],
            'version' => '1.0',
          ],
          'links' => [
            'self' => $self_link,
          ],
        ]);
      }
      else {
        $is_to_one_relationship = static::isResourceIdentifier($relationship_document['data']);
        $resource_identifiers = $is_to_one_relationship ? [
          $relationship_document['data'],
        ] : $relationship_document['data'];
        $individual_responses = static::toResourceResponses($this
          ->getResponses(static::getResourceLinks($resource_identifiers), $request_options));
        $related_response = static::toCollectionResourceResponse($individual_responses, $self_link, !$is_to_one_relationship);
      }
    }
    $expected_related_responses[$relationship_field_name] = $related_response;
  }
  return $expected_related_responses ?: [];
}