Index: /trunk/src/bp-friends/classes/class-bp-friends-friendship.php
===================================================================
--- /trunk/src/bp-friends/classes/class-bp-friends-friendship.php	(revision 13426)
+++ /trunk/src/bp-friends/classes/class-bp-friends-friendship.php	(revision 13427)
@@ -266,4 +266,11 @@
 		if ( empty( $user_id ) ) {
 			$user_id = bp_loggedin_user_id();
+		}
+
+		$friendships = array();
+		$operator    = strtoupper( $operator );
+
+		if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) {
+			return $friendships;
 		}
 
@@ -301,4 +308,7 @@
 		}
 
+		$int_keys  = array( 'id', 'initiator_user_id', 'friend_user_id' );
+		$bool_keys = array( 'is_confirmed', 'is_limited' );
+
 		// Assemble filter array.
 		$filters = wp_array_slice_assoc( $r, array( 'id', 'initiator_user_id', 'friend_user_id', 'is_confirmed', 'is_limited' ) );
@@ -306,11 +316,12 @@
 			if ( is_null( $filter_value ) ) {
 				unset( $filters[ $filter_name ] );
+			} elseif ( in_array( $filter_name, $int_keys, true ) ) {
+				$filters[ $filter_name ] = (int) $filter_value;
+			} else {
+				$filters[ $filter_name ] = (bool) $filter_value;
 			}
 		}
 
 		// Populate friendship array from cache, and normalize.
-		$friendships = array();
-		$int_keys    = array( 'id', 'initiator_user_id', 'friend_user_id' );
-		$bool_keys   = array( 'is_confirmed', 'is_limited' );
 		foreach ( $friendship_ids as $friendship_id ) {
 			// Create a limited BP_Friends_Friendship object (don't fetch the user details).
@@ -333,15 +344,15 @@
 
 			// We need to support the same operators as wp_list_filter().
-			if ( 'OR' == $operator || 'NOT' == $operator ) {
+			if ( 'OR' === $operator || 'NOT' === $operator ) {
 				$matched = 0;
 
 				foreach ( $filters as $filter_name => $filter_value ) {
-					if ( isset( $friendship->{$filter_name} ) && $filter_value == $friendship->{$filter_name} ) {
+					if ( isset( $friendship->{$filter_name} ) && $filter_value === $friendship->{$filter_name} ) {
 						$matched++;
 					}
 				}
 
-				if ( ( 'OR' == $operator && $matched > 0 )
-				  || ( 'NOT' == $operator && 0 == $matched ) ) {
+				if ( ( 'OR' === $operator && $matched > 0 )
+				  || ( 'NOT' === $operator && 0 === $matched ) ) {
 					$friendships[ $friendship->id ] = $friendship;
 				}
@@ -353,5 +364,5 @@
 				 */
 				foreach ( $filters as $filter_name => $filter_value ) {
-					if ( ! isset( $friendship->{$filter_name} ) || $filter_value != $friendship->{$filter_name} ) {
+					if ( ! isset( $friendship->{$filter_name} ) || $filter_value !== $friendship->{$filter_name} ) {
 						continue 2;
 					}
@@ -429,11 +440,17 @@
 
 		$friendships = self::get_friendships( $user_id, $args );
+		$user_id     = (int) $user_id;
 
 		$fids = array();
 		foreach ( $friendships as $friendship ) {
+			$friend_id = $friendship->friend_user_id;
+			if ( $friendship->friend_user_id === $user_id ) {
+				$friend_id = $friendship->initiator_user_id;
+			}
+
 			if ( ! empty( $assoc_arr ) ) {
-				$fids[] = array( 'user_id' => ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id );
+				$fids[] = array( 'user_id' => $friend_id );
 			} else {
-				$fids[] = ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id;
+				$fids[] = $friend_id;
 			}
 		}
@@ -633,5 +650,5 @@
 
 		// Can't friend yourself.
-		if ( $initiator_userid === $possible_friend_userid ) {
+		if ( (int) $initiator_userid === (int) $possible_friend_userid ) {
 			return 'not_friends';
 		}
@@ -658,4 +675,5 @@
 
 		$bp                  = buddypress();
+		$user_id             = (int) $user_id;
 		$possible_friend_ids = wp_parse_id_list( $possible_friend_ids );
 
@@ -928,7 +946,15 @@
 		$sql     = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends );
 		$results = $wpdb->get_results( $sql );
+		$user_id = (int) $user_id;
 
 		for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) {
-			$fids[] = ( $results[ $i ]->friend_user_id === $user_id ) ? $results[ $i ]->initiator_user_id : $results[ $i ]->friend_user_id;
+			$friend_user_id    = (int) $results[ $i ]->friend_user_id;
+			$initiator_user_id = (int) $results[ $i ]->initiator_user_id;
+
+			if ( $friend_user_id === $user_id ) {
+				$fids[] = $initiator_user_id;
+			} else {
+				$fids[] = $friend_user_id;
+			}
 		}
 
@@ -1045,5 +1071,6 @@
 		global $wpdb;
 
-		$bp = buddypress();
+		$bp      = buddypress();
+		$user_id = (int) $user_id;
 
 		// Get all friendships, of any status, for the user.
@@ -1054,5 +1081,9 @@
 			$friendship_ids[] = $friendship->id;
 			if ( $friendship->is_confirmed ) {
-				$friend_ids[] = ( $friendship->friend_user_id == $user_id ) ? $friendship->initiator_user_id : $friendship->friend_user_id;
+				if ( $friendship->friend_user_id === $user_id ) {
+					$friend_ids[] = $friendship->initiator_user_id;
+				} else {
+					$friend_ids[] = $friendship->friend_user_id;
+				}
 			}
 		}
Index: /trunk/tests/phpunit/testcases/friends/class-bp-friends-friendship.php
===================================================================
--- /trunk/tests/phpunit/testcases/friends/class-bp-friends-friendship.php	(revision 13426)
+++ /trunk/tests/phpunit/testcases/friends/class-bp-friends-friendship.php	(revision 13427)
@@ -192,3 +192,70 @@
 		$this->assertEquals( $first_query_count, $wpdb->num_queries );
 	}
+
+	/**
+	 * @ticket BP8844
+	 */
+	public function test_get_random_friends() {
+		$user_id = self::factory()->user->create();
+		$friends = self::factory()->user->create_many( 4 );
+
+		foreach ( $friends as $initiator_id ) {
+			friends_add_friend( $initiator_id, $user_id, true );
+		}
+
+		$random = BP_Friends_Friendship::get_random_friends( $user_id, 4 );
+		$this->assertFalse( in_array( $user_id, $random, true ), 'The requested user id should not be listed into random friends' );
+		$this->assertTrue( 4 === count( $random ) );
+	}
+
+	/**
+	 * @ticket BP8844
+	 */
+	public function test_get_friend_user_ids() {
+		$user_id = self::factory()->user->create();
+		$friends = self::factory()->user->create_many( 4 );
+
+		foreach ( $friends as $initiator_id ) {
+			friends_add_friend( $initiator_id, $user_id, true );
+		}
+
+		$friend_user_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );
+		$this->assertFalse( in_array( $user_id, $friend_user_ids, true ), 'The requested user id should not be listed into random friends' );
+		$this->assertTrue( 4 === count( $friend_user_ids ) );
+	}
+
+	/**
+	 * @ticket BP8844
+	 */
+	public function test_delete_all_for_user() {
+		$user_id = self::factory()->user->create();
+		$friends = self::factory()->user->create_many( 4 );
+
+		foreach ( $friends as $initiator_id ) {
+			friends_add_friend( $initiator_id, $user_id, true );
+		}
+
+		BP_Friends_Friendship::delete_all_for_user( $user_id );
+		$friend_user_ids = BP_Friends_Friendship::get_friendship_ids_for_user( $user_id );
+		$this->assertEmpty( $friend_user_ids );
+	}
+
+	/**
+	 * @ticket BP8844
+	 */
+	public function test_get_friendships() {
+		$u1 = self::factory()->user->create();
+		$u2 = self::factory()->user->create();
+		$u3 = self::factory()->user->create();
+
+		friends_add_friend( $u2, $u1, false );
+		friends_add_friend( $u3, $u1, true );
+
+		$friendships = BP_Friends_Friendship::get_friendships( $u1, array( 'initiator_user_id' => $u2 ), 'not' );
+		$friendship = reset( $friendships );
+		$this->assertTrue( $u3 === $friendship->initiator_user_id && 1 === count( $friendships ) );
+
+		$friendships = BP_Friends_Friendship::get_friendships( $u1, array( 'initiator_user_id' => $u3, 'is_confirmed' => 0 ), 'or' );
+		$this->assertTrue( 2 === count( $friendships ) );
+	}
 }
