e3a17bb9c4504cb09bd4d12f2603a98cb7286478
[yaffs-website] / web / core / modules / datetime / src / Tests / Views / ArgumentDateTimeTest.php
1 <?php
2
3 namespace Drupal\datetime\Tests\Views;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests the Drupal\datetime\Plugin\views\filter\Date handler.
9  *
10  * @group datetime
11  */
12 class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $testViews = ['test_argument_datetime'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     // Add some basic test nodes.
26     $dates = [
27       '2000-10-10',
28       '2001-10-10',
29       '2002-01-01',
30     ];
31     foreach ($dates as $date) {
32       $this->nodes[] = $this->drupalCreateNode([
33         'field_date' => [
34           'value' => $date,
35         ]
36       ]);
37     }
38   }
39
40   /**
41    * Test year argument.
42    *
43    * @see \Drupal\datetime\Plugin\views\argument\YearDate
44    */
45   public function testDatetimeArgumentYear() {
46     $view = Views::getView('test_argument_datetime');
47
48     // The 'default' display has the 'year' argument.
49     $view->setDisplay('default');
50     $this->executeView($view, ['2000']);
51     $expected = [];
52     $expected[] = ['nid' => $this->nodes[0]->id()];
53     $this->assertIdenticalResultset($view, $expected, $this->map);
54     $view->destroy();
55
56     $view->setDisplay('default');
57     $this->executeView($view, ['2002']);
58     $expected = [];
59     $expected[] = ['nid' => $this->nodes[2]->id()];
60     $this->assertIdenticalResultset($view, $expected, $this->map);
61     $view->destroy();
62   }
63
64   /**
65    * Test month argument.
66    *
67    * @see \Drupal\datetime\Plugin\views\argument\MonthDate
68    */
69   public function testDatetimeArgumentMonth() {
70     $view = Views::getView('test_argument_datetime');
71     // The 'embed_1' display has the 'month' argument.
72     $view->setDisplay('embed_1');
73
74     $this->executeView($view, ['10']);
75     $expected = [];
76     $expected[] = ['nid' => $this->nodes[0]->id()];
77     $expected[] = ['nid' => $this->nodes[1]->id()];
78     $this->assertIdenticalResultset($view, $expected, $this->map);
79     $view->destroy();
80
81     $view->setDisplay('embed_1');
82     $this->executeView($view, ['01']);
83     $expected = [];
84     $expected[] = ['nid' => $this->nodes[2]->id()];
85     $this->assertIdenticalResultset($view, $expected, $this->map);
86     $view->destroy();
87   }
88
89   /**
90    * Test day argument.
91    *
92    * @see \Drupal\datetime\Plugin\views\argument\DayDate
93    */
94   public function testDatetimeArgumentDay() {
95     $view = Views::getView('test_argument_datetime');
96
97     // The 'embed_2' display has the 'day' argument.
98     $view->setDisplay('embed_2');
99     $this->executeView($view, ['10']);
100     $expected = [];
101     $expected[] = ['nid' => $this->nodes[0]->id()];
102     $expected[] = ['nid' => $this->nodes[1]->id()];
103     $this->assertIdenticalResultset($view, $expected, $this->map);
104     $view->destroy();
105
106     $view->setDisplay('embed_2');
107     $this->executeView($view, ['01']);
108     $expected = [];
109     $expected[] = ['nid' => $this->nodes[2]->id()];
110     $this->assertIdenticalResultset($view, $expected, $this->map);
111     $view->destroy();
112   }
113
114   /**
115    * Test year, month, and day arguments combined.
116    */
117   public function testDatetimeArgumentAll() {
118     $view = Views::getView('test_argument_datetime');
119     // The 'embed_3' display has year, month, and day arguments.
120     $view->setDisplay('embed_3');
121
122     $this->executeView($view, ['2000', '10', '10']);
123     $expected = [];
124     $expected[] = ['nid' => $this->nodes[0]->id()];
125     $this->assertIdenticalResultset($view, $expected, $this->map);
126     $view->destroy();
127
128     $view->setDisplay('embed_3');
129     $this->executeView($view, ['2002', '01', '01']);
130     $expected = [];
131     $expected[] = ['nid' => $this->nodes[2]->id()];
132     $this->assertIdenticalResultset($view, $expected, $this->map);
133     $view->destroy();
134   }
135
136   /**
137    * Test week WW argument.
138    */
139   public function testDatetimeArgumentWeek() {
140     $view = Views::getView('test_argument_datetime');
141     // The 'embed_4' display has WW argument.
142     $view->setDisplay('embed_4');
143
144     $this->executeView($view, ['41']);
145     $expected = [];
146     $expected[] = ['nid' => $this->nodes[0]->id()];
147     $expected[] = ['nid' => $this->nodes[1]->id()];
148     $this->assertIdenticalResultset($view, $expected, $this->map);
149     $view->destroy();
150
151     $view->setDisplay('embed_4');
152     $this->executeView($view, ['01']);
153     $expected = [];
154     $expected[] = ['nid' => $this->nodes[2]->id()];
155     $this->assertIdenticalResultset($view, $expected, $this->map);
156     $view->destroy();
157   }
158
159   /**
160    * Test full_date CCYYMMDD argument.
161    */
162   public function testDatetimeArgumentFullDate() {
163     $view = Views::getView('test_argument_datetime');
164     // The 'embed_5' display has CCYYMMDD argument.
165     $view->setDisplay('embed_5');
166
167     $this->executeView($view, ['20001010']);
168     $expected = [];
169     $expected[] = ['nid' => $this->nodes[0]->id()];
170     $this->assertIdenticalResultset($view, $expected, $this->map);
171     $view->destroy();
172
173     $view->setDisplay('embed_5');
174     $this->executeView($view, ['20020101']);
175     $expected = [];
176     $expected[] = ['nid' => $this->nodes[2]->id()];
177     $this->assertIdenticalResultset($view, $expected, $this->map);
178     $view->destroy();
179   }
180
181   /**
182    * Test year_month CCYYMM argument.
183    */
184   public function testDatetimeArgumentYearMonth() {
185     $view = Views::getView('test_argument_datetime');
186     // The 'embed_6' display has CCYYMM argument.
187     $view->setDisplay('embed_6');
188
189     $this->executeView($view, ['200010']);
190     $expected = [];
191     $expected[] = ['nid' => $this->nodes[0]->id()];
192     $this->assertIdenticalResultset($view, $expected, $this->map);
193     $view->destroy();
194
195     $view->setDisplay('embed_6');
196     $this->executeView($view, ['200201']);
197     $expected = [];
198     $expected[] = ['nid' => $this->nodes[2]->id()];
199     $this->assertIdenticalResultset($view, $expected, $this->map);
200     $view->destroy();
201   }
202
203 }