You are here

public function EntitySerializationTest::testCacheableComputedField in Drupal 9

Tests normalizing cacheable computed field.

File

core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php, line 375

Class

EntitySerializationTest
Tests that entities can be serialized to supported core formats.

Namespace

Drupal\Tests\serialization\Kernel

Code

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);
}