/web/core/modules/views/tests/src/Kernel/Handler/ArgumentDateTest.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 319 lines · 224 code · 41 blank · 54 comment · 0 complexity · c41825c290ae20122887c352b9128a2b MD5 · raw file

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