.../src/JsonApiComputedFieldItemList.php | 23 ++++++++-------------- tests/src/Functional/JsonApiRegressionTest.php | 17 ++++++++-------- .../Normalizer/Value/FieldNormalizerValueTest.php | 1 + 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php b/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php index ccacdc3..1c37232 100644 --- a/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php +++ b/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php @@ -3,6 +3,7 @@ namespace Drupal\jsonapi_test_computed_field; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\CacheableDependencyTrait; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Field\FieldItemList; use Drupal\Core\TypedData\ComputedItemListTrait; @@ -13,23 +14,15 @@ use Drupal\Core\TypedData\ComputedItemListTrait; class JsonApiComputedFieldItemList extends FieldItemList implements CacheableDependencyInterface { use ComputedItemListTrait; - - /** - * The cacheability metadata. - * - * @var \Drupal\Core\Cache\CacheableMetadata - */ - protected $cacheabilityMetadata; + use CacheableDependencyTrait; /** * {@inheritdoc} */ protected function computeValue() { - $this->cacheabilityMetadata = (new CacheableMetadata()) - ->setCacheContexts(['user']) - ->setCacheTags(['field:jsonapi_test_computed_field']) - ->setCacheMaxAge(8000); - + $this->cacheContexts[] = 'user'; + $this->cacheTags[] = 'field:jsonapi_test_computed_field'; + $this->cacheMaxAge = 8000; $this->list[0] = $this->createItem(0, 'jsonapi_test_computed_field'); } @@ -38,7 +31,7 @@ class JsonApiComputedFieldItemList extends FieldItemList implements CacheableDep */ public function getCacheContexts() { $this->ensureComputedValue(); - return $this->cacheabilityMetadata->getCacheContexts(); + return $this->cacheContexts; } /** @@ -46,7 +39,7 @@ class JsonApiComputedFieldItemList extends FieldItemList implements CacheableDep */ public function getCacheTags() { $this->ensureComputedValue(); - return $this->cacheabilityMetadata->getCacheTags(); + return $this->cacheTags; } /** @@ -54,7 +47,7 @@ class JsonApiComputedFieldItemList extends FieldItemList implements CacheableDep */ public function getCacheMaxAge() { $this->ensureComputedValue(); - return $this->cacheabilityMetadata->getCacheMaxAge(); + return $this->cacheMaxAge; } } diff --git a/tests/src/Functional/JsonApiRegressionTest.php b/tests/src/Functional/JsonApiRegressionTest.php index d87e771..0f45084 100644 --- a/tests/src/Functional/JsonApiRegressionTest.php +++ b/tests/src/Functional/JsonApiRegressionTest.php @@ -502,31 +502,32 @@ class JsonApiRegressionTest extends JsonApiFunctionalTestBase { } /** - * Regression test for 2997123. + * Ensures that cacheability of computed fields is bubbled. + * + * @see https://www.drupal.org/project/drupal/issues/2352009 */ - public function testAllowComputedFieldsToSetCacheablityMetaData() { + public function testNormalizedComputedFieldCacheabilityBubblingFromIssue2352009() { // Set up data model. $this->assertTrue($this->container->get('module_installer')->install(['jsonapi_test_computed_field'], TRUE), 'Installed modules.'); - - // Ensure the anonymous user can view the entity. Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('view test entity')->save(); + // Create data. $entity_test = EntityTest::create([ 'name' => 'Test Entity for Computed Field', 'type' => 'entity_test', ]); - $entity_test->save(); // Test. $url = Url::fromUri(sprintf('internal:/jsonapi/entity_test/entity_test/%s', $entity_test->uuid())); $response = $this->request('GET', $url, []); $this->assertSame(200, $response->getStatusCode()); - + // @see \Drupal\jsonapi_test_computed_field\JsonApiComputedFieldItemList::computeValue() $this->assertContains('user', explode(' ', $response->getHeader('X-Drupal-Cache-Contexts')[0])); $this->assertContains('field:jsonapi_test_computed_field', explode(' ', $response->getHeader('X-Drupal-Cache-Tags')[0])); - $this->assertSame('_MISS', $response->getHeader('X-Drupal-Cache')[0]); - $this->assertSame('_UNCACHEABLE', $response->getHeader('X-Drupal-Dynamic-Cache')[0]); + // @todo When https://www.drupal.org/project/drupal/issues/2352009 lands, we should assert that the max-age of the response is 8000. + $this->assertSame('MISS', $response->getHeader('X-Drupal-Cache')[0]); + $this->assertSame('UNCACHEABLE', $response->getHeader('X-Drupal-Dynamic-Cache')[0]); } } diff --git a/tests/src/Unit/Normalizer/Value/FieldNormalizerValueTest.php b/tests/src/Unit/Normalizer/Value/FieldNormalizerValueTest.php index 409770e..8094e7b 100644 --- a/tests/src/Unit/Normalizer/Value/FieldNormalizerValueTest.php +++ b/tests/src/Unit/Normalizer/Value/FieldNormalizerValueTest.php @@ -48,6 +48,7 @@ class FieldNormalizerValueTest extends UnitTestCase { $cacheability = new CacheableMetadata(); $cacheability->addCacheContexts(['user']); $cacheability->addCacheTags(['field:foo']); + $cacheability->setCacheMaxAge(8000); $object = new FieldNormalizerValue($cacheability, $values, $cardinality, 'attributes'); $this->assertEquals($expected, $object->rasterizeValue()); $this->assertSame(['ccfoo', 'user'], $object->getCacheContexts());