Make WordPress Core


Ignore:
Timestamp:
02/16/2015 02:09:40 PM (9 years ago)
Author:
boonebgorges
Message:

Improve 'orderby' syntax for WP_Comment_Query.

Since [29027], WP_Query has supported an array of values for the $orderby
parameter, with field names as array keys and ASC/DESC as the array values.
This changeset introduces the same syntax to WP_Comment_Query.

We leverage the new support for multiple ORDER BY clauses to fix a bug that
causes comments to be queried in an indeterminate order when sorting by the
default comment_date_gmt and comments share the same value for
comment_date_gmt. By always including a comment_ID subclause at the end of
the ORDER BY statement, we ensure that comments always have a unique fallback
for sorting.

This changeset also includes improvements paralleling those introduced to
WP_Query in [31312] and [31340], which allow $orderby to accept array keys
from specific $meta_query clauses. This change lets devs sort by multiple
clauses of an associated meta query. See #31045.

Fixes #30478. See #31265.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/query.php

    r31015 r31467  
    613613
    614614        $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'key' ) ) );
    615         $this->assertEquals( 2, count( $comments ) );
    616         $this->assertEquals( $comment_id, $comments[0]->comment_ID );
    617         $this->assertEquals( $comment_id3, $comments[1]->comment_ID );
     615        $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) );
    618616
    619617        $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'meta_value' ) ) );
    620         $this->assertEquals( 2, count( $comments ) );
    621         $this->assertEquals( $comment_id, $comments[0]->comment_ID );
    622         $this->assertEquals( $comment_id3, $comments[1]->comment_ID );
     618        $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) );
    623619
    624620        // value1 is present on two different keys for $comment_id yet we should get only one instance
     
    630626        $this->assertEquals( 1, count( $comments ) );
    631627    }
     628
     629    /**
     630     * @ticket 30478
     631     */
     632    public function test_orderby_clause_key() {
     633        $comments = $this->factory->comment->create_many( 3 );
     634        add_comment_meta( $comments[0], 'foo', 'aaa' );
     635        add_comment_meta( $comments[1], 'foo', 'zzz' );
     636        add_comment_meta( $comments[2], 'foo', 'jjj' );
     637
     638        $q = new WP_Comment_Query();
     639        $found = $q->query( array(
     640            'fields' => 'ids',
     641            'meta_query' => array(
     642                'foo_key' => array(
     643                    'key' => 'foo',
     644                    'compare' => 'EXISTS',
     645                ),
     646            ),
     647            'orderby' => 'foo_key',
     648            'order' => 'DESC',
     649        ) );
     650
     651        $this->assertEquals( array( $comments[1], $comments[2], $comments[0] ), $found );
     652    }
     653
     654    /**
     655     * @ticket 30478
     656     */
     657    public function test_orderby_clause_key_as_secondary_sort() {
     658        $c1 = $this->factory->comment->create( array(
     659            'comment_date' => '2015-01-28 03:00:00',
     660        ) );
     661        $c2 = $this->factory->comment->create( array(
     662            'comment_date' => '2015-01-28 05:00:00',
     663        ) );
     664        $c3 = $this->factory->comment->create( array(
     665            'comment_date' => '2015-01-28 03:00:00',
     666        ) );
     667
     668        add_comment_meta( $c1, 'foo', 'jjj' );
     669        add_comment_meta( $c2, 'foo', 'zzz' );
     670        add_comment_meta( $c3, 'foo', 'aaa' );
     671
     672        $q = new WP_Comment_Query();
     673        $found = $q->query( array(
     674            'fields' => 'ids',
     675            'meta_query' => array(
     676                'foo_key' => array(
     677                    'key' => 'foo',
     678                    'compare' => 'EXISTS',
     679                ),
     680            ),
     681            'orderby' => array(
     682                'comment_date' => 'asc',
     683                'foo_key' => 'asc',
     684            ),
     685        ) );
     686
     687        $this->assertEquals( array( $c3, $c1, $c2 ), $found );
     688    }
     689
     690    /**
     691     * @ticket 30478
     692     */
     693    public function test_orderby_more_than_one_clause_key() {
     694        $comments = $this->factory->comment->create_many( 3 );
     695
     696        add_comment_meta( $comments[0], 'foo', 'jjj' );
     697        add_comment_meta( $comments[1], 'foo', 'zzz' );
     698        add_comment_meta( $comments[2], 'foo', 'jjj' );
     699        add_comment_meta( $comments[0], 'bar', 'aaa' );
     700        add_comment_meta( $comments[1], 'bar', 'ccc' );
     701        add_comment_meta( $comments[2], 'bar', 'bbb' );
     702
     703        $q = new WP_Comment_Query();
     704        $found = $q->query( array(
     705            'fields' => 'ids',
     706            'meta_query' => array(
     707                'foo_key' => array(
     708                    'key' => 'foo',
     709                    'compare' => 'EXISTS',
     710                ),
     711                'bar_key' => array(
     712                    'key' => 'bar',
     713                    'compare' => 'EXISTS',
     714                ),
     715            ),
     716            'orderby' => array(
     717                'foo_key' => 'asc',
     718                'bar_key' => 'desc',
     719            ),
     720        ) );
     721
     722        $this->assertEquals( array( $comments[2], $comments[0], $comments[1] ), $found );
     723    }
     724
    632725
    633726    /**
     
    9861079
    9871080    public function test_orderby_default() {
     1081        global $wpdb;
     1082
    9881083        $q = new WP_Comment_Query();
    9891084        $q->query( array() );
    9901085
    991         $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );
     1086        $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
    9921087    }
    9931088
    9941089    public function test_orderby_single() {
     1090        global $wpdb;
     1091
    9951092        $q = new WP_Comment_Query();
    9961093        $q->query( array(
     
    9981095        ) );
    9991096
    1000         $this->assertContains( 'ORDER BY comment_agent', $q->request );
     1097        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent", $q->request );
    10011098    }
    10021099
    10031100    public function test_orderby_single_invalid() {
     1101        global $wpdb;
     1102
    10041103        $q = new WP_Comment_Query();
    10051104        $q->query( array(
     
    10071106        ) );
    10081107
    1009         $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );
     1108        $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
    10101109    }
    10111110
    10121111    public function test_orderby_comma_separated() {
     1112        global $wpdb;
     1113
    10131114        $q = new WP_Comment_Query();
    10141115        $q->query( array(
     
    10161117        ) );
    10171118
    1018         $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
    1019     }
    1020 
    1021     public function test_orderby_array() {
     1119        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
     1120    }
     1121
     1122    public function test_orderby_flat_array() {
     1123        global $wpdb;
     1124
    10221125        $q = new WP_Comment_Query();
    10231126        $q->query( array(
     
    10251128        ) );
    10261129
    1027         $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
     1130        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
    10281131    }
    10291132
    10301133    public function test_orderby_array_contains_invalid_item() {
     1134        global $wpdb;
     1135
    10311136        $q = new WP_Comment_Query();
    10321137        $q->query( array(
     
    10341139        ) );
    10351140
    1036         $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );
     1141        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
    10371142    }
    10381143
    10391144    public function test_orderby_array_contains_all_invalid_items() {
     1145        global $wpdb;
     1146
    10401147        $q = new WP_Comment_Query();
    10411148        $q->query( array(
     
    10431150        ) );
    10441151
    1045         $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );
     1152        $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
    10461153    }
    10471154
     
    10801187
    10811188        $this->assertNotContains( 'ORDER BY', $q->request );
     1189    }
     1190
     1191    /**
     1192     * @ticket 30478
     1193     */
     1194    public function test_orderby_array() {
     1195        global $wpdb;
     1196
     1197        $q = new WP_Comment_Query();
     1198        $found = $q->query( array(
     1199            'fields' => 'ids',
     1200            'orderby' => array(
     1201                'comment_agent' => 'DESC',
     1202                'comment_date_gmt' => 'ASC',
     1203                'comment_ID' => 'DESC',
     1204            ),
     1205        ) );
     1206
     1207        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID DESC", $q->request );
     1208    }
     1209
     1210    /**
     1211     * @ticket 30478
     1212     */
     1213    public function test_orderby_array_should_discard_invalid_columns() {
     1214        global $wpdb;
     1215
     1216        $q = new WP_Comment_Query();
     1217        $found = $q->query( array(
     1218            'fields' => 'ids',
     1219            'orderby' => array(
     1220                'comment_agent' => 'DESC',
     1221                'foo' => 'ASC',
     1222                'comment_ID' => 'DESC',
     1223            ),
     1224        ) );
     1225
     1226        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_ID DESC", $q->request );
     1227    }
     1228
     1229    /**
     1230     * @ticket 30478
     1231     */
     1232    public function test_orderby_array_should_convert_invalid_order_to_DESC() {
     1233        global $wpdb;
     1234
     1235        $q = new WP_Comment_Query();
     1236        $found = $q->query( array(
     1237            'fields' => 'ids',
     1238            'orderby' => array(
     1239                'comment_agent' => 'DESC',
     1240                'comment_date_gmt' => 'foo',
     1241                'comment_ID' => 'DESC',
     1242            ),
     1243        ) );
     1244
     1245        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt DESC, $wpdb->comments.comment_ID DESC", $q->request );
     1246    }
     1247
     1248    /**
     1249     * @ticket 30478
     1250     */
     1251    public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date_gmt() {
     1252        global $wpdb;
     1253
     1254        $q = new WP_Comment_Query();
     1255        $found = $q->query( array(
     1256            'fields' => 'ids',
     1257            'orderby' => array(
     1258                'comment_agent' => 'DESC',
     1259                'comment_date_gmt' => 'ASC',
     1260            ),
     1261        ) );
     1262
     1263        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID ASC", $q->request );
     1264    }
     1265
     1266    /**
     1267     * @ticket 30478
     1268     */
     1269    public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date() {
     1270        global $wpdb;
     1271
     1272        $q = new WP_Comment_Query();
     1273        $found = $q->query( array(
     1274            'fields' => 'ids',
     1275            'orderby' => array(
     1276                'comment_agent' => 'DESC',
     1277                'comment_date' => 'ASC',
     1278            ),
     1279        ) );
     1280
     1281        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date ASC, $wpdb->comments.comment_ID ASC", $q->request );
     1282    }
     1283
     1284    /**
     1285     * @ticket 30478
     1286     */
     1287    public function test_orderby_array_should_sort_by_comment_ID_DESC_as_fallback_when_not_sorted_by_date() {
     1288        global $wpdb;
     1289
     1290        $q = new WP_Comment_Query();
     1291        $found = $q->query( array(
     1292            'fields' => 'ids',
     1293            'orderby' => array(
     1294                'comment_agent' => 'ASC',
     1295            ),
     1296        ) );
     1297
     1298        $this->assertContains( "ORDER BY $wpdb->comments.comment_agent ASC, $wpdb->comments.comment_ID DESC", $q->request );
     1299    }
     1300
     1301    /**
     1302     * @ticket 30478
     1303     */
     1304    public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_ASC() {
     1305        $now = current_time( 'mysql', 1 );
     1306        $comments = $this->factory->comment->create_many( 5, array(
     1307            'comment_post_ID' => $this->post_id,
     1308            'comment_date_gmt' => $now,
     1309        ) );
     1310
     1311        $q = new WP_Comment_Query();
     1312        $found = $q->query( array(
     1313            'orderby' => 'comment_date_gmt',
     1314            'order' => 'ASC',
     1315        ) );
     1316
     1317        // $comments is ASC by default.
     1318        $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) );
     1319    }
     1320
     1321    /**
     1322     * @ticket 30478
     1323     */
     1324    public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_DESC() {
     1325        $now = current_time( 'mysql', 1 );
     1326        $comments = $this->factory->comment->create_many( 5, array(
     1327            'comment_post_ID' => $this->post_id,
     1328            'comment_date_gmt' => $now,
     1329        ) );
     1330
     1331        $q = new WP_Comment_Query();
     1332        $found = $q->query( array(
     1333            'orderby' => 'comment_date_gmt',
     1334            'order' => 'DESC',
     1335        ) );
     1336
     1337        // $comments is ASC by default.
     1338        rsort( $comments );
     1339
     1340        $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) );
    10821341    }
    10831342
Note: See TracChangeset for help on using the changeset viewer.