You are here

protected function NodeTest::assertCacheableNormalizations in Drupal 8

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

Asserts that normalizations are cached in an incremental way.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to NodeTest::assertCacheableNormalizations()
NodeTest::testGetIndividual in core/modules/jsonapi/tests/src/Functional/NodeTest.php
Tests GETting an individual resource, plus edge cases to ensure good DX.

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function assertCacheableNormalizations() {

  // Save the entity to invalidate caches.
  $this->entity
    ->save();
  $uuid = $this->entity
    ->uuid();
  $cache = \Drupal::service('render_cache')
    ->get([
    '#cache' => [
      'keys' => [
        'node--camelids',
        $uuid,
      ],
      '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',
  ]);
}