3073c4f24c109784214227853082c8814ce91f85
[yaffs-website] / web / core / modules / datetime / tests / src / Kernel / Views / FilterDateTimeTest.php
1 <?php
2
3 namespace Drupal\Tests\datetime\Kernel\Views;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the Drupal\datetime\Plugin\views\filter\Date handler.
10  *
11  * @group datetime
12  */
13 class FilterDateTimeTest extends DateTimeHandlerTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $testViews = ['test_filter_datetime'];
19
20   /**
21    * For offset tests, set a date 1 day in the future.
22    */
23   protected static $date;
24
25   /**
26    * Use a non-UTC timezone.
27    *
28    * @var string
29    */
30   protected static $timezone = 'America/Vancouver';
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp($import_test_views = TRUE) {
36     parent::setUp($import_test_views);
37
38     static::$date = REQUEST_TIME + 86400;
39
40     // Set the timezone.
41     date_default_timezone_set(static::$timezone);
42
43     // Add some basic test nodes.
44     $dates = [
45       '2000-10-10T00:01:30',
46       '2001-10-10T12:12:12',
47       '2002-10-10T14:14:14',
48       // The date storage timezone is used (this mimics the steps taken in the
49       // widget: \Drupal\datetime\Plugin\Field\FieldWidget::messageFormValues().
50       \Drupal::service('date.formatter')->format(static::$date, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
51     ];
52     foreach ($dates as $date) {
53       $node = Node::create([
54         'title' => $this->randomMachineName(8),
55         'type' => 'page',
56         'field_date' => [
57           'value' => $date,
58         ]
59       ]);
60       $node->save();
61       $this->nodes[] = $node;
62     }
63   }
64
65   /**
66    * Test filter operations.
67    */
68   public function testDatetimeFilter() {
69     $this->_testOffset();
70     $this->_testBetween();
71     $this->_testExact();
72   }
73
74   /**
75    * Test offset operations.
76    */
77   protected function _testOffset() {
78     $view = Views::getView('test_filter_datetime');
79     $field = static::$field_name . '_value';
80
81     // Test simple operations.
82     $view->initHandlers();
83
84     $view->filter[$field]->operator = '>';
85     $view->filter[$field]->value['type'] = 'offset';
86     $view->filter[$field]->value['value'] = '+1 hour';
87     $view->setDisplay('default');
88     $this->executeView($view);
89     $expected_result = [
90       ['nid' => $this->nodes[3]->id()],
91     ];
92     $this->assertIdenticalResultset($view, $expected_result, $this->map);
93     $view->destroy();
94
95     // Test offset for between operator.
96     $view->initHandlers();
97     $view->filter[$field]->operator = 'between';
98     $view->filter[$field]->value['type'] = 'offset';
99     $view->filter[$field]->value['max'] = '+2 days';
100     $view->filter[$field]->value['min'] = '+1 hour';
101     $view->setDisplay('default');
102     $this->executeView($view);
103     $expected_result = [
104       ['nid' => $this->nodes[3]->id()],
105     ];
106     $this->assertIdenticalResultset($view, $expected_result, $this->map);
107   }
108
109   /**
110    * Test between operations.
111    */
112   protected function _testBetween() {
113     $view = Views::getView('test_filter_datetime');
114     $field = static::$field_name . '_value';
115
116     // Test between with min and max.
117     $view->initHandlers();
118     $view->filter[$field]->operator = 'between';
119     $view->filter[$field]->value['min'] = '2001-01-01';
120     $view->filter[$field]->value['max'] = '2002-01-01';
121     $view->setDisplay('default');
122     $this->executeView($view);
123     $expected_result = [
124       ['nid' => $this->nodes[1]->id()],
125     ];
126     $this->assertIdenticalResultset($view, $expected_result, $this->map);
127     $view->destroy();
128
129     // Test between with just max.
130     $view->initHandlers();
131     $view->filter[$field]->operator = 'between';
132     $view->filter[$field]->value['max'] = '2002-01-01';
133     $view->setDisplay('default');
134     $this->executeView($view);
135     $expected_result = [
136       ['nid' => $this->nodes[0]->id()],
137       ['nid' => $this->nodes[1]->id()],
138     ];
139     $this->assertIdenticalResultset($view, $expected_result, $this->map);
140     $view->destroy();
141
142     // Test not between with min and max.
143     $view->initHandlers();
144     $view->filter[$field]->operator = 'not between';
145     $view->filter[$field]->value['min'] = '2001-01-01';
146     // Set maximum date to date of node 1 to test range borders.
147     $view->filter[$field]->value['max'] = '2001-10-10T12:12:12';
148     $view->setDisplay('default');
149     $this->executeView($view);
150     $expected_result = [
151       ['nid' => $this->nodes[0]->id()],
152       ['nid' => $this->nodes[2]->id()],
153       ['nid' => $this->nodes[3]->id()],
154     ];
155     $this->assertIdenticalResultset($view, $expected_result, $this->map);
156     $view->destroy();
157
158     // Test not between with just max.
159     $view->initHandlers();
160     $view->filter[$field]->operator = 'not between';
161     $view->filter[$field]->value['max'] = '2001-01-01';
162     $view->setDisplay('default');
163     $this->executeView($view);
164     $expected_result = [
165       ['nid' => $this->nodes[1]->id()],
166       ['nid' => $this->nodes[2]->id()],
167       ['nid' => $this->nodes[3]->id()],
168     ];
169     $this->assertIdenticalResultset($view, $expected_result, $this->map);
170   }
171
172   /**
173    * Test exact date matching.
174    */
175   protected function _testExact() {
176     $view = Views::getView('test_filter_datetime');
177     $field = static::$field_name . '_value';
178
179     // Test between with min and max.
180     $view->initHandlers();
181     $view->filter[$field]->operator = '=';
182     $view->filter[$field]->value['min'] = '';
183     $view->filter[$field]->value['max'] = '';
184     // Use the date from node 3. Use the site timezone (mimics a value entered
185     // through the UI).
186     $view->filter[$field]->value['value'] = \Drupal::service('date.formatter')->format(static::$date, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, static::$timezone);
187     $view->setDisplay('default');
188     $this->executeView($view);
189     $expected_result = [
190       ['nid' => $this->nodes[3]->id()],
191     ];
192     $this->assertIdenticalResultset($view, $expected_result, $this->map);
193     $view->destroy();
194
195   }
196
197 }