You are here

protected function ResourceTestBase::doTestRelated 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::doTestRelated()
  2. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::doTestRelated()

Performs one round of related route testing.

By putting this behavior in its own method, authorization and other variations can be done in the calling method around assertions. For example, it can be run once with an authorized user and again without one.

Parameters

array $request_options: Request options to apply.

See also

\GuzzleHttp\ClientInterface::request()

1 call to ResourceTestBase::doTestRelated()
ResourceTestBase::testRelated in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests GETing related resource of an individual resource.

File

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

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function doTestRelated(array $request_options) {
  $relationship_field_names = $this
    ->getRelationshipFieldNames($this->entity);

  // If there are no relationship fields, we can't test related routes.
  if (empty($relationship_field_names)) {
    return;
  }

  // Builds an array of expected responses, keyed by relationship field name.
  $expected_relationship_responses = $this
    ->getExpectedRelatedResponses($relationship_field_names, $request_options);

  // Fetches actual responses as an array keyed by relationship field name.
  $related_responses = $this
    ->getRelatedResponses($relationship_field_names, $request_options);
  foreach ($relationship_field_names as $relationship_field_name) {

    /* @var \Drupal\jsonapi\ResourceResponse $expected_resource_response */
    $expected_resource_response = $expected_relationship_responses[$relationship_field_name];

    /* @var \Psr\Http\Message\ResponseInterface $actual_response */
    $actual_response = $related_responses[$relationship_field_name];

    // Dynamic Page Cache miss because cache should vary based on the
    // 'include' query param.
    $expected_cacheability = $expected_resource_response
      ->getCacheableMetadata();
    $this
      ->assertResourceResponse($expected_resource_response
      ->getStatusCode(), $expected_resource_response
      ->getResponseData(), $actual_response, $expected_cacheability
      ->getCacheTags(), $expected_cacheability
      ->getCacheContexts(), FALSE, $actual_response
      ->getStatusCode() === 200 ? $expected_cacheability
      ->getCacheMaxAge() === 0 ? 'UNCACHEABLE' : 'MISS' : FALSE);
  }
}