View source
<?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
class SortDateTimeTest extends DateTimeHandlerTestBase {
public static $testViews = [
'test_sort_datetime',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$dates = [
'2014-10-10T00:03:00',
'2000-10-10T00:01:00',
'2000-10-10T00:02:00',
'2000-10-10T00:03:00',
'2000-10-10T00:03:02',
'2000-10-10T00:03:01',
'2000-10-10T00:03:03',
];
foreach ($dates as $date) {
$node = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $date,
],
]);
$node
->save();
$this->nodes[] = $node;
}
}
public function testDateTimeSort() {
$field = static::$field_name . '_value';
$view = Views::getView('test_sort_datetime');
$view
->initHandlers();
$view->sort[$field]->options['granularity'] = 'minute';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[0]
->id(),
],
[
'nid' => $this->nodes[3]
->id(),
],
[
'nid' => $this->nodes[4]
->id(),
],
[
'nid' => $this->nodes[5]
->id(),
],
[
'nid' => $this->nodes[6]
->id(),
],
[
'nid' => $this->nodes[2]
->id(),
],
[
'nid' => $this->nodes[1]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$field = static::$field_name . '_value';
$view->sort[$field]->options['order'] = 'ASC';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[1]
->id(),
],
[
'nid' => $this->nodes[2]
->id(),
],
[
'nid' => $this->nodes[3]
->id(),
],
[
'nid' => $this->nodes[5]
->id(),
],
[
'nid' => $this->nodes[4]
->id(),
],
[
'nid' => $this->nodes[6]
->id(),
],
[
'nid' => $this->nodes[0]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->sort[$field]->options['granularity'] = 'year';
$view->sort[$field]->options['order'] = 'DESC';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[0]
->id(),
],
[
'nid' => $this->nodes[1]
->id(),
],
[
'nid' => $this->nodes[2]
->id(),
],
[
'nid' => $this->nodes[3]
->id(),
],
[
'nid' => $this->nodes[4]
->id(),
],
[
'nid' => $this->nodes[5]
->id(),
],
[
'nid' => $this->nodes[6]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->sort[$field]->options['granularity'] = 'second';
$view->sort[$field]->options['order'] = 'DESC';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[0]
->id(),
],
[
'nid' => $this->nodes[6]
->id(),
],
[
'nid' => $this->nodes[4]
->id(),
],
[
'nid' => $this->nodes[5]
->id(),
],
[
'nid' => $this->nodes[3]
->id(),
],
[
'nid' => $this->nodes[2]
->id(),
],
[
'nid' => $this->nodes[1]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
}
}