function RendererBubblingTest::testContextBubblingCustomCacheBin

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

Tests cache context bubbling with a custom cache bin.

File

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

Class

RendererBubblingTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Render%21Renderer.php/class/Renderer/9" title="Turns a render array into an HTML string." class="local">\Drupal\Core\Render\Renderer</a> @group Render

Namespace

Drupal\Tests\Core\Render

Code

public function testContextBubblingCustomCacheBin() {
    $bin = $this->randomMachineName();
    $this->setUpRequest();
    $this->memoryCache = new MemoryBackend();
    $custom_cache = new MemoryBackend();
    $this->cacheFactory
        ->expects($this->atLeastOnce())
        ->method('get')
        ->with($bin)
        ->willReturnCallback(function ($requested_bin) use ($bin, $custom_cache) {
        if ($requested_bin === $bin) {
            return $custom_cache;
        }
        else {
            throw new \Exception();
        }
    });
    $this->cacheContextsManager
        ->expects($this->any())
        ->method('convertTokensToKeys')
        ->willReturnArgument(0);
    $build = [
        '#cache' => [
            'keys' => [
                'parent',
            ],
            'contexts' => [
                'foo',
            ],
            'bin' => $bin,
        ],
        '#markup' => 'parent',
        'child' => [
            '#cache' => [
                'contexts' => [
                    'bar',
                ],
                'max-age' => 3600,
            ],
        ],
    ];
    $this->renderer
        ->renderRoot($build);
    $this->assertRenderCacheItem('parent:foo', [
        '#cache_redirect' => TRUE,
        '#cache' => [
            'keys' => [
                'parent',
            ],
            'contexts' => [
                'foo',
                'bar',
            ],
            'tags' => [],
            'bin' => $bin,
            'max-age' => 3600,
        ],
    ], $bin);
}

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