function RendererTest::testRenderCache

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/RendererTest.php \Drupal\Tests\Core\Render\RendererTest::testRenderCache()
  2. 8.9.x core/tests/Drupal/Tests/Core/Render/RendererTest.php \Drupal\Tests\Core\Render\RendererTest::testRenderCache()
  3. 10 core/tests/Drupal/Tests/Core/Render/RendererTest.php \Drupal\Tests\Core\Render\RendererTest::testRenderCache()

@covers ::render @covers ::doRender @covers \Drupal\Core\Render\RenderCache::get @covers \Drupal\Core\Render\RenderCache::set

@dataProvider providerRenderCache

File

core/tests/Drupal/Tests/Core/Render/RendererTest.php, line 833

Class

RendererTest
@coversDefaultClass \Drupal\Core\Render\Renderer @group Render

Namespace

Drupal\Tests\Core\Render

Code

public function testRenderCache($child_access, $expected_tags) : void {
    $this->setUpRequest();
    $this->setUpMemoryCache();
    // Create an empty element.
    $test_element = [
        '#cache' => [
            'keys' => [
                'render_cache_test',
            ],
            'tags' => [
                'render_cache_tag',
            ],
        ],
        '#markup' => '',
        'child' => [
            '#access' => $child_access,
            '#cache' => [
                'keys' => [
                    'render_cache_test_child',
                ],
                'tags' => [
                    'render_cache_tag_child:1',
                    'render_cache_tag_child:2',
                ],
            ],
            '#markup' => '',
        ],
    ];
    // Render the element and confirm that it goes through the rendering
    // process (which will set $element['#printed']).
    $element = $test_element;
    $this->renderer
        ->renderRoot($element);
    $this->assertTrue(isset($element['#printed']), 'No cache hit');
    // Render the element again and confirm that it is retrieved from the cache
    // instead (so $element['#printed'] will not be set).
    $element = $test_element;
    $this->renderer
        ->renderRoot($element);
    $this->assertFalse(isset($element['#printed']), 'Cache hit');
    // Test that cache tags are correctly collected from the render element,
    // including the ones from its subchild.
    $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.
    $cache_item = $this->cacheFactory
        ->get('render')
        ->get([
        'render_cache_test',
    ], CacheableMetadata::createFromRenderArray($element));
    $this->assertSame(Cache::mergeTags($expected_tags, [
        'rendered',
    ]), $cache_item->tags);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.