Index: tests/date-query.php
===================================================================
--- tests/date-query.php	(revision 0)
+++ tests/date-query.php	(working copy)
@@ -0,0 +1,1032 @@
+<?php
+
+/**
+ * Tests to make sure querying posts based on various date parameters works as expected.
+ *
+ * @ticket 18694
+ *
+ * @group datequery
+ * @group datequery-posts
+ */
+class Tests_Query_Date_Posts extends WP_UnitTestCase {
+
+	public $q;
+
+	public function setUp() {
+		parent::setUp();
+
+		// Be careful modifying this. Tests are coded to expect this exact sample data.
+		$post_dates = array(
+			'1972-05-24 14:53:45',
+			'1984-07-28 19:28:56',
+			'2003-05-27 22:45:07',
+			'2004-01-03 08:54:10',
+			'2004-05-22 12:34:12',
+			'2005-02-17 00:00:15',
+			'2005-12-31 23:59:20',
+			'2007-01-22 03:49:21',
+			'2007-05-16 17:32:22',
+			'2007-09-24 07:17:23',
+			'2008-03-29 09:04:25',
+			'2008-07-15 11:32:26',
+			'2008-12-10 13:06:27',
+			'2009-06-11 21:30:28',
+			'2009-12-18 10:42:29',
+			'2010-06-17 17:09:30',
+			'2011-02-23 12:12:31',
+			'2011-07-04 01:56:32',
+			'2011-12-12 16:39:33',
+			'2012-06-13 14:03:34',
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+			'2025-05-20 10:13:01',
+		);
+
+		foreach ( $post_dates as $post_date ) {
+			$this->factory->post->create( array( 'post_date' => $post_date ) );
+		}
+
+		unset( $this->q );
+		$this->q = new WP_Query();
+	}
+
+	public function _get_query_result( $args = array() ) {
+		$args = wp_parse_args( $args, array(
+			'post_status'    => 'any', // For the future post
+			'posts_per_page' => '-1',  // To make sure results are accurate
+			'orderby'        => 'ID',  // Same order they were created
+			'order'          => 'ASC',
+		) );
+
+		return $this->q->query( $args );
+	}
+
+
+	/**
+	 * Simple legacy date parameter tests
+	 */
+
+	public function test_simple_year_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'year' => 2008,
+		) );
+
+		$expected_dates = array(
+			'2008-03-29 09:04:25',
+			'2008-07-15 11:32:26',
+			'2008-12-10 13:06:27',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_year_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'year' => 2000,
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_year_non_valid_numbers() {
+		$test_values = array(
+			'abc',
+			-1,
+			true,
+			false,
+			0,
+			'0',
+		);
+
+		foreach ( $test_values as $test_value ) {
+			$posts = $this->_get_query_result( array(
+				'year' => 2000,
+			) );
+
+			$this->assertCount( 0, $posts, $test_value );
+		}
+	}
+
+	public function test_simple_m_with_year_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => '2007',
+		) );
+
+		$expected_dates = array(
+			'2007-01-22 03:49:21',
+			'2007-05-16 17:32:22',
+			'2007-09-24 07:17:23',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_m_with_year_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'm' => '1999',
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_m_with_yearmonth_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => '202504',
+		) );
+
+		$expected_dates = array(
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_m_with_yearmonth_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'm' => '202502',
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_m_with_yearmonthday_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => '20250420',
+		) );
+
+		$expected_dates = array(
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_m_with_yearmonthday_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'm' => '20250419',
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_m_with_yearmonthdayhour_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => '2025042010',
+		) );
+
+		$expected_dates = array(
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_m_with_yearmonthdayhour_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'm' => '2025042009',
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_m_with_yearmonthdayhourminute_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => '202504201013',
+		) );
+
+		$expected_dates = array(
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_m_with_yearmonthdayhourminute_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'm' => '202504201012',
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => '20250420101301',
+		) );
+
+		$expected_dates = array(
+			'2025-04-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'm' => '20250420101302',
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashes_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => '2025-04-20 10:13:00',
+		) );
+
+		$expected_dates = array(
+			'2025-04-20 10:13:00',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashesletters_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'm' => 'alpha2025-04-20 10:13:00',
+		) );
+
+		$expected_dates = array(
+			'2025-04-20 10:13:00',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_monthnum_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'monthnum' => 5,
+		) );
+
+		$expected_dates = array(
+			'1972-05-24 14:53:45',
+			'2003-05-27 22:45:07',
+			'2004-05-22 12:34:12',
+			'2007-05-16 17:32:22',
+			'2025-05-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_monthnum_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'monthnum' => 8,
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_w_as_in_week_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'w' => 24,
+		) );
+
+		$expected_dates = array(
+			'2009-06-11 21:30:28',
+			'2010-06-17 17:09:30',
+			'2012-06-13 14:03:34',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_w_as_in_week_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'w' => 2,
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_day_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'day' => 22,
+		) );
+
+		$expected_dates = array(
+			'2004-05-22 12:34:12',
+			'2007-01-22 03:49:21',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_day_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'day' => 30,
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_hour_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'hour' => 21,
+		) );
+
+		$expected_dates = array(
+			'2009-06-11 21:30:28',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_hour_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'hour' => 2,
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_minute_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'minute' => 32,
+		) );
+
+		$expected_dates = array(
+			'2007-05-16 17:32:22',
+			'2008-07-15 11:32:26',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_minute_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'minute' => 1,
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_simple_second_expecting_results() {
+		$posts = $this->_get_query_result( array(
+			'second' => 30,
+		) );
+
+		$expected_dates = array(
+			'2010-06-17 17:09:30',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_simple_second_expecting_noresults() {
+		$posts = $this->_get_query_result( array(
+			'second' => 50,
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+
+	/**
+	 * WP_Date_Query tests
+	 */
+
+	public function test_date_query_before_array() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'before' => array(
+						'year' => 2008,
+						'month' => 6,
+					),
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'1972-05-24 14:53:45',
+			'1984-07-28 19:28:56',
+			'2003-05-27 22:45:07',
+			'2004-01-03 08:54:10',
+			'2004-05-22 12:34:12',
+			'2005-02-17 00:00:15',
+			'2005-12-31 23:59:20',
+			'2007-01-22 03:49:21',
+			'2007-05-16 17:32:22',
+			'2007-09-24 07:17:23',
+			'2008-03-29 09:04:25',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	/**
+	 * Specifically tests to make sure values are defaulting to
+	 * their minimum values when being used with "before".
+	 */
+	public function test_date_query_before_array_test_defaulting() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'before' => array(
+						'year' => 2008,
+					),
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'1972-05-24 14:53:45',
+			'1984-07-28 19:28:56',
+			'2003-05-27 22:45:07',
+			'2004-01-03 08:54:10',
+			'2004-05-22 12:34:12',
+			'2005-02-17 00:00:15',
+			'2005-12-31 23:59:20',
+			'2007-01-22 03:49:21',
+			'2007-05-16 17:32:22',
+			'2007-09-24 07:17:23',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_before_string() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'before' => 'May 4th, 2008',
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'1972-05-24 14:53:45',
+			'1984-07-28 19:28:56',
+			'2003-05-27 22:45:07',
+			'2004-01-03 08:54:10',
+			'2004-05-22 12:34:12',
+			'2005-02-17 00:00:15',
+			'2005-12-31 23:59:20',
+			'2007-01-22 03:49:21',
+			'2007-05-16 17:32:22',
+			'2007-09-24 07:17:23',
+			'2008-03-29 09:04:25',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_after_array() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'after' => array(
+						'year'  => 2009,
+						'month' => 12,
+						'day'   => 31,
+					),
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2010-06-17 17:09:30',
+			'2011-02-23 12:12:31',
+			'2011-07-04 01:56:32',
+			'2011-12-12 16:39:33',
+			'2012-06-13 14:03:34',
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+			'2025-05-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	/**
+	 * Specifically tests to make sure values are defaulting to
+	 * their maximum values when being used with "after".
+	 */
+	public function test_date_query_after_array_test_defaulting() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'after' => array(
+						'year' => 2008,
+					),
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2009-06-11 21:30:28',
+			'2009-12-18 10:42:29',
+			'2010-06-17 17:09:30',
+			'2011-02-23 12:12:31',
+			'2011-07-04 01:56:32',
+			'2011-12-12 16:39:33',
+			'2012-06-13 14:03:34',
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+			'2025-05-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_after_string() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'after' => '2009-12-18 10:42:29',
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2010-06-17 17:09:30',
+			'2011-02-23 12:12:31',
+			'2011-07-04 01:56:32',
+			'2011-12-12 16:39:33',
+			'2012-06-13 14:03:34',
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+			'2025-05-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_after_string_inclusive() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'after'     => '2009-12-18 10:42:29',
+					'inclusive' => true,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2009-12-18 10:42:29',
+			'2010-06-17 17:09:30',
+			'2011-02-23 12:12:31',
+			'2011-07-04 01:56:32',
+			'2011-12-12 16:39:33',
+			'2012-06-13 14:03:34',
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+			'2025-05-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_year_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'year' => 2009,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2009-06-11 21:30:28',
+			'2009-12-18 10:42:29',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_year_expecting_noresults() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'year' => 2001,
+				),
+			),
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_date_query_month_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'month' => 12,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2005-12-31 23:59:20',
+			'2008-12-10 13:06:27',
+			'2009-12-18 10:42:29',
+			'2011-12-12 16:39:33',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_month_expecting_noresults() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'month' => 8,
+				),
+			),
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_date_query_week_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'week' => 1,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2004-01-03 08:54:10',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_week_expecting_noresults() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'week' => 10,
+				),
+			),
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_date_query_day_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'day' => 17,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2005-02-17 00:00:15',
+			'2010-06-17 17:09:30',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_day_expecting_noresults() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'day' => 19,
+				),
+			),
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_date_query_dayofweek_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'dayofweek' => 7,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'1984-07-28 19:28:56',
+			'2004-01-03 08:54:10',
+			'2004-05-22 12:34:12',
+			'2005-12-31 23:59:20',
+			'2008-03-29 09:04:25',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_hour_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'hour' => 13,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2008-12-10 13:06:27',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_hour_expecting_noresults() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'hour' => 2,
+				),
+			),
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_date_query_minute_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'minute' => 56,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2011-07-04 01:56:32',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_minute_expecting_noresults() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'minute' => 2,
+				),
+			),
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_date_query_second_expecting_results() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'second' => 21,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2007-01-22 03:49:21',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_second_expecting_noresults() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'second' => 1,
+				),
+			),
+		) );
+
+		$this->assertCount( 0, $posts );
+	}
+
+	public function test_date_query_between_two_times() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'hour'    => 9,
+					'minute'  => 0,
+					'compare' => '>=',
+				),
+				array(
+					'hour'    => '17',
+					'minute'  => '0',
+					'compare' => '<=',
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'1972-05-24 14:53:45',
+			'2004-05-22 12:34:12',
+			'2008-03-29 09:04:25',
+			'2008-07-15 11:32:26',
+			'2008-12-10 13:06:27',
+			'2009-12-18 10:42:29',
+			'2011-02-23 12:12:31',
+			'2011-12-12 16:39:33',
+			'2012-06-13 14:03:34',
+			'2025-04-20 10:13:00',
+			'2025-04-20 10:13:01',
+			'2025-05-20 10:13:01',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_relation_or() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'hour' => 14,
+				),
+				array(
+					'minute' => 34,
+				),
+				'relation' => 'OR',
+			),
+		) );
+
+		$expected_dates = array(
+			'1972-05-24 14:53:45',
+			'2004-05-22 12:34:12',
+			'2012-06-13 14:03:34',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+
+	public function test_date_query_compare_greater_than_or_equal_to() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$posts = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'hour' => 14,
+					'minute' => 34,
+				),
+				'compare' => '>=',
+			),
+		) );
+
+		$expected_dates = array(
+			'1972-05-24 14:53:45',
+			'1984-07-28 19:28:56',
+			'2003-05-27 22:45:07',
+			'2005-12-31 23:59:20',
+			'2007-05-16 17:32:22',
+			'2009-06-11 21:30:28',
+			'2010-06-17 17:09:30',
+			'2011-12-12 16:39:33',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
+	}
+}
+
+/**
+ * Tests for things specifically related to comments.
+ * No need to do a full repeat of all of the post tests again
+ * since the query SQL is the same for both just with a different column.
+ *
+ * @group datequery
+ * @group datequery-comments
+ */
+class Tests_Query_Date_Comments extends WP_UnitTestCase {
+
+	public function setUp() {
+		parent::setUp();
+
+		// Just some dummy posts to use as parents for comments
+		$posts = array();
+		for ( $i = 1; $i <= 2; $i++ ) {
+			$posts[$i] = $this->factory->post->create();
+		}
+
+		// Be careful modifying this. Tests are coded to expect this exact sample data.
+		// Format is 'datetime' => 'post number (not ID)'
+		$comment_dates = array(
+			'2007-01-22 03:49:21' => 1,
+			'2007-05-16 17:32:22' => 1,
+			'2007-09-24 07:17:23' => 1,
+			'2008-03-29 09:04:25' => 1,
+			'2008-07-15 11:32:26' => 2, // This one should never be in the results
+			'2008-12-10 13:06:27' => 1,
+			'2009-06-11 21:30:28' => 1,
+			'2009-12-18 10:42:29' => 1,
+		);
+
+		foreach ( $comment_dates as $comment_date => $comment_parent ) {
+			$result = $this->factory->comment->create( array(
+				'comment_date'   => $comment_date,
+				'comment_parent' => $posts[ $comment_parent ],
+			) );
+		}
+	}
+
+	public function _get_query_result( $args = array() ) {
+		$args = wp_parse_args( $args, array(
+			'post_id' => 1,
+			'orderby' => 'comment_ID',  // Same order they were created
+			'order'   => 'ASC',
+		) );
+
+		return get_comments( $args );
+	}
+
+	public function test_year() {
+		if ( ! class_exists( 'WP_Date_Query' ) )
+			$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
+
+		$comments = $this->_get_query_result( array(
+			'date_query' => array(
+				array(
+					'year' => 2008,
+				),
+			),
+		) );
+
+		$expected_dates = array(
+			'2008-03-29 09:04:25',
+			'2008-12-10 13:06:27',
+		);
+
+		$this->assertEquals( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) );
+	}
+}
\ No newline at end of file
