Skip to:
Content

BuddyPress.org

Changeset 8022


Ignore:
Timestamp:
03/03/2014 01:48:45 AM (11 years ago)
Author:
boonebgorges
Message:

Caching improvements related to bp_core_get_user_displayname()

  • The cache check in BP_XProfile_Data::exists() should be aware that cache misses are stored as objects and check for misses accordingly
  • Ensure that fullname cache is busted when field 1 is updated
  • Some unit tests
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-cache.php

    r7975 r8022  
    132132}
    133133add_action( 'xprofile_updated_profile', 'xprofile_clear_profile_data_object_cache'   );
     134
     135/**
     136 * Clear the fullname cache when field 1 is updated.
     137 *
     138 * xprofile_clear_profile_data_object_cache() will make this redundant in most
     139 * cases, except where the field is updated directly with xprofile_set_field_data()
     140 *
     141 * @since BuddyPress (2.0.0)
     142 */
     143function xprofile_clear_fullname_cache_on_profile_field_edit( $data ) {
     144    if ( 1 == $data->field_id ) {
     145        wp_cache_delete( 'bp_user_fullname_' . $data->user_id, 'bp' );
     146    }
     147}
     148add_action( 'xprofile_data_after_save', 'xprofile_clear_fullname_cache_on_profile_field_edit' );
    134149
    135150/**
  • trunk/bp-xprofile/bp-xprofile-classes.php

    r7965 r8022  
    11571157        $cached = wp_cache_get( $this->field_id, 'bp_xprofile_data_' . $this->user_id );
    11581158
    1159         if ( $cached ) {
     1159        if ( $cached && ! empty( $cached->id ) ) {
    11601160            $retval = true;
    11611161        } else {
  • trunk/tests/testcases/members/functions.php

    r7910 r8022  
    101101        $this->assertSame( home_url( 'new-members-slug' ) . '/' . $user->user_nicename . '/', bp_core_get_user_domain( $user_id ) );
    102102    }
     103
     104    /**
     105     * @group bp_core_get_user_displayname
     106     */
     107    public function test_bp_core_get_user_displayname_empty_username() {
     108        $this->assertFalse( bp_core_get_user_displayname( '' ) );
     109    }
     110
     111    /**
     112     * @group bp_core_get_user_displayname
     113     */
     114    public function test_bp_core_get_user_displayname_translate_username() {
     115        $u = $this->create_user();
     116
     117        $user = new WP_User( $u );
     118
     119        $found = bp_core_get_user_displayname( $u );
     120        $this->assertNotEmpty( $found );
     121        $this->assertSame( $found, bp_core_get_user_displayname( $user->user_login ) );
     122    }
     123
     124    /**
     125     * @group bp_core_get_user_displayname
     126     */
     127    public function test_bp_core_get_user_displayname_bad_username() {
     128        $this->assertFalse( bp_core_get_user_displayname( 'i_dont_exist' ) );
     129    }
     130
     131    /**
     132     * @group bp_core_get_user_displayname
     133     * @group cache
     134     */
     135    public function test_bp_core_get_user_displayname_xprofile_populate_cache() {
     136        $xprofile_is_active = bp_is_active( 'xprofile' );
     137        buddypress()->active_components['xprofile'] = '1';
     138
     139        $u = $this->create_user( array(
     140            'display_name' => 'Foo',
     141        ) );
     142        bp_core_get_user_displayname( $u );
     143
     144        $this->assertSame( 'Foo', wp_cache_get( 'bp_user_fullname_' . $u, 'bp' ) );
     145
     146        if ( ! $xprofile_is_active ) {
     147            unset( buddypress()->active_components['xprofile'] );
     148        }
     149    }
     150
     151    /**
     152     * @group bp_core_get_user_displayname
     153     * @group cache
     154     */
     155    public function test_bp_core_get_user_displayname_xprofile_bust_cache_after_xprofile_update() {
     156        $xprofile_is_active = bp_is_active( 'xprofile' );
     157        buddypress()->active_components['xprofile'] = '1';
     158
     159        $u = $this->create_user();
     160        xprofile_set_field_data( 1, $u, 'Foo Foo' );
     161
     162        $this->assertFalse( wp_cache_get( 'bp_user_fullname_' . $u, 'bp' ) );
     163
     164        if ( ! $xprofile_is_active ) {
     165            unset( buddypress()->active_components['xprofile'] );
     166        }
     167    }
     168
     169    /**
     170     * @group bp_core_get_user_displayname
     171     */
     172    public function test_bp_core_get_user_displayname_xprofile_exists() {
     173        $xprofile_is_active = bp_is_active( 'xprofile' );
     174        buddypress()->active_components['xprofile'] = '1';
     175
     176        $u = $this->create_user();
     177        xprofile_set_field_data( 1, $u, 'Foo Foo' );
     178
     179        $this->assertSame( 'Foo Foo', bp_core_get_user_displayname( $u ) );
     180
     181        if ( ! $xprofile_is_active ) {
     182            unset( buddypress()->active_components['xprofile'] );
     183        }
     184    }
     185
     186    /**
     187     * @group bp_core_get_user_displayname
     188     */
     189    public function test_bp_core_get_user_displayname_xprofile_does_not_exist() {
     190        $xprofile_is_active = bp_is_active( 'xprofile' );
     191        buddypress()->active_components['xprofile'] = '1';
     192
     193        $u = $this->create_user( array(
     194            'display_name' => 'Foo Foo',
     195        ) );
     196
     197        // Delete directly because BP won't let you delete a required
     198        // field through the API
     199        global $wpdb, $bp;
     200        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u ) );
     201        wp_cache_delete( 'bp_user_fullname_' . $u, 'bp' );
     202        wp_cache_delete( 1, 'bp_xprofile_data_' . $u, 'bp' );
     203
     204        $this->assertSame( '', xprofile_get_field_data( 1, $u ) );
     205        $this->assertSame( 'Foo Foo', bp_core_get_user_displayname( $u ) );
     206        $this->assertSame( 'Foo Foo', xprofile_get_field_data( 1, $u ) );
     207
     208        if ( ! $xprofile_is_active ) {
     209            unset( buddypress()->active_components['xprofile'] );
     210        }
     211    }
    103212}
Note: See TracChangeset for help on using the changeset viewer.