function RendererBubblingTest::testConditionalCacheContextBubblingSelfHealing

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

Tests the self-healing of the redirect with conditional cache contexts.

@todo Revisit now that we have self-healing tests for VariationCache. This is essentially a clone of the other bubbling tests now.

File

core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php, line 317

Class

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

Namespace

Drupal\Tests\Core\Render

Code

public function testConditionalCacheContextBubblingSelfHealing() : void {
    $current_user_role =& $this->currentUserRole;
    $this->setUpRequest();
    $this->setUpMemoryCache();
    $test_element = [
        '#cache' => [
            'keys' => [
                'parent',
            ],
            'tags' => [
                'a',
            ],
        ],
        '#markup' => 'parent',
        'child' => [
            '#cache' => [
                'contexts' => [
                    'user.roles',
                ],
                'tags' => [
                    'b',
                ],
            ],
            'grandchild' => [
                '#access_callback' => function () use (&$current_user_role) {
                    // Only role A cannot access this subtree.
                    return $current_user_role !== 'A';
                },
                '#cache' => [
                    'contexts' => [
                        'foo',
                    ],
                    'tags' => [
                        'c',
                    ],
                    // A lower max-age; the redirecting cache item should be updated.
'max-age' => 1800,
                ],
                'great grandchild' => [
                    '#access_callback' => function () use (&$current_user_role) {
                        // Only role C can access this subtree.
                        return $current_user_role === 'C';
                    },
                    '#cache' => [
                        'contexts' => [
                            'bar',
                        ],
                        'tags' => [
                            'd',
                        ],
                        // A lower max-age; the redirecting cache item should be updated.
'max-age' => 300,
                    ],
                ],
            ],
        ],
    ];
    // Request 1: role A, the grandchild isn't accessible => bubbled cache
    // contexts: user.roles.
    $element = $test_element;
    $current_user_role = 'A';
    $this->renderer
        ->renderRoot($element);
    $this->assertRenderCacheItem([
        'parent',
    ], [
        '#attached' => [],
        '#cache' => [
            'contexts' => [
                'user.roles',
            ],
            'tags' => [
                'a',
                'b',
            ],
            'max-age' => Cache::PERMANENT,
        ],
        '#markup' => 'parent',
    ]);
    // Request 2: role B, the grandchild is accessible => bubbled cache
    // contexts: foo, user.roles + merged max-age: 1800.
    $element = $test_element;
    $current_user_role = 'B';
    $this->renderer
        ->renderRoot($element);
    $this->assertRenderCacheItem([
        'parent',
    ], [
        '#attached' => [],
        '#cache' => [
            'contexts' => [
                'user.roles',
                'foo',
            ],
            'tags' => [
                'a',
                'b',
                'c',
            ],
            'max-age' => 1800,
        ],
        '#markup' => 'parent',
    ]);
    // Verify that request 1 is still cached and accessible.
    $current_user_role = 'A';
    $this->assertRenderCacheItem([
        'parent',
    ], [
        '#attached' => [],
        '#cache' => [
            'contexts' => [
                'user.roles',
            ],
            'tags' => [
                'a',
                'b',
            ],
            'max-age' => Cache::PERMANENT,
        ],
        '#markup' => 'parent',
    ]);
    // Request 3: role C, both the grandchild and the great grandchild are
    // accessible => bubbled cache contexts: foo, bar, user.roles + merged
    // max-age: 300.
    $element = $test_element;
    $current_user_role = 'C';
    $this->renderer
        ->renderRoot($element);
    $this->assertRenderCacheItem([
        'parent',
    ], [
        '#attached' => [],
        '#cache' => [
            'contexts' => [
                'user.roles',
                'foo',
                'bar',
            ],
            'tags' => [
                'a',
                'b',
                'c',
                'd',
            ],
            'max-age' => 300,
        ],
        '#markup' => 'parent',
    ]);
    // Verify that request 2 and 3 are still cached and accessible.
    $current_user_role = 'A';
    $this->assertRenderCacheItem([
        'parent',
    ], [
        '#attached' => [],
        '#cache' => [
            'contexts' => [
                'user.roles',
            ],
            'tags' => [
                'a',
                'b',
            ],
            'max-age' => Cache::PERMANENT,
        ],
        '#markup' => 'parent',
    ]);
    $current_user_role = 'B';
    $this->assertRenderCacheItem([
        'parent',
    ], [
        '#attached' => [],
        '#cache' => [
            'contexts' => [
                'user.roles',
                'foo',
            ],
            'tags' => [
                'a',
                'b',
                'c',
            ],
            'max-age' => 1800,
        ],
        '#markup' => 'parent',
    ]);
}

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