You are here

public function JsonApiDocumentTopLevelNormalizerTest::testCacheableMetadata in Drupal 8

Ensure that cacheability metadata is properly added.

@dataProvider testCacheableMetadataProvider

Parameters

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

array|null $fields: Fields to include in the response, keyed by resource type.

array|null $includes: Resources paths to include in the response.

File

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

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testCacheableMetadata(CacheableMetadata $expected_metadata, $fields = NULL, $includes = NULL) {
  list($request, $resource_type) = $this
    ->generateProphecies('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);
  $this
    ->assertArraySubset($expected_metadata
    ->getCacheTags(), $jsonapi_doc_object
    ->getCacheTags());
  $this
    ->assertArraySubset($expected_metadata
    ->getCacheContexts(), $jsonapi_doc_object
    ->getCacheContexts());
  $this
    ->assertSame($expected_metadata
    ->getCacheMaxAge(), $jsonapi_doc_object
    ->getCacheMaxAge());
}