function RendererTest::testRenderCacheProperties

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

Tests that #cache_properties are properly handled.

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

@dataProvider providerTestRenderCacheProperties

Parameters

array $expected_results: An associative array of expected results keyed by property name.

File

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

Class

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

Namespace

Drupal\Tests\Core\Render

Code

public function testRenderCacheProperties(array $expected_results) : void {
    $this->setUpRequest();
    $this->setUpMemoryCache();
    $element = $original = [
        '#cache' => [
            'keys' => [
                'render_cache_test',
            ],
        ],
        // Collect expected property names.
'#cache_properties' => array_keys(array_filter($expected_results)),
        'child1' => [
            '#markup' => Markup::create('1'),
        ],
        'child2' => [
            '#markup' => Markup::create('2'),
        ],
        // Mark the value as safe.
'#custom_property' => Markup::create('custom_value'),
        '#custom_property_array' => [
            'custom value',
        ],
    ];
    $this->renderer
        ->renderRoot($element);
    $cache = $this->cacheFactory
        ->get('render');
    $data = $cache->get([
        'render_cache_test',
    ], CacheableMetadata::createFromRenderArray($element))->data;
    // Check that parent markup is ignored when caching children's markup.
    $this->assertEquals($data['#markup'] === '', (bool) Element::children($data));
    // Check that the element properties are cached as specified.
    foreach ($expected_results as $property => $expected) {
        $cached = !empty($data[$property]);
        $this->assertEquals($cached, (bool) $expected);
        // Check that only the #markup key is preserved for children.
        if ($cached) {
            $this->assertEquals($data[$property], $original[$property]);
        }
    }
    // #custom_property_array can not be a safe_cache_property.
    $safe_cache_properties = array_diff(Element::properties(array_filter($expected_results)), [
        '#custom_property_array',
    ]);
    foreach ($safe_cache_properties as $cache_property) {
        $this->assertInstanceOf(MarkupInterface::class, $data[$cache_property]);
    }
}

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