function EntitySerializationTest::testCacheableComputedField
Tests normalizing cacheable computed field.
File
- 
              core/modules/ serialization/ tests/ src/ Kernel/ EntitySerializationTest.php, line 379 
Class
- EntitySerializationTest
- Tests that entities can be serialized to supported core formats.
Namespace
Drupal\Tests\serialization\KernelCode
public function testCacheableComputedField() {
  $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY] = new CacheableMetadata();
  $entity = EntityTestComputedField::create();
  $normalized = $this->serializer
    ->normalize($entity, NULL, $context);
  $this->assertEquals('computed test cacheable string field', $normalized['computed_test_cacheable_string_field'][0]['value']);
  $this->assertInstanceOf(CacheableDependencyInterface::class, $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
  // See \Drupal\entity_test\Plugin\Field\ComputedTestCacheableStringItemList::computeValue().
  $this->assertEquals($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->getCacheContexts(), [
    'url.query_args:computed_test_cacheable_string_field',
  ]);
  $this->assertEquals($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->getCacheTags(), [
    'field:computed_test_cacheable_string_field',
  ]);
  $this->assertEquals($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->getCacheMaxAge(), 800);
}Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
