X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fdatetime%2Ftests%2Fsrc%2FKernel%2FViews%2FSortDateTimeTest.php;fp=web%2Fcore%2Fmodules%2Fdatetime%2Ftests%2Fsrc%2FKernel%2FViews%2FSortDateTimeTest.php;h=7375c4db29e26997b1709a44f836f432c48c1d42;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/datetime/tests/src/Kernel/Views/SortDateTimeTest.php b/web/core/modules/datetime/tests/src/Kernel/Views/SortDateTimeTest.php new file mode 100644 index 000000000..7375c4db2 --- /dev/null +++ b/web/core/modules/datetime/tests/src/Kernel/Views/SortDateTimeTest.php @@ -0,0 +1,130 @@ + $this->randomMachineName(8), + 'type' => 'page', + 'field_date' => [ + 'value' => $date, + ] + ]); + $node->save(); + $this->nodes[] = $node; + } + } + + /** + * Tests the datetime sort handler. + */ + public function testDateTimeSort() { + $field = static::$field_name . '_value'; + $view = Views::getView('test_sort_datetime'); + + // Set granularity to 'minute', and the secondary node ID order should + // define the order of nodes with the same minute. + $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(); + + // Check ASC. + $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(); + + // Change granularity to 'year', and the secondary node ID order should + // define the order of nodes with the same year. + $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(); + + // Change granularity to 'second'. + $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(); + } + +}