Skip to:
Content

BuddyPress.org

Ticket #5703: 5703.02.patch

File 5703.02.patch, 5.0 KB (added by boonebgorges, 11 years ago)
  • src/bp-friends/bp-friends-filters.php

    diff --git src/bp-friends/bp-friends-filters.php src/bp-friends/bp-friends-filters.php
    index 9c66de1..6ada0e7 100644
    function bp_friends_filter_user_query_populate_extras( BP_User_Query $user_query 
    3838                $friend_id = bp_loggedin_user_id() == $fs->initiator_user_id ? $fs->friend_user_id : $fs->initiator_user_id;
    3939
    4040                if ( isset( $user_query->results[ $friend_id ] ) ) {
    41                         $user_query->results[ $friend_id ]->is_friend = $fs->is_confirmed;
     41                        if ( 0 == $fs->is_confirmed ) {
     42                                $status = $fs->initiator_user_id == bp_loggedin_user_id() ? 'pending' : 'awaiting_response';
     43                        } else {
     44                                $status = 'is_friend';
     45                        }
     46
     47                        $user_query->results[ $friend_id ]->is_friend         = $fs->is_confirmed;
     48                        $user_query->results[ $friend_id ]->friendship_status = $status;
    4249                }
    4350        }
    4451}
  • src/bp-friends/bp-friends-functions.php

    diff --git src/bp-friends/bp-friends-functions.php src/bp-friends/bp-friends-functions.php
    index 67780dd..fcf30c2 100644
    function friends_check_friendship( $user_id, $possible_friend_id ) { 
    195195/**
    196196 * Get the friendship status of two friends.
    197197 *
    198  * Will return 'is_friends', 'not_friends', or 'pending'.
     198 * Will return 'is_friends', 'not_friends', 'pending' or 'awaiting_response'.
    199199 *
    200200 * @param int $user_id ID of the first user.
    201201 * @param int $possible_friend_id ID of the other user.
    202202 * @return string Friend status of the two users.
    203203 */
    204204function friends_check_friendship_status( $user_id, $possible_friend_id ) {
     205        global $members_template;
     206
     207        // check the BP_User_Query first
     208        // @see bp_friends_filter_user_query_populate_extras()
     209        if ( ! empty( $members_template->in_the_loop ) ) {
     210                if ( isset( $members_template->member->friendship_status ) ) {
     211                        return $members_template->member->friendship_status;
     212                } else {
     213                        return 'not_friends';
     214                }
     215        }
     216
    205217        return BP_Friends_Friendship::check_is_friend( $user_id, $possible_friend_id );
    206218}
    207219
  • tests/phpunit/testcases/friends/functions.php

    diff --git tests/phpunit/testcases/friends/functions.php tests/phpunit/testcases/friends/functions.php
    index df59edd..b2904aa 100644
    class BP_Tests_Friends_Functions extends BP_UnitTestCase { 
    135135
    136136                $this->assertTrue( friends_add_friend( $u1, $u2 ) );
    137137        }
     138
     139        /**
     140         * @group friends_check_friendship_status
     141         */
     142        public function test_friends_check_friendship_status_in_members_loop() {
     143                $now = time();
     144                $u1 = $this->create_user( array(
     145                        'last_activity' => date( 'Y-m-d H:i:s', $now ),
     146                ) );
     147                $u2 = $this->create_user( array(
     148                        'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
     149                ) );
     150                $u3 = $this->create_user( array(
     151                        'last_activity' => date( 'Y-m-d H:i:s', $now - 200 ),
     152                ) );
     153                $u4 = $this->create_user( array(
     154                        'last_activity' => date( 'Y-m-d H:i:s', $now - 300 ),
     155                ) );
     156                $u5 = $this->create_user( array(
     157                        'last_activity' => date( 'Y-m-d H:i:s', $now - 400 ),
     158                ) );
     159
     160                friends_add_friend( $u1, $u2, true );
     161                friends_add_friend( $u1, $u3, false );
     162                friends_add_friend( $u4, $u1, false );
     163
     164                $old_user = get_current_user_id();
     165                $this->set_current_user( $u1 );
     166
     167                $found = array();
     168                if ( bp_has_members() ) : while ( bp_members() ) : bp_the_member();
     169                        $found[ bp_get_member_user_id() ] = friends_check_friendship_status( $u1, bp_get_member_user_id() );
     170                endwhile; endif;
     171
     172                $expected = array(
     173                        $u1 => 'not_friends',
     174                        $u2 => 'is_friend',
     175                        $u3 => 'pending',
     176                        $u4 => 'awaiting_response',
     177                        $u5 => 'not_friends',
     178                );
     179
     180                $this->assertSame( $expected, $found );
     181
     182                // clean up
     183                $GLOBALS['members_template'] = null;
     184                $this->set_current_user( $old_user );
     185        }
     186
     187        /**
     188         * @group friends_check_friendship_status
     189         */
     190        public function test_friends_check_friendship_status_not_in_members_loop() {
     191                $now = time();
     192                $u1 = $this->create_user( array(
     193                        'last_activity' => date( 'Y-m-d H:i:s', $now ),
     194                ) );
     195                $u2 = $this->create_user( array(
     196                        'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
     197                ) );
     198                $u3 = $this->create_user( array(
     199                        'last_activity' => date( 'Y-m-d H:i:s', $now - 200 ),
     200                ) );
     201                $u4 = $this->create_user( array(
     202                        'last_activity' => date( 'Y-m-d H:i:s', $now - 300 ),
     203                ) );
     204                $u5 = $this->create_user( array(
     205                        'last_activity' => date( 'Y-m-d H:i:s', $now - 400 ),
     206                ) );
     207
     208                friends_add_friend( $u1, $u2, true );
     209                friends_add_friend( $u1, $u3, false );
     210                friends_add_friend( $u4, $u1, false );
     211
     212                $found = array(
     213                        $u1 => friends_check_friendship_status( $u1, $u1 ),
     214                        $u2 => friends_check_friendship_status( $u1, $u2 ),
     215                        $u3 => friends_check_friendship_status( $u1, $u3 ),
     216                        $u4 => friends_check_friendship_status( $u1, $u4 ),
     217                        $u5 => friends_check_friendship_status( $u1, $u5 ),
     218                );
     219
     220                $expected = array(
     221                        $u1 => 'not_friends',
     222                        $u2 => 'is_friend',
     223                        $u3 => 'pending',
     224                        $u4 => 'awaiting_response',
     225                        $u5 => 'not_friends',
     226                );
     227
     228                $this->assertSame( $expected, $found );
     229        }
    138230}