Skip to:
Content

BuddyPress.org

Ticket #7245: 7245.diff

File 7245.diff, 3.1 KB (added by boonebgorges, 5 years ago)
  • src/bp-core/classes/class-bp-core-user.php

    diff --git src/bp-core/classes/class-bp-core-user.php src/bp-core/classes/class-bp-core-user.php
    index a9657aa..f102892 100644
    class BP_Core_User { 
    912912                // Set cache.
    913913                wp_cache_set( $user_id, $activity[ $user_id ], 'bp_last_activity' );
    914914
     915                /**
     916                 * Fires when a user's last_activity value has been updated.
     917                 *
     918                 * @since 2.7.0
     919                 *
     920                 * @param int    $user_id ID of the user.
     921                 * @param string $time    Last activity timestamp, in 'Y-m-d H:i:s' format.
     922                 */
     923                do_action( 'bp_core_user_updated_last_activity', $user_id, $time );
     924
    915925                return $updated;
    916926        }
    917927
  • src/bp-members/bp-members-cache.php

    diff --git src/bp-members/bp-members-cache.php src/bp-members/bp-members-cache.php
    index 49b4b50..ed4aa23 100644
    function bp_members_clear_member_type_cache( $user_id ) { 
    6161}
    6262add_action( 'wpmu_delete_user', 'bp_members_clear_member_type_cache' );
    6363add_action( 'delete_user', 'bp_members_clear_member_type_cache' );
     64
     65/**
     66 * Invalidate activity caches when a user's last_activity value is changed.
     67 *
     68 * @since 2.7.0
     69 *
     70 * @return bool True on success, false on failure.
     71 */
     72function bp_members_reset_activity_cache_incrementor() {
     73        return bp_core_reset_incrementor( 'bp_activity' );
     74}
     75add_action( 'bp_core_user_updated_last_activity', 'bp_members_reset_activity_cache_incrementor' );
  • tests/phpunit/testcases/members/cache.php

    diff --git tests/phpunit/testcases/members/cache.php tests/phpunit/testcases/members/cache.php
    index e4d7e04..1e61a7a 100644
    class BP_Tests_Members_Cache extends BP_UnitTestCase { 
    2222                // check if function references cache or hits the DB by comparing query count
    2323                $this->assertEquals( $num_queries, $wpdb->num_queries );
    2424        }
     25
     26        /**
     27         * @ticket BP7237
     28         * @ticket BP6643
     29         * @ticket BP7245
     30         */
     31        public function test_last_activity_should_bust_activity_cache() {
     32                global $wpdb;
     33
     34                $u1 = $this->factory->user->create();
     35                $u2 = $this->factory->user->create();
     36
     37                $time_1 = date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
     38                $time_2 = date( 'Y-m-d H:i:s', time() - ( HOUR_IN_SECONDS * 2 ) );
     39                bp_update_user_last_activity( $u1, $time_1 );
     40                bp_update_user_last_activity( $u2, $time_2 );
     41
     42                $activity_args = array(
     43                        'filter' => array(
     44                                'object' => buddypress()->members->id,
     45                                'action' => 'last_activity',
     46                        ),
     47                        'max' => 1,
     48                );
     49
     50                // Prime cache.
     51                $a1 = bp_activity_get( $activity_args );
     52                $expected = array( $u1, $u2 );
     53                $found = array_map( 'intval', wp_list_pluck( $a1['activities'], 'user_id' ) );
     54                $this->assertSame( $expected, $found );
     55
     56                // Bump u2 activity so it should appear first.
     57                $new_time = date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
     58                bp_update_user_last_activity( $u2, $new_time );
     59
     60                $a2 = bp_activity_get( $activity_args );
     61                $expected = array( $u2, $u1 );
     62                $found = array_map( 'intval', wp_list_pluck( $a2['activities'], 'user_id' ) );
     63                $this->assertSame( $expected, $found );
     64        }
    2565}
    2666