You are here

public function RendererTest::testRenderCache in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 @covers \Drupal\Core\Render\RenderCache::createCacheID

File

core/tests/Drupal/Tests/Core/Render/RendererTest.php, line 616
Contains \Drupal\Tests\Core\Render\RendererTest.

Class

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

Namespace

Drupal\Tests\Core\Render

Code

public function testRenderCache() {
  $this
    ->setUpRequest();
  $this
    ->setupMemoryCache();

  // Create an empty element.
  $test_element = [
    '#cache' => [
      'keys' => [
        'render_cache_test',
      ],
      'tags' => [
        'render_cache_tag',
      ],
    ],
    '#markup' => '',
    'child' => [
      '#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.
  $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.
  $cache_item = $this->cacheFactory
    ->get('render')
    ->get('render_cache_test:en:stark');
  $this
    ->assertSame(Cache::mergeTags($expected_tags, [
    'rendered',
  ]), $cache_item->tags);
}