Skip to content

Instantly share code, notes, and snippets.

@Viper007Bond
Created August 31, 2012 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Viper007Bond/3547740 to your computer and use it in GitHub Desktop.
Save Viper007Bond/3547740 to your computer and use it in GitHub Desktop.
WordPress Date Query Unit Tests
<?php
class Tests_WP_Query_Date_Base 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',
'2015-04-20 10:13:00',
);
foreach ( $post_dates as $post_date ) {
$post_id = $this->factory->post->create( array(
'post_date' => $post_date,
) );
if ( is_wp_error( $post_id ) ) {
$this->assertTrue( false ); // Better way to mark a test failed?
exit( 'Failed to insert post in ' . __CLASS__ . '. Aborting to avoid bad things!' );
}
}
unset( $this->q );
$this->q = new WP_Query();
/*
// Debug to make sure posts are being created correctly
$query = $this->_get_query_result();
file_put_contents( ABSPATH . '/date-query-export.txt', var_export( $query->posts, true ) );
/**/
}
public function _get_query_result( $args = array() ) {
$args = wp_parse_args( $args, array(
'posts_per_page' => '-1',
'orderby' => 'ID',
'order' => 'ASC',
) );
return $this->q->query( $args );
}
}
/**
* @group datequery
* @group datequerylegacy
*/
class Tests_WP_Query_Date_Legacy extends Tests_WP_Query_Date_Base {
public function test_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_year_expecting_noresults() {
$posts = $this->_get_query_result( array(
'year' => 2000,
) );
$this->assertCount( 0, $posts );
}
public function test_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',
);
$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
}
public function test_monthnum_expecting_noresults() {
$posts = $this->_get_query_result( array(
'monthnum' => 8,
) );
$this->assertCount( 0, $posts );
}
public function test_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_w_as_in_week_expecting_noresults() {
$posts = $this->_get_query_result( array(
'w' => 2,
) );
$this->assertCount( 0, $posts );
}
public function test_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_day_expecting_noresults() {
$posts = $this->_get_query_result( array(
'day' => 30,
) );
$this->assertCount( 0, $posts );
}
public function test_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_hour_expecting_noresults() {
$posts = $this->_get_query_result( array(
'hour' => 2,
) );
$this->assertCount( 0, $posts );
}
public function test_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_minute_expecting_noresults() {
$posts = $this->_get_query_result( array(
'minute' => 1,
) );
$this->assertCount( 0, $posts );
}
public function test_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_second_expecting_noresults() {
$posts = $this->_get_query_result( array(
'second' => 50,
) );
$this->assertCount( 0, $posts );
}
}
/**
* @group datequery
* @group wpdatequery
*/
class Tests_WP_Query_Date_Query extends Tests_WP_Query_Date_Base {
public function setUp() {
if ( ! class_exists( 'WP_Date_Query' ) ) {
$this->markTestSkipped( 'WP_Date_Query is missing, skipping tests.' );
} else {
parent::setUp();
}
$this->markTestIncomplete( 'This test has not been implemented yet.' );
}
public function test_before_2008() {
$count = $this->_get_query_result( array(
'date_query' => array(
array(
'before' => array(
'year' => 2008,
),
),
),
) );
// Do assertions here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment