.../Tests/Core/Render/RendererBubblingTest.php | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php index 173cf8d..2d49252 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php @@ -554,6 +554,100 @@ public function testConditionalCacheContextBubblingSelfHealing() { } /** + * Tests the cache tag for a cache redirect. + * + * This cache tag must be present on: + * - the redirecting cache item (source) + * - the redirected cache item (destination) + * - any render array whose subtree contains the redirect + * + * This is a unit test, for an integration test, see + * testConditionalCacheContextBubblingSelfHealing(). + */ + public function testCacheRedirectCacheTag() { + $bin = $this->randomMachineName(); + + $this->setUpRequest(); + $custom_cache = new MemoryBackend($bin); + $this->cacheFactory->expects($this->atLeastOnce()) + ->method('get') + ->with($bin) + ->willReturnCallback(function($requested_bin) use ($bin, $custom_cache) { + if ($requested_bin === $bin) { + return $custom_cache; + } + else { + throw new \Exception(); + } + }); + + $this->elementInfo->expects($this->any()) + ->method('getInfo') + ->willReturn([]); + + // Create a test render tree with a child and subchild. The subchild + // specifies an additional cache context that bubbles and causes a cache + // redirect. + $test_build = [ + '#type' => 'container', + '#cache' => [ + 'keys' => ['parent'], + 'contexts' => ['a'], + 'bin' => $bin, + ], + '#title' => 'Parent', + ]; + $test_build['child'] = [ + '#type' => 'container', + '#title' => 'Child', + ]; + $test_build['child']['subchild'] = [ + '#cache' => [ + 'contexts' => ['b'], + ], + '#markup' => 'Varies by B.', + ]; + $expected_cache_redirect_cache_tag = 'render_cache_redirect:' . hash('sha1', serialize([$test_build['#cache']['keys'], $test_build['#cache']['contexts'], $test_build['#cache']['bin']])); + + // Render the tree. + $build = $test_build; + $output = $this->renderer->renderRoot($build); + $this->assertEquals('Varies by B.', $output); + // First, assert that the expected cache redirect exists, and it must + // include the cache redirect cache tag. + $this->assertRenderCacheItem('parent:a', [ + '#cache_redirect' => TRUE, + '#cache' => [ + 'keys' => ['parent'], + 'contexts' => ['a', 'b'], + 'tags' => [$expected_cache_redirect_cache_tag], + 'bin' => $bin, + 'max-age' => Cache::PERMANENT, + ], + ], $bin); + // Second, assert that the expected redirect destination exists, and it must + // include the cache redirect cache tag. + $this->assertRenderCacheItem('parent:a:b', [ + '#cache' => [ + 'contexts' => ['a', 'b'], + 'tags' => [$expected_cache_redirect_cache_tag], + 'max-age' => Cache::PERMANENT, + ], + '#markup' => 'Varies by B.', + '#attached' => [], + ], $bin); + // Third, assert that $build is rendered as expected, and it must include + // the cache redirect cache tag. + $this->assertSame([ + 'keys' => ['parent'], + 'contexts' => ['a', 'b'], + 'bin' => $bin, + 'tags' => [$expected_cache_redirect_cache_tag], + 'max-age' => Cache::PERMANENT, + ], $build['#cache']); + } + + /** * Tests bubbling of bubbleable metadata added by #pre_render callbacks. * * @dataProvider providerTestBubblingWithPrerender