Make WordPress Core

Changeset 30003


Ignore:
Timestamp:
10/24/2014 02:33:46 AM (10 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r29965 r30003  
    222222 */
    223223class WP_Comment_Query {
     224    /**
     225     * SQL for database query.
     226     *
     227     * @since 4.0.1
     228     * @access public
     229     * @var string
     230     */
     231    public $request;
     232
    224233    /**
    225234     * Metadata query container
     
    579588            $groupby = 'GROUP BY ' . $groupby;
    580589        }
    581         $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits";
     590
     591        $this->request = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby $orderby $order $limits";
    582592
    583593        if ( $this->query_vars['count'] ) {
    584             return $wpdb->get_var( $query );
     594            return $wpdb->get_var( $this->request );
    585595        }
    586596
    587597        if ( 'ids' == $this->query_vars['fields'] ) {
    588             $this->comments = $wpdb->get_col( $query );
     598            $this->comments = $wpdb->get_col( $this->request );
    589599            return array_map( 'intval', $this->comments );
    590600        }
    591601
    592         $results = $wpdb->get_results( $query );
     602        $results = $wpdb->get_results( $this->request );
    593603        /**
    594604         * Filter the comment query results.
  • trunk/tests/phpunit/tests/comment/query.php

    r29982 r30003  
    517517        $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
    518518    }
     519
     520    public function test_orderby_default() {
     521        $q = new WP_Comment_Query();
     522        $q->query( array() );
     523
     524        $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );
     525    }
     526
     527    public function test_orderby_single() {
     528        $q = new WP_Comment_Query();
     529        $q->query( array(
     530            'orderby' => 'comment_agent',
     531        ) );
     532
     533        $this->assertContains( 'ORDER BY comment_agent', $q->request );
     534    }
     535
     536    public function test_orderby_single_invalid() {
     537        $q = new WP_Comment_Query();
     538        $q->query( array(
     539            'orderby' => 'foo',
     540        ) );
     541
     542        $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );
     543    }
     544
     545    public function test_orderby_comma_separated() {
     546        $q = new WP_Comment_Query();
     547        $q->query( array(
     548            'orderby' => 'comment_agent, comment_approved',
     549        ) );
     550
     551        $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
     552    }
     553
     554    public function test_orderby_array() {
     555        $q = new WP_Comment_Query();
     556        $q->query( array(
     557            'orderby' => array( 'comment_agent', 'comment_approved' ),
     558        ) );
     559
     560        $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
     561    }
     562
     563    public function test_orderby_array_contains_invalid_item() {
     564        $q = new WP_Comment_Query();
     565        $q->query( array(
     566            'orderby' => array( 'comment_agent', 'foo', 'comment_approved' ),
     567        ) );
     568
     569        $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
     570    }
     571
     572    public function test_orderby_array_contains_all_invalid_items() {
     573        $q = new WP_Comment_Query();
     574        $q->query( array(
     575            'orderby' => array( 'foo', 'bar', 'baz' ),
     576        ) );
     577
     578        $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );
     579    }
    519580}
Note: See TracChangeset for help on using the changeset viewer.