.../Tests/Core/Render/RendererPlaceholdersTest.php | 103 ++++++++++++++++++++- .../Drupal/Tests/Core/Render/RendererTestBase.php | 15 +++ 2 files changed, 114 insertions(+), 4 deletions(-) diff --git a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php index d4d7d09..d0c6589 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php @@ -155,6 +155,22 @@ public function providerPlaceholders() { '#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callbackPerUser', $args], ], ]; + // Note the absence of '#create_placeholder', presence of high-invalidation + // frequency cache tag created by the #lazy_builder callback. + // @see \Drupal\Tests\Core\Render\PlaceholdersTest::callbackTagCurrentTemperature() + $base_element_a7 = [ + '#attached' => [ + 'drupalSettings' => [ + 'foo' => 'bar', + ], + ], + 'placeholder' => [ + '#cache' => [ + 'contexts' => [], + ], + '#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callbackTagCurrentTemperature', $args], + ], + ]; // Note the absence of '#create_placeholder', but the presence of // '#attached[placeholders]'. $base_element_b = [ @@ -187,6 +203,7 @@ public function providerPlaceholders() { FALSE, [], [], + [], ]; // Case two: render array that has a placeholder that is: @@ -201,6 +218,7 @@ public function providerPlaceholders() { $expected_placeholder_render_array, $keys, [], + [], [ '#markup' => '
This is a rendered placeholder!
', '#attached' => [ @@ -228,6 +246,7 @@ public function providerPlaceholders() { FALSE, [], [], + [], ]; // Case four: render array that has a placeholder that is: @@ -243,6 +262,7 @@ public function providerPlaceholders() { FALSE, [], [], + [], ]; // Case five: render array that has a placeholder that is: @@ -258,6 +278,7 @@ public function providerPlaceholders() { FALSE, [], [], + [], ]; // Case six: render array that has a placeholder that is: @@ -277,6 +298,7 @@ public function providerPlaceholders() { $expected_placeholder_render_array, $cid_parts, [], + [], [ '#markup' => 'This is a rendered placeholder!
', '#attached' => [ @@ -305,6 +327,7 @@ public function providerPlaceholders() { FALSE, [], [], + [], ]; // Case eight: render array that has a placeholder that is: @@ -320,6 +343,7 @@ public function providerPlaceholders() { $expected_placeholder_render_array, $keys, [], + [], [ '#markup' => 'This is a rendered placeholder!
', '#attached' => [ @@ -347,6 +371,7 @@ public function providerPlaceholders() { FALSE, ['user'], [], + [], ]; // Case twelve: render array that has a placeholder that is: @@ -362,6 +387,7 @@ public function providerPlaceholders() { $expected_placeholder_render_array, $keys, ['user'], + [], [ '#markup' => 'This is a rendered placeholder!
', '#attached' => [ @@ -377,6 +403,51 @@ public function providerPlaceholders() { ], ]; + // Case thirteen: render array that has a placeholder that is: + // - automatically created, and automatically triggered due to high + // invalidation frequency cache tags that are bubbled + // - uncacheable + $element_without_cache_keys = $base_element_a7; + $expected_placeholder_render_array = $extract_placeholder_render_array($base_element_a7['placeholder']); + $cases[] = [ + $element_without_cache_keys, + $args, + $expected_placeholder_render_array, + FALSE, + [], + ['current-temperature'], + [], + ]; + + // Case fourteen: render array that has a placeholder that is: + // - automatically created, and automatically triggered due to high + // invalidation frequency cache tags that are bubbled + // - cacheable + $element_with_cache_keys = $base_element_a7; + $element_with_cache_keys['placeholder']['#cache']['keys'] = $keys; + $expected_placeholder_render_array['#cache']['keys'] = $keys; + $cases[] = [ + $element_with_cache_keys, + $args, + $expected_placeholder_render_array, + $keys, + [], + [], + [ + '#markup' => 'This is a rendered placeholder!
', + '#attached' => [ + 'drupalSettings' => [ + 'dynamic_animal' => $args[0], + ], + ], + '#cache' => [ + 'contexts' => [], + 'tags' => ['current-temperature'], + 'max-age' => Cache::PERMANENT, + ], + ], + ]; + // Case fifteen: render array that has a placeholder that is: // - manually created // - uncacheable @@ -390,6 +461,7 @@ public function providerPlaceholders() { FALSE, [], [], + [], ]; // Case sixteen: render array that has a placeholder that is: @@ -410,6 +482,7 @@ public function providerPlaceholders() { $expected_placeholder_render_array, $keys, [], + [], [ '#markup' => 'This is a rendered placeholder!
', '#attached' => [ @@ -484,7 +557,7 @@ protected function assertPlaceholderRenderCache($cid_parts, array $bubbled_cache * * @dataProvider providerPlaceholders */ - public function testUncacheableParent($element, $args, array $expected_placeholder_render_array, $placeholder_cid_parts, array $bubbled_cache_contexts, array $placeholder_expected_render_cache_array) { + public function testUncacheableParent($element, $args, array $expected_placeholder_render_array, $placeholder_cid_parts, array $bubbled_cache_contexts, array $bubbled_cache_tags, array $placeholder_expected_render_cache_array) { if ($placeholder_cid_parts) { $this->setupMemoryCache(); } @@ -516,7 +589,7 @@ public function testUncacheableParent($element, $args, array $expected_placehold * * @dataProvider providerPlaceholders */ - public function testCacheableParent($test_element, $args, array $expected_placeholder_render_array, $placeholder_cid_parts, array $bubbled_cache_contexts, array $placeholder_expected_render_cache_array) { + public function testCacheableParent($test_element, $args, array $expected_placeholder_render_array, $placeholder_cid_parts, array $bubbled_cache_contexts, array $bubbled_cache_tags, array $placeholder_expected_render_cache_array) { $element = $test_element; $this->setupMemoryCache(); @@ -556,7 +629,7 @@ public function testCacheableParent($test_element, $args, array $expected_placeh // - uncacheable + A6 (cache context) $edge_case_a6_uncacheable = $has_uncacheable_lazy_builder && $test_element['placeholder']['#lazy_builder'][0] === 'Drupal\Tests\Core\Render\PlaceholdersTest::callbackPerUser'; // - uncacheable + A7 (cache tag) - // @todo + $edge_case_a7_uncacheable = $has_uncacheable_lazy_builder && $test_element['placeholder']['#lazy_builder'][0] === 'Drupal\Tests\Core\Render\PlaceholdersTest::callbackTagCurrentTemperature'; // The redirect-cacheable edge case: a high-cardinality cache context is // bubbled from a #lazy_builder callback for an uncacheable placeholder. The // element containing the uncacheable placeholder has cache keys set, and @@ -592,6 +665,28 @@ public function testCacheableParent($test_element, $args, array $expected_placeh ]; $this->assertEquals($expected_element, $cached_element, 'The parent is render cached with a redirect in ase a cache context is bubbled from an uncacheable child (no #cache[keys]) with a #lazy_builder.'); } + // The normally cacheable edge case: a high-invalidation frequency cache tag + // is bubbled from a #lazy_builder callback for an uncacheable placeholder. + // The element continaing the uncacheable placeholder has cache keys set, + // and also has the bubbled cache tags. + elseif ($edge_case_a7_uncacheable) { + $cached_element = $cached->data; + $expected_element = [ + '#markup' => '#cache enabled, GET
This is a rendered placeholder!
', + '#attached' => [ + 'drupalSettings' => [ + 'foo' => 'bar', + 'dynamic_animal' => $args[0], + ], + ], + '#cache' => [ + 'contexts' => [], + 'tags' => $bubbled_cache_tags, + 'max-age' => Cache::PERMANENT, + ], + ]; + $this->assertEquals($expected_element, $cached_element, 'The correct data is cached: the stored #markup and #attached properties are not affected by placeholder #lazy_builder callbacks.'); + } // The regular case. else { $cached_element = $cached->data; @@ -609,7 +704,7 @@ public function testCacheableParent($test_element, $args, array $expected_placeh ], '#cache' => [ 'contexts' => [], - 'tags' => [], + 'tags' => $bubbled_cache_tags, 'max-age' => Cache::PERMANENT, ], ]; diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php index 3051c42..5145d58 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php @@ -275,4 +275,19 @@ public static function callbackPerUser($animal) { return $build; } + /** + * #lazy_builder callback; attaches setting, generates markup, cache tag. + * + * @param string $animal + * An animal. + * + * @return array + * A renderable array. + */ + public static function callbackTagCurrentTemperature($animal) { + $build = static::callback($animal); + $build['#cache']['tags'][] = 'current-temperature'; + return $build; + } + }