Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/23/2023 12:04:36 AM (22 months ago)
Author:
imath
Message:

Improve comparison consistency in BP_Friends_Friendship

  • Fix a bug introduced in [13092] making sure to cast IDs retrieved by $wpdb->results() as integers when comparing them to another integer (user ID).
  • Use strong comparisons everywhere in the class.
  • Add unit tests.

Props boonebgorges

See #8844 (trunk)
Closes https://github.com/buddypress/buddypress/pull/68

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/friends/class-bp-friends-friendship.php

    r13086 r13427  
    192192        $this->assertEquals( $first_query_count, $wpdb->num_queries );
    193193    }
     194
     195    /**
     196     * @ticket BP8844
     197     */
     198    public function test_get_random_friends() {
     199        $user_id = self::factory()->user->create();
     200        $friends = self::factory()->user->create_many( 4 );
     201
     202        foreach ( $friends as $initiator_id ) {
     203            friends_add_friend( $initiator_id, $user_id, true );
     204        }
     205
     206        $random = BP_Friends_Friendship::get_random_friends( $user_id, 4 );
     207        $this->assertFalse( in_array( $user_id, $random, true ), 'The requested user id should not be listed into random friends' );
     208        $this->assertTrue( 4 === count( $random ) );
     209    }
     210
     211    /**
     212     * @ticket BP8844
     213     */
     214    public function test_get_friend_user_ids() {
     215        $user_id = self::factory()->user->create();
     216        $friends = self::factory()->user->create_many( 4 );
     217
     218        foreach ( $friends as $initiator_id ) {
     219            friends_add_friend( $initiator_id, $user_id, true );
     220        }
     221
     222        $friend_user_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );
     223        $this->assertFalse( in_array( $user_id, $friend_user_ids, true ), 'The requested user id should not be listed into random friends' );
     224        $this->assertTrue( 4 === count( $friend_user_ids ) );
     225    }
     226
     227    /**
     228     * @ticket BP8844
     229     */
     230    public function test_delete_all_for_user() {
     231        $user_id = self::factory()->user->create();
     232        $friends = self::factory()->user->create_many( 4 );
     233
     234        foreach ( $friends as $initiator_id ) {
     235            friends_add_friend( $initiator_id, $user_id, true );
     236        }
     237
     238        BP_Friends_Friendship::delete_all_for_user( $user_id );
     239        $friend_user_ids = BP_Friends_Friendship::get_friendship_ids_for_user( $user_id );
     240        $this->assertEmpty( $friend_user_ids );
     241    }
     242
     243    /**
     244     * @ticket BP8844
     245     */
     246    public function test_get_friendships() {
     247        $u1 = self::factory()->user->create();
     248        $u2 = self::factory()->user->create();
     249        $u3 = self::factory()->user->create();
     250
     251        friends_add_friend( $u2, $u1, false );
     252        friends_add_friend( $u3, $u1, true );
     253
     254        $friendships = BP_Friends_Friendship::get_friendships( $u1, array( 'initiator_user_id' => $u2 ), 'not' );
     255        $friendship = reset( $friendships );
     256        $this->assertTrue( $u3 === $friendship->initiator_user_id && 1 === count( $friendships ) );
     257
     258        $friendships = BP_Friends_Friendship::get_friendships( $u1, array( 'initiator_user_id' => $u3, 'is_confirmed' => 0 ), 'or' );
     259        $this->assertTrue( 2 === count( $friendships ) );
     260    }
    194261}
Note: See TracChangeset for help on using the changeset viewer.