You are here

protected function ResourceTestBase::doTestSparseFieldSets in JSON:API 8

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

Tests sparse field sets.

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::doTestSparseFieldSets()
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 2517

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function doTestSparseFieldSets(Url $url, array $request_options) {
  $field_sets = $this
    ->getSparseFieldSets();
  $expected_cacheability = new CacheableMetadata();
  foreach ($field_sets as $type => $field_set) {
    if ($type === 'all') {
      assert($this
        ->getExpectedCacheTags($field_set) === $this
        ->getExpectedCacheTags());
      assert($this
        ->getExpectedCacheContexts($field_set) === $this
        ->getExpectedCacheContexts());
    }
    $query = [
      'fields[' . static::$resourceTypeName . ']' => implode(',', $field_set),
    ];
    $expected_document = $this
      ->getExpectedDocument();
    $expected_cacheability
      ->setCacheTags($this
      ->getExpectedCacheTags($field_set));
    $expected_cacheability
      ->setCacheContexts($this
      ->getExpectedCacheContexts($field_set));

    // This tests sparse field sets on included entities.
    if (strpos($type, 'nested') === 0) {
      $this
        ->grantPermissionsToTestedRole([
        'access user profiles',
      ]);
      $query['fields[user--user]'] = implode(',', $field_set);
      $query['include'] = 'uid';
      $owner = $this->entity
        ->getOwner();
      $owner_resource = static::toResourceIdentifier($owner);
      foreach ($field_set as $field_name) {
        $owner_resource['attributes'][$field_name] = $owner
          ->get($field_name)[0]
          ->get('value')
          ->getCastedValue();
      }
      $owner_resource['links']['self'] = static::getResourceLink($owner_resource);
      $expected_document['included'] = [
        $owner_resource,
      ];
      $expected_cacheability
        ->addCacheableDependency($owner);
      $expected_cacheability
        ->addCacheableDependency(static::entityAccess($owner, 'view', $this->account));
    }

    // Remove fields not in the sparse field set.
    foreach ([
      'attributes',
      'relationships',
    ] as $member) {
      if (!empty($expected_document['data'][$member])) {
        $remaining = array_intersect_key($expected_document['data'][$member], array_flip($field_set));
        if (empty($remaining)) {
          unset($expected_document['data'][$member]);
        }
        else {
          $expected_document['data'][$member] = $remaining;
        }
      }
    }
    $url
      ->setOption('query', $query);

    // 'self' link should include the 'fields' query param.
    $expected_document['links']['self'] = $url
      ->setAbsolute()
      ->toString();
    $response = $this
      ->request('GET', $url, $request_options);

    // Dynamic Page Cache miss because cache should vary based on the 'field'
    // query param.
    $this
      ->assertResourceResponse(200, $expected_document, $response, $expected_cacheability
      ->getCacheTags(), $expected_cacheability
      ->getCacheContexts(), FALSE, 'MISS');
  }

  // Test Dynamic Page Cache hit for a query with the same field set.
  $response = $this
    ->request('GET', $url, $request_options);
  $this
    ->assertResourceResponse(200, FALSE, $response, $expected_cacheability
    ->getCacheTags(), $expected_cacheability
    ->getCacheContexts(), FALSE, 'HIT');
}