From ce810ba5b3d47ef09149f8baa258d5327cb8527d Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 24 Oct 2014 02:33:46 +0000 Subject: [PATCH] Add unit tests for WP_Comment_Query 'orderby' param. For better testability, the SQL string generated in `WP_Comment_Query::get_posts()` is now stored as a 'request' property on the object. See #29902. git-svn-id: https://develop.svn.wordpress.org/trunk@30003 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 18 ++++++-- tests/phpunit/tests/comment/query.php | 61 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index f3746750a2..20b208ed9e 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -221,6 +221,15 @@ function get_comments( $args = '' ) { * @since 3.1.0 */ class WP_Comment_Query { + /** + * SQL for database query. + * + * @since 4.0.1 + * @access public + * @var string + */ + public $request; + /** * Metadata query container * @@ -578,18 +587,19 @@ class WP_Comment_Query { if ( $groupby ) { $groupby = 'GROUP BY ' . $groupby; } - $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits"; + + $this->request = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby $orderby $order $limits"; if ( $this->query_vars['count'] ) { - return $wpdb->get_var( $query ); + return $wpdb->get_var( $this->request ); } if ( 'ids' == $this->query_vars['fields'] ) { - $this->comments = $wpdb->get_col( $query ); + $this->comments = $wpdb->get_col( $this->request ); return array_map( 'intval', $this->comments ); } - $results = $wpdb->get_results( $query ); + $results = $wpdb->get_results( $this->request ); /** * Filter the comment query results. * diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 61f39acd22..0d8bb02c54 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -516,4 +516,65 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found ); } + + public function test_orderby_default() { + $q = new WP_Comment_Query(); + $q->query( array() ); + + $this->assertContains( 'ORDER BY comment_date_gmt', $q->request ); + } + + public function test_orderby_single() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => 'comment_agent', + ) ); + + $this->assertContains( 'ORDER BY comment_agent', $q->request ); + } + + public function test_orderby_single_invalid() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => 'foo', + ) ); + + $this->assertContains( 'ORDER BY comment_date_gmt', $q->request ); + } + + public function test_orderby_comma_separated() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => 'comment_agent, comment_approved', + ) ); + + $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request ); + } + + public function test_orderby_array() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => array( 'comment_agent', 'comment_approved' ), + ) ); + + $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request ); + } + + public function test_orderby_array_contains_invalid_item() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => array( 'comment_agent', 'foo', 'comment_approved' ), + ) ); + + $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request ); + } + + public function test_orderby_array_contains_all_invalid_items() { + $q = new WP_Comment_Query(); + $q->query( array( + 'orderby' => array( 'foo', 'bar', 'baz' ), + ) ); + + $this->assertContains( 'ORDER BY comment_date_gmt', $q->request ); + } }