You are here

protected function ResourceTestBase::doTestIncluded in JSON:API 8

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

Tests included resources.

Parameters

\Drupal\Core\Url $url: The base URL with which to test includes.

array $request_options: Request options to apply.

See also

\GuzzleHttp\ClientInterface::request()

1 call to ResourceTestBase::doTestIncluded()
ResourceTestBase::testGetIndividual in tests/src/Functional/ResourceTestBase.php
Tests GETting an individual resource, plus edge cases to ensure good DX.

File

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

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function doTestIncluded(Url $url, array $request_options) {
  $relationship_field_names = $this
    ->getRelationshipFieldNames($this->entity);

  // If there are no relationship fields, we can't include anything.
  if (empty($relationship_field_names)) {
    return;
  }
  $field_sets = [
    'empty' => [],
    'all' => $relationship_field_names,
  ];
  if (count($relationship_field_names) > 1) {
    $about_half_the_fields = floor(count($relationship_field_names) / 2);
    $field_sets['some'] = array_slice($relationship_field_names, $about_half_the_fields);
  }
  $nested_includes = $this
    ->getNestedIncludePaths();
  if (!empty($nested_includes)) {
    $field_sets['nested'] = $nested_includes;
  }
  foreach ($field_sets as $type => $included_paths) {
    foreach (array_intersect_key(static::getIncludePermissions(), array_flip($included_paths)) as $permissions) {
      $this
        ->grantPermissionsToTestedRole($permissions);
    }
    $expected_response = $this
      ->getExpectedIncludeResponse($included_paths, $request_options);
    $query = [
      'include' => implode(',', $included_paths),
    ];
    $url
      ->setOption('query', $query);
    $actual_response = $this
      ->request('GET', $url, $request_options);
    $response_document = Json::decode((string) $actual_response
      ->getBody());
    $expected_document = $expected_response
      ->getResponseData();

    // @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_response->getCacheableMetadata();
    // $this->assertResourceResponse(
    //   200,
    //   FALSE,
    //   $actual_response,
    //   $expected_cacheability->getCacheTags(),
    //   \Drupal::service('cache_contexts_manager')->optimizeTokens($expected_cacheability->getCacheContexts()),
    //   FALSE,
    //   $expected_cacheability->getCacheMaxAge() === 0 ? 'UNCACHEABLE' : 'MISS'
    // );
    // @codingStandardsIgnoreEnd
    $this
      ->assertSameDocument($expected_document, $response_document);
  }
}