Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/20/2016 02:42:15 PM (10 years ago)
Author:
dcavins
Message:

Use new function BP_Friends_Friendships::get_friendships().

Take advantage of the caching introduced
in BP_Friends_Friendships::get_friendships()
by using get_friendships() as the underlying function in
get_friend_user_ids(), get_friendship_id(), total_friend_count(),
check_is_friend(),
and delete_all_for_user().

Props dcavins, boonebgorges.

Fixes #6978.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/friends/functions.php

    r9819 r11124  
    268268                return $value;
    269269        }
     270
     271        /**
     272         * @group friendship_caching
     273         */
     274        public function test_friends_check_friendship_should_hit_user_cache() {
     275                global $wpdb;
     276                $now = time();
     277                $u1 = $this->factory->user->create( array(
     278                        'last_activity' => date( 'Y-m-d H:i:s', $now ),
     279                ) );
     280                $u2 = $this->factory->user->create( array(
     281                        'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
     282                ) );
     283                $u3 = $this->factory->user->create( array(
     284                        'last_activity' => date( 'Y-m-d H:i:s', $now - 200 ),
     285                ) );
     286
     287                friends_add_friend( $u1, $u2, true );
     288                friends_add_friend( $u1, $u3, false );
     289
     290                friends_check_friendship_status( $u1, $u2 );
     291                $first_query_count = $wpdb->num_queries;
     292
     293                friends_check_friendship_status( $u1, $u3 );
     294
     295                $this->assertEquals( $first_query_count, $wpdb->num_queries );
     296        }
     297
     298        /**
     299         * @group friendship_caching
     300         */
     301        public function test_friends_check_friendship_should_hit_friendship_object_cache() {
     302                global $wpdb;
     303                $now = time();
     304                $u1 = $this->factory->user->create( array(
     305                        'last_activity' => date( 'Y-m-d H:i:s', $now ),
     306                ) );
     307                $u2 = $this->factory->user->create( array(
     308                        'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
     309                ) );
     310
     311                friends_add_friend( $u1, $u2, true );
     312
     313                friends_check_friendship_status( $u1, $u2 );
     314                $first_query_count = $wpdb->num_queries;
     315
     316                /*
     317                 * We expect this to generate one query to find $u2's friendships,
     318                 * but the friendship object itself should come from cache.
     319                 */
     320                friends_check_friendship_status( $u2, $u1 );
     321
     322                $this->assertEquals( $first_query_count + 1, $wpdb->num_queries );
     323        }
    270324}
Note: See TracChangeset for help on using the changeset viewer.