.../Comment/CommentHalJsonTestBase.php | 9 +++++ .../EntityTest/EntityTestHalJsonAnonTest.php | 9 +++++ .../EntityResource/Node/NodeHalJsonAnonTest.php | 9 +++++ .../EntityResource/Term/TermHalJsonAnonTest.php | 9 +++++ .../EntityResource/User/UserHalJsonAnonTest.php | 9 +++++ .../EntityResource/Block/BlockResourceTestBase.php | 19 ++++++++++ .../EntityResource/EntityResourceTestBase.php | 41 ++++++++++++++++++++++ 7 files changed, 105 insertions(+) diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php b/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php index 9e42f56..1d07465 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\hal\Functional\EntityResource\Comment; +use Drupal\Core\Cache\Cache; use Drupal\entity_test\Entity\EntityTest; use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait; use Drupal\Tests\rest\Functional\EntityResource\Comment\CommentResourceTestBase; @@ -130,4 +131,12 @@ protected function getNormalizedPostEntity() { ]; } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + // The 'url.site' cache context is added for '_links' in the response. + return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']); + } + } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php index 616ffc0..fd02f12 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\hal\Functional\EntityResource\EntityTest; +use Drupal\Core\Cache\Cache; use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait; use Drupal\Tests\rest\Functional\AnonResourceTestTrait; use Drupal\Tests\rest\Functional\EntityResource\EntityTest\EntityTestResourceTestBase; @@ -93,4 +94,12 @@ protected function getNormalizedPostEntity() { ]; } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + // The 'url.site' cache context is added for '_links' in the response. + return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']); + } + } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php index 72d19ae..7a7be74 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\hal\Functional\EntityResource\Node; +use Drupal\Core\Cache\Cache; use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait; use Drupal\Tests\rest\Functional\AnonResourceTestTrait; use Drupal\Tests\rest\Functional\EntityResource\Node\NodeResourceTestBase; @@ -125,4 +126,12 @@ protected function getNormalizedPostEntity() { ]; } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + // The 'url.site' cache context is added for '_links' in the response. + return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']); + } + } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php index ba7fefe..7adc742 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\hal\Functional\EntityResource\Term; +use Drupal\Core\Cache\Cache; use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait; use Drupal\Tests\rest\Functional\AnonResourceTestTrait; use Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase; @@ -67,4 +68,12 @@ protected function getNormalizedPostEntity() { ]; } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + // The 'url.site' cache context is added for '_links' in the response. + return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']); + } + } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/User/UserHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/User/UserHalJsonAnonTest.php index 3904967..278e8dc 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/User/UserHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/User/UserHalJsonAnonTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\hal\Functional\EntityResource\User; +use Drupal\Core\Cache\Cache; use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait; use Drupal\Tests\rest\Functional\AnonResourceTestTrait; use Drupal\Tests\rest\Functional\EntityResource\User\UserResourceTestBase; @@ -67,4 +68,12 @@ protected function getNormalizedPostEntity() { ]; } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + // The 'url.site' cache context is added for '_links' in the response. + return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']); + } + } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php index d32393e..ce67f83 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php @@ -108,4 +108,23 @@ protected function getNormalizedPostEntity() { // @todo Update when POSTing config entity support is added in https://www.drupal.org/node/2300677. } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + // @see ::createEntity() + return []; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheTags() { + // Because the 'user.permissions' cache context is missing, the cache tag + // for the anonymous user role is never added automatically. + return array_filter(parent::getExpectedCacheTags(), function ($tag) { + return $tag !== 'config:user.role.anonymous'; + }); + } + } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index f091331..4315577 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\rest\Functional\EntityResource; +use Drupal\Core\Cache\Cache; use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\FieldableEntityInterface; @@ -240,6 +241,36 @@ protected function getNormalizedPatchEntity() { } /** + * The expected cache tags for the GET/HEAD response of the test entity. + * + * @see ::testGet + * + * @return string[] + */ + protected function getExpectedCacheTags() { + $expected_cache_tags = [ + 'config:rest.resource.entity.' . static::$entityTypeId, + ]; + if (!static::$auth) { + $expected_cache_tags[] = 'config:user.role.anonymous'; + } + return Cache::mergeTags($expected_cache_tags, $this->entity->getCacheTags()); + } + + /** + * The expected cache contexts for the GET/HEAD response of the test entity. + * + * @see ::testGet + * + * @return string[] + */ + protected function getExpectedCacheContexts() { + return [ + 'user.permissions', + ]; + } + + /** * Tests GETting an entity, plus edge cases to ensure good DX. */ public function testGet() { @@ -334,6 +365,10 @@ public function testGet() { else { $this->assertFalse($response->hasHeader('X-Drupal-Cache')); } + $cache_tags_header_value = $response->getHeader('X-Drupal-Cache-Tags')[0]; + $this->assertEquals($this->getExpectedCacheTags(), empty($cache_tags_header_value) ? [] : explode(' ', $cache_tags_header_value)); + $cache_contexts_header_value = $response->getHeader('X-Drupal-Cache-Contexts')[0]; + $this->assertEquals($this->getExpectedCacheContexts(), empty($cache_contexts_header_value) ? [] : explode(' ', $cache_contexts_header_value)); // Comparing the exact serialization is pointless, because the order of // fields does not matter (at least not yet). That's why we only compare the // normalized entity with the decoded response: it's comparing PHP arrays @@ -567,6 +602,7 @@ public function testPost() { $response = $this->request('POST', $url, $request_options); $this->assertResourceResponse(201, FALSE, $response); $this->assertSame([str_replace($this->entity->id(), static::$firstCreatedEntityId, $this->entity->toUrl('canonical')->setAbsolute(TRUE)->toString())], $response->getHeader('Location')); + $this->assertFalse($response->hasHeader('X-Drupal-Cache')); $this->config('rest.settings')->set('bc_entity_resource_permissions', TRUE)->save(TRUE); @@ -587,6 +623,7 @@ public function testPost() { $response = $this->request('POST', $url, $request_options); $this->assertResourceResponse(201, FALSE, $response); $this->assertSame([str_replace($this->entity->id(), static::$secondCreatedEntityId, $this->entity->toUrl('canonical')->setAbsolute(TRUE)->toString())], $response->getHeader('Location')); + $this->assertFalse($response->hasHeader('X-Drupal-Cache')); } /** @@ -757,6 +794,7 @@ public function testPatch() { // 200 for well-formed request. $response = $this->request('PATCH', $url, $request_options); $this->assertResourceResponse(200, FALSE, $response); + $this->assertFalse($response->hasHeader('X-Drupal-Cache')); // Ensure that fields do not get deleted if they're not present in the PATCH // request. Test this using the configurable field that we added, but which // is not sent in the PATCH request. @@ -780,6 +818,7 @@ public function testPatch() { // 200 for well-formed request. $response = $this->request('PATCH', $url, $request_options); $this->assertResourceResponse(200, FALSE, $response); + $this->assertFalse($response->hasHeader('X-Drupal-Cache')); } /** @@ -858,6 +897,7 @@ public function testDelete() { // @todo Uncomment the following line when https://www.drupal.org/node/2821711 is fixed. // $this->assertSame(FALSE, $response->hasHeader('Content-Type')); $this->assertSame('', $response->getBody()->getContents()); + $this->assertFalse($response->hasHeader('X-Drupal-Cache')); $this->config('rest.settings')->set('bc_entity_resource_permissions', TRUE)->save(TRUE); @@ -881,6 +921,7 @@ public function testDelete() { // @todo Uncomment the following line when https://www.drupal.org/node/2821711 is fixed. // $this->assertSame(FALSE, $response->hasHeader('Content-Type')); $this->assertSame('', $response->getBody()->getContents()); + $this->assertFalse($response->hasHeader('X-Drupal-Cache')); }