Skip to:
Content

BuddyPress.org

Changeset 10435


Ignore:
Timestamp:
01/05/2016 04:00:00 AM (9 years ago)
Author:
boonebgorges
Message:

In activity queries, use id column for secondary sort.

The default sort order for activity queries is by date_recorded. But this
order can be indeterminate when two items share the same date_recorded. To
prevent indeterminacy, we fall back on the always unique id column.

Fixes #6720.
Props henry.wright, Mamaduka.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r10417 r10435  
    529529
    530530                /** This filter is documented in bp-activity/bp-activity-classes.php */
    531                 $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
     531                $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
    532532            } else {
    533533                $pag_sql    = '';
     
    544544                 * @param string $sort       Final sort direction for legacy query.
    545545                 */
    546                 $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
     546                $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
    547547            }
    548548
    549549        } else {
    550550            // Query first for activity IDs.
    551             $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}";
     551            $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort}";
    552552
    553553            if ( ! empty( $per_page ) && ! empty( $page ) ) {
  • trunk/tests/phpunit/testcases/activity/template.php

    r9819 r10435  
    523523        // clean up!
    524524        $activities_template = $reset_activities_template;
     525    }
     526
     527    /**
     528     * @group scope
     529     * @ticket BP6720
     530     */
     531    public function test_bp_has_activities_scope_friends_should_respect_id_order_when_record_dates_are_same() {
     532        $u1 = $this->factory->user->create();
     533        $u2 = $this->factory->user->create();
     534
     535        friends_add_friend( $u1, $u2, true );
     536
     537        // Friend's very fast status updates.
     538        $a1 = $this->factory->activity->create( array(
     539            'user_id' => $u2,
     540            'type' => 'activity_update',
     541            'recorded_time' => date( 'Y-m-d H:i:s', 1451944920 ),
     542        ) );
     543        $a2 = $this->factory->activity->create( array(
     544            'user_id' => $u2,
     545            'type' => 'activity_update',
     546            'recorded_time' => date( 'Y-m-d H:i:s', 1451944920 ),
     547        ) );
     548
     549        global $activities_template;
     550        $reset_activities_template = $activities_template;
     551
     552        // Get activities in 'friends' scope
     553        bp_has_activities( array(
     554            'user_id' => $u1,
     555            'scope' => 'friends',
     556        ) );
     557
     558        $found = $activities_template->activities;
     559
     560        // Clean up!
     561        $activities_template = $reset_activities_template;
     562
     563        $this->assertEquals( array( $a2, $a1 ), wp_list_pluck( $found, 'id' ) );
     564    }
     565
     566    /**
     567     * @group scope
     568     * @ticket BP6720
     569     */
     570    public function test_bp_has_activities_scope_groups_should_respect_id_order_when_record_dates_are_same() {
     571        $u1 = $this->factory->user->create();
     572        $u2 = $this->factory->user->create();
     573        $u3 = $this->factory->user->create();
     574
     575        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     576
     577        // Two user join first user's group same time
     578        $a1 = $this->factory->activity->create( array(
     579            'user_id'   => $u2,
     580            'component' => 'groups',
     581            'item_id'   => $g1,
     582            'type'      => 'joined_group',
     583            'recorded_time' => date( 'Y-m-d H:i:s', 1451944920 ),
     584        ) );
     585        $a2 = $this->factory->activity->create( array(
     586            'user_id'   => $u3,
     587            'component' => 'groups',
     588            'item_id'   => $g1,
     589            'type'      => 'joined_group',
     590            'recorded_time' => date( 'Y-m-d H:i:s', 1451944920 ),
     591        ) );
     592
     593        global $activities_template;
     594        $reset_activities_template = $activities_template;
     595
     596        // Get activities in 'groups' scope
     597        bp_has_activities( array(
     598            'user_id' => $u1,
     599            'scope' => 'groups',
     600        ) );
     601
     602        $found = $activities_template->activities;
     603
     604        // Clean up!
     605        $activities_template = $reset_activities_template;
     606
     607        $this->assertEquals( array( $a2, $a1 ), wp_list_pluck( $found, 'id' ) );
    525608    }
    526609
Note: See TracChangeset for help on using the changeset viewer.