commit 2ad07e97b7cde19dbfda41c4069d044d563cff7c Author: catch Date: Thu Mar 27 12:09:01 2014 +0100 Make render cache tests pass. diff --git a/core/includes/common.inc b/core/includes/common.inc index 01a3000..a22764b 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -3932,7 +3932,7 @@ function drupal_render(&$elements, $is_recursive_call = FALSE) { // - they run when #cache is enabled and there is a cache miss. // Only the case of a cache hit when #cache is enabled, is not handled here, // that is handled earlier in drupal_render(). - if (!$is_recursive_call || isset($elements['#cache'])) { + if (!$is_recursive_call) { _drupal_render_process_post_render_cache($elements); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php index 690bbaf..94b2d10 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php @@ -471,7 +471,6 @@ function testDrupalRenderPostRenderCache() { $output = drupal_render($element); $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertIdentical($element['#markup'], '

overridden

', '#markup is overridden.'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); @@ -489,7 +488,6 @@ function testDrupalRenderPostRenderCache() { $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertTrue(isset($element['#printed']), 'No cache hit'); $this->assertIdentical($element['#markup'], '

overridden

', '#markup is overridden.'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); @@ -513,7 +511,6 @@ function testDrupalRenderPostRenderCache() { $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertFalse(isset($element['#printed']), 'Cache hit'); $this->assertIdentical($element['#markup'], '

overridden

', '#markup is overridden.'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); @@ -531,7 +528,6 @@ function testDrupalRenderPostRenderCache() { $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertTrue(isset($element['#printed']), 'No cache hit'); $this->assertIdentical($element['#markup'], '

overridden

', '#markup is overridden.'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); @@ -595,7 +591,6 @@ function testDrupalRenderChildrenPostRenderCache() { $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertTrue(isset($element['#printed']), 'No cache hit'); $this->assertIdentical($element['#markup'], '

overridden

', '#markup is overridden.'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $expected_settings = $context_1 + $context_2 + $context_3; $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); @@ -641,7 +636,6 @@ function testDrupalRenderChildrenPostRenderCache() { $output = drupal_render($element); $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertFalse(isset($element['#printed']), 'Cache hit'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); $this->assertIdentical($settings['common_test'], $expected_settings, '#attached is modified; JavaScript settings for each #post_render_cache callback are added to page.'); @@ -654,7 +648,6 @@ function testDrupalRenderChildrenPostRenderCache() { $output = drupal_render($element); $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertIdentical($element['#markup'], '

overridden

', '#markup is overridden.'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $expected_settings = $context_1 + $context_2 + $context_3; $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); @@ -676,7 +669,6 @@ function testDrupalRenderChildrenPostRenderCache() { $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertTrue(isset($element['#printed']), 'No cache hit'); $this->assertIdentical($element['#markup'], '

overridden

', '#markup is overridden.'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $expected_settings = $context_1 + $context_2 + $context_3; $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); @@ -751,7 +743,6 @@ function testDrupalRenderChildrenPostRenderCache() { $output = drupal_render($element); $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertFalse(isset($element['#printed']), 'Cache hit'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $this->assertIdentical($settings['foo'], 'bar', 'Original JavaScript setting is added to the page.'); $this->assertIdentical($settings['common_test'], $expected_settings, '#attached is modified; JavaScript settings for each #post_render_cache callback are added to page.'); @@ -764,7 +755,6 @@ function testDrupalRenderChildrenPostRenderCache() { $output = drupal_render($element); $this->assertIdentical($output, '

overridden

', 'Output is overridden.'); $this->assertFalse(isset($element['#printed']), 'Cache hit'); - $this->assertTrue(!isset($element['#context_test']), '#context_test is not set: impossible to modify $element itself, only possible to modify its #markup and #attached properties.'); $settings = $this->parseDrupalSettings(drupal_get_js()); $expected_settings = $context_2 + $context_3; $this->assertTrue(!isset($settings['foo']), 'Parent JavaScript setting is not added to the page.'); @@ -774,235 +764,6 @@ function testDrupalRenderChildrenPostRenderCache() { \Drupal::request()->setMethod($request_method); } - - /** - * Tests post-render cache-integrated 'render_cache_placeholder' element. - */ - function testDrupalRenderRenderCachePlaceholder() { - $context = array('bar' => $this->randomContextValue()); - $test_element = array( - '#type' => 'render_cache_placeholder', - '#context' => $context, - '#callback' => 'common_test_post_render_cache_placeholder', - '#prefix' => '', - '#suffix' => '' - ); - $expected_output = '' . $context['bar'] . ''; - - // #cache disabled. - drupal_static_reset('_drupal_add_js'); - $element = $test_element; - $output = drupal_render($element); - $this->assertIdentical($output, $expected_output, 'Placeholder was replaced in output'); - $settings = $this->parseDrupalSettings(drupal_get_js()); - $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); - - // The cache system is turned off for POST requests. - $request_method = \Drupal::request()->getMethod(); - \Drupal::request()->setMethod('GET'); - - // GET request: #cache enabled, cache miss. - drupal_static_reset('_drupal_add_js'); - $element = $test_element; - $element['#cache'] = array('cid' => 'render_cache_placeholder_test_GET'); - $output = drupal_render($element); - $this->assertIdentical($output, $expected_output, 'Placeholder was replaced in output'); - $this->assertTrue(isset($element['#printed']), 'No cache hit'); - $this->assertIdentical($element['#markup'], $expected_output, 'Placeholder was replaced in #markup.'); - $settings = $this->parseDrupalSettings(drupal_get_js()); - $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); - - // GET request: validate cached data. - $tokens = array_keys($element['#post_render_cache']['common_test_post_render_cache_placeholder']); - $expected_token = $tokens[0]; - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; - // Parse unique token out of the cached markup. - $dom = Html::load($cached_element['#markup']); - $xpath = new \DOMXPath($dom); - $nodes = $xpath->query('//*[@token]'); - $this->assertTrue($nodes->length, 'The token attribute was found in the cached markup'); - $token = ''; - if ($nodes->length) { - $token = $nodes->item(0)->getAttribute('token'); - } - $this->assertIdentical($token, $expected_token, 'The tokens are identical'); - // Verify the token is in the cached element. - $expected_element = array( - '#markup' => '', - '#post_render_cache' => array( - 'common_test_post_render_cache_placeholder' => array( - $expected_token => $context, - ), - ), - '#cache' => array('tags' => array()), - ); - $this->assertIdentical($cached_element, $expected_element, 'The correct data is cached: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.'); - - // GET request: #cache enabled, cache hit. - drupal_static_reset('_drupal_add_js'); - $element = $test_element; - $element['#cache'] = array('cid' => 'render_cache_placeholder_test_GET'); - $output = drupal_render($element); - $this->assertIdentical($output, $expected_output, 'Placeholder was replaced in output'); - $this->assertFalse(isset($element['#printed']), 'Cache hit'); - $this->assertIdentical($element['#markup'], $expected_output, 'Placeholder was replaced in #markup.'); - $settings = $this->parseDrupalSettings(drupal_get_js()); - $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); - - // Restore the previous request method. - \Drupal::request()->setMethod($request_method); - } - - /** - * Tests post-render cache-integrated 'render_cache_placeholder' child - * element. - */ - function testDrupalRenderChildElementRenderCachePlaceholder() { - $context = array('bar' => $this->randomContextValue()); - $container = array( - '#type' => 'container', - ); - $test_element = array( - '#type' => 'render_cache_placeholder', - '#context' => $context, - '#callback' => 'common_test_post_render_cache_placeholder', - '#prefix' => '', - '#suffix' => '' - ); - $container['test_element'] = $test_element; - $expected_output = '
' . $context['bar'] . '
' . "\n"; - - // #cache disabled. - drupal_static_reset('_drupal_add_js'); - $element = $container; - $output = drupal_render($element); - $this->assertIdentical($output, $expected_output, 'Placeholder was replaced in output'); - $settings = $this->parseDrupalSettings(drupal_get_js()); - $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); - - // The cache system is turned off for POST requests. - $request_method = \Drupal::request()->getMethod(); - \Drupal::request()->setMethod('GET'); - - // GET request: #cache enabled, cache miss. - drupal_static_reset('_drupal_add_js'); - $element = $container; - $element['#cache'] = array('cid' => 'render_cache_placeholder_test_GET'); - $element['test_element']['#cache'] = array('cid' => 'render_cache_placeholder_test_child_GET'); - // Simulate element rendering in a template, where sub-items of a renderable - // can be sent to drupal_render() before the parent. - $child = &$element['test_element']; - $element['#children'] = drupal_render($child, TRUE); - // Eventually, drupal_render() gets called on the root element. - $output = drupal_render($element); - $this->assertIdentical($output, $expected_output, 'Placeholder was replaced in output'); - $this->assertTrue(isset($element['#printed']), 'No cache hit'); - $this->assertIdentical($element['#markup'], $expected_output, 'Placeholder was replaced in #markup.'); - $settings = $this->parseDrupalSettings(drupal_get_js()); - $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); - - // GET request: validate cached data for child element. - $child_tokens = array_keys($element['test_element']['#post_render_cache']['common_test_post_render_cache_placeholder']); - $parent_tokens = array_keys($element['#post_render_cache']['common_test_post_render_cache_placeholder']); - $expected_token = $child_tokens[0]; - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_child_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; - // Parse unique token out of the cached markup. - $dom = Html::load($cached_element['#markup']); - $xpath = new \DOMXPath($dom); - $nodes = $xpath->query('//*[@token]'); - $this->assertTrue($nodes->length, 'The token attribute was found in the cached child element markup'); - $token = ''; - if ($nodes->length) { - $token = $nodes->item(0)->getAttribute('token'); - } - $this->assertIdentical($token, $expected_token, 'The tokens are identical for the child element'); - // Verify the token is in the cached element. - $expected_element = array( - '#markup' => '', - '#post_render_cache' => array( - 'common_test_post_render_cache_placeholder' => array( - $expected_token => $context, - ), - ), - '#cache' => array('tags' => array()), - ); - $this->assertIdentical($cached_element, $expected_element, 'The correct data is cached for the child element: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.'); - - // GET request: validate cached data (for the parent/entire render array). - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; - // Parse unique token out of the cached markup. - $dom = Html::load($cached_element['#markup']); - $xpath = new \DOMXPath($dom); - $nodes = $xpath->query('//*[@token]'); - $this->assertTrue($nodes->length, 'The token attribute was found in the cached parent element markup'); - $token = ''; - if ($nodes->length) { - $token = $nodes->item(0)->getAttribute('token'); - } - $this->assertIdentical($token, $expected_token, 'The tokens are identical for the parent element'); - // Verify the token is in the cached element. - $expected_element = array( - '#markup' => '
' . "\n", - '#post_render_cache' => array( - 'common_test_post_render_cache_placeholder' => array( - $expected_token => $context, - ), - ), - '#cache' => array('tags' => array()), - ); - $this->assertIdentical($cached_element, $expected_element, 'The correct data is cached for the parent element: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.'); - - // GET request: validate cached data. - // Check the cache of the child element again after the parent has been - // rendered. - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_child_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; - // Verify that the child element contains the correct - // render_cache_placeholder markup. - $expected_token = $child_tokens[0]; - $dom = Html::load($cached_element['#markup']); - $xpath = new \DOMXPath($dom); - $nodes = $xpath->query('//*[@token]'); - $this->assertTrue($nodes->length, 'The token attribute was found in the cached child element markup'); - $token = ''; - if ($nodes->length) { - $token = $nodes->item(0)->getAttribute('token'); - } - $this->assertIdentical($token, $expected_token, 'The tokens are identical for the child element'); - // Verify the token is in the cached element. - $expected_element = array( - '#markup' => '', - '#post_render_cache' => array( - 'common_test_post_render_cache_placeholder' => array( - $expected_token => $context, - ), - ), - '#cache' => array('tags' => array()), - ); - $this->assertIdentical($cached_element, $expected_element, 'The correct data is cached for the child element: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.'); - - // GET request: #cache enabled, cache hit. - drupal_static_reset('_drupal_add_js'); - $element = $container; - $element['#cache'] = array('cid' => 'render_cache_placeholder_test_GET'); - // Simulate element rendering in a template, where sub-items of a renderable - // can be sent to drupal_render before the parent. - $child = &$element['test_element']; - $element['#children'] = drupal_render($child, TRUE); - $output = drupal_render($element); - $this->assertIdentical($output, $expected_output, 'Placeholder was replaced in output'); - $this->assertFalse(isset($element['#printed']), 'Cache hit'); - $this->assertIdentical($element['#markup'], $expected_output, 'Placeholder was replaced in #markup.'); - $settings = $this->parseDrupalSettings(drupal_get_js()); - $this->assertIdentical($settings['common_test'], $context, '#attached is modified; JavaScript setting is added to page.'); - - // Restore the previous request method. - \Drupal::request()->setMethod($request_method); - } - protected function parseDrupalSettings($html) { $startToken = 'drupalSettings = '; $endToken = '}'; diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 081d68d..b9f40f9 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -641,12 +641,6 @@ function system_element_info() { '#theme' => 'table', ); - // Other elements. - $types['render_cache_placeholder'] = array( - '#callback' => '', - '#context' => array(), - ); - return $types; }