.../Tests/Core/Render/RendererBubblingTest.php | 62 ++++++++++++++++++++-- .../Drupal/Tests/Core/Render/RendererTestBase.php | 8 ++- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php index 56e17fb..405c4ef 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Render; +use Drupal\Core\Cache\MemoryBackend; use Drupal\Core\KeyValueStore\KeyValueMemoryFactory; use Drupal\Core\Render\Element; use Drupal\Core\Render\Renderer; @@ -28,15 +29,15 @@ protected function setUp() { $this->rendererConfig['required_cache_contexts'] = []; parent::setUp(); - - $this->setUpRequest(); - $this->setupMemoryCache(); } /** * Tests bubbling of assets when NOT using #pre_render callbacks. */ public function testBubblingWithoutPreRender() { + $this->setUpRequest(); + $this->setupMemoryCache(); + $this->elementInfo->expects($this->any()) ->method('getInfo') ->willReturn([]); @@ -77,6 +78,58 @@ public function testBubblingWithoutPreRender() { } /** + * Tests cache context bubbling with a custom cache bin. + */ + public function testContextBubblingCustomCacheBin() { + $bin = $this->randomMachineName(); + + $this->setUpRequest(); + $this->memoryCache = new MemoryBackend('render'); + $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->cacheContextsManager->expects($this->any()) + ->method('convertTokensToKeys') + ->willReturnArgument(0); + + $build = [ + '#cache' => [ + 'keys' => ['parent'], + 'contexts' => ['foo'], + 'bin' => $bin, + ], + '#markup' => 'parent', + 'child' => [ + '#cache' => [ + 'contexts' => ['bar'], + 'max-age' => 3600, + ], + ], + ]; + $this->renderer->render($build); + + $this->assertRenderCacheItem('parent:foo', [ + '#cache_redirect' => TRUE, + '#cache' => [ + 'keys' => ['parent'], + 'contexts' => ['bar', 'foo'], + 'tags' => [], + 'bin' => $bin, + ], + ], $bin); + } + + /** * Tests cache context bubbling in edge cases, because it affects the CID. * * ::testBubblingWithPrerender() already tests the common case. @@ -440,6 +493,9 @@ public function testConditionalCacheContextBubblingSelfHealing() { * @dataProvider providerTestBubblingWithPrerender */ public function testBubblingWithPrerender($test_element) { + $this->setUpRequest(); + $this->setupMemoryCache(); + // Mock the State service. $memory_state = new State(new KeyValueMemoryFactory());; \Drupal::getContainer()->set('state', $memory_state); diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php index 0a8a28a..19772a3 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php @@ -172,6 +172,7 @@ protected function setupMemoryCache() { $this->cacheFactory->expects($this->atLeastOnce()) ->method('get') + ->with('render') ->willReturn($this->memoryCache); } @@ -195,9 +196,12 @@ protected function setUpRequest($method = 'GET') { * The expected cache ID. * @param mixed $data * The expected data for that cache ID. + * @param string $bin + * The expected cache bin. */ - protected function assertRenderCacheItem($cid, $data) { - $cached = $this->memoryCache->get($cid); + protected function assertRenderCacheItem($cid, $data, $bin = 'render') { + $cache_backend = $this->cacheFactory->get($bin); + $cached = $cache_backend->get($cid); $this->assertNotFalse($cached, sprintf('Expected cache item "%s" exists.', $cid)); if ($cached !== FALSE) { $this->assertEquals($data, $cached->data, sprintf('Cache item "%s" has the expected data.', $cid));