dateQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. /**
  3. * Tests to make sure querying posts based on various date parameters using "date_query" works as expected.
  4. *
  5. * @ticket 18694
  6. *
  7. * @group query
  8. * @group date
  9. * @group datequery
  10. */
  11. class Tests_Query_DateQuery extends WP_UnitTestCase {
  12. public $q;
  13. public function setUp() {
  14. parent::setUp();
  15. // Be careful modifying this. Tests are coded to expect this exact sample data.
  16. $post_dates = array(
  17. '1972-05-24 14:53:45',
  18. '1984-07-28 19:28:56',
  19. '2003-05-27 22:45:07',
  20. '2004-01-03 08:54:10',
  21. '2004-05-22 12:34:12',
  22. '2005-02-17 00:00:15',
  23. '2005-12-31 23:59:20',
  24. '2007-01-22 03:49:21',
  25. '2007-05-16 17:32:22',
  26. '2007-09-24 07:17:23',
  27. '2008-03-29 09:04:25',
  28. '2008-07-15 11:32:26',
  29. '2008-12-10 13:06:27',
  30. '2009-06-11 21:30:28',
  31. '2009-12-18 10:42:29',
  32. '2010-06-17 17:09:30',
  33. '2011-02-23 12:12:31',
  34. '2011-07-04 01:56:32',
  35. '2011-12-12 16:39:33',
  36. '2012-06-13 14:03:34',
  37. '2025-04-20 10:13:00',
  38. '2025-04-20 10:13:01',
  39. '2025-05-20 10:13:01',
  40. );
  41. foreach ( $post_dates as $post_date ) {
  42. $this->factory->post->create( array( 'post_date' => $post_date ) );
  43. }
  44. unset( $this->q );
  45. $this->q = new WP_Query();
  46. }
  47. public function _get_query_result( $args = array() ) {
  48. $args = wp_parse_args( $args, array(
  49. 'post_status' => 'any', // For the future post
  50. 'posts_per_page' => '-1', // To make sure results are accurate
  51. 'orderby' => 'ID', // Same order they were created
  52. 'order' => 'ASC',
  53. ) );
  54. return $this->q->query( $args );
  55. }
  56. public function test_date_query_before_array() {
  57. $posts = $this->_get_query_result( array(
  58. 'date_query' => array(
  59. array(
  60. 'before' => array(
  61. 'year' => 2008,
  62. 'month' => 6,
  63. ),
  64. ),
  65. ),
  66. ) );
  67. $expected_dates = array(
  68. '1972-05-24 14:53:45',
  69. '1984-07-28 19:28:56',
  70. '2003-05-27 22:45:07',
  71. '2004-01-03 08:54:10',
  72. '2004-05-22 12:34:12',
  73. '2005-02-17 00:00:15',
  74. '2005-12-31 23:59:20',
  75. '2007-01-22 03:49:21',
  76. '2007-05-16 17:32:22',
  77. '2007-09-24 07:17:23',
  78. '2008-03-29 09:04:25',
  79. );
  80. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  81. }
  82. /**
  83. * Specifically tests to make sure values are defaulting to
  84. * their minimum values when being used with "before".
  85. */
  86. public function test_date_query_before_array_test_defaulting() {
  87. $posts = $this->_get_query_result( array(
  88. 'date_query' => array(
  89. array(
  90. 'before' => array(
  91. 'year' => 2008,
  92. ),
  93. ),
  94. ),
  95. ) );
  96. $expected_dates = array(
  97. '1972-05-24 14:53:45',
  98. '1984-07-28 19:28:56',
  99. '2003-05-27 22:45:07',
  100. '2004-01-03 08:54:10',
  101. '2004-05-22 12:34:12',
  102. '2005-02-17 00:00:15',
  103. '2005-12-31 23:59:20',
  104. '2007-01-22 03:49:21',
  105. '2007-05-16 17:32:22',
  106. '2007-09-24 07:17:23',
  107. );
  108. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  109. }
  110. public function test_date_query_before_string() {
  111. $posts = $this->_get_query_result( array(
  112. 'date_query' => array(
  113. array(
  114. 'before' => 'May 4th, 2008',
  115. ),
  116. ),
  117. ) );
  118. $expected_dates = array(
  119. '1972-05-24 14:53:45',
  120. '1984-07-28 19:28:56',
  121. '2003-05-27 22:45:07',
  122. '2004-01-03 08:54:10',
  123. '2004-05-22 12:34:12',
  124. '2005-02-17 00:00:15',
  125. '2005-12-31 23:59:20',
  126. '2007-01-22 03:49:21',
  127. '2007-05-16 17:32:22',
  128. '2007-09-24 07:17:23',
  129. '2008-03-29 09:04:25',
  130. );
  131. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  132. }
  133. public function test_date_query_after_array() {
  134. $posts = $this->_get_query_result( array(
  135. 'date_query' => array(
  136. array(
  137. 'after' => array(
  138. 'year' => 2009,
  139. 'month' => 12,
  140. 'day' => 31,
  141. ),
  142. ),
  143. ),
  144. ) );
  145. $expected_dates = array(
  146. '2010-06-17 17:09:30',
  147. '2011-02-23 12:12:31',
  148. '2011-07-04 01:56:32',
  149. '2011-12-12 16:39:33',
  150. '2012-06-13 14:03:34',
  151. '2025-04-20 10:13:00',
  152. '2025-04-20 10:13:01',
  153. '2025-05-20 10:13:01',
  154. );
  155. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  156. }
  157. /**
  158. * Specifically tests to make sure values are defaulting to
  159. * their maximum values when being used with "after".
  160. */
  161. public function test_date_query_after_array_test_defaulting() {
  162. $posts = $this->_get_query_result( array(
  163. 'date_query' => array(
  164. array(
  165. 'after' => array(
  166. 'year' => 2008,
  167. ),
  168. ),
  169. ),
  170. ) );
  171. $expected_dates = array(
  172. '2009-06-11 21:30:28',
  173. '2009-12-18 10:42:29',
  174. '2010-06-17 17:09:30',
  175. '2011-02-23 12:12:31',
  176. '2011-07-04 01:56:32',
  177. '2011-12-12 16:39:33',
  178. '2012-06-13 14:03:34',
  179. '2025-04-20 10:13:00',
  180. '2025-04-20 10:13:01',
  181. '2025-05-20 10:13:01',
  182. );
  183. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  184. }
  185. public function test_date_query_after_string() {
  186. $posts = $this->_get_query_result( array(
  187. 'date_query' => array(
  188. array(
  189. 'after' => '2009-12-18 10:42:29',
  190. ),
  191. ),
  192. ) );
  193. $expected_dates = array(
  194. '2010-06-17 17:09:30',
  195. '2011-02-23 12:12:31',
  196. '2011-07-04 01:56:32',
  197. '2011-12-12 16:39:33',
  198. '2012-06-13 14:03:34',
  199. '2025-04-20 10:13:00',
  200. '2025-04-20 10:13:01',
  201. '2025-05-20 10:13:01',
  202. );
  203. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  204. }
  205. public function test_date_query_after_string_inclusive() {
  206. $posts = $this->_get_query_result( array(
  207. 'date_query' => array(
  208. array(
  209. 'after' => '2009-12-18 10:42:29',
  210. 'inclusive' => true,
  211. ),
  212. ),
  213. ) );
  214. $expected_dates = array(
  215. '2009-12-18 10:42:29',
  216. '2010-06-17 17:09:30',
  217. '2011-02-23 12:12:31',
  218. '2011-07-04 01:56:32',
  219. '2011-12-12 16:39:33',
  220. '2012-06-13 14:03:34',
  221. '2025-04-20 10:13:00',
  222. '2025-04-20 10:13:01',
  223. '2025-05-20 10:13:01',
  224. );
  225. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  226. }
  227. public function test_date_query_year_expecting_results() {
  228. $posts = $this->_get_query_result( array(
  229. 'date_query' => array(
  230. array(
  231. 'year' => 2009,
  232. ),
  233. ),
  234. ) );
  235. $expected_dates = array(
  236. '2009-06-11 21:30:28',
  237. '2009-12-18 10:42:29',
  238. );
  239. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  240. }
  241. public function test_date_query_year_expecting_noresults() {
  242. $posts = $this->_get_query_result( array(
  243. 'date_query' => array(
  244. array(
  245. 'year' => 2001,
  246. ),
  247. ),
  248. ) );
  249. $this->assertCount( 0, $posts );
  250. }
  251. public function test_date_query_month_expecting_results() {
  252. $posts = $this->_get_query_result( array(
  253. 'date_query' => array(
  254. array(
  255. 'month' => 12,
  256. ),
  257. ),
  258. ) );
  259. $expected_dates = array(
  260. '2005-12-31 23:59:20',
  261. '2008-12-10 13:06:27',
  262. '2009-12-18 10:42:29',
  263. '2011-12-12 16:39:33',
  264. );
  265. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  266. }
  267. public function test_date_query_month_expecting_noresults() {
  268. $posts = $this->_get_query_result( array(
  269. 'date_query' => array(
  270. array(
  271. 'month' => 8,
  272. ),
  273. ),
  274. ) );
  275. $this->assertCount( 0, $posts );
  276. }
  277. public function test_date_query_week_expecting_results() {
  278. $posts = $this->_get_query_result( array(
  279. 'date_query' => array(
  280. array(
  281. 'week' => 1,
  282. ),
  283. ),
  284. ) );
  285. $expected_dates = array(
  286. '2004-01-03 08:54:10',
  287. );
  288. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  289. }
  290. public function test_date_query_week_expecting_noresults() {
  291. $posts = $this->_get_query_result( array(
  292. 'date_query' => array(
  293. array(
  294. 'week' => 10,
  295. ),
  296. ),
  297. ) );
  298. $this->assertCount( 0, $posts );
  299. }
  300. public function test_date_query_day_expecting_results() {
  301. $posts = $this->_get_query_result( array(
  302. 'date_query' => array(
  303. array(
  304. 'day' => 17,
  305. ),
  306. ),
  307. ) );
  308. $expected_dates = array(
  309. '2005-02-17 00:00:15',
  310. '2010-06-17 17:09:30',
  311. );
  312. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  313. }
  314. public function test_date_query_day_expecting_noresults() {
  315. $posts = $this->_get_query_result( array(
  316. 'date_query' => array(
  317. array(
  318. 'day' => 19,
  319. ),
  320. ),
  321. ) );
  322. $this->assertCount( 0, $posts );
  323. }
  324. public function test_date_query_dayofweek_expecting_results() {
  325. $posts = $this->_get_query_result( array(
  326. 'date_query' => array(
  327. array(
  328. 'dayofweek' => 7,
  329. ),
  330. ),
  331. ) );
  332. $expected_dates = array(
  333. '1984-07-28 19:28:56',
  334. '2004-01-03 08:54:10',
  335. '2004-05-22 12:34:12',
  336. '2005-12-31 23:59:20',
  337. '2008-03-29 09:04:25',
  338. );
  339. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  340. }
  341. public function test_date_query_hour_expecting_results() {
  342. $posts = $this->_get_query_result( array(
  343. 'date_query' => array(
  344. array(
  345. 'hour' => 13,
  346. ),
  347. ),
  348. ) );
  349. $expected_dates = array(
  350. '2008-12-10 13:06:27',
  351. );
  352. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  353. }
  354. public function test_date_query_hour_expecting_noresults() {
  355. $posts = $this->_get_query_result( array(
  356. 'date_query' => array(
  357. array(
  358. 'hour' => 2,
  359. ),
  360. ),
  361. ) );
  362. $this->assertCount( 0, $posts );
  363. }
  364. public function test_date_query_minute_expecting_results() {
  365. $posts = $this->_get_query_result( array(
  366. 'date_query' => array(
  367. array(
  368. 'minute' => 56,
  369. ),
  370. ),
  371. ) );
  372. $expected_dates = array(
  373. '2011-07-04 01:56:32',
  374. );
  375. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  376. }
  377. public function test_date_query_minute_expecting_noresults() {
  378. $posts = $this->_get_query_result( array(
  379. 'date_query' => array(
  380. array(
  381. 'minute' => 2,
  382. ),
  383. ),
  384. ) );
  385. $this->assertCount( 0, $posts );
  386. }
  387. public function test_date_query_second_expecting_results() {
  388. $posts = $this->_get_query_result( array(
  389. 'date_query' => array(
  390. array(
  391. 'second' => 21,
  392. ),
  393. ),
  394. ) );
  395. $expected_dates = array(
  396. '2007-01-22 03:49:21',
  397. );
  398. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  399. }
  400. public function test_date_query_second_expecting_noresults() {
  401. $posts = $this->_get_query_result( array(
  402. 'date_query' => array(
  403. array(
  404. 'second' => 2,
  405. ),
  406. ),
  407. ) );
  408. $this->assertCount( 0, $posts );
  409. }
  410. public function test_date_query_between_two_times() {
  411. $posts = $this->_get_query_result( array(
  412. 'date_query' => array(
  413. array(
  414. 'hour' => 9,
  415. 'minute' => 0,
  416. 'compare' => '>=',
  417. ),
  418. array(
  419. 'hour' => '17',
  420. 'minute' => '0',
  421. 'compare' => '<=',
  422. ),
  423. ),
  424. ) );
  425. $expected_dates = array(
  426. '1972-05-24 14:53:45',
  427. '2004-05-22 12:34:12',
  428. '2008-03-29 09:04:25',
  429. '2008-07-15 11:32:26',
  430. '2008-12-10 13:06:27',
  431. '2009-12-18 10:42:29',
  432. '2011-02-23 12:12:31',
  433. '2011-12-12 16:39:33',
  434. '2012-06-13 14:03:34',
  435. '2025-04-20 10:13:00',
  436. '2025-04-20 10:13:01',
  437. '2025-05-20 10:13:01',
  438. );
  439. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  440. }
  441. public function test_date_query_relation_or() {
  442. $posts = $this->_get_query_result( array(
  443. 'date_query' => array(
  444. array(
  445. 'hour' => 14,
  446. ),
  447. array(
  448. 'minute' => 34,
  449. ),
  450. 'relation' => 'OR',
  451. ),
  452. ) );
  453. $expected_dates = array(
  454. '1972-05-24 14:53:45',
  455. '2004-05-22 12:34:12',
  456. '2012-06-13 14:03:34',
  457. );
  458. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  459. }
  460. public function test_date_query_compare_greater_than_or_equal_to() {
  461. $posts = $this->_get_query_result( array(
  462. 'date_query' => array(
  463. array(
  464. 'hour' => 14,
  465. 'minute' => 34,
  466. ),
  467. 'compare' => '>=',
  468. ),
  469. ) );
  470. $expected_dates = array(
  471. '1972-05-24 14:53:45',
  472. '1984-07-28 19:28:56',
  473. '2003-05-27 22:45:07',
  474. '2005-12-31 23:59:20',
  475. '2007-05-16 17:32:22',
  476. '2009-06-11 21:30:28',
  477. '2010-06-17 17:09:30',
  478. '2011-12-12 16:39:33',
  479. );
  480. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  481. }
  482. }