You are here

protected function ResourceResponseTestTrait::getExpectedIncludedResourceResponse in JSON:API 8

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

Gets an array of expected ResourceResponses for the given include paths.

Parameters

array $include_paths: The list of relationship include paths for which to get expected data.

array $request_options: Request options to apply.

Return value

\Drupal\jsonapi\ResourceResponse The expected ResourceResponse.

See also

\GuzzleHttp\ClientInterface::request()

1 call to ResourceResponseTestTrait::getExpectedIncludedResourceResponse()
ResourceTestBase::getExpectedIncludeResponse in tests/src/Functional/ResourceTestBase.php
Gets an expected ResourceResponse with includes for the given field set.

File

tests/src/Functional/ResourceResponseTestTrait.php, line 145

Class

ResourceResponseTestTrait
Utility methods for handling resource responses.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedIncludedResourceResponse(array $include_paths, array $request_options) {
  $resource_data = array_reduce($include_paths, function ($data, $path) use ($request_options) {
    $field_names = explode('.', $path);
    $entity = $this->entity;
    foreach ($field_names as $field_name) {
      $collected_responses = [];
      $field_access = static::entityFieldAccess($entity, $field_name, 'view', $this->account);
      if (!$field_access
        ->isAllowed()) {
        $collected_responses[] = static::getAccessDeniedResponse($entity, $field_access, $field_name, 'The current user is not allowed to view this relationship.');
        break;
      }
      if ($target_entity = $entity->{$field_name}->entity) {
        $target_access = static::entityAccess($target_entity, 'view', $this->account);
        if (!$target_access
          ->isAllowed()) {
          $resource_identifier = static::toResourceIdentifier($target_entity);
          if (!static::collectionHasResourceIdentifier($resource_identifier, $data['already_checked'])) {
            $data['already_checked'][] = $resource_identifier;

            // @todo remove this in https://www.drupal.org/project/jsonapi/issues/2943176
            $error_id = '/' . $resource_identifier['type'] . '/' . $resource_identifier['id'];
            $collected_responses[] = static::getAccessDeniedResponse($entity, $target_access, NULL, NULL, '/data', $error_id);
          }
          break;
        }
      }
      $psr_responses = $this
        ->getResponses([
        static::getRelatedLink(static::toResourceIdentifier($entity), $field_name),
      ], $request_options);
      $collected_responses[] = static::toCollectionResourceResponse(static::toResourceResponses($psr_responses), NULL, TRUE);
      $entity = $entity->{$field_name}->entity;
    }
    if (!empty($collected_responses)) {
      $data['responses'][$path] = static::toCollectionResourceResponse($collected_responses, NULL, TRUE);
    }
    return $data;
  }, [
    'responses' => [],
    'already_checked' => [],
  ]);
  return static::toCollectionResourceResponse($resource_data['responses'], NULL, TRUE);
}