date.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * Tests to make sure querying posts based on various date parameters works as expected.
  4. *
  5. * @group query
  6. * @group date
  7. */
  8. class Tests_Query_Date extends WP_UnitTestCase {
  9. public $q;
  10. public function setUp() {
  11. parent::setUp();
  12. // Be careful modifying this. Tests are coded to expect this exact sample data.
  13. $post_dates = array(
  14. '1972-05-24 14:53:45',
  15. '1984-07-28 19:28:56',
  16. '2003-05-27 22:45:07',
  17. '2004-01-03 08:54:10',
  18. '2004-05-22 12:34:12',
  19. '2005-02-17 00:00:15',
  20. '2005-12-31 23:59:20',
  21. '2007-01-22 03:49:21',
  22. '2007-05-16 17:32:22',
  23. '2007-09-24 07:17:23',
  24. '2008-03-29 09:04:25',
  25. '2008-07-15 11:32:26',
  26. '2008-12-10 13:06:27',
  27. '2009-06-11 21:30:28',
  28. '2009-12-18 10:42:29',
  29. '2010-06-17 17:09:30',
  30. '2011-02-23 12:12:31',
  31. '2011-07-04 01:56:32',
  32. '2011-12-12 16:39:33',
  33. '2012-06-13 14:03:34',
  34. '2025-04-20 10:13:00',
  35. '2025-04-20 10:13:01',
  36. '2025-05-20 10:13:01',
  37. );
  38. foreach ( $post_dates as $post_date ) {
  39. $this->factory->post->create( array( 'post_date' => $post_date ) );
  40. }
  41. unset( $this->q );
  42. $this->q = new WP_Query();
  43. }
  44. public function _get_query_result( $args = array() ) {
  45. $args = wp_parse_args( $args, array(
  46. 'post_status' => 'any', // For the future post
  47. 'posts_per_page' => '-1', // To make sure results are accurate
  48. 'orderby' => 'ID', // Same order they were created
  49. 'order' => 'ASC',
  50. ) );
  51. return $this->q->query( $args );
  52. }
  53. public function test_simple_year_expecting_results() {
  54. $posts = $this->_get_query_result( array(
  55. 'year' => 2008,
  56. ) );
  57. $expected_dates = array(
  58. '2008-03-29 09:04:25',
  59. '2008-07-15 11:32:26',
  60. '2008-12-10 13:06:27',
  61. );
  62. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  63. }
  64. public function test_simple_year_expecting_noresults() {
  65. $posts = $this->_get_query_result( array(
  66. 'year' => 2000,
  67. ) );
  68. $this->assertCount( 0, $posts );
  69. }
  70. public function test_simple_m_with_year_expecting_results() {
  71. $posts = $this->_get_query_result( array(
  72. 'm' => '2007',
  73. ) );
  74. $expected_dates = array(
  75. '2007-01-22 03:49:21',
  76. '2007-05-16 17:32:22',
  77. '2007-09-24 07:17:23',
  78. );
  79. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  80. }
  81. public function test_simple_m_with_year_expecting_noresults() {
  82. $posts = $this->_get_query_result( array(
  83. 'm' => '1999',
  84. ) );
  85. $this->assertCount( 0, $posts );
  86. }
  87. public function test_simple_m_with_yearmonth_expecting_results() {
  88. $posts = $this->_get_query_result( array(
  89. 'm' => '202504',
  90. ) );
  91. $expected_dates = array(
  92. '2025-04-20 10:13:00',
  93. '2025-04-20 10:13:01',
  94. );
  95. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  96. }
  97. public function test_simple_m_with_yearmonth_expecting_noresults() {
  98. $posts = $this->_get_query_result( array(
  99. 'm' => '202502',
  100. ) );
  101. $this->assertCount( 0, $posts );
  102. }
  103. public function test_simple_m_with_yearmonthday_expecting_results() {
  104. $posts = $this->_get_query_result( array(
  105. 'm' => '20250420',
  106. ) );
  107. $expected_dates = array(
  108. '2025-04-20 10:13:00',
  109. '2025-04-20 10:13:01',
  110. );
  111. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  112. }
  113. public function test_simple_m_with_yearmonthday_expecting_noresults() {
  114. $posts = $this->_get_query_result( array(
  115. 'm' => '20250419',
  116. ) );
  117. $this->assertCount( 0, $posts );
  118. }
  119. public function test_simple_m_with_yearmonthdayhour_expecting_results() {
  120. $posts = $this->_get_query_result( array(
  121. 'm' => '2025042010',
  122. ) );
  123. $expected_dates = array(
  124. '2025-04-20 10:13:00',
  125. '2025-04-20 10:13:01',
  126. );
  127. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  128. }
  129. public function test_simple_m_with_yearmonthdayhour_expecting_noresults() {
  130. $posts = $this->_get_query_result( array(
  131. 'm' => '2025042009',
  132. ) );
  133. $this->assertCount( 0, $posts );
  134. }
  135. /**
  136. * @ticket 24884
  137. */
  138. public function test_simple_m_with_yearmonthdayhourminute_expecting_results() {
  139. $posts = $this->_get_query_result( array(
  140. 'm' => '202504201013',
  141. ) );
  142. $expected_dates = array(
  143. '2025-04-20 10:13:00',
  144. '2025-04-20 10:13:01',
  145. );
  146. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  147. }
  148. /**
  149. * @ticket 24884
  150. */
  151. public function test_simple_m_with_yearmonthdayhourminute_expecting_noresults() {
  152. $posts = $this->_get_query_result( array(
  153. 'm' => '202504201012',
  154. ) );
  155. $this->assertCount( 0, $posts );
  156. }
  157. /**
  158. * @ticket 24884
  159. */
  160. public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_results() {
  161. $posts = $this->_get_query_result( array(
  162. 'm' => '20250420101301',
  163. ) );
  164. $expected_dates = array(
  165. '2025-04-20 10:13:01',
  166. );
  167. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  168. }
  169. /**
  170. * @ticket 24884
  171. */
  172. public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_noresults() {
  173. $posts = $this->_get_query_result( array(
  174. 'm' => '20250420101302',
  175. ) );
  176. $this->assertCount( 0, $posts );
  177. }
  178. /**
  179. * @ticket 24884
  180. */
  181. public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashes_expecting_results() {
  182. $posts = $this->_get_query_result( array(
  183. 'm' => '2025-04-20 10:13:00',
  184. ) );
  185. $expected_dates = array(
  186. '2025-04-20 10:13:00',
  187. );
  188. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  189. }
  190. /**
  191. * @ticket 24884
  192. */
  193. public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashesletters_expecting_results() {
  194. $posts = $this->_get_query_result( array(
  195. 'm' => 'alpha2025-04-20 10:13:00',
  196. ) );
  197. $expected_dates = array(
  198. '2025-04-20 10:13:00',
  199. );
  200. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  201. }
  202. public function test_simple_monthnum_expecting_results() {
  203. $posts = $this->_get_query_result( array(
  204. 'monthnum' => 5,
  205. ) );
  206. $expected_dates = array(
  207. '1972-05-24 14:53:45',
  208. '2003-05-27 22:45:07',
  209. '2004-05-22 12:34:12',
  210. '2007-05-16 17:32:22',
  211. '2025-05-20 10:13:01',
  212. );
  213. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  214. }
  215. public function test_simple_monthnum_expecting_noresults() {
  216. $posts = $this->_get_query_result( array(
  217. 'monthnum' => 8,
  218. ) );
  219. $this->assertCount( 0, $posts );
  220. }
  221. public function test_simple_w_as_in_week_expecting_results() {
  222. $posts = $this->_get_query_result( array(
  223. 'w' => 24,
  224. ) );
  225. $expected_dates = array(
  226. '2009-06-11 21:30:28',
  227. '2010-06-17 17:09:30',
  228. '2012-06-13 14:03:34',
  229. );
  230. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  231. }
  232. public function test_simple_w_as_in_week_expecting_noresults() {
  233. $posts = $this->_get_query_result( array(
  234. 'w' => 2,
  235. ) );
  236. $this->assertCount( 0, $posts );
  237. }
  238. public function test_simple_day_expecting_results() {
  239. $posts = $this->_get_query_result( array(
  240. 'day' => 22,
  241. ) );
  242. $expected_dates = array(
  243. '2004-05-22 12:34:12',
  244. '2007-01-22 03:49:21',
  245. );
  246. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  247. }
  248. public function test_simple_day_expecting_noresults() {
  249. $posts = $this->_get_query_result( array(
  250. 'day' => 30,
  251. ) );
  252. $this->assertCount( 0, $posts );
  253. }
  254. public function test_simple_hour_expecting_results() {
  255. $posts = $this->_get_query_result( array(
  256. 'hour' => 21,
  257. ) );
  258. $expected_dates = array(
  259. '2009-06-11 21:30:28',
  260. );
  261. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  262. }
  263. public function test_simple_hour_expecting_noresults() {
  264. $posts = $this->_get_query_result( array(
  265. 'hour' => 2,
  266. ) );
  267. $this->assertCount( 0, $posts );
  268. }
  269. public function test_simple_minute_expecting_results() {
  270. $posts = $this->_get_query_result( array(
  271. 'minute' => 32,
  272. ) );
  273. $expected_dates = array(
  274. '2007-05-16 17:32:22',
  275. '2008-07-15 11:32:26',
  276. );
  277. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  278. }
  279. public function test_simple_minute_expecting_noresults() {
  280. $posts = $this->_get_query_result( array(
  281. 'minute' => 1,
  282. ) );
  283. $this->assertCount( 0, $posts );
  284. }
  285. public function test_simple_second_expecting_results() {
  286. $posts = $this->_get_query_result( array(
  287. 'second' => 30,
  288. ) );
  289. $expected_dates = array(
  290. '2010-06-17 17:09:30',
  291. );
  292. $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
  293. }
  294. public function test_simple_second_expecting_noresults() {
  295. $posts = $this->_get_query_result( array(
  296. 'second' => 50,
  297. ) );
  298. $this->assertCount( 0, $posts );
  299. }
  300. }