Version 1
[yaffs-website] / web / core / modules / datetime / src / Tests / Views / SortDateTimeTest.php
1 <?php
2
3 namespace Drupal\datetime\Tests\Views;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests for core Drupal\datetime\Plugin\views\sort\Date handler.
9  *
10  * @group datetime
11  */
12 class SortDateTimeTest extends DateTimeHandlerTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $testViews = ['test_sort_datetime'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     // Add some basic test nodes.
26     $dates = [
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',
34     ];
35     foreach ($dates as $date) {
36       $this->nodes[] = $this->drupalCreateNode([
37         'field_date' => [
38           'value' => $date,
39         ]
40       ]);
41     }
42   }
43
44   /**
45    * Tests the datetime sort handler.
46    */
47   public function testDateTimeSort() {
48     $field = static::$field_name . '_value';
49     $view = Views::getView('test_sort_datetime');
50
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);
57     $expected_result = [
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()],
65     ];
66     $this->assertIdenticalResultset($view, $expected_result, $this->map);
67     $view->destroy();
68
69     // Check ASC.
70     $view->initHandlers();
71     $field = static::$field_name . '_value';
72     $view->sort[$field]->options['order'] = 'ASC';
73     $view->setDisplay('default');
74     $this->executeView($view);
75     $expected_result = [
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()],
83     ];
84     $this->assertIdenticalResultset($view, $expected_result, $this->map);
85     $view->destroy();
86
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);
94     $expected_result = [
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()],
102     ];
103     $this->assertIdenticalResultset($view, $expected_result, $this->map);
104     $view->destroy();
105
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);
112     $expected_result = [
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()],
120     ];
121     $this->assertIdenticalResultset($view, $expected_result, $this->map);
122     $view->destroy();
123   }
124
125 }