View source
<?php
namespace Drupal\views\Tests\Handler;
use Drupal\Core\Cache\Cache;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Tests\ViewKernelTestBase;
use Drupal\views\Views;
class SortRandomTest extends ViewKernelTestBase {
public static $testViews = array(
'test_view',
);
protected function dataSet() {
$data = parent::dataSet();
for ($i = 0; $i < 55; $i++) {
$data[] = array(
'name' => 'name_' . $i,
'age' => $i,
'job' => 'job_' . $i,
'created' => rand(0, time()),
'status' => 1,
);
}
return $data;
}
protected function getBasicRandomView() {
$view = Views::getView('test_view');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('sorts', array(
'random' => array(
'id' => 'random',
'field' => 'random',
'table' => 'views',
),
));
return $view;
}
public function testRandomOrdering() {
$view = Views::getView('test_view');
$this
->executeView($view);
$this
->assertEqual(count($this
->dataSet()), count($view->result), 'The number of returned rows match.');
$this
->assertIdenticalResultset($view, $this
->dataSet(), array(
'views_test_data_name' => 'name',
'views_test_data_age' => 'age',
));
$view_random = $this
->getBasicRandomView();
$this
->executeView($view_random);
$this
->assertEqual(count($this
->dataSet()), count($view_random->result), 'The number of returned rows match.');
$this
->assertNotIdenticalResultset($view_random, $view->result, array(
'views_test_data_name' => 'views_test_data_name',
'views_test_data_age' => 'views_test_data_name',
));
$view_random_2 = $this
->getBasicRandomView();
$this
->executeView($view_random_2);
$this
->assertEqual(count($this
->dataSet()), count($view_random_2->result), 'The number of returned rows match.');
$this
->assertNotIdenticalResultset($view_random, $view->result, array(
'views_test_data_name' => 'views_test_data_name',
'views_test_data_age' => 'views_test_data_name',
));
}
public function testRandomOrderingWithRenderCaching() {
$view_random = $this
->getBasicRandomView();
$display =& $view_random->storage
->getDisplay('default');
$display['display_options']['cache'] = [
'type' => 'tag',
];
$view_random->storage
->save();
$renderer = \Drupal::service('renderer');
$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);
$this
->assertNotEqual($result, $result2);
}
}