function NodeTest::assertCacheableNormalizations

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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

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 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',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.