You are here

public function RendererBubblingTest::testContextBubblingCustomCacheBin in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 79
Contains \Drupal\Tests\Core\Render\RendererBubblingTest.

Class

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

Namespace

Drupal\Tests\Core\Render

Code

public function testContextBubblingCustomCacheBin() {
  $bin = $this
    ->randomMachineName();
  $this
    ->setUpRequest();
  $this->memoryCache = new MemoryBackend('render');
  $custom_cache = new MemoryBackend($bin);
  $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' => [
        'bar',
        'foo',
      ],
      'tags' => [],
      'bin' => $bin,
      'max-age' => 3600,
    ],
  ], $bin);
}