core/lib/Drupal/Core/Render/HtmlResponse.php | 2 +-
core/lib/Drupal/Core/Render/RenderCache.php | 14 +++----
.../Tests/Core/Render/RendererBubblingTest.php | 45 +++++++++++++++++++++-
3 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/core/lib/Drupal/Core/Render/HtmlResponse.php b/core/lib/Drupal/Core/Render/HtmlResponse.php
index c5339d6..7f9ab4d 100644
--- a/core/lib/Drupal/Core/Render/HtmlResponse.php
+++ b/core/lib/Drupal/Core/Render/HtmlResponse.php
@@ -42,6 +42,6 @@ public function setContent($content) {
$content = $content['#markup'];
}
- parent::setContent($content);
+ return parent::setContent($content);
}
}
diff --git a/core/lib/Drupal/Core/Render/RenderCache.php b/core/lib/Drupal/Core/Render/RenderCache.php
index abd37dc..4f35500 100644
--- a/core/lib/Drupal/Core/Render/RenderCache.php
+++ b/core/lib/Drupal/Core/Render/RenderCache.php
@@ -103,11 +103,9 @@ public function set(array &$elements, array $pre_bubbling_elements) {
* Helper for ::set().
*
* @param array $elements
- * Unlike ::set(), this is not passed by reference, which means side-effects
- * do not exist anymore here; they're isolated to set().
* @param array $pre_bubbling_elements
*/
- protected function doSet(array $elements, array $pre_bubbling_elements) {
+ protected function doSet(array &$elements, array $pre_bubbling_elements) {
$cid = $this->createCacheID($elements);
$data = $this->getCacheableRenderArray($elements);
@@ -250,7 +248,8 @@ protected function doSet(array $elements, array $pre_bubbling_elements) {
// stored in the cache redirect are discovered to be incomplete.
$redirect_cache_tag = 'render_cache_redirect:' . hash('sha1', serialize([$pre_bubbling_elements['#cache']['keys'], $pre_bubbling_elements['#cache']['contexts'], $bin]));
$redirect_cacheability_updated->addCacheTags([$redirect_cache_tag]);
- $data['#cache']['tags'] = Cache::mergeTags($data['#cache']['tags'], [$redirect_cache_tag]);
+ $elements['#cache']['tags'] = Cache::mergeTags($elements['#cache']['tags'], [$redirect_cache_tag]);
+ $data = $this->getCacheableRenderArray($elements);
// Stored cache contexts incomplete: this request causes cache contexts to
// be added to the redirecting cache item.
@@ -285,11 +284,10 @@ protected function doSet(array $elements, array $pre_bubbling_elements) {
if (array_diff($redirect_cacheability_updated->getCacheContexts(), $data['#cache']['contexts'])) {
// Ensure the about-to-be-cached data uses the merged cache contexts and
// the redirect cache tag.
- $data['#cache']['keys'] = $elements['#cache']['keys'];
- $data['#cache']['contexts'] = $redirect_cacheability_updated->getCacheContexts();
+ $elements['#cache']['contexts'] = $redirect_cacheability_updated->getCacheContexts();
+ $data = $this->getCacheableRenderArray($elements);
// Recalculate the cache ID.
- $cid = $this->createCacheID($data);
- $data = $this->getCacheableRenderArray($data);
+ $cid = $this->createCacheID($elements);
}
}
$cache->set($cid, $data, $this->maxAgeToExpire($elements['#cache']['max-age']), Cache::mergeTags($data['#cache']['tags'], ['rendered']));
diff --git a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php
index 7a4c77a..173cf8d 100644
--- a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php
@@ -10,6 +10,7 @@
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\Render\Element;
+use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Render\Renderer;
use Drupal\Core\State\State;
use Drupal\Core\Cache\Cache;
@@ -307,6 +308,9 @@ public function providerTestContextBubblingEdgeCases() {
/**
* Tests the self-healing of the redirect with conditional cache contexts.
+ *
+ * The cached response portions of this test prove why cache redirects need
+ * cache tags.
*/
public function testConditionalCacheContextBubblingSelfHealing() {
$current_user_role = &$this->currentUserRole;
@@ -314,11 +318,17 @@ public function testConditionalCacheContextBubblingSelfHealing() {
$this->setUpRequest();
$this->setupMemoryCache();
+ // A mocked response cache, to ensure
+ $response_cache = new MemoryBackend('response');
+
// The cache redirect will be updated three times, and hence its cache tag
// will also be invalidated three times.
$this->cacheTagsInvalidator->expects($this->exactly(3))
->method('invalidateTags')
- ->with(['render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f']);
+ ->with(['render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'])
+ ->willReturnCallback(function () use ($response_cache) {
+ $response_cache->invalidateTags(['render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f']);
+ });
$test_element = [
'#cache' => [
@@ -358,11 +368,28 @@ public function testConditionalCacheContextBubblingSelfHealing() {
],
];
+ $store_in_response_cache = function ($element) use (&$current_user_role, &$response_cache) {
+ if ($current_user_role === 'A') {
+ /** @var \Drupal\Core\Render\HtmlResponse $response */
+ $response = (new HtmlResponse())->setContent($element);
+ $response_cache->set('the_cached_response_for_role_A', $response, $response->getCacheableMetadata()->getCacheMaxAge(), $response->getCacheableMetadata()->getCacheTags());
+ }
+ };
+
+ $assert_cached_response = function (array $bubbled_cache_contexts_from_redirect) use (&$response_cache) {
+ $cached = $response_cache->get('the_cached_response_for_role_A');
+ $this->assertNotFalse($cached);
+ $cached_response = $cached->data;
+ $this->assertEquals($cached_response->getContent(), 'parent');
+ $this->assertEquals($cached_response->getCacheableMetadata()->getCacheContexts(), Cache::mergeContexts(['user.roles'], $bubbled_cache_contexts_from_redirect));
+ };
+
// Request 1: role A, the grandchild isn't accessible => bubbled cache
// contexts: user.roles.
$element = $test_element;
$current_user_role = 'A';
$this->renderer->renderRoot($element);
+ $store_in_response_cache($element);
$this->assertRenderCacheItem('parent', [
'#cache_redirect' => TRUE,
'#cache' => [
@@ -382,12 +409,14 @@ public function testConditionalCacheContextBubblingSelfHealing() {
],
'#markup' => 'parent',
]);
+ $assert_cached_response([]);
// Request 2: role B, the grandchild is accessible => bubbled cache
// contexts: foo, user.roles + merged max-age: 1800.
$element = $test_element;
$current_user_role = 'B';
$this->renderer->renderRoot($element);
+ $store_in_response_cache($element);
$this->assertRenderCacheItem('parent', [
'#cache_redirect' => TRUE,
'#cache' => [
@@ -407,6 +436,10 @@ public function testConditionalCacheContextBubblingSelfHealing() {
],
'#markup' => 'parent',
]);
+ // Despite this not being for role A, the cached response is still
+ // invalidated, because the set of cache contexts for the cache redirect has
+ // changed.
+ $this->assertFalse($response_cache->get('the_cached_response_for_role_A'));
// Request 3: role A again, the grandchild is inaccessible again => bubbled
// cache contexts: user.roles; but that's a subset of the already-bubbled
@@ -421,6 +454,7 @@ public function testConditionalCacheContextBubblingSelfHealing() {
$element = $test_element;
$current_user_role = 'A';
$this->renderer->renderRoot($element);
+ $store_in_response_cache($element);
$this->assertRenderCacheItem('parent', [
'#cache_redirect' => TRUE,
'#cache' => [
@@ -443,6 +477,7 @@ public function testConditionalCacheContextBubblingSelfHealing() {
],
'#markup' => 'parent',
]);
+ $assert_cached_response(['foo']);
// Request 4: role C, both the grandchild and the grandgrandchild are
// accessible => bubbled cache contexts: foo, bar, user.roles + merged
@@ -450,6 +485,7 @@ public function testConditionalCacheContextBubblingSelfHealing() {
$element = $test_element;
$current_user_role = 'C';
$this->renderer->renderRoot($element);
+ $store_in_response_cache($element);
$final_parent_cache_item = [
'#cache_redirect' => TRUE,
'#cache' => [
@@ -470,11 +506,16 @@ public function testConditionalCacheContextBubblingSelfHealing() {
],
'#markup' => 'parent',
]);
+ // Despite this not being for role A, the cached response is still
+ // invalidated, because the set of cache contexts for the cache redirect has
+ // changed.
+ $this->assertFalse($response_cache->get('the_cached_response_for_role_A'));
// Request 5: role A again, verifying the merging like we did for request 3.
$element = $test_element;
$current_user_role = 'A';
$this->renderer->renderRoot($element);
+ $store_in_response_cache($element);
$this->assertRenderCacheItem('parent', $final_parent_cache_item);
$this->assertRenderCacheItem('parent:bar:foo:r.A', [
'#attached' => [],
@@ -489,11 +530,13 @@ public function testConditionalCacheContextBubblingSelfHealing() {
],
'#markup' => 'parent',
]);
+ $assert_cached_response(['bar', 'foo']);
// Request 6: role B again, verifying the merging like we did for request 3.
$element = $test_element;
$current_user_role = 'B';
$this->renderer->renderRoot($element);
+ $store_in_response_cache($element);
$this->assertRenderCacheItem('parent', $final_parent_cache_item);
$this->assertRenderCacheItem('parent:bar:foo:r.B', [
'#attached' => [],