You are here

public function SortRandomTest::testRandomOrderingWithRenderCaching in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/views/src/Tests/Handler/SortRandomTest.php \Drupal\views\Tests\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/src/Tests/Handler/SortRandomTest.php, line 114
Contains \Drupal\views\Tests\Handler\SortRandomTest.

Class

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

Namespace

Drupal\views\Tests\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);
}