.../src/JsonApiComputedFieldItemList.php | 2 +- tests/src/Functional/EntityTestTest.php | 28 +++++++++++++++++++++- .../Serializer/ComputedFieldSerializerTest.php | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php b/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php index 9970342..2f21edf 100644 --- a/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php +++ b/tests/modules/jsonapi_test_computed_field/src/JsonApiComputedFieldItemList.php @@ -20,7 +20,7 @@ class JsonApiComputedFieldItemList extends FieldItemList { /** @var \Drupal\jsonapi_test_computed_field\Plugin\Field\FieldType\CacheableStringItem $item */ $item = $this->createItem(0, 'jsonapi_test_computed_field'); $cacheability = (new CacheableMetadata()) - ->setCacheContexts(['user']) + ->setCacheContexts(['url.query_args:jsonapi_test_computed_field']) ->setCacheTags(['field:jsonapi_test_computed_field']) ->setCacheMaxAge(800); $item->get('value')->setCacheability($cacheability); diff --git a/tests/src/Functional/EntityTestTest.php b/tests/src/Functional/EntityTestTest.php index 49a031b..730e8c0 100644 --- a/tests/src/Functional/EntityTestTest.php +++ b/tests/src/Functional/EntityTestTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\jsonapi\Functional; +use Drupal\Core\Cache\Cache; use Drupal\Core\Url; use Drupal\entity_test\Entity\EntityTest; use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait; @@ -19,7 +20,7 @@ class EntityTestTest extends ResourceTestBase { /** * {@inheritdoc} */ - public static $modules = ['entity_test']; + public static $modules = ['entity_test', 'jsonapi_test_computed_field']; /** * {@inheritdoc} @@ -115,6 +116,7 @@ class EntityTestTest extends ResourceTestBase { 'attributes' => [ 'created' => (new \DateTime())->setTimestamp($this->entity->get('created')->value)->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339), 'field_test_text' => NULL, + 'jsonapi_test_computed_field' => 'jsonapi_test_computed_field', 'langcode' => 'en', 'name' => 'Llama', 'entity_test_type' => 'entity_test', @@ -178,4 +180,28 @@ class EntityTestTest extends ResourceTestBase { ])); } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheTags(array $sparse_fieldset = NULL) { + $tags = parent::getExpectedCacheTags($sparse_fieldset); + // @see jsonapi_test_computed_field_entity_base_field_info() + if ($sparse_fieldset === NULL || in_array('jsonapi_test_computed_field', $sparse_fieldset)) { + $tags = Cache::mergeTags($tags, ['field:jsonapi_test_computed_field']); + } + return $tags; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) { + $contexts = parent::getExpectedCacheContexts($sparse_fieldset); + // @see jsonapi_test_computed_field_entity_base_field_info() + if ($sparse_fieldset === NULL || in_array('jsonapi_test_computed_field', $sparse_fieldset)) { + $contexts = Cache::mergeContexts($contexts, ['url.query_args:jsonapi_test_computed_field']); + } + return $contexts; + } + } diff --git a/tests/src/Kernel/Serializer/ComputedFieldSerializerTest.php b/tests/src/Kernel/Serializer/ComputedFieldSerializerTest.php index 11da5e7..66633fb 100644 --- a/tests/src/Kernel/Serializer/ComputedFieldSerializerTest.php +++ b/tests/src/Kernel/Serializer/ComputedFieldSerializerTest.php @@ -75,7 +75,7 @@ class ComputedFieldSerializerTest extends JsonapiKernelTestBase { $context = ['account' => $this->user]; $value = $this->sut->normalize($this->entity, 'api_json', $context); $this->assertInstanceOf(EntityNormalizerValue::class, $value); - $this->assertContains('user', $value->getCacheContexts()); + $this->assertContains('url.query_args:jsonapi_test_computed_field', $value->getCacheContexts()); $this->assertContains('field:jsonapi_test_computed_field', $value->getCacheTags()); $this->assertSame(800, $value->getCacheMaxAge()); }