Skip to:
Content

BuddyPress.org

Changeset 12317


Ignore:
Timestamp:
12/07/2018 06:41:16 PM (8 years ago)
Author:
boonebgorges
Message:

XProfile: Add caching to group and field queries.

See #7435.

Location:
trunk
Files:
4 edited

Legend:

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

    r11818 r12317  
    288288add_action( 'xprofile_field_before_save', 'bp_xprofile_reset_fields_by_name_cache_incrementor' );
    289289
     290/**
     291 * Resets all incremented bp_xprofile_groups caches.
     292 *
     293 * @since 5.0.0
     294 */
     295function bp_xprofile_reset_groups_cache_incrementor() {
     296        bp_core_reset_incrementor( 'bp_xprofile_groups' );
     297}
     298add_action( 'xprofile_group_after_delete', 'bp_xprofile_reset_groups_cache_incrementor' );
     299add_action( 'xprofile_group_after_save', 'bp_xprofile_reset_groups_cache_incrementor' );
     300add_action( 'xprofile_field_after_delete', 'bp_xprofile_reset_groups_cache_incrementor' );
     301add_action( 'xprofile_field_after_save', 'bp_xprofile_reset_groups_cache_incrementor' );
     302
    290303// List actions to clear super cached pages on, if super cache is installed.
    291304add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' );
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php

    r11847 r12317  
    978978                $parent     = $wpdb->query( $sql );
    979979
     980                $retval = false;
     981
    980982                // Update $field_id with new $position and $field_group_id.
    981983                if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {
     
    985987                        $wpdb->query( $sql );
    986988
    987                         // Invalidate profile field cache.
     989                        // Invalidate profile field and group query cache.
    988990                        wp_cache_delete( $field_id, 'bp_xprofile_fields' );
    989991
    990                         return $parent;
    991                 }
    992 
    993                 return false;
     992                        $retval = $parent;
     993                }
     994
     995                bp_core_reset_incrementor( 'bp_xprofile_groups' );
     996
     997                return $retval;
    994998        }
    995999
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r12210 r12317  
    289289                );
    290290
    291                 // WHERE.
     291                $bp = buddypress();
     292
     293                $group_ids = self::get_group_ids( $r );
     294
     295                // Get all group data.
     296                $groups = self::get_group_data( $group_ids );
     297
     298                // Bail if not also getting fields.
     299                if ( empty( $r['fetch_fields'] ) ) {
     300                        return $groups;
     301                }
     302
     303                // Get the group ids from the groups we found.
     304                $group_ids = wp_list_pluck( $groups, 'id' );
     305
     306                // Store for meta cache priming.
     307                $object_ids['group'] = $group_ids;
     308
     309                // Bail if no groups found.
     310                if ( empty( $group_ids ) ) {
     311                        return $groups;
     312                }
     313
     314                $field_ids = self::get_group_field_ids( $group_ids, $r );
     315
     316                foreach( $groups as $group ) {
     317                        $group->fields = array();
     318                }
     319
     320                // Bail if no fields.
     321                if ( empty( $field_ids ) ) {
     322                        return $groups;
     323                }
     324
     325                $field_ids = array_map( 'intval', $field_ids );
     326
     327                // Prime the field cache.
     328                $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
     329                if ( ! empty( $uncached_field_ids ) ) {
     330                        $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
     331                        $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
     332                        foreach ( $uncached_fields as $uncached_field ) {
     333                                $fid = intval( $uncached_field->id );
     334                                wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
     335                        }
     336                }
     337
     338                // Pull field objects from the cache.
     339                $fields = array();
     340                foreach ( $field_ids as $field_id ) {
     341                        $fields[] = xprofile_get_field( $field_id, null, false );
     342                }
     343
     344                // Store field IDs for meta cache priming.
     345                $object_ids['field'] = $field_ids;
     346
     347                // Maybe fetch field data.
     348                if ( ! empty( $r['fetch_field_data'] ) ) {
     349
     350                        // Get field data for user ID.
     351                        if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) {
     352                                $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids );
     353                        }
     354
     355                        // Remove data-less fields, if necessary.
     356                        if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) {
     357
     358                                // Loop through the results and find the fields that have data.
     359                                foreach( (array) $field_data as $data ) {
     360
     361                                        // Empty fields may contain a serialized empty array.
     362                                        $maybe_value = maybe_unserialize( $data->value );
     363
     364                                        // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
     365                                        if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) {
     366
     367                                                // Fields that have data get removed from the list.
     368                                                unset( $field_ids[ $key ] );
     369                                        }
     370                                }
     371
     372                                // The remaining members of $field_ids are empty. Remove them.
     373                                foreach( $fields as $field_key => $field ) {
     374                                        if ( in_array( $field->id, $field_ids ) ) {
     375                                                unset( $fields[ $field_key ] );
     376                                        }
     377                                }
     378
     379                                // Reset indexes.
     380                                $fields = array_values( $fields );
     381                        }
     382
     383                        // Field data was found.
     384                        if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) {
     385
     386                                // Loop through fields.
     387                                foreach( (array) $fields as $field_key => $field ) {
     388
     389                                        // Loop through the data in each field.
     390                                        foreach( (array) $field_data as $data ) {
     391
     392                                                // Assign correct data value to the field.
     393                                                if ( $field->id == $data->field_id ) {
     394                                                        $fields[ $field_key ]->data        = new stdClass;
     395                                                        $fields[ $field_key ]->data->value = $data->value;
     396                                                        $fields[ $field_key ]->data->id    = $data->id;
     397                                                }
     398
     399                                                // Store for meta cache priming.
     400                                                $object_ids['data'][] = $data->id;
     401                                        }
     402                                }
     403                        }
     404                }
     405
     406                // Prime the meta cache, if necessary.
     407                if ( ! empty( $r['update_meta_cache'] ) ) {
     408                        bp_xprofile_update_meta_cache( $object_ids );
     409                }
     410
     411                // Maybe fetch visibility levels.
     412                if ( ! empty( $r['fetch_visibility_level'] ) ) {
     413                        $fields = self::fetch_visibility_level( $r['user_id'], $fields );
     414                }
     415
     416                // Merge the field array back in with the group array.
     417                foreach( (array) $groups as $group ) {
     418                        // Indexes may have been shifted after previous deletions, so we get a
     419                        // fresh one each time through the loop.
     420                        $index = array_search( $group, $groups );
     421
     422                        foreach( (array) $fields as $field ) {
     423                                if ( $group->id === $field->group_id ) {
     424                                        $groups[ $index ]->fields[] = $field;
     425                                }
     426                        }
     427
     428                        // When we unset fields above, we may have created empty groups.
     429                        // Remove them, if necessary.
     430                        if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) {
     431                                unset( $groups[ $index ] );
     432                        }
     433
     434                        // Reset indexes.
     435                        $groups = array_values( $groups );
     436                }
     437
     438                return $groups;
     439        }
     440
     441        /**
     442         * Gets group IDs, based on passed parameters.
     443         *
     444         * @since 5.0.0
     445         *
     446         * @param array $args {
     447         *    Array of optional arguments:
     448         *    @type int   $profile_group_id  Limit results to a single profile group. Default false.
     449         *    @type array $exclude_groups    Comma-separated list or array of group IDs to exclude. Default false.
     450         *    @type bool  $hide_empty_groups True to hide groups that don't have any fields. Default: false.
     451         * }
     452         * @return array
     453         */
     454        public static function get_group_ids( $args = array() ) {
     455                global $wpdb;
     456
     457                $r = array_merge(
     458                        array(
     459                                'profile_group_id'  => false,
     460                                'exclude_groups'    => false,
     461                                'hide_empty_groups' => false,
     462                        ),
     463                        $args
     464                );
     465
     466                $bp = buddypress();
     467
    292468                if ( ! empty( $r['profile_group_id'] ) ) {
    293469                        $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
     
    299475                }
    300476
    301                 $bp = buddypress();
    302 
    303477                // Include or exclude empty groups.
    304478                if ( ! empty( $r['hide_empty_groups'] ) ) {
    305                         $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" );
     479                        $group_ids_sql = "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC";
    306480                } else {
    307                         $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" );
    308                 }
    309 
    310                 // Get all group data.
    311                 $groups = self::get_group_data( $group_ids );
    312 
    313                 // Bail if not also getting fields.
    314                 if ( empty( $r['fetch_fields'] ) ) {
    315                         return $groups;
    316                 }
    317 
    318                 // Get the group ids from the groups we found.
    319                 $group_ids = wp_list_pluck( $groups, 'id' );
    320 
    321                 // Store for meta cache priming.
    322                 $object_ids['group'] = $group_ids;
    323 
    324                 // Bail if no groups found.
     481                        $group_ids_sql = "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC";
     482                }
     483
     484                $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' );
     485                if ( false === $cached ) {
     486                        $group_ids = $wpdb->get_col( $group_ids_sql );
     487                        bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids );
     488                } else {
     489                        $group_ids = $cached;
     490                }
     491
     492                return array_map( 'intval', $group_ids );
     493        }
     494
     495        /**
     496         * Gets group field IDs, based on passed parameters.
     497         *
     498         * @since 5.0.0
     499         *
     500         * @param array $group_ids Array of group IDs.
     501         * @param array $args {
     502         *    Array of optional arguments:
     503         *      @type array        $exclude_fields    Comma-separated list or array of field IDs to exclude.
     504         *                                            Default empty.
     505         *      @type int          $user_id           Limit results to fields associated with a given user's
     506         *                                            member type. Default empty.
     507         *      @type array|string $member_type       Limit fields by those restricted to a given member type, or array of
     508         *                                            member types. If `$user_id` is provided, the value of `$member_type`
     509         *                                            is honored.
     510         * }
     511         * @return array
     512         */
     513        public static function get_group_field_ids( $group_ids, $args = array() ) {
     514                global $wpdb;
     515
     516                $r = array_merge(
     517                        array(
     518                                'exclude_fields' => false,
     519                                'user_id' => false,
     520                                'member_type' => false,
     521                        ),
     522                        $args
     523                );
     524
     525                $bp = buddypress();
     526
     527                // Setup IN query from group IDs.
    325528                if ( empty( $group_ids ) ) {
    326                         return $groups;
    327                 }
    328 
    329                 // Setup IN query from group IDs.
    330                 $group_ids_in = implode( ',', (array) $group_ids );
     529                        $group_ids = array( 0 );
     530                }
     531                $group_ids_in = implode( ',', array_map( 'intval', $group_ids ) );
    331532
    332533                // Support arrays and comma-separated strings.
     
    371572                }
    372573
    373                 // Fetch the fields.
    374                 $field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
    375 
    376                 foreach( $groups as $group ) {
    377                         $group->fields = array();
    378                 }
    379 
    380                 // Bail if no fields.
    381                 if ( empty( $field_ids ) ) {
    382                         return $groups;
    383                 }
    384 
    385                 $field_ids = array_map( 'intval', $field_ids );
    386 
    387                 // Prime the field cache.
    388                 $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
    389                 if ( ! empty( $uncached_field_ids ) ) {
    390                         $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
    391                         $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
    392                         foreach ( $uncached_fields as $uncached_field ) {
    393                                 $fid = intval( $uncached_field->id );
    394                                 wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
    395                         }
    396                 }
    397 
    398                 // Pull field objects from the cache.
    399                 $fields = array();
    400                 foreach ( $field_ids as $field_id ) {
    401                         $fields[] = xprofile_get_field( $field_id, null, false );
    402                 }
    403 
    404                 // Store field IDs for meta cache priming.
    405                 $object_ids['field'] = $field_ids;
    406 
    407                 // Maybe fetch field data.
    408                 if ( ! empty( $r['fetch_field_data'] ) ) {
    409 
    410                         // Get field data for user ID.
    411                         if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) {
    412                                 $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids );
    413                         }
    414 
    415                         // Remove data-less fields, if necessary.
    416                         if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) {
    417 
    418                                 // Loop through the results and find the fields that have data.
    419                                 foreach( (array) $field_data as $data ) {
    420 
    421                                         // Empty fields may contain a serialized empty array.
    422                                         $maybe_value = maybe_unserialize( $data->value );
    423 
    424                                         // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
    425                                         if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) {
    426 
    427                                                 // Fields that have data get removed from the list.
    428                                                 unset( $field_ids[ $key ] );
    429                                         }
    430                                 }
    431 
    432                                 // The remaining members of $field_ids are empty. Remove them.
    433                                 foreach( $fields as $field_key => $field ) {
    434                                         if ( in_array( $field->id, $field_ids ) ) {
    435                                                 unset( $fields[ $field_key ] );
    436                                         }
    437                                 }
    438 
    439                                 // Reset indexes.
    440                                 $fields = array_values( $fields );
    441                         }
    442 
    443                         // Field data was found.
    444                         if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) {
    445 
    446                                 // Loop through fields.
    447                                 foreach( (array) $fields as $field_key => $field ) {
    448 
    449                                         // Loop through the data in each field.
    450                                         foreach( (array) $field_data as $data ) {
    451 
    452                                                 // Assign correct data value to the field.
    453                                                 if ( $field->id == $data->field_id ) {
    454                                                         $fields[ $field_key ]->data        = new stdClass;
    455                                                         $fields[ $field_key ]->data->value = $data->value;
    456                                                         $fields[ $field_key ]->data->id    = $data->id;
    457                                                 }
    458 
    459                                                 // Store for meta cache priming.
    460                                                 $object_ids['data'][] = $data->id;
    461                                         }
    462                                 }
    463                         }
    464                 }
    465 
    466                 // Prime the meta cache, if necessary.
    467                 if ( ! empty( $r['update_meta_cache'] ) ) {
    468                         bp_xprofile_update_meta_cache( $object_ids );
    469                 }
    470 
    471                 // Maybe fetch visibility levels.
    472                 if ( ! empty( $r['fetch_visibility_level'] ) ) {
    473                         $fields = self::fetch_visibility_level( $r['user_id'], $fields );
    474                 }
    475 
    476                 // Merge the field array back in with the group array.
    477                 foreach( (array) $groups as $group ) {
    478                         // Indexes may have been shifted after previous deletions, so we get a
    479                         // fresh one each time through the loop.
    480                         $index = array_search( $group, $groups );
    481 
    482                         foreach( (array) $fields as $field ) {
    483                                 if ( $group->id === $field->group_id ) {
    484                                         $groups[ $index ]->fields[] = $field;
    485                                 }
    486                         }
    487 
    488                         // When we unset fields above, we may have created empty groups.
    489                         // Remove them, if necessary.
    490                         if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) {
    491                                 unset( $groups[ $index ] );
    492                         }
    493 
    494                         // Reset indexes.
    495                         $groups = array_values( $groups );
    496                 }
    497 
    498                 return $groups;
     574                $field_ids_sql = "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order";
     575
     576                $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' );
     577                if ( false === $cached ) {
     578                        $field_ids = $wpdb->get_col( $field_ids_sql );
     579                        bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids );
     580                } else {
     581                        $field_ids = $cached;
     582                }
     583
     584                return array_map( 'intval', $field_ids );
    499585        }
    500586
     
    614700                }
    615701
    616                 // Purge profile field group cache.
     702                // Purge profile field group and group query caches.
    617703                wp_cache_delete( 'all', 'bp_xprofile_groups' );
     704                bp_core_reset_incrementor( 'bp_xprofile_groups' );
    618705
    619706                $bp = buddypress();
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php

    r12210 r12317  
    442442                $this->assertSame( "Test ' Name", $e1->name );
    443443        }
     444
     445        /**
     446         * @group BP7435
     447         * @group cache
     448         */
     449        public function test_group_ids_query_should_be_cached() {
     450                global $wpdb;
     451
     452                $group_ids   = array( 1 ); // Default group.
     453                $group_ids[] = self::factory()->xprofile_group->create();
     454                $group_ids[] = self::factory()->xprofile_group->create();
     455                $group_ids[] = self::factory()->xprofile_group->create();
     456
     457                $params_1 = array(
     458                        'exclude_groups' => false,
     459                );
     460
     461                $params_2 = array(
     462                        'exclude_groups' => array( 0 ),
     463                );
     464
     465                // Prime cache.
     466                $found_1 = BP_XProfile_Group::get_group_ids( $params_1 );
     467                $this->assertEqualSets( $group_ids, $found_1 );
     468
     469                $num_queries = $wpdb->num_queries;
     470
     471                $found_1 = BP_XProfile_Group::get_group_ids( $params_1 );
     472                $this->assertEqualSets( $group_ids, $found_1 );
     473                $this->assertSame( $num_queries, $wpdb->num_queries );
     474
     475                // Different parameters should trigger a cache miss.
     476                $found_2 = BP_XProfile_Group::get_group_ids( $params_2 );
     477                $this->assertEqualSets( $group_ids, $found_2 );
     478                $this->assertNotSame( $num_queries, $wpdb->num_queries );
     479        }
     480
     481        /**
     482         * @group BP7435
     483         * @group cache
     484         */
     485        public function test_group_ids_query_cache_should_be_busted_on_group_delete() {
     486                $group_ids    = array( 1 ); // Default group.
     487                $group_ids[1] = self::factory()->xprofile_group->create();
     488                $group_ids[2] = self::factory()->xprofile_group->create();
     489                $group_ids[3] = self::factory()->xprofile_group->create();
     490
     491                // Prime cache.
     492                $found = BP_XProfile_Group::get_group_ids();
     493                $this->assertContains( $group_ids[1], $found );
     494
     495                $g1 = new BP_XProfile_Group( $group_ids[1] );
     496                $g1->delete();
     497
     498                $found = BP_XProfile_Group::get_group_ids();
     499                $this->assertNotContains( $group_ids[1], $found );
     500        }
     501
     502        /**
     503         * @group BP7435
     504         * @group cache
     505         */
     506        public function test_group_ids_query_cache_should_be_busted_on_group_save() {
     507                global $wpdb;
     508
     509                $group_ids    = array( 1 ); // Default group.
     510                $group_ids[1] = self::factory()->xprofile_group->create();
     511                $group_ids[2] = self::factory()->xprofile_group->create();
     512                $group_ids[3] = self::factory()->xprofile_group->create();
     513
     514                // Prime cache.
     515                $found = BP_XProfile_Group::get_group_ids();
     516                $this->assertContains( $group_ids[1], $found );
     517
     518                $g1 = new BP_XProfile_Group( $group_ids[1] );
     519                $g1->save();
     520
     521                $num_queries = $wpdb->num_queries;
     522
     523                $found = BP_XProfile_Group::get_group_ids();
     524                $this->assertNotSame( $num_queries, $wpdb->num_queries );
     525        }
     526
     527        /**
     528         * @group BP7435
     529         * @group cache
     530         */
     531        public function test_group_ids_query_cache_should_be_busted_on_field_save() {
     532                global $wpdb;
     533
     534                $group_ids    = array( 1 ); // Default group.
     535                $group_ids[1] = self::factory()->xprofile_group->create();
     536                $group_ids[2] = self::factory()->xprofile_group->create();
     537                $group_ids[3] = self::factory()->xprofile_group->create();
     538
     539                $f = self::factory()->xprofile_field->create( array(
     540                        'field_group_id' => $group_ids[1],
     541                ) );
     542                $field = new BP_XProfile_Field( $f );
     543
     544                // Prime cache.
     545                $found = BP_XProfile_Group::get_group_ids();
     546                $this->assertContains( $group_ids[1], $found );
     547
     548                $field->save();
     549
     550                $num_queries = $wpdb->num_queries;
     551
     552                $found = BP_XProfile_Group::get_group_ids();
     553                $this->assertNotSame( $num_queries, $wpdb->num_queries );
     554        }
     555
     556        /**
     557         * @group BP7435
     558         * @group cache
     559         */
     560        public function test_group_ids_query_cache_should_be_busted_on_field_delete() {
     561                $group_ids    = array( 1 ); // Default group.
     562                $group_ids[1] = self::factory()->xprofile_group->create();
     563                $group_ids[2] = self::factory()->xprofile_group->create();
     564                $group_ids[3] = self::factory()->xprofile_group->create();
     565
     566                $f = self::factory()->xprofile_field->create( array(
     567                        'field_group_id' => $group_ids[1],
     568                ) );
     569                $field = new BP_XProfile_Field( $f );
     570
     571                $args = array(
     572                        'hide_empty_groups' => true,
     573                );
     574
     575                // Prime cache.
     576                $found = BP_XProfile_Group::get_group_ids( $args );
     577                $this->assertContains( $group_ids[1], $found );
     578
     579                $field->delete();
     580
     581                $found = BP_XProfile_Group::get_group_ids( $args );
     582                $this->assertNotContains( $group_ids[1], $found );
     583        }
     584
     585        /**
     586         * @group BP7435
     587         * @group cache
     588         */
     589        public function test_group_field_ids_query_cache() {
     590                global $wpdb;
     591
     592                $group_id = self::factory()->xprofile_group->create();
     593
     594                $f = self::factory()->xprofile_field->create( array(
     595                        'field_group_id' => $group_id,
     596                ) );
     597                $field = new BP_XProfile_Field( $f );
     598
     599                // Prime cache.
     600                $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
     601                $this->assertContains( $f, $found );
     602
     603                $num_queries = $wpdb->num_queries;
     604
     605                $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
     606                $this->assertContains( $f, $found );
     607                $this->assertSame( $num_queries, $wpdb->num_queries );
     608        }
     609
     610        /**
     611         * @group BP7435
     612         * @group cache
     613         */
     614        public function test_group_field_ids_query_cache_should_be_busted_on_field_save() {
     615                global $wpdb;
     616
     617                $group_id = self::factory()->xprofile_group->create();
     618
     619                $f = self::factory()->xprofile_field->create( array(
     620                        'field_group_id' => $group_id,
     621                ) );
     622                $field = new BP_XProfile_Field( $f );
     623
     624                // Prime cache.
     625                $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
     626                $this->assertContains( $f, $found );
     627
     628                $field->save();
     629                $num_queries = $wpdb->num_queries;
     630
     631                $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
     632                $this->assertContains( $f, $found );
     633                $this->assertNotSame( $num_queries, $wpdb->num_queries );
     634        }
     635
     636        /**
     637         * @group BP7435
     638         * @group cache
     639         */
     640        public function test_group_field_ids_query_cache_should_be_busted_on_field_delete() {
     641                $group_id = self::factory()->xprofile_group->create();
     642
     643                $f = self::factory()->xprofile_field->create( array(
     644                        'field_group_id' => $group_id,
     645                ) );
     646                $field = new BP_XProfile_Field( $f );
     647
     648                // Prime cache.
     649                $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
     650                $this->assertContains( $f, $found );
     651
     652                $field->delete();
     653
     654                $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
     655                $this->assertNotContains( $f, $found );
     656        }
    444657}
Note: See TracChangeset for help on using the changeset viewer.