function 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 \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer[[api-linebreak]] @group jsonapi @group #slow
 
Namespace
Drupal\Tests\jsonapi\Kernel\NormalizerCode
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.