You are here

protected function NodeTest::assertCacheableNormalizations in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::assertCacheableNormalizations()
  2. 9 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::assertCacheableNormalizations()

Asserts that normalizations are cached in an incremental way.

@internal

Throws

\Drupal\Core\Entity\EntityStorageException

File

core/modules/jsonapi/tests/src/Functional/NodeTest.php, line 377

Class

NodeTest
JSON:API integration test for the "Node" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function assertCacheableNormalizations() : void {

  // Save the entity to invalidate caches.
  $this->entity
    ->save();
  $uuid = $this->entity
    ->uuid();
  $language = $this->entity
    ->language()
    ->getId();
  $cache = \Drupal::service('render_cache')
    ->get([
    '#cache' => [
      'keys' => [
        'node--camelids',
        $uuid,
        $language,
      ],
      'bin' => 'jsonapi_normalizations',
    ],
  ]);

  // After saving the entity the normalization should not be cached.
  $this
    ->assertFalse($cache);

  // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.
  $url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
    'entity' => $uuid,
  ]);

  // $url = $this->entity->toUrl('jsonapi');
  $request_options = $this
    ->getAuthenticationRequestOptions();
  $request_options[RequestOptions::QUERY] = [
    'fields' => [
      'node--camelids' => 'title',
    ],
  ];
  $this
    ->request('GET', $url, $request_options);

  // Ensure the normalization cache is being incrementally built. After
  // requesting the title, only the title is in the cache.
  $this
    ->assertNormalizedFieldsAreCached([
    'title',
  ]);
  $request_options[RequestOptions::QUERY] = [
    'fields' => [
      'node--camelids' => 'field_rest_test',
    ],
  ];
  $this
    ->request('GET', $url, $request_options);

  // After requesting an additional field, then that field is in the cache and
  // the old one is still there.
  $this
    ->assertNormalizedFieldsAreCached([
    'title',
    'field_rest_test',
  ]);
}