| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Tests for query result ordering, specifically indeterminate ordering. |
| | 5 | * |
| | 6 | * @group query |
| | 7 | */ |
| | 8 | class Tests_Query_Orderby extends WP_UnitTestCase { |
| | 9 | function test_order_by_comment_count_does_not_cause_duplication_when_paginating() { |
| | 10 | global $wpdb; |
| | 11 | |
| | 12 | $posts = self::factory()->post->create_many( 5 ); |
| | 13 | |
| | 14 | $wpdb->update( |
| | 15 | $wpdb->posts, |
| | 16 | array( |
| | 17 | 'comment_count' => 50, |
| | 18 | ), |
| | 19 | array( |
| | 20 | 'ID' => $posts[2], |
| | 21 | ) |
| | 22 | ); |
| | 23 | $wpdb->update( |
| | 24 | $wpdb->posts, |
| | 25 | array( |
| | 26 | 'comment_count' => 100, |
| | 27 | ), |
| | 28 | array( |
| | 29 | 'ID' => $posts[4], |
| | 30 | ) |
| | 31 | ); |
| | 32 | $args = array( |
| | 33 | 'posts_per_page' => 1, |
| | 34 | 'paged' => 1, |
| | 35 | 'orderby' => 'comment_count', |
| | 36 | 'order' => 'desc', |
| | 37 | ); |
| | 38 | |
| | 39 | $query = new WP_Query( $args ); |
| | 40 | $actual = array(); |
| | 41 | $expected = array( |
| | 42 | $posts[4], // 100 comments |
| | 43 | $posts[2], // 50 comments |
| | 44 | $posts[3], // 0 comments |
| | 45 | $posts[1], // 0 comments |
| | 46 | $posts[0], // 0 comments |
| | 47 | ); |
| | 48 | |
| | 49 | while ( $query->have_posts() ) { |
| | 50 | $actual = array_merge( $actual, wp_list_pluck( $query->posts, 'ID' ) ); |
| | 51 | $args['paged']++; |
| | 52 | $query = new WP_Query( $args ); |
| | 53 | } |
| | 54 | |
| | 55 | $this->assertSame( $expected, $actual ); |
| | 56 | } |
| | 57 | } |