Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Handler / ArgumentDateTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Handler;
4
5 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the core date argument handlers.
10  *
11  * @group views
12  * @see \Drupal\views\Plugin\views\argument\Date
13  */
14 class ArgumentDateTest extends ViewsKernelTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_argument_date'];
22
23   /**
24    * Stores the column map for this testCase.
25    *
26    * @var array
27    */
28   protected $columnMap = [
29     'id' => 'id',
30   ];
31
32   /**
33    * {@inheritdoc}
34    */
35   public function viewsData() {
36     $data = parent::viewsData();
37
38     $date_plugins = [
39       'date_fulldate',
40       'date_day',
41       'date_month',
42       'date_week',
43       'date_year',
44       'date_year_month',
45     ];
46     foreach ($date_plugins as $plugin_id) {
47       $data['views_test_data'][$plugin_id] = $data['views_test_data']['created'];
48       $data['views_test_data'][$plugin_id]['real field'] = 'created';
49       $data['views_test_data'][$plugin_id]['argument']['id'] = $plugin_id;
50     }
51     return $data;
52   }
53
54   /**
55    * Tests the CreatedFullDate handler.
56    *
57    * @see \Drupal\node\Plugin\views\argument\CreatedFullDate
58    */
59   public function testCreatedFullDateHandler() {
60     $view = Views::getView('test_argument_date');
61     $view->setDisplay('default');
62     $this->executeView($view, ['20000102']);
63     $expected = [];
64     $expected[] = ['id' => 2];
65     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
66     $view->destroy();
67
68     $view->setDisplay('default');
69     $this->executeView($view, ['20000101']);
70     $expected = [];
71     $expected[] = ['id' => 1];
72     $expected[] = ['id' => 3];
73     $expected[] = ['id' => 4];
74     $expected[] = ['id' => 5];
75     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
76     $view->destroy();
77
78     $view->setDisplay('default');
79     $this->executeView($view, ['20001023']);
80     $expected = [];
81     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
82     $view->destroy();
83   }
84
85   /**
86    * Tests the Day handler.
87    *
88    * @see \Drupal\node\Plugin\views\argument\CreatedDay
89    */
90   public function testDayHandler() {
91     $view = Views::getView('test_argument_date');
92     $view->setDisplay('embed_1');
93     $this->executeView($view, ['02']);
94     $expected = [];
95     $expected[] = ['id' => 2];
96     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
97     $view->destroy();
98
99     $view->setDisplay('embed_1');
100     $this->executeView($view, ['01']);
101     $expected = [];
102     $expected[] = ['id' => 1];
103     $expected[] = ['id' => 3];
104     $expected[] = ['id' => 4];
105     $expected[] = ['id' => 5];
106     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
107     $view->destroy();
108
109     $view->setDisplay('embed_1');
110     $this->executeView($view, ['23']);
111     $expected = [];
112     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
113   }
114
115   /**
116    * Tests the Month handler.
117    *
118    * @see \Drupal\node\Plugin\views\argument\CreatedMonth
119    */
120   public function testMonthHandler() {
121     $view = Views::getView('test_argument_date');
122     $view->setDisplay('embed_2');
123     $this->executeView($view, ['01']);
124     $expected = [];
125     $expected[] = ['id' => 1];
126     $expected[] = ['id' => 2];
127     $expected[] = ['id' => 3];
128     $expected[] = ['id' => 4];
129     $expected[] = ['id' => 5];
130     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
131     $view->destroy();
132
133     $view->setDisplay('embed_2');
134     $this->executeView($view, ['12']);
135     $expected = [];
136     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
137   }
138
139   /**
140    * Tests the Week handler.
141    *
142    * @see \Drupal\node\Plugin\views\argument\CreatedWeek
143    */
144   public function testWeekHandler() {
145     $this->container->get('database')->update('views_test_data')
146       ->fields(['created' => gmmktime(0, 0, 0, 9, 26, 2008)])
147       ->condition('id', 1)
148       ->execute();
149
150     $this->container->get('database')->update('views_test_data')
151       ->fields(['created' => gmmktime(0, 0, 0, 2, 29, 2004)])
152       ->condition('id', 2)
153       ->execute();
154
155     $this->container->get('database')->update('views_test_data')
156       ->fields(['created' => gmmktime(0, 0, 0, 1, 1, 2000)])
157       ->condition('id', 3)
158       ->execute();
159
160     $this->container->get('database')->update('views_test_data')
161       ->fields(['created' => gmmktime(0, 0, 0, 1, 10, 2000)])
162       ->condition('id', 4)
163       ->execute();
164
165     $this->container->get('database')->update('views_test_data')
166       ->fields(['created' => gmmktime(0, 0, 0, 2, 1, 2000)])
167       ->condition('id', 5)
168       ->execute();
169
170     $view = Views::getView('test_argument_date');
171     $view->setDisplay('embed_3');
172     // Check the week calculation for a leap year.
173     // @see http://wikipedia.org/wiki/ISO_week_date#Calculation
174     $this->executeView($view, ['39']);
175     $expected = [];
176     $expected[] = ['id' => 1];
177     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
178     $view->destroy();
179
180     $view->setDisplay('embed_3');
181     // Check the week calculation for the 29th of February in a leap year.
182     // @see http://wikipedia.org/wiki/ISO_week_date#Calculation
183     $this->executeView($view, ['09']);
184     $expected = [];
185     $expected[] = ['id' => 2];
186     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
187     $view->destroy();
188
189     $view->setDisplay('embed_3');
190     // The first jan 2000 was still in the last week of the previous year.
191     $this->executeView($view, ['52']);
192     $expected = [];
193     $expected[] = ['id' => 3];
194     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
195     $view->destroy();
196
197     $view->setDisplay('embed_3');
198     $this->executeView($view, ['02']);
199     $expected = [];
200     $expected[] = ['id' => 4];
201     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
202     $view->destroy();
203
204     $view->setDisplay('embed_3');
205     $this->executeView($view, ['05']);
206     $expected = [];
207     $expected[] = ['id' => 5];
208     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
209     $view->destroy();
210
211     $view->setDisplay('embed_3');
212     $this->executeView($view, ['23']);
213     $expected = [];
214     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
215   }
216
217   /**
218    * Tests the Year handler.
219    *
220    * @see \Drupal\node\Plugin\views\argument\CreatedYear
221    */
222   public function testYearHandler() {
223     $this->container->get('database')->update('views_test_data')
224       ->fields(['created' => gmmktime(0, 0, 0, 1, 1, 2001)])
225       ->condition('id', 3)
226       ->execute();
227
228     $this->container->get('database')->update('views_test_data')
229       ->fields(['created' => gmmktime(0, 0, 0, 1, 1, 2002)])
230       ->condition('id', 4)
231       ->execute();
232
233     $this->container->get('database')->update('views_test_data')
234       ->fields(['created' => gmmktime(0, 0, 0, 1, 1, 2002)])
235       ->condition('id', 5)
236       ->execute();
237
238     $view = Views::getView('test_argument_date');
239     $view->setDisplay('embed_4');
240     $this->executeView($view, ['2000']);
241     $expected = [];
242     $expected[] = ['id' => 1];
243     $expected[] = ['id' => 2];
244     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
245     $view->destroy();
246
247     $view->setDisplay('embed_4');
248     $this->executeView($view, ['2001']);
249     $expected = [];
250     $expected[] = ['id' => 3];
251     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
252     $view->destroy();
253
254     $view->setDisplay('embed_4');
255     $this->executeView($view, ['2002']);
256     $expected = [];
257     $expected[] = ['id' => 4];
258     $expected[] = ['id' => 5];
259     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
260     $view->destroy();
261
262     $view->setDisplay('embed_4');
263     $this->executeView($view, ['23']);
264     $expected = [];
265     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
266   }
267
268   /**
269    * Tests the YearMonth handler.
270    *
271    * @see \Drupal\node\Plugin\views\argument\CreatedYearMonth
272    */
273   public function testYearMonthHandler() {
274     $this->container->get('database')->update('views_test_data')
275       ->fields(['created' => gmmktime(0, 0, 0, 1, 1, 2001)])
276       ->condition('id', 3)
277       ->execute();
278
279     $this->container->get('database')->update('views_test_data')
280       ->fields(['created' => gmmktime(0, 0, 0, 4, 1, 2001)])
281       ->condition('id', 4)
282       ->execute();
283
284     $this->container->get('database')->update('views_test_data')
285       ->fields(['created' => gmmktime(0, 0, 0, 4, 1, 2001)])
286       ->condition('id', 5)
287       ->execute();
288
289     $view = Views::getView('test_argument_date');
290     $view->setDisplay('embed_5');
291     $this->executeView($view, ['200001']);
292     $expected = [];
293     $expected[] = ['id' => 1];
294     $expected[] = ['id' => 2];
295     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
296     $view->destroy();
297
298     $view->setDisplay('embed_5');
299     $this->executeView($view, ['200101']);
300     $expected = [];
301     $expected[] = ['id' => 3];
302     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
303     $view->destroy();
304
305     $view->setDisplay('embed_5');
306     $this->executeView($view, ['200104']);
307     $expected = [];
308     $expected[] = ['id' => 4];
309     $expected[] = ['id' => 5];
310     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
311     $view->destroy();
312
313     $view->setDisplay('embed_5');
314     $this->executeView($view, ['201301']);
315     $expected = [];
316     $this->assertIdenticalResultset($view, $expected, $this->columnMap);
317   }
318
319 }