Skip to:
Content

BuddyPress.org

Changeset 10043


Ignore:
Timestamp:
08/11/2015 04:54:03 PM (10 years ago)
Author:
r-a-y
Message:

BP User Query: Remove invalid IDs from $user_ids property after query is run.

If the user_ids parameter is passed to BP_User_Query with invalid user
IDs, after the user query is run, the user_ids property still references
these invalid user IDs. This can lead to devs to assume that the user ID
exists when it does not.

This commit removes these invalid user IDs and includes a unit test.

See #6577.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-user-query.php

    r10012 r10043  
    596596
    597597        // Match up to the user ids from the main query
    598         foreach ( $this->user_ids as $uid ) {
     598        foreach ( $this->user_ids as $key => $uid ) {
    599599            if ( isset( $r[ $uid ] ) ) {
    600600                $this->results[ $uid ] = $r[ $uid ];
     
    603603                // (as opposed to 'ID') property
    604604                $this->results[ $uid ]->id = $uid;
     605
     606            // remove user ID from original user_ids property
     607            } else {
     608                unset( $this->user_ids[ $key ] );
    605609            }
    606610        }
  • trunk/tests/phpunit/testcases/core/class-bp-user-query.php

    r9976 r10043  
    6767    }
    6868
     69    /**
     70     * @group user_ids
     71     */
     72    public function test_bp_user_query_user_ids_with_invalid_user_id() {
     73        $now = time();
     74        $u1 = $this->factory->user->create();
     75        $u2 = $this->factory->user->create();
     76
     77        // invalid user ID
     78        $u3 = $u2 + 1;
     79
     80        $old_user = get_current_user_id();
     81        $this->set_current_user( $u1 );
     82
     83        // pass 'user_ids' to user query to trigger this bug
     84        $q = new BP_User_Query( array(
     85            'user_ids' => array( $u2, $u3 )
     86        ) );
     87
     88        // $q->user_ids property should now not contain invalid user IDs
     89        $this->assertNotContains( $u3, $q->user_ids );
     90
     91        // clean up
     92        $this->set_current_user( $old_user );
     93    }
     94
    6995    public function test_bp_user_query_sort_by_popular() {
    7096        $u1 = $this->factory->user->create();
Note: See TracChangeset for help on using the changeset viewer.