You are here

protected function ResourceTestBase::doTestRelated in JSON:API 8

Same name and namespace in other branches
  1. 8.2 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 tests/src/Functional/ResourceTestBase.php
Tests GETing related resource of an individual resource.

File

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

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.
  $relationship_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 = $relationship_responses[$relationship_field_name];

    // @todo uncomment this assertion in https://www.drupal.org/project/jsonapi/issues/2929428
    // Dynamic Page Cache miss because cache should vary based on the
    // 'include' query param.
    // @codingStandardsIgnoreStart

    //$expected_cacheability = $expected_resource_response->getCacheableMetadata();

    //$this->assertResourceResponse(

    //  $expected_resource_response->getStatusCode(),
    //  $expected_document,
    //  $actual_response,
    //  $expected_cacheability->getCacheTags(),
    //  \Drupal::service('cache_contexts_manager')->optimizeTokens($expected_cacheability->getCacheContexts()),
    //  FALSE,
    //  $expected_cacheability->getCacheMaxAge() === 0 ? 'UNCACHEABLE' : 'MISS'

    //);

    // @codingStandardsIgnoreEnd
    $this
      ->assertSame($expected_resource_response
      ->getStatusCode(), $actual_response
      ->getStatusCode());
    $expected_document = $expected_resource_response
      ->getResponseData();
    $actual_document = Json::decode((string) $actual_response
      ->getBody());
    $this
      ->assertSameDocument($expected_document, $actual_document);
  }
}