Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/06/2014 02:23:51 AM (10 years ago)
Author:
boonebgorges
Message:

Refactor BP_XProfile_ProfileData::get_all_for_user() for performance

Method has been refactored to avoid joins, especially against global tables.
Direct SQL queries have been removed in favor of using existing query methods,
which has the added benefit of allowing existing caching strategies to be
inherited automatically from those methods.

Adds unit tests for the method.

See #1332

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/testcases/xprofile/class-bp-xprofile-profiledata.php

    r7806 r7808  
    264264    }
    265265
     266    /**
     267     * @group get_all_for_user
     268     */
     269    public function test_get_all_for_user_uncached() {
     270        $u = $this->create_user();
     271        $g1 = $this->factory->xprofile_group->create();
     272        $g2 = $this->factory->xprofile_group->create();
     273        $f1 = $this->factory->xprofile_field->create( array(
     274            'type' => 'textbox',
     275            'field_group_id' => $g1->id,
     276        ) );
     277        $f2 = $this->factory->xprofile_field->create( array(
     278            'type' => 'radio',
     279            'field_group_id' => $g2->id,
     280        ) );
     281
     282        $time = bp_core_current_time();
     283
     284        // Get the fullname field - hackish
     285        $f0_id = xprofile_get_field_id_from_name( bp_xprofile_fullname_field_name() );
     286        $f0 = new BP_XProfile_Field( $f0_id );
     287        $g0 = new BP_XProfile_Group( $f0->group_id );
     288        $d0 = new BP_XProfile_ProfileData( $f0->id, $u );
     289
     290        $d1 = new BP_XProfile_ProfileData();
     291        $d1->user_id = $u;
     292        $d1->field_id = $f1->id;
     293        $d1->value = 'foo';
     294        $d1->last_updated = $time;
     295        $d1->save();
     296
     297        $d2 = new BP_XProfile_ProfileData();
     298        $d2->user_id = $u;
     299        $d2->field_id = $f2->id;
     300        $d2->value = 'bar';
     301        $d2->last_updated = $time;
     302        $d2->save();
     303
     304        // Ensure it's deleted from cache
     305        wp_cache_delete( $f1->id, 'bp_xprofile_data_' . $u );
     306        wp_cache_delete( $f2->id, 'bp_xprofile_data_' . $u );
     307
     308        $u_obj = new WP_User( $u );
     309
     310        $expected = array(
     311            'user_login' => $u_obj->user_login,
     312            'user_nicename' => $u_obj->user_nicename,
     313            'user_email' => $u_obj->user_email,
     314            $f0->name => array(
     315                'field_group_id' => $g0->id,
     316                'field_group_name' => $g0->name,
     317                'field_id' => $f0->id,
     318                'field_type' => $f0->type,
     319                'field_data' => $d0->value,
     320            ),
     321            $f1->name => array(
     322                'field_group_id' => $g1->id,
     323                'field_group_name' => $g1->name,
     324                'field_id' => $f1->id,
     325                'field_type' => $f1->type,
     326                'field_data' => $d1->value,
     327            ),
     328            $f2->name => array(
     329                'field_group_id' => $g2->id,
     330                'field_group_name' => $g2->name,
     331                'field_id' => $f2->id,
     332                'field_type' => $f2->type,
     333                'field_data' => $d2->value,
     334            ),
     335        );
     336
     337        $this->assertEquals( $expected, BP_XProfile_ProfileData::get_all_for_user( $u ) );
     338    }
     339
     340    /**
     341     * @group get_all_for_user
     342     */
     343    public function test_get_all_for_user_cached() {
     344        $u = $this->create_user();
     345        $g1 = $this->factory->xprofile_group->create();
     346        $g2 = $this->factory->xprofile_group->create();
     347        $f1 = $this->factory->xprofile_field->create( array(
     348            'type' => 'textbox',
     349            'field_group_id' => $g1->id,
     350        ) );
     351        $f2 = $this->factory->xprofile_field->create( array(
     352            'type' => 'radio',
     353            'field_group_id' => $g2->id,
     354        ) );
     355
     356        $time = bp_core_current_time();
     357
     358        $g0 = new BP_XProfile_Group( 1 );
     359        $f0 = new BP_XProfile_Field( 1 );
     360        $d0 = new BP_XProfile_ProfileData( 1, $u );
     361
     362        $d1 = new stdClass;
     363        $d1->user_id = $u;
     364        $d1->field_id = $f1->id;
     365        $d1->value = 'foo';
     366        $d1->last_updated = $time;
     367
     368        $d2 = new stdClass;
     369        $d2->user_id = $u;
     370        $d2->field_id = $f2->id;
     371        $d2->value = 'bar';
     372        $d2->last_updated = $time;
     373
     374        wp_cache_set( $f1->id, $d1, 'bp_xprofile_data_' . $u );
     375        wp_cache_set( $f2->id, $d2, 'bp_xprofile_data_' . $u );
     376
     377        $u_obj = new WP_User( $u );
     378
     379        $expected = array(
     380            'user_login' => $u_obj->user_login,
     381            'user_nicename' => $u_obj->user_nicename,
     382            'user_email' => $u_obj->user_email,
     383            $f0->name => array(
     384                'field_group_id' => $g0->id,
     385                'field_group_name' => $g0->name,
     386                'field_id' => $f0->id,
     387                'field_type' => $f0->type,
     388                'field_data' => $d0->value,
     389            ),
     390            $f1->name => array(
     391                'field_group_id' => $g1->id,
     392                'field_group_name' => $g1->name,
     393                'field_id' => $f1->id,
     394                'field_type' => $f1->type,
     395                'field_data' => $d1->value,
     396            ),
     397            $f2->name => array(
     398                'field_group_id' => $g2->id,
     399                'field_group_name' => $g2->name,
     400                'field_id' => $f2->id,
     401                'field_type' => $f2->type,
     402                'field_data' => $d2->value,
     403            ),
     404        );
     405
     406        $this->assertEquals( $expected, BP_XProfile_ProfileData::get_all_for_user( $u ) );
     407    }
     408
    266409}
Note: See TracChangeset for help on using the changeset viewer.