From 89ecbcc7cb447ea1030fdcac7168161d64ed3299 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sun, 4 May 2014 23:06:07 +0000 Subject: [PATCH] In `WP_Date_Query::get_sql_for_subquery()`, don't parse duplicate parameters - only parse one of `w` and `week` or `month` and `monthnum`. Adds unit tests. Props oso96_2000, ChriCo. Fixes #25835. git-svn-id: https://develop.svn.wordpress.org/trunk@28252 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/date.php | 8 ++--- tests/phpunit/tests/query/dateQuery.php | 45 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/date.php b/src/wp-includes/date.php index 99bbb4e012..2f30fd53f2 100644 --- a/src/wp-includes/date.php +++ b/src/wp-includes/date.php @@ -256,16 +256,12 @@ class WP_Date_Query { if ( isset( $query['month'] ) && $value = $this->build_value( $compare, $query['month'] ) ) $where_parts[] = "MONTH( $column ) $compare $value"; - - // Legacy - if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) ) + else if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) ) $where_parts[] = "MONTH( $column ) $compare $value"; if ( isset( $query['week'] ) && false !== ( $value = $this->build_value( $compare, $query['week'] ) ) ) $where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; - - // Legacy - if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) ) + else if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) ) $where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; if ( isset( $query['dayofyear'] ) && $value = $this->build_value( $compare, $query['dayofyear'] ) ) diff --git a/tests/phpunit/tests/query/dateQuery.php b/tests/phpunit/tests/query/dateQuery.php index 47cc71d563..5e456e5c5d 100644 --- a/tests/phpunit/tests/query/dateQuery.php +++ b/tests/phpunit/tests/query/dateQuery.php @@ -555,4 +555,49 @@ class Tests_Query_DateQuery extends WP_UnitTestCase { $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); } + + public function test_date_params_monthnum_m_duplicate() { + $posts = $this->_get_query_result( array( + 'date_query' => array( + 'month' => 5, + 'monthnum' => 9 + ), + ) ); + + $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' ) ); + + $this->assertContains( "AND ( ( MONTH( post_date ) = 5 ) ) AND", $this->q->request ); + + $this->assertNotContains( "AND ( ( MONTH( post_date ) = 5 AND MONTH( post_date ) = 9 ) ) AND", $this->q->request ); + } + + public function test_date_params_week_w_duplicate() { + $posts = $this->_get_query_result( array( + 'date_query' => array( + 'week' => 21, + 'w' => 22 + ), + ) ); + + $expected_dates = array( + '1972-05-24 14:53:45', + '2004-05-22 12:34:12', + '2025-05-20 10:13:01', + ); + + + $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); + + $this->assertContains( "AND ( ( WEEK( post_date, 1 ) = 21 ) ) AND", $this->q->request ); + + $this->assertNotContains( "AND ( ( WEEK( post_date, 1 ) = 21 AND WEEK( post_date, 1 ) = 22 ) ) AND", $this->q->request ); + } } \ No newline at end of file