Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / datetime / tests / src / Kernel / Views / ArgumentDateTimeTest.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 ArgumentDateTimeTest extends DateTimeHandlerTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $testViews = ['test_argument_datetime'];
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp($import_test_views = TRUE) {
24     parent::setUp($import_test_views);
25
26     // Add some basic test nodes.
27     $dates = [
28       '2000-10-10',
29       '2001-10-10',
30       '2002-01-01',
31       // Add a date that is the year 2002 in UTC, but 2003 in the site's time
32       // zone (Australia/Sydney).
33       '2002-12-31T23:00:00',
34     ];
35     foreach ($dates as $date) {
36       $node = Node::create([
37         'title' => $this->randomMachineName(8),
38         'type' => 'page',
39         'field_date' => [
40           'value' => $date,
41         ]
42       ]);
43       $node->save();
44       $this->nodes[] = $node;
45     }
46   }
47
48   /**
49    * Test year argument.
50    *
51    * @see \Drupal\datetime\Plugin\views\argument\YearDate
52    */
53   public function testDatetimeArgumentYear() {
54     $view = Views::getView('test_argument_datetime');
55
56     // The 'default' display has the 'year' argument.
57     $view->setDisplay('default');
58     $this->executeView($view, ['2000']);
59     $expected = [];
60     $expected[] = ['nid' => $this->nodes[0]->id()];
61     $this->assertIdenticalResultset($view, $expected, $this->map);
62     $view->destroy();
63
64     $view->setDisplay('default');
65     $this->executeView($view, ['2002']);
66     $expected = [];
67     $expected[] = ['nid' => $this->nodes[2]->id()];
68     $this->assertIdenticalResultset($view, $expected, $this->map);
69     $view->destroy();
70
71     $view->setDisplay('default');
72     $this->executeView($view, ['2003']);
73     $expected = [];
74     $expected[] = ['nid' => $this->nodes[3]->id()];
75     $this->assertIdenticalResultset($view, $expected, $this->map);
76     $view->destroy();
77
78     // Tests different system timezone with the same nodes.
79     $this->setSiteTimezone('America/Vancouver');
80
81     $view->setDisplay('default');
82     $this->executeView($view, ['2002']);
83     $expected = [];
84     // Only the 3rd node is returned here since UTC 2002-01-01T00:00:00 is still
85     // in 2001 for this user timezone.
86     $expected[] = ['nid' => $this->nodes[3]->id()];
87     $this->assertIdenticalResultset($view, $expected, $this->map);
88     $view->destroy();
89   }
90
91   /**
92    * Test month argument.
93    *
94    * @see \Drupal\datetime\Plugin\views\argument\MonthDate
95    */
96   public function testDatetimeArgumentMonth() {
97     $view = Views::getView('test_argument_datetime');
98     // The 'embed_1' display has the 'month' argument.
99     $view->setDisplay('embed_1');
100
101     $this->executeView($view, ['10']);
102     $expected = [];
103     $expected[] = ['nid' => $this->nodes[0]->id()];
104     $expected[] = ['nid' => $this->nodes[1]->id()];
105     $this->assertIdenticalResultset($view, $expected, $this->map);
106     $view->destroy();
107
108     $view->setDisplay('embed_1');
109     $this->executeView($view, ['01']);
110     $expected = [];
111     $expected[] = ['nid' => $this->nodes[2]->id()];
112     $expected[] = ['nid' => $this->nodes[3]->id()];
113     $this->assertIdenticalResultset($view, $expected, $this->map);
114     $view->destroy();
115   }
116
117   /**
118    * Test day argument.
119    *
120    * @see \Drupal\datetime\Plugin\views\argument\DayDate
121    */
122   public function testDatetimeArgumentDay() {
123     $view = Views::getView('test_argument_datetime');
124
125     // The 'embed_2' display has the 'day' argument.
126     $view->setDisplay('embed_2');
127     $this->executeView($view, ['10']);
128     $expected = [];
129     $expected[] = ['nid' => $this->nodes[0]->id()];
130     $expected[] = ['nid' => $this->nodes[1]->id()];
131     $this->assertIdenticalResultset($view, $expected, $this->map);
132     $view->destroy();
133
134     $view->setDisplay('embed_2');
135     $this->executeView($view, ['01']);
136     $expected = [];
137     $expected[] = ['nid' => $this->nodes[2]->id()];
138     $expected[] = ['nid' => $this->nodes[3]->id()];
139     $this->assertIdenticalResultset($view, $expected, $this->map);
140     $view->destroy();
141   }
142
143   /**
144    * Test year, month, and day arguments combined.
145    */
146   public function testDatetimeArgumentAll() {
147     $view = Views::getView('test_argument_datetime');
148     // The 'embed_3' display has year, month, and day arguments.
149     $view->setDisplay('embed_3');
150
151     $this->executeView($view, ['2000', '10', '10']);
152     $expected = [];
153     $expected[] = ['nid' => $this->nodes[0]->id()];
154     $this->assertIdenticalResultset($view, $expected, $this->map);
155     $view->destroy();
156
157     $view->setDisplay('embed_3');
158     $this->executeView($view, ['2002', '01', '01']);
159     $expected = [];
160     $expected[] = ['nid' => $this->nodes[2]->id()];
161     $this->assertIdenticalResultset($view, $expected, $this->map);
162     $view->destroy();
163   }
164
165   /**
166    * Test week WW argument.
167    */
168   public function testDatetimeArgumentWeek() {
169     $view = Views::getView('test_argument_datetime');
170     // The 'embed_4' display has WW argument.
171     $view->setDisplay('embed_4');
172
173     $this->executeView($view, ['41']);
174     $expected = [];
175     $expected[] = ['nid' => $this->nodes[0]->id()];
176     $expected[] = ['nid' => $this->nodes[1]->id()];
177     $this->assertIdenticalResultset($view, $expected, $this->map);
178     $view->destroy();
179
180     $view->setDisplay('embed_4');
181     $this->executeView($view, ['01']);
182     $expected = [];
183     $expected[] = ['nid' => $this->nodes[2]->id()];
184     $expected[] = ['nid' => $this->nodes[3]->id()];
185     $this->assertIdenticalResultset($view, $expected, $this->map);
186     $view->destroy();
187   }
188
189   /**
190    * Test full_date CCYYMMDD argument.
191    */
192   public function testDatetimeArgumentFullDate() {
193     $view = Views::getView('test_argument_datetime');
194     // The 'embed_5' display has CCYYMMDD argument.
195     $view->setDisplay('embed_5');
196
197     $this->executeView($view, ['20001010']);
198     $expected = [];
199     $expected[] = ['nid' => $this->nodes[0]->id()];
200     $this->assertIdenticalResultset($view, $expected, $this->map);
201     $view->destroy();
202
203     $view->setDisplay('embed_5');
204     $this->executeView($view, ['20020101']);
205     $expected = [];
206     $expected[] = ['nid' => $this->nodes[2]->id()];
207     $this->assertIdenticalResultset($view, $expected, $this->map);
208     $view->destroy();
209   }
210
211   /**
212    * Test year_month CCYYMM argument.
213    */
214   public function testDatetimeArgumentYearMonth() {
215     $view = Views::getView('test_argument_datetime');
216     // The 'embed_6' display has CCYYMM argument.
217     $view->setDisplay('embed_6');
218
219     $this->executeView($view, ['200010']);
220     $expected = [];
221     $expected[] = ['nid' => $this->nodes[0]->id()];
222     $this->assertIdenticalResultset($view, $expected, $this->map);
223     $view->destroy();
224
225     $view->setDisplay('embed_6');
226     $this->executeView($view, ['200201']);
227     $expected = [];
228     $expected[] = ['nid' => $this->nodes[2]->id()];
229     $this->assertIdenticalResultset($view, $expected, $this->map);
230     $view->destroy();
231   }
232
233 }