You are here

public function SortRandomTest::testRandomOrderingWithRenderCaching in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/SortRandomTest.php \Drupal\Tests\views\Kernel\Handler\SortRandomTest::testRandomOrderingWithRenderCaching()
  2. 10 core/modules/views/tests/src/Kernel/Handler/SortRandomTest.php \Drupal\Tests\views\Kernel\Handler\SortRandomTest::testRandomOrderingWithRenderCaching()

Tests random ordering with tags based caching.

The random sorting should opt out of caching by defining a max age of 0. At the same time, the row render caching still works.

File

core/modules/views/tests/src/Kernel/Handler/SortRandomTest.php, line 109

Class

SortRandomTest
Tests for core Drupal\views\Plugin\views\sort\Random handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRandomOrderingWithRenderCaching() {
  $view_random = $this
    ->getBasicRandomView();
  $display =& $view_random->storage
    ->getDisplay('default');
  $display['display_options']['cache'] = [
    'type' => 'tag',
  ];
  $view_random->storage
    ->save();

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');

  /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
  $render_cache = \Drupal::service('render_cache');
  $original = $build = DisplayPluginBase::buildBasicRenderable($view_random
    ->id(), 'default');
  $result = $renderer
    ->renderPlain($build);
  $original['#cache'] += [
    'contexts' => [],
  ];
  $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container
    ->getParameter('renderer.config')['required_cache_contexts']);
  $this
    ->assertFalse($render_cache
    ->get($original), 'Ensure there is no render cache entry.');
  $build = DisplayPluginBase::buildBasicRenderable($view_random
    ->id(), 'default');
  $result2 = $renderer
    ->renderPlain($build);

  // Ensure that the random ordering works and don't produce the same result.
  $this
    ->assertNotEqual($result, $result2);
}