RendererBubblingTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Render\RendererBubblingTest.
  5. */
  6. namespace Drupal\Tests\Core\Render;
  7. use Drupal\Core\Cache\MemoryBackend;
  8. use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
  9. use Drupal\Core\Security\TrustedCallbackInterface;
  10. use Drupal\Core\State\State;
  11. use Drupal\Core\Cache\Cache;
  12. /**
  13. * @coversDefaultClass \Drupal\Core\Render\Renderer
  14. * @group Render
  15. */
  16. class RendererBubblingTest extends RendererTestBase {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function setUp() {
  21. // Disable the required cache contexts, so that this test can test just the
  22. // bubbling behavior.
  23. $this->rendererConfig['required_cache_contexts'] = [];
  24. parent::setUp();
  25. }
  26. /**
  27. * Tests bubbling of assets when NOT using #pre_render callbacks.
  28. */
  29. public function testBubblingWithoutPreRender() {
  30. $this->setUpRequest();
  31. $this->setupMemoryCache();
  32. $this->cacheContextsManager->expects($this->any())
  33. ->method('convertTokensToKeys')
  34. ->willReturnArgument(0);
  35. // Create an element with a child and subchild. Each element loads a
  36. // different library using #attached.
  37. $element = [
  38. '#type' => 'container',
  39. '#cache' => [
  40. 'keys' => ['simpletest', 'renderer', 'children_attached'],
  41. ],
  42. '#attached' => ['library' => ['test/parent']],
  43. '#title' => 'Parent',
  44. ];
  45. $element['child'] = [
  46. '#type' => 'container',
  47. '#attached' => ['library' => ['test/child']],
  48. '#title' => 'Child',
  49. ];
  50. $element['child']['subchild'] = [
  51. '#attached' => ['library' => ['test/subchild']],
  52. '#markup' => 'Subchild',
  53. ];
  54. // Render the element and verify the presence of #attached JavaScript.
  55. $this->renderer->renderRoot($element);
  56. $expected_libraries = ['test/parent', 'test/child', 'test/subchild'];
  57. $this->assertEquals($element['#attached']['library'], $expected_libraries, 'The element, child and subchild #attached libraries are included.');
  58. // Load the element from cache and verify the presence of the #attached
  59. // JavaScript.
  60. $element = ['#cache' => ['keys' => ['simpletest', 'renderer', 'children_attached']]];
  61. $this->assertTrue(strlen($this->renderer->renderRoot($element)) > 0, 'The element was retrieved from cache.');
  62. $this->assertEquals($element['#attached']['library'], $expected_libraries, 'The element, child and subchild #attached libraries are included.');
  63. }
  64. /**
  65. * Tests cache context bubbling with a custom cache bin.
  66. */
  67. public function testContextBubblingCustomCacheBin() {
  68. $bin = $this->randomMachineName();
  69. $this->setUpRequest();
  70. $this->memoryCache = new MemoryBackend();
  71. $custom_cache = new MemoryBackend();
  72. $this->cacheFactory->expects($this->atLeastOnce())
  73. ->method('get')
  74. ->with($bin)
  75. ->willReturnCallback(function ($requested_bin) use ($bin, $custom_cache) {
  76. if ($requested_bin === $bin) {
  77. return $custom_cache;
  78. }
  79. else {
  80. throw new \Exception();
  81. }
  82. });
  83. $this->cacheContextsManager->expects($this->any())
  84. ->method('convertTokensToKeys')
  85. ->willReturnArgument(0);
  86. $build = [
  87. '#cache' => [
  88. 'keys' => ['parent'],
  89. 'contexts' => ['foo'],
  90. 'bin' => $bin,
  91. ],
  92. '#markup' => 'parent',
  93. 'child' => [
  94. '#cache' => [
  95. 'contexts' => ['bar'],
  96. 'max-age' => 3600,
  97. ],
  98. ],
  99. ];
  100. $this->renderer->renderRoot($build);
  101. $this->assertRenderCacheItem('parent:foo', [
  102. '#cache_redirect' => TRUE,
  103. '#cache' => [
  104. 'keys' => ['parent'],
  105. 'contexts' => ['bar', 'foo'],
  106. 'tags' => [],
  107. 'bin' => $bin,
  108. 'max-age' => 3600,
  109. ],
  110. ], $bin);
  111. }
  112. /**
  113. * Tests cache context bubbling in edge cases, because it affects the CID.
  114. *
  115. * ::testBubblingWithPrerender() already tests the common case.
  116. *
  117. * @dataProvider providerTestContextBubblingEdgeCases
  118. */
  119. public function testContextBubblingEdgeCases(array $element, array $expected_top_level_contexts, array $expected_cache_items) {
  120. $this->setUpRequest();
  121. $this->setupMemoryCache();
  122. $this->cacheContextsManager->expects($this->any())
  123. ->method('convertTokensToKeys')
  124. ->willReturnArgument(0);
  125. $this->renderer->renderRoot($element);
  126. $this->assertEquals($expected_top_level_contexts, $element['#cache']['contexts'], 'Expected cache contexts found.');
  127. foreach ($expected_cache_items as $cid => $expected_cache_item) {
  128. $this->assertRenderCacheItem($cid, $expected_cache_item);
  129. }
  130. }
  131. public function providerTestContextBubblingEdgeCases() {
  132. $data = [];
  133. // Cache contexts of inaccessible children aren't bubbled (because those
  134. // children are not rendered at all).
  135. $test_element = [
  136. '#cache' => [
  137. 'keys' => ['parent'],
  138. 'contexts' => [],
  139. ],
  140. '#markup' => 'parent',
  141. 'child' => [
  142. '#access' => FALSE,
  143. '#cache' => [
  144. 'contexts' => ['foo'],
  145. ],
  146. ],
  147. ];
  148. $expected_cache_items = [
  149. 'parent' => [
  150. '#attached' => [],
  151. '#cache' => [
  152. 'contexts' => [],
  153. 'tags' => [],
  154. 'max-age' => Cache::PERMANENT,
  155. ],
  156. '#markup' => 'parent',
  157. ],
  158. ];
  159. $data[] = [$test_element, [], $expected_cache_items];
  160. // Assert cache contexts are sorted when they are used to generate a CID.
  161. // (Necessary to ensure that different render arrays where the same keys +
  162. // set of contexts are present point to the same cache item. Regardless of
  163. // the contexts' order. A sad necessity because PHP doesn't have sets.)
  164. $test_element = [
  165. '#cache' => [
  166. 'keys' => ['set_test'],
  167. 'contexts' => [],
  168. ],
  169. ];
  170. $expected_cache_items = [
  171. 'set_test:bar:baz:foo' => [
  172. '#attached' => [],
  173. '#cache' => [
  174. 'contexts' => [],
  175. 'tags' => [],
  176. 'max-age' => Cache::PERMANENT,
  177. ],
  178. '#markup' => '',
  179. ],
  180. ];
  181. $context_orders = [
  182. ['foo', 'bar', 'baz'],
  183. ['foo', 'baz', 'bar'],
  184. ['bar', 'foo', 'baz'],
  185. ['bar', 'baz', 'foo'],
  186. ['baz', 'foo', 'bar'],
  187. ['baz', 'bar', 'foo'],
  188. ];
  189. foreach ($context_orders as $context_order) {
  190. $test_element['#cache']['contexts'] = $context_order;
  191. sort($context_order);
  192. $expected_cache_items['set_test:bar:baz:foo']['#cache']['contexts'] = $context_order;
  193. $data[] = [$test_element, $context_order, $expected_cache_items];
  194. }
  195. // A parent with a certain set of cache contexts is unaffected by a child
  196. // that has a subset of those contexts.
  197. $test_element = [
  198. '#cache' => [
  199. 'keys' => ['parent'],
  200. 'contexts' => ['foo', 'bar', 'baz'],
  201. ],
  202. '#markup' => 'parent',
  203. 'child' => [
  204. '#cache' => [
  205. 'contexts' => ['foo', 'baz'],
  206. 'max-age' => 3600,
  207. ],
  208. ],
  209. ];
  210. $expected_cache_items = [
  211. 'parent:bar:baz:foo' => [
  212. '#attached' => [],
  213. '#cache' => [
  214. 'contexts' => ['bar', 'baz', 'foo'],
  215. 'tags' => [],
  216. 'max-age' => 3600,
  217. ],
  218. '#markup' => 'parent',
  219. ],
  220. ];
  221. $data[] = [$test_element, ['bar', 'baz', 'foo'], $expected_cache_items];
  222. // A parent with a certain set of cache contexts that is a subset of the
  223. // cache contexts of a child gets a redirecting cache item for the cache ID
  224. // created pre-bubbling (without the child's additional cache contexts). It
  225. // points to a cache item with a post-bubbling cache ID (i.e. with the
  226. // child's additional cache contexts).
  227. // Furthermore, the redirecting cache item also includes the children's
  228. // cache tags, since changes in the children may cause those children to get
  229. // different cache contexts and therefore cause different cache contexts to
  230. // be stored in the redirecting cache item.
  231. $test_element = [
  232. '#cache' => [
  233. 'keys' => ['parent'],
  234. 'contexts' => ['foo'],
  235. 'tags' => ['yar', 'har'],
  236. ],
  237. '#markup' => 'parent',
  238. 'child' => [
  239. '#cache' => [
  240. 'contexts' => ['bar'],
  241. 'tags' => ['fiddle', 'dee'],
  242. ],
  243. '#markup' => '',
  244. ],
  245. ];
  246. $expected_cache_items = [
  247. 'parent:foo' => [
  248. '#cache_redirect' => TRUE,
  249. '#cache' => [
  250. // The keys + contexts this redirects to.
  251. 'keys' => ['parent'],
  252. 'contexts' => ['bar', 'foo'],
  253. 'tags' => ['dee', 'fiddle', 'har', 'yar'],
  254. 'bin' => 'render',
  255. 'max-age' => Cache::PERMANENT,
  256. ],
  257. ],
  258. 'parent:bar:foo' => [
  259. '#attached' => [],
  260. '#cache' => [
  261. 'contexts' => ['bar', 'foo'],
  262. 'tags' => ['dee', 'fiddle', 'har', 'yar'],
  263. 'max-age' => Cache::PERMANENT,
  264. ],
  265. '#markup' => 'parent',
  266. ],
  267. ];
  268. $data[] = [$test_element, ['bar', 'foo'], $expected_cache_items];
  269. // Ensure that bubbleable metadata has been collected from children and set
  270. // correctly to the main level of the render array. That ensures that correct
  271. // bubbleable metadata exists if render array gets rendered multiple times.
  272. $test_element = [
  273. '#cache' => [
  274. 'keys' => ['parent'],
  275. 'tags' => ['yar', 'har'],
  276. ],
  277. '#markup' => 'parent',
  278. 'child' => [
  279. '#render_children' => TRUE,
  280. 'subchild' => [
  281. '#cache' => [
  282. 'contexts' => ['foo'],
  283. 'tags' => ['fiddle', 'dee'],
  284. ],
  285. '#attached' => [
  286. 'library' => ['foo/bar'],
  287. ],
  288. '#markup' => '',
  289. ],
  290. ],
  291. ];
  292. $expected_cache_items = [
  293. 'parent:foo' => [
  294. '#attached' => ['library' => ['foo/bar']],
  295. '#cache' => [
  296. 'contexts' => ['foo'],
  297. 'tags' => ['dee', 'fiddle', 'har', 'yar'],
  298. 'max-age' => Cache::PERMANENT,
  299. ],
  300. '#markup' => 'parent',
  301. ],
  302. ];
  303. $data[] = [$test_element, ['foo'], $expected_cache_items];
  304. return $data;
  305. }
  306. /**
  307. * Tests the self-healing of the redirect with conditional cache contexts.
  308. */
  309. public function testConditionalCacheContextBubblingSelfHealing() {
  310. $current_user_role = &$this->currentUserRole;
  311. $this->setUpRequest();
  312. $this->setupMemoryCache();
  313. $test_element = [
  314. '#cache' => [
  315. 'keys' => ['parent'],
  316. 'tags' => ['a'],
  317. ],
  318. '#markup' => 'parent',
  319. 'child' => [
  320. '#cache' => [
  321. 'contexts' => ['user.roles'],
  322. 'tags' => ['b'],
  323. ],
  324. 'grandchild' => [
  325. '#access_callback' => function () use (&$current_user_role) {
  326. // Only role A cannot access this subtree.
  327. return $current_user_role !== 'A';
  328. },
  329. '#cache' => [
  330. 'contexts' => ['foo'],
  331. 'tags' => ['c'],
  332. // A lower max-age; the redirecting cache item should be updated.
  333. 'max-age' => 1800,
  334. ],
  335. 'grandgrandchild' => [
  336. '#access_callback' => function () use (&$current_user_role) {
  337. // Only role C can access this subtree.
  338. return $current_user_role === 'C';
  339. },
  340. '#cache' => [
  341. 'contexts' => ['bar'],
  342. 'tags' => ['d'],
  343. // A lower max-age; the redirecting cache item should be updated.
  344. 'max-age' => 300,
  345. ],
  346. ],
  347. ],
  348. ],
  349. ];
  350. // Request 1: role A, the grandchild isn't accessible => bubbled cache
  351. // contexts: user.roles.
  352. $element = $test_element;
  353. $current_user_role = 'A';
  354. $this->renderer->renderRoot($element);
  355. $this->assertRenderCacheItem('parent', [
  356. '#cache_redirect' => TRUE,
  357. '#cache' => [
  358. 'keys' => ['parent'],
  359. 'contexts' => ['user.roles'],
  360. 'tags' => ['a', 'b'],
  361. 'bin' => 'render',
  362. 'max-age' => Cache::PERMANENT,
  363. ],
  364. ]);
  365. $this->assertRenderCacheItem('parent:r.A', [
  366. '#attached' => [],
  367. '#cache' => [
  368. 'contexts' => ['user.roles'],
  369. 'tags' => ['a', 'b'],
  370. 'max-age' => Cache::PERMANENT,
  371. ],
  372. '#markup' => 'parent',
  373. ]);
  374. // Request 2: role B, the grandchild is accessible => bubbled cache
  375. // contexts: foo, user.roles + merged max-age: 1800.
  376. $element = $test_element;
  377. $current_user_role = 'B';
  378. $this->renderer->renderRoot($element);
  379. $this->assertRenderCacheItem('parent', [
  380. '#cache_redirect' => TRUE,
  381. '#cache' => [
  382. 'keys' => ['parent'],
  383. 'contexts' => ['foo', 'user.roles'],
  384. 'tags' => ['a', 'b', 'c'],
  385. 'bin' => 'render',
  386. 'max-age' => 1800,
  387. ],
  388. ]);
  389. $this->assertRenderCacheItem('parent:foo:r.B', [
  390. '#attached' => [],
  391. '#cache' => [
  392. 'contexts' => ['foo', 'user.roles'],
  393. 'tags' => ['a', 'b', 'c'],
  394. 'max-age' => 1800,
  395. ],
  396. '#markup' => 'parent',
  397. ]);
  398. // Request 3: role A again, the grandchild is inaccessible again => bubbled
  399. // cache contexts: user.roles; but that's a subset of the already-bubbled
  400. // cache contexts, so nothing is actually changed in the redirecting cache
  401. // item. However, the cache item we were looking for in request 1 is
  402. // technically the same one we're looking for now (it's the exact same
  403. // request), but with one additional cache context. This is necessary to
  404. // avoid "cache ping-pong". (Requests 1 and 3 are identical, but without the
  405. // right merging logic to handle request 2, the redirecting cache item would
  406. // toggle between only the 'user.roles' cache context and both the 'foo'
  407. // and 'user.roles' cache contexts, resulting in a cache miss every time.)
  408. $element = $test_element;
  409. $current_user_role = 'A';
  410. $this->renderer->renderRoot($element);
  411. $this->assertRenderCacheItem('parent', [
  412. '#cache_redirect' => TRUE,
  413. '#cache' => [
  414. 'keys' => ['parent'],
  415. 'contexts' => ['foo', 'user.roles'],
  416. 'tags' => ['a', 'b', 'c'],
  417. 'bin' => 'render',
  418. 'max-age' => 1800,
  419. ],
  420. ]);
  421. $this->assertRenderCacheItem('parent:foo:r.A', [
  422. '#attached' => [],
  423. '#cache' => [
  424. 'contexts' => ['foo', 'user.roles'],
  425. 'tags' => ['a', 'b'],
  426. // Note that the max-age here is unaffected. When role A, the grandchild
  427. // is never rendered, so neither is its max-age of 1800 present here,
  428. // despite 1800 being the max-age of the redirecting cache item.
  429. 'max-age' => Cache::PERMANENT,
  430. ],
  431. '#markup' => 'parent',
  432. ]);
  433. // Request 4: role C, both the grandchild and the grandgrandchild are
  434. // accessible => bubbled cache contexts: foo, bar, user.roles + merged
  435. // max-age: 300.
  436. $element = $test_element;
  437. $current_user_role = 'C';
  438. $this->renderer->renderRoot($element);
  439. $final_parent_cache_item = [
  440. '#cache_redirect' => TRUE,
  441. '#cache' => [
  442. 'keys' => ['parent'],
  443. 'contexts' => ['bar', 'foo', 'user.roles'],
  444. 'tags' => ['a', 'b', 'c', 'd'],
  445. 'bin' => 'render',
  446. 'max-age' => 300,
  447. ],
  448. ];
  449. $this->assertRenderCacheItem('parent', $final_parent_cache_item);
  450. $this->assertRenderCacheItem('parent:bar:foo:r.C', [
  451. '#attached' => [],
  452. '#cache' => [
  453. 'contexts' => ['bar', 'foo', 'user.roles'],
  454. 'tags' => ['a', 'b', 'c', 'd'],
  455. 'max-age' => 300,
  456. ],
  457. '#markup' => 'parent',
  458. ]);
  459. // Request 5: role A again, verifying the merging like we did for request 3.
  460. $element = $test_element;
  461. $current_user_role = 'A';
  462. $this->renderer->renderRoot($element);
  463. $this->assertRenderCacheItem('parent', $final_parent_cache_item);
  464. $this->assertRenderCacheItem('parent:bar:foo:r.A', [
  465. '#attached' => [],
  466. '#cache' => [
  467. 'contexts' => ['bar', 'foo', 'user.roles'],
  468. 'tags' => ['a', 'b'],
  469. // Note that the max-age here is unaffected. When role A, the grandchild
  470. // is never rendered, so neither is its max-age of 1800 present here,
  471. // nor the grandgrandchild's max-age of 300, despite 300 being the
  472. // max-age of the redirecting cache item.
  473. 'max-age' => Cache::PERMANENT,
  474. ],
  475. '#markup' => 'parent',
  476. ]);
  477. // Request 6: role B again, verifying the merging like we did for request 3.
  478. $element = $test_element;
  479. $current_user_role = 'B';
  480. $this->renderer->renderRoot($element);
  481. $this->assertRenderCacheItem('parent', $final_parent_cache_item);
  482. $this->assertRenderCacheItem('parent:bar:foo:r.B', [
  483. '#attached' => [],
  484. '#cache' => [
  485. 'contexts' => ['bar', 'foo', 'user.roles'],
  486. 'tags' => ['a', 'b', 'c'],
  487. // Note that the max-age here is unaffected. When role B, the
  488. // grandgrandchild is never rendered, so neither is its max-age of 300
  489. // present here, despite 300 being the max-age of the redirecting cache
  490. // item.
  491. 'max-age' => 1800,
  492. ],
  493. '#markup' => 'parent',
  494. ]);
  495. }
  496. /**
  497. * Tests bubbling of bubbleable metadata added by #pre_render callbacks.
  498. *
  499. * @dataProvider providerTestBubblingWithPrerender
  500. */
  501. public function testBubblingWithPrerender($test_element) {
  502. $this->setUpRequest();
  503. $this->setupMemoryCache();
  504. // Mock the State service.
  505. $memory_state = new State(new KeyValueMemoryFactory());
  506. \Drupal::getContainer()->set('state', $memory_state);
  507. $this->controllerResolver->expects($this->any())
  508. ->method('getControllerFromDefinition')
  509. ->willReturnArgument(0);
  510. // Simulate the theme system/Twig: a recursive call to Renderer::render(),
  511. // just like the theme system or a Twig template would have done.
  512. $this->themeManager->expects($this->any())
  513. ->method('render')
  514. ->willReturnCallback(function ($hook, $vars) {
  515. return $this->renderer->render($vars['foo']);
  516. });
  517. // ::bubblingPreRender() verifies that a #pre_render callback for a render
  518. // array that is cacheable and …
  519. // - … is cached does NOT get called. (Also mock a render cache item.)
  520. // - … is not cached DOES get called.
  521. \Drupal::state()->set('bubbling_nested_pre_render_cached', FALSE);
  522. \Drupal::state()->set('bubbling_nested_pre_render_uncached', FALSE);
  523. $this->memoryCache->set('cached_nested', ['#markup' => 'Cached nested!', '#attached' => [], '#cache' => ['contexts' => [], 'tags' => []]]);
  524. // Simulate the rendering of an entire response (i.e. a root call).
  525. $output = $this->renderer->renderRoot($test_element);
  526. // First, assert the render array is of the expected form.
  527. $this->assertEquals('Cache context!Cache tag!Asset!Placeholder!barquxNested!Cached nested!', trim($output), 'Expected HTML generated.');
  528. $this->assertEquals(['child.cache_context'], $test_element['#cache']['contexts'], 'Expected cache contexts found.');
  529. $this->assertEquals(['child:cache_tag'], $test_element['#cache']['tags'], 'Expected cache tags found.');
  530. $expected_attached = [
  531. 'drupalSettings' => ['foo' => 'bar'],
  532. 'placeholders' => [],
  533. ];
  534. $this->assertEquals($expected_attached, $test_element['#attached'], 'Expected attachments found.');
  535. // Second, assert that #pre_render callbacks are only executed if they don't
  536. // have a render cache hit (and hence a #pre_render callback for a render
  537. // cached item cannot bubble more metadata).
  538. $this->assertTrue(\Drupal::state()->get('bubbling_nested_pre_render_uncached'));
  539. $this->assertFalse(\Drupal::state()->get('bubbling_nested_pre_render_cached'));
  540. }
  541. /**
  542. * Provides two test elements: one without, and one with the theme system.
  543. *
  544. * @return array
  545. */
  546. public function providerTestBubblingWithPrerender() {
  547. $data = [];
  548. // Test element without theme.
  549. $data[] = [
  550. [
  551. 'foo' => [
  552. '#pre_render' => [__NAMESPACE__ . '\\BubblingTest::bubblingPreRender'],
  553. ],
  554. ],
  555. ];
  556. // Test element with theme.
  557. $data[] = [
  558. [
  559. '#theme' => 'common_test_render_element',
  560. 'foo' => [
  561. '#pre_render' => [__NAMESPACE__ . '\\BubblingTest::bubblingPreRender'],
  562. ],
  563. ],
  564. ];
  565. return $data;
  566. }
  567. /**
  568. * Tests that an element's cache keys cannot be changed during its rendering.
  569. */
  570. public function testOverWriteCacheKeys() {
  571. $this->setUpRequest();
  572. $this->setupMemoryCache();
  573. // Ensure a logic exception
  574. $data = [
  575. '#cache' => [
  576. 'keys' => ['llama', 'bar'],
  577. ],
  578. '#pre_render' => [__NAMESPACE__ . '\\BubblingTest::bubblingCacheOverwritePrerender'],
  579. ];
  580. $this->expectException(\LogicException::class);
  581. $this->expectExceptionMessage('Cache keys may not be changed after initial setup. Use the contexts property instead to bubble additional metadata.');
  582. $this->renderer->renderRoot($data);
  583. }
  584. }
  585. class BubblingTest implements TrustedCallbackInterface {
  586. /**
  587. * #pre_render callback for testBubblingWithPrerender().
  588. */
  589. public static function bubblingPreRender($elements) {
  590. $elements += [
  591. 'child_cache_context' => [
  592. '#cache' => [
  593. 'contexts' => ['child.cache_context'],
  594. ],
  595. '#markup' => 'Cache context!',
  596. ],
  597. 'child_cache_tag' => [
  598. '#cache' => [
  599. 'tags' => ['child:cache_tag'],
  600. ],
  601. '#markup' => 'Cache tag!',
  602. ],
  603. 'child_asset' => [
  604. '#attached' => [
  605. 'drupalSettings' => ['foo' => 'bar'],
  606. ],
  607. '#markup' => 'Asset!',
  608. ],
  609. 'child_placeholder' => [
  610. '#create_placeholder' => TRUE,
  611. '#lazy_builder' => [__CLASS__ . '::bubblingPlaceholder', ['bar', 'qux']],
  612. ],
  613. 'child_nested_pre_render_uncached' => [
  614. '#cache' => ['keys' => ['uncached_nested']],
  615. '#pre_render' => [__CLASS__ . '::bubblingNestedPreRenderUncached'],
  616. ],
  617. 'child_nested_pre_render_cached' => [
  618. '#cache' => ['keys' => ['cached_nested']],
  619. '#pre_render' => [__CLASS__ . '::bubblingNestedPreRenderCached'],
  620. ],
  621. ];
  622. return $elements;
  623. }
  624. /**
  625. * #pre_render callback for testBubblingWithPrerender().
  626. */
  627. public static function bubblingNestedPreRenderUncached($elements) {
  628. \Drupal::state()->set('bubbling_nested_pre_render_uncached', TRUE);
  629. $elements['#markup'] = 'Nested!';
  630. return $elements;
  631. }
  632. /**
  633. * #pre_render callback for testBubblingWithPrerender().
  634. */
  635. public static function bubblingNestedPreRenderCached($elements) {
  636. \Drupal::state()->set('bubbling_nested_pre_render_cached', TRUE);
  637. return $elements;
  638. }
  639. /**
  640. * #lazy_builder callback for testBubblingWithPrerender().
  641. */
  642. public static function bubblingPlaceholder($foo, $baz) {
  643. return [
  644. '#markup' => 'Placeholder!' . $foo . $baz,
  645. ];
  646. }
  647. /**
  648. * #pre_render callback for testOverWriteCacheKeys().
  649. */
  650. public static function bubblingCacheOverwritePrerender($elements) {
  651. // Overwrite the #cache entry with new data.
  652. $elements['#cache'] = [
  653. 'keys' => ['llama', 'foo'],
  654. ];
  655. $elements['#markup'] = 'Setting cache keys just now!';
  656. return $elements;
  657. }
  658. /**
  659. * {@inheritdoc}
  660. */
  661. public static function trustedCallbacks() {
  662. return ['bubblingPreRender', 'bubblingNestedPreRenderUncached', 'bubblingNestedPreRenderCached', 'bubblingPlaceholder', 'bubblingCacheOverwritePrerender'];
  663. }
  664. }