function JsonApiDocumentTopLevelNormalizerTest::testCacheableMetadata

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testCacheableMetadata()
  2. 8.9.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testCacheableMetadata()
  3. 10 core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testCacheableMetadata()

Ensure that cacheability metadata is properly added.

@dataProvider testCacheableMetadataProvider

Parameters

\Drupal\Core\Cache\CacheableMetadata $expected_metadata: The expected cacheable metadata.

File

core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 761

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21jsonapi%21src%21Normalizer%21JsonApiDocumentTopLevelNormalizer.php/class/JsonApiDocumentTopLevelNormalizer/11.x" title="Normalizes the top-level document according to the JSON:API specification." class="local">\Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer</a> @group jsonapi @group #slow

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testCacheableMetadata(CacheableMetadata $expected_metadata) : void {
    $resource_type = $this->container
        ->get('jsonapi.resource_type.repository')
        ->get('node', 'article');
    $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
    $context = [
        'resource_type' => $resource_type,
        'account' => NULL,
    ];
    $jsonapi_doc_object = $this->getNormalizer()
        ->normalize(new JsonApiDocumentTopLevel(new ResourceObjectData([
        $resource_object,
    ], 1), new NullIncludedData(), new LinkCollection([])), 'api_json', $context);
    foreach ($expected_metadata->getCacheTags() as $tag) {
        $this->assertContains($tag, $jsonapi_doc_object->getCacheTags());
    }
    foreach ($expected_metadata->getCacheContexts() as $context) {
        $this->assertContains($context, $jsonapi_doc_object->getCacheContexts());
    }
    $this->assertSame($expected_metadata->getCacheMaxAge(), $jsonapi_doc_object->getCacheMaxAge());
}

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