|
9 | 9 | use Drupal\Core\Cache\Cache;
|
10 | 10 | use Drupal\Core\Cache\CacheableMetadata;
|
11 | 11 | use Drupal\Core\Render\Markup;
|
| 12 | +use Drupal\Core\Render\PlaceholderingRenderCache; |
12 | 13 | use Drupal\Core\Render\RenderContext;
|
| 14 | +use Drupal\Core\Render\Renderer; |
13 | 15 | use Drupal\Core\Security\TrustedCallbackInterface;
|
14 | 16 |
|
15 | 17 | /**
|
@@ -1047,6 +1049,50 @@ public function testRenderChildrenPlaceholdersDifferentArguments() {
|
1047 | 1049 | $this->assertSame($element['#attached']['drupalSettings'], $expected_js_settings, '#attached is modified; both the original JavaScript setting and the ones added by each #lazy_builder callback exist.');
|
1048 | 1050 | }
|
1049 | 1051 |
|
| 1052 | + /** |
| 1053 | + * Tests the creation of an element with a lazy_builder_preview. |
| 1054 | + * |
| 1055 | + * @covers ::render |
| 1056 | + * @covers ::doRender |
| 1057 | + * @covers \Drupal\Core\Render\RenderCache::get |
| 1058 | + * @covers ::replacePlaceholders |
| 1059 | + */ |
| 1060 | + public function testRenderLazyBuilderPreview() { |
| 1061 | + $this->setUpRequest(); |
| 1062 | + $this->setupMemoryCache(); |
| 1063 | + $this->renderCache = new TestPlaceholderingRenderCache($this->requestStack, $this->cacheFactory, $this->cacheContextsManager, $this->placeholderGenerator); |
| 1064 | + $this->renderer = new Renderer($this->callableResolver, $this->themeManager, $this->elementInfo, $this->placeholderGenerator, $this->renderCache, $this->requestStack, $this->rendererConfig); |
| 1065 | + |
| 1066 | + $this->cacheContextsManager->expects($this->any()) |
| 1067 | + ->method('convertTokensToKeys') |
| 1068 | + ->willReturnArgument(0); |
| 1069 | + $this->callableResolver->expects($this->any()) |
| 1070 | + ->method('getCallableFromDefinition') |
| 1071 | + ->willReturnArgument(0); |
| 1072 | + |
| 1073 | + $test_element = $this->generatePlaceholderWithLazyBuilderPreview(); |
| 1074 | + |
| 1075 | + $element1 = $element2 = $test_element; |
| 1076 | + // Render the element twice so that it is in the render cache. |
| 1077 | + $result = $this->renderer->renderRoot($element1); |
| 1078 | + $result = $this->renderer->renderRoot($element2); |
| 1079 | + $placeholder_string = (string) $this->renderCache->placeholderElements[0]['#markup']; |
| 1080 | + $this->assertSame($this->renderCache->placeholderElements[0]['#attached']['placeholders'][$placeholder_string]['#preview'], ['#markup' => 'Lazy Builder Preview']); |
| 1081 | + } |
| 1082 | + |
| 1083 | + /** |
| 1084 | + * Generates an element with a lazy builder and preview. |
| 1085 | + */ |
| 1086 | + public function generatePlaceholderWithLazyBuilderPreview(): array { |
| 1087 | + return [ |
| 1088 | + '#cache' => [ |
| 1089 | + 'keys' => ['test_render'], |
| 1090 | + ], |
| 1091 | + '#lazy_builder' => [__namespace__ . '\\PlaceholdersTest::callbackPerUser', ['foo']], |
| 1092 | + '#lazy_builder_preview' => ['#markup' => 'Lazy Builder Preview'], |
| 1093 | + ]; |
| 1094 | + } |
| 1095 | + |
1050 | 1096 | /**
|
1051 | 1097 | * Generates an element with placeholders at 3 levels.
|
1052 | 1098 | *
|
@@ -1156,3 +1202,18 @@ public static function trustedCallbacks() {
|
1156 | 1202 | }
|
1157 | 1203 |
|
1158 | 1204 | }
|
| 1205 | + |
| 1206 | +class TestPlaceholderingRenderCache extends PlaceholderingRenderCache { |
| 1207 | + |
| 1208 | + /** |
| 1209 | + * The placeholder elements created during rendering. |
| 1210 | + */ |
| 1211 | + public array $placeholderElements = []; |
| 1212 | + |
| 1213 | + protected function createPlaceholderAndRemember(array $rendered_elements, array $pre_bubbling_elements) { |
| 1214 | + $placeholder_element = parent::createPlaceholderAndRemember($rendered_elements, $pre_bubbling_elements); |
| 1215 | + $this->placeholderElements[] = $placeholder_element; |
| 1216 | + return $placeholder_element; |
| 1217 | + } |
| 1218 | + |
| 1219 | +} |
0 commit comments