3 namespace Drupal\datetime\Tests\Views;
5 use Drupal\views\Views;
8 * Tests for core Drupal\datetime\Plugin\views\sort\Date handler.
12 class SortDateTimeTest extends DateTimeHandlerTestBase {
17 public static $testViews = ['test_sort_datetime'];
22 protected function setUp() {
25 // Add some basic test nodes.
27 '2014-10-10T00:03:00',
28 '2000-10-10T00:01:00',
29 '2000-10-10T00:02:00',
30 '2000-10-10T00:03:00',
31 '2000-10-10T00:03:02',
32 '2000-10-10T00:03:01',
33 '2000-10-10T00:03:03',
35 foreach ($dates as $date) {
36 $this->nodes[] = $this->drupalCreateNode([
45 * Tests the datetime sort handler.
47 public function testDateTimeSort() {
48 $field = static::$field_name . '_value';
49 $view = Views::getView('test_sort_datetime');
51 // Set granularity to 'minute', and the secondary node ID order should
52 // define the order of nodes with the same minute.
53 $view->initHandlers();
54 $view->sort[$field]->options['granularity'] = 'minute';
55 $view->setDisplay('default');
56 $this->executeView($view);
58 ['nid' => $this->nodes[0]->id()],
59 ['nid' => $this->nodes[3]->id()],
60 ['nid' => $this->nodes[4]->id()],
61 ['nid' => $this->nodes[5]->id()],
62 ['nid' => $this->nodes[6]->id()],
63 ['nid' => $this->nodes[2]->id()],
64 ['nid' => $this->nodes[1]->id()],
66 $this->assertIdenticalResultset($view, $expected_result, $this->map);
70 $view->initHandlers();
71 $field = static::$field_name . '_value';
72 $view->sort[$field]->options['order'] = 'ASC';
73 $view->setDisplay('default');
74 $this->executeView($view);
76 ['nid' => $this->nodes[1]->id()],
77 ['nid' => $this->nodes[2]->id()],
78 ['nid' => $this->nodes[3]->id()],
79 ['nid' => $this->nodes[5]->id()],
80 ['nid' => $this->nodes[4]->id()],
81 ['nid' => $this->nodes[6]->id()],
82 ['nid' => $this->nodes[0]->id()],
84 $this->assertIdenticalResultset($view, $expected_result, $this->map);
87 // Change granularity to 'year', and the secondary node ID order should
88 // define the order of nodes with the same year.
89 $view->initHandlers();
90 $view->sort[$field]->options['granularity'] = 'year';
91 $view->sort[$field]->options['order'] = 'DESC';
92 $view->setDisplay('default');
93 $this->executeView($view);
95 ['nid' => $this->nodes[0]->id()],
96 ['nid' => $this->nodes[1]->id()],
97 ['nid' => $this->nodes[2]->id()],
98 ['nid' => $this->nodes[3]->id()],
99 ['nid' => $this->nodes[4]->id()],
100 ['nid' => $this->nodes[5]->id()],
101 ['nid' => $this->nodes[6]->id()],
103 $this->assertIdenticalResultset($view, $expected_result, $this->map);
106 // Change granularity to 'second'.
107 $view->initHandlers();
108 $view->sort[$field]->options['granularity'] = 'second';
109 $view->sort[$field]->options['order'] = 'DESC';
110 $view->setDisplay('default');
111 $this->executeView($view);
113 ['nid' => $this->nodes[0]->id()],
114 ['nid' => $this->nodes[6]->id()],
115 ['nid' => $this->nodes[4]->id()],
116 ['nid' => $this->nodes[5]->id()],
117 ['nid' => $this->nodes[3]->id()],
118 ['nid' => $this->nodes[2]->id()],
119 ['nid' => $this->nodes[1]->id()],
121 $this->assertIdenticalResultset($view, $expected_result, $this->map);