Commit b8ae2f14 authored by catch's avatar catch
Browse files

Issue #3374253 by TwoD, smustgrave, catch, lauriii: The renderer throws away...

Issue #3374253 by TwoD, smustgrave, catch, lauriii: The renderer throws away cache metadata from access result if it is not allowed
parent 12f09143
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -233,6 +233,15 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
      if ($elements['#access'] instanceof AccessResultInterface) {
        $this->addCacheableDependency($elements, $elements['#access']);
        if (!$elements['#access']->isAllowed()) {
          // Abort, but bubble new cache metadata from the access result.
          $context = $this->getCurrentRenderContext();
          if (!isset($context)) {
            trigger_error("Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead.", E_USER_WARNING);
            return '';
          }
          $context->push(new BubbleableMetadata());
          $context->update($elements);
          $context->bubble();
          return '';
        }
      }
@@ -592,7 +601,7 @@ public function executeInRenderContext(RenderContext $context, callable $callabl
  /**
   * Returns the current render context.
   *
   * @return \Drupal\Core\Render\RenderContext
   * @return \Drupal\Core\Render\RenderContext|null
   *   The current render context.
   */
  protected function getCurrentRenderContext() {
+0 −5
Original line number Diff line number Diff line
@@ -645,11 +645,6 @@ parameters:
			count: 1
			path: lib/Drupal/Core/Render/RenderCache.php

		-
			message: "#^Variable \\$context in isset\\(\\) always exists and is not nullable\\.$#"
			count: 1
			path: lib/Drupal/Core/Render/Renderer.php

		-
			message: "#^Variable \\$transaction in isset\\(\\) always exists and is not nullable\\.$#"
			count: 1
+36 −6
Original line number Diff line number Diff line
@@ -790,14 +790,48 @@ public function testRenderWithThemeArguments() {
    $this->assertEquals($this->renderer->renderRoot($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works');
  }

  /**
   * Provides a list of access conditions and expected cache metadata.
   *
   * @return array
   */
  public function providerRenderCache() {
    return [
      'full access' => [
        NULL,
        [
          'render_cache_tag',
          'render_cache_tag_child:1',
          'render_cache_tag_child:2',
        ],
      ],
      'no child access' => [
        AccessResult::forbidden()
          ->addCacheTags([
            'render_cache_tag_child_access:1',
            'render_cache_tag_child_access:2',
          ]),
        [
          'render_cache_tag',
          'render_cache_tag_child:1',
          'render_cache_tag_child:2',
          'render_cache_tag_child_access:1',
          'render_cache_tag_child_access:2',
        ],
      ],
    ];
  }

  /**
   * @covers ::render
   * @covers ::doRender
   * @covers \Drupal\Core\Render\RenderCache::get
   * @covers \Drupal\Core\Render\RenderCache::set
   * @covers \Drupal\Core\Render\RenderCache::createCacheID
   *
   * @dataProvider providerRenderCache
   */
  public function testRenderCache() {
  public function testRenderCache($child_access, $expected_tags) {
    $this->setUpRequest();
    $this->setupMemoryCache();

@@ -809,6 +843,7 @@ public function testRenderCache() {
      ],
      '#markup' => '',
      'child' => [
        '#access' => $child_access,
        '#cache' => [
          'keys' => ['render_cache_test_child'],
          'tags' => ['render_cache_tag_child:1', 'render_cache_tag_child:2'],
@@ -831,11 +866,6 @@ public function testRenderCache() {

    // Test that cache tags are correctly collected from the render element,
    // including the ones from its subchild.
    $expected_tags = [
      'render_cache_tag',
      'render_cache_tag_child:1',
      'render_cache_tag_child:2',
    ];
    $this->assertEquals($expected_tags, $element['#cache']['tags'], 'Cache tags were collected from the element and its subchild.');

    // The cache item also has a 'rendered' cache tag.