function SortRandomTest::testRandomOrderingWithRenderCaching

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/SortRandomTest.php \Drupal\Tests\views\Kernel\Handler\SortRandomTest::testRandomOrderingWithRenderCaching()
  2. 8.9.x core/modules/views/tests/src/Kernel/Handler/SortRandomTest.php \Drupal\Tests\views\Kernel\Handler\SortRandomTest::testRandomOrderingWithRenderCaching()
  3. 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 111

Class

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

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRandomOrderingWithRenderCaching() : void {
    $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->renderInIsolation($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->renderInIsolation($build);
    // Ensure that the random ordering works and don't produce the same result.
    // We use assertNotSame and cast values to strings since HTML tags are
    // significant.
    $this->assertNotSame((string) $result, (string) $result2);
}

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