Skip to:
Content

BuddyPress.org

Ticket #8540: 8540.1.patch

File 8540.1.patch, 22.1 KB (added by dcavins, 2 years ago)

Improve behavior of BP_Signup class.

  • src/bp-members/bp-members-cache.php

    diff --git src/bp-members/bp-members-cache.php src/bp-members/bp-members-cache.php
    index 54185a454..296582726 100644
    function bp_members_reset_activity_cache_incrementor() { 
    7373        return bp_core_reset_incrementor( 'bp_activity_with_last_activity' );
    7474}
    7575add_action( 'bp_core_user_updated_last_activity', 'bp_members_reset_activity_cache_incrementor' );
     76
     77/**
     78 * Bust signup caches when editing or deleting.
     79 *
     80 * @since 10.0.0
     81 *
     82 * @param int $signup_id The ID of the signup affected.
     83 */
     84function bp_members_delete_signup_cache( $signup_id = 0 ) {
     85        wp_cache_delete( $signup_id, 'bp_signups' );
     86}
     87add_action( 'bp_core_signups_after_add',         'bp_members_delete_signup_cache' );
     88add_action( 'bp_core_signups_after_update_meta', 'bp_members_delete_signup_cache' );
     89
     90/**
     91 * Bust signup caches for arrays of signup IDs.
     92 *
     93 * @since 10.0.0
     94 *
     95 * @param array $signup_ids The IDs of the signups affected.
     96 */
     97function bp_members_delete_signup_cache_multiples( $signup_ids = array() ) {
     98        // Ensure that the incoming item is an array.
     99        $signup_ids = wp_parse_id_list( $signup_ids );
     100        foreach ( $signup_ids as $signup_id ) {
     101                bp_members_delete_signup_cache( $signup_id );
     102        }
     103}
     104add_action( 'bp_core_signup_after_resend',   'bp_members_delete_signup_cache_multiples' );
     105add_action( 'bp_core_signup_after_activate', 'bp_members_delete_signup_cache_multiples' );
     106add_action( 'bp_core_signup_after_delete',   'bp_members_delete_signup_cache_multiples' );
     107
     108/**
     109 * Reset cache incrementor for BP_Signups.
     110 *
     111 * This function invalidates all cached results of BP_Signup queries,
     112 * whenever one of the following events takes place:
     113 *   - A record is created or updated.
     114 *   - A record is deleted.
     115 *
     116 * @since 10.0.0
     117 *
     118 * @return bool True on success, false on failure.
     119 */
     120function bp_members_reset_signup_cache_incrementor() {
     121        return bp_core_reset_incrementor( 'bp_signups' );
     122}
     123add_filter( 'bp_core_signups_after_add',         'bp_members_reset_signup_cache_incrementor' );
     124add_action( 'bp_core_activated_user',            'bp_members_reset_signup_cache_incrementor' );
     125add_action( 'bp_core_signup_after_activate',     'bp_members_reset_signup_cache_incrementor' );
     126add_action( 'bp_core_signups_after_update_meta', 'bp_members_reset_signup_cache_incrementor' );
     127add_action( 'bp_core_signup_after_delete',       'bp_members_reset_signup_cache_incrementor' );
     128
  • src/bp-members/classes/class-bp-signup.php

    diff --git src/bp-members/classes/class-bp-signup.php src/bp-members/classes/class-bp-signup.php
    index f1eece597..b0f1d6673 100644
    class BP_Signup { 
    8989         * @param integer $signup_id The ID for the signup being queried.
    9090         */
    9191        public function __construct( $signup_id = 0 ) {
    92                 if ( !empty( $signup_id ) ) {
     92                if ( ! empty( $signup_id ) ) {
    9393                        $this->id = $signup_id;
    9494                        $this->populate();
    9595                }
    class BP_Signup { 
    103103        public function populate() {
    104104                global $wpdb;
    105105
    106                 $signups_table = buddypress()->members->table_name_signups;
    107                 $signup        = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$signups_table} WHERE signup_id = %d AND active = 0", $this->id ) );
     106                // Get BuddyPress.
     107                $bp    = buddypress();
    108108
    109                 $this->avatar         = get_avatar( $signup->user_email, 32 );
     109                // Check cache for signup data.
     110                $signup = wp_cache_get( $this->id, 'bp_signups' );
     111
     112                // Cache missed, so query the DB.
     113                if ( false === $signup ) {
     114                        $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id = %d AND active = 0", $this->id ) );
     115
     116                        wp_cache_set( $this->id, $signup, 'bp_signups' );
     117                }
     118
     119                // No signup found so set the ID and bail.
     120                if ( empty( $signup ) || is_wp_error( $signup ) ) {
     121                        $this->id = 0;
     122                        return;
     123                }
     124
     125                /*
     126                 * Add every db column to the object.
     127                 */
     128                $this->signup_id      = $this->id;
     129                $this->domain         = $signup->domain;
     130                $this->path           = $signup->path;
     131                $this->title          = $signup->title;
    110132                $this->user_login     = $signup->user_login;
    111133                $this->user_email     = $signup->user_email;
    112                 $this->meta           = maybe_unserialize( $signup->meta );
    113                 $this->user_name      = ! empty( $this->meta['field_1'] ) ? wp_unslash( $this->meta['field_1'] ) : '';
    114134                $this->registered     = $signup->registered;
     135                $this->activated      = $signup->activated;
     136                $this->active         = $signup->active;
    115137                $this->activation_key = $signup->activation_key;
     138                $this->meta           = maybe_unserialize( $signup->meta );
     139
     140                // Add richness.
     141                $this->avatar         = get_avatar( $signup->user_email, 32 );
     142                $this->user_name      = ! empty( $this->meta['field_1'] ) ? wp_unslash( $this->meta['field_1'] ) : '';
     143
     144                // When was the activation email sent?
     145                if ( isset( $this->meta['sent_date'] ) ) {
     146                        $this->date_sent = $this->meta['sent_date'];
     147                } else {
     148                        $this->date_sent = '0000-00-00 00:00:00';
     149                }
     150
     151                /**
     152                 * Calculate a diff between now & last time
     153                 * an activation link has been resent.
     154                 */
     155                $sent_at = mysql2date('U', $this->date_sent );
     156                $now     = current_time( 'timestamp', true );
     157                $diff    = $now - $sent_at;
     158
     159                /**
     160                 * Set a boolean to track whether an activation link
     161                 * was sent in the last day.
     162                 */
     163                $this->recently_sent = ( $diff < 1 * DAY_IN_SECONDS );
     164
     165                // How many times has the activation email been sent?
     166                if ( isset( $this->meta['count_sent'] ) ) {
     167                        $this->count_sent = absint( $this->meta['count_sent'] );
     168                } else {
     169                        $this->count_sent = 0;
     170                }
    116171        }
    117172
    118173        /** Static Methods *******************************************************/
    class BP_Signup { 
    126181         * @param array $args {
    127182         *     The argument to retrieve desired signups.
    128183         *     @type int         $offset         Offset amount. Default 0.
    129          *     @type int         $number         How many to fetch. Default 1.
     184         *     @type int         $number         How many to fetch. Pass -1 to fetch all. Default 1.
    130185         *     @type bool|string $usersearch     Whether or not to search for a username. Default false.
    131186         *     @type string      $orderby        Order By parameter. Possible values are `signup_id`, `login`, `email`,
    132187         *                                       `registered`, `activated`. Default `signup_id`.
    class BP_Signup { 
    145200        public static function get( $args = array() ) {
    146201                global $wpdb;
    147202
    148                 $r = bp_parse_args( $args,
     203                $bp = buddypress();
     204                $r  = bp_parse_args( $args,
    149205                        array(
    150206                                'offset'         => 0,
    151207                                'number'         => 1,
    class BP_Signup { 
    154210                                'order'          => 'DESC',
    155211                                'include'        => false,
    156212                                'activation_key' => '',
     213                                'user_email'     => '',
    157214                                'user_login'     => '',
    158215                                'fields'         => 'all',
    159216                        ),
    class BP_Signup { 
    171228
    172229                $r['orderby'] = sanitize_title( $r['orderby'] );
    173230
    174                 $sql = array();
    175                 $signups_table  = buddypress()->members->table_name_signups;
    176                 $sql['select']  = "SELECT * FROM {$signups_table}";
    177                 $sql['where']   = array();
    178                 $sql['where'][] = "active = 0";
     231                $sql = array(
     232                        'select'     => "SELECT DISTINCT signup_id",
     233                        'from'       => "{$bp->members->table_name_signups}",
     234                        'where'      => array(
     235                                'active = 0',
     236                        ),
     237                        'orderby'    => '',
     238                        'limit'      => '',
     239                );
    179240
    180241                if ( empty( $r['include'] ) ) {
    181242
    class BP_Signup { 
    190251                                $sql['where'][] = $wpdb->prepare( "activation_key = %s", $r['activation_key'] );
    191252                        }
    192253
     254                        // User email.
     255                        if ( ! empty( $r['user_email'] ) ) {
     256                                $sql['where'][] = $wpdb->prepare( "user_email = %s", $r['user_email'] );
     257                        }
     258
    193259                        // User login.
    194260                        if ( ! empty( $r['user_login'] ) ) {
    195261                                $sql['where'][] = $wpdb->prepare( "user_login = %s", $r['user_login'] );
    196262                        }
    197263
    198                         $sql['orderby'] = "ORDER BY {$r['orderby']}";
    199                         $sql['order']   = bp_esc_sql_order( $r['order'] );
    200                         $sql['limit']   = $wpdb->prepare( "LIMIT %d, %d", $r['offset'], $r['number'] );
     264                        $order          = bp_esc_sql_order( $r['order'] );
     265                        $sql['orderby'] = "ORDER BY {$r['orderby']} {$order}";
     266
     267                        $number = intval( $r['number'] );
     268                        if ( -1 !== $number ) {
     269                                $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", absint( $r['offset'] ), $number );
     270                        }
    201271                } else {
    202                         $in = implode( ',', wp_parse_id_list( $r['include'] ) );
    203                         $sql['in'] = "AND signup_id IN ({$in})";
     272                        $in             = implode( ',', wp_parse_id_list( $r['include'] ) );
     273                        $sql['where'][] = "signup_id IN ({$in})";
    204274                }
    205275
    206276                // Implode WHERE clauses.
    207277                $sql['where'] = 'WHERE ' . implode( ' AND ', $sql['where'] );
    208278
     279                $paged_signups_sql = "{$sql['select']} FROM {$sql['from']} {$sql['where']} {$sql['orderby']} {$sql['limit']}";
     280
    209281                /**
    210282                 * Filters the Signups paged query.
    211283                 *
    class BP_Signup { 
    216288                 * @param array  $args  Array of original arguments for get() method.
    217289                 * @param array  $r     Array of parsed arguments for get() method.
    218290                 */
    219                 $paged_signups = $wpdb->get_results( apply_filters( 'bp_members_signups_paged_query', join( ' ', $sql ), $sql, $args, $r ) );
     291                $paged_signups_sql = apply_filters( 'bp_members_signups_paged_query', $paged_signups_sql, $sql, $args, $r );
    220292
    221                 if ( empty( $paged_signups ) ) {
    222                         return array( 'signups' => false, 'total' => false );
     293                $cached = bp_core_get_incremented_cache( $paged_signups_sql, 'bp_signups' );
     294                if ( false === $cached ) {
     295                        $paged_signup_ids = $wpdb->get_col( $paged_signups_sql );
     296                        bp_core_set_incremented_cache( $paged_signups_sql, 'bp_signups', $paged_signup_ids );
     297                } else {
     298                        $paged_signup_ids = $cached;
    223299                }
    224300
    225301                // We only want the IDs.
    226302                if ( 'ids' === $r['fields'] ) {
    227                         $paged_signups = wp_list_pluck( $paged_signups, 'signup_id' );
    228                 } else {
    229 
    230                         // Used to calculate a diff between now & last
    231                         // time an activation link has been resent.
    232                         $now = current_time( 'timestamp', true );
    233 
    234                         foreach ( (array) $paged_signups as $key => $signup ) {
    235303
    236                                 $signup->id   = intval( $signup->signup_id );
     304                        $paged_signups = array_map( 'intval', $paged_signup_ids );
    237305
    238                                 $signup->meta = ! empty( $signup->meta ) ? maybe_unserialize( $signup->meta ) : false;
    239 
    240                                 $signup->user_name = '';
    241                                 if ( ! empty( $signup->meta['field_1'] ) ) {
    242                                         $signup->user_name = wp_unslash( $signup->meta['field_1'] );
    243                                 }
    244 
    245                                 // Sent date defaults to date of registration.
    246                                 if ( ! empty( $signup->meta['sent_date'] ) ) {
    247                                         $signup->date_sent = $signup->meta['sent_date'];
    248                                 } else {
    249                                         $signup->date_sent = $signup->registered;
    250                                 }
    251 
    252                                 $sent_at = mysql2date('U', $signup->date_sent );
    253                                 $diff    = $now - $sent_at;
    254 
    255                                 /**
    256                                  * Add a boolean in case the last time an activation link
    257                                  * has been sent happened less than a day ago.
    258                                  */
    259                                 if ( $diff < 1 * DAY_IN_SECONDS ) {
    260                                         $signup->recently_sent = true;
    261                                 }
     306                } else {
    262307
    263                                 if ( ! empty( $signup->meta['count_sent'] ) ) {
    264                                         $signup->count_sent = absint( $signup->meta['count_sent'] );
    265                                 } else {
    266                                         $signup->count_sent = 1;
     308                        $uncached_signup_ids = bp_get_non_cached_ids( $paged_signup_ids, 'bp_signups' );
     309                        if ( $uncached_signup_ids ) {
     310                                $signup_ids_sql      = implode( ',', array_map( 'intval', $uncached_signup_ids ) );
     311                                $signup_data_objects = $wpdb->get_results( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id IN ({$signup_ids_sql})" );
     312                                foreach ( $signup_data_objects as $signup_data_object ) {
     313                                        wp_cache_set( $signup_data_object->signup_id, $signup_data_object, 'bp_signups' );
    267314                                }
     315                        }
    268316
    269                                 $paged_signups[ $key ] = $signup;
     317                        $paged_signups = array();
     318                        foreach ( $paged_signup_ids as $paged_signup_id ) {
     319                                $paged_signups[] = new BP_Signup( $paged_signup_id );
    270320                        }
     321
    271322                }
    272323
    273                 unset( $sql['limit'] );
    274                 $sql['select'] = preg_replace( "/SELECT.*?FROM/", "SELECT COUNT(*) FROM", $sql['select'] );
     324                // Find the total number of signups in the results set.
     325                $total_signups_sql = "SELECT COUNT(DISTINCT signup_id) FROM {$sql['from']} {$sql['where']}";
    275326
    276327                /**
    277328                 * Filters the Signups count query.
    class BP_Signup { 
    283334                 * @param array  $args  Array of original arguments for get() method.
    284335                 * @param array  $r     Array of parsed arguments for get() method.
    285336                 */
    286                 $total_signups = $wpdb->get_var( apply_filters( 'bp_members_signups_count_query', join( ' ', $sql ), $sql, $args, $r ) );
     337                $total_signups_sql = apply_filters( 'bp_members_signups_count_query', $total_signups_sql, $sql, $args, $r );
     338
     339                $cached = bp_core_get_incremented_cache( $total_signups_sql, 'bp_signups' );
     340                if ( false === $cached ) {
     341                        $total_signups = (int) $wpdb->get_var( $total_signups_sql );
     342                        bp_core_set_incremented_cache( $total_signups_sql, 'bp_signups', $total_signups );
     343                } else {
     344                        $total_signups = (int) $cached;
     345                }
    287346
    288347                return array( 'signups' => $paged_signups, 'total' => $total_signups );
    289348        }
    class BP_Signup { 
    318377                                'user_email'     => '',
    319378                                'registered'     => current_time( 'mysql', true ),
    320379                                'activation_key' => '',
    321                                 'meta'           => '',
     380                                'meta'           => array(),
    322381                        ),
    323382                        'bp_core_signups_add_args'
    324383                );
    325384
     385                // Ensure that sent_date and count_sent are set in meta.
     386                if ( ! isset( $r['meta']['sent_date'] ) ) {
     387                        $r['meta']['sent_date'] = '0000-00-00 00:00:00';
     388                }
     389                if ( ! isset( $r['meta']['count_sent'] ) ) {
     390                        $r['meta']['count_sent'] = 0;
     391                }
     392
    326393                $r['meta'] = maybe_serialize( $r['meta'] );
    327394
    328395                $inserted = $wpdb->insert(
    class BP_Signup { 
    337404                        $retval = false;
    338405                }
    339406
     407                /**
     408                 * Fires after adding a new BP_Signup.
     409                 *
     410                 * @since 10.0.0
     411                 *
     412                 * @param int|bool $retval ID of the BP_Signup just added.
     413                 * @param array    $r      Array of parsed arguments for add() method.
     414                 * @param array    $args   Array of original arguments for add() method.
     415                 */
     416                do_action( 'bp_core_signups_after_add', $retval, $r, $args );
     417
    340418                /**
    341419                 * Filters the result of a signup addition.
    342420                 *
    343421                 * @since 2.0.0
    344422                 *
    345                  * @param int|bool $retval Newly added user ID on success, false on failure.
     423                 * @param int|bool $retval Newly added signup ID on success, false on failure.
    346424                 */
    347425                return apply_filters( 'bp_core_signups_add', $retval );
    348426        }
    class BP_Signup { 
    420498                        }
    421499                }
    422500
     501                /**
     502                 * Fires after adding a new WP User (backcompat).
     503                 *
     504                 * @since 10.0.0
     505                 *
     506                 * @param int $user_id ID of the WP_User just added.
     507                 */
     508                do_action( 'bp_core_signups_after_add_backcompat', $user_id );
     509
    423510                /**
    424511                 * Filters the user ID for the backcompat functionality.
    425512                 *
    class BP_Signup { 
    445532                        return false;
    446533                }
    447534
    448                 $user_status = $wpdb->get_var( $wpdb->prepare( "SELECT user_status FROM {$wpdb->users} WHERE ID = %d", $user_id ) );
     535                $user        = get_user_by( 'id', $user_id );
     536                $user_status = $user->user_status;
    449537
    450538                /**
    451539                 * Filters the user status of a provided user ID.
    class BP_Signup { 
    511599         * @return int The number of signups.
    512600         */
    513601        public static function count_signups() {
    514                 global $wpdb;
    515 
    516                 $signups_table = buddypress()->members->table_name_signups;
    517                 $count_signups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) AS total FROM {$signups_table} WHERE active = %d", 0 ) );
     602                $all_signups   = self::get(
     603                        array(
     604                                'fields' => 'ids',
     605                        )
     606                );
     607                $count_signups = $all_signups['total'];
    518608
    519609                /**
    520610                 * Filters the total inactive signups.
    class BP_Signup { 
    556646                        return false;
    557647                }
    558648
     649                $signup_id = absint( $r['signup_id'] );
     650
     651                // Figure out which meta keys should be updated.
     652                $signup       = new BP_Signup( $signup_id );
     653                $blended_meta = wp_parse_args( $r['meta'], $signup->meta );
     654
    559655                $wpdb->update(
    560656                        // Signups table.
    561657                        buddypress()->members->table_name_signups,
    562658                        // Data to update.
    563659                        array(
    564                                 'meta' => serialize( $r['meta'] ),
     660                                'meta' => serialize( $blended_meta ),
    565661                        ),
    566662                        // WHERE.
    567663                        array(
    568                                 'signup_id' => $r['signup_id'],
     664                                'signup_id' => $signup_id,
    569665                        ),
    570666                        // Data sanitization format.
    571667                        array(
    class BP_Signup { 
    577673                        )
    578674                );
    579675
     676                /**
     677                 * Fires after updating the meta of a new BP_Signup.
     678                 *
     679                 * @since 10.0.0
     680                 *
     681                 * @param int|bool $signup_id    ID of the BP_Signup updated.
     682                 * @param array    $r            Array of parsed arguments to update() method.
     683                 * @param array    $args         Array of original arguments to update() method.
     684                 * @param array    $blended_meta The complete set of meta to save.
     685                 */
     686                do_action( 'bp_core_signups_after_update_meta', $signup_id, $r, $args, $blended_meta );
     687
    580688                /**
    581689                 * Filters the signup ID which received a meta update.
    582690                 *
    class BP_Signup { 
    621729
    622730                foreach ( $signups as $signup ) {
    623731
    624                         $meta               = $signup->meta;
    625                         $meta['sent_date']  = current_time( 'mysql', true );
    626                         $meta['count_sent'] = $signup->count_sent + 1;
     732                        $meta = array(
     733                                'sent_date'  => current_time( 'mysql', true ),
     734                                'count_sent' => $signup->count_sent + 1
     735                        );
    627736
    628737                        // Send activation email.
    629738                        if ( is_multisite() ) {
  • tests/phpunit/testcases/members/class-bp-signup.php

    diff --git tests/phpunit/testcases/members/class-bp-signup.php tests/phpunit/testcases/members/class-bp-signup.php
    index 4a420d414..cee9cbe47 100644
    class BP_Tests_BP_Signup extends BP_UnitTestCase { 
    356356
    357357                $this->assertEquals( array( $s3, $s2, $s1 ), $ss['signups'] );
    358358        }
     359
     360        /**
     361         * @group cache
     362         */
     363        public function test_get_queries_should_be_cached() {
     364                global $wpdb;
     365
     366                $s = self::factory()->signup->create();
     367
     368                $found1 = BP_Signup::get(
     369                        array(
     370                                'fields' => 'ids',
     371                        )
     372                );
     373
     374                $num_queries = $wpdb->num_queries;
     375
     376                $found2 = BP_Signup::get(
     377                        array(
     378                                'fields' => 'ids',
     379                        )
     380                );
     381
     382                $this->assertEqualSets( $found1, $found2 );
     383                $this->assertSame( $num_queries, $wpdb->num_queries );
     384        }
     385
     386        /**
     387         * @group cache
     388         */
     389        public function test_get_query_caches_should_be_busted_by_add() {
     390                $s1 = self::factory()->signup->create();
     391
     392                $found1 = BP_Signup::get(
     393                        array(
     394                                'fields' => 'ids',
     395                        )
     396                );
     397                $this->assertEqualSets( array( $s1 ), $found1['signups'] );
     398
     399                $s2 = self::factory()->signup->create();
     400                $found2 = BP_Signup::get(
     401                        array(
     402                                'fields' => 'ids',
     403                        )
     404                );
     405                $this->assertEqualSets( array( $s2 ), $found2['signups'] );
     406        }
     407
     408        /**
     409         * @group cache
     410         */
     411        public function test_get_query_caches_should_be_busted_by_meta_update() {
     412                $time = bp_core_current_time();
     413
     414                $args = array(
     415                        'domain' => 'foo',
     416                        'path' => 'bar',
     417                        'title' => 'Foo bar',
     418                        'user_login' => 'user1',
     419                        'user_email' => 'user1@example.com',
     420                        'registered' => $time,
     421                        'activation_key' => '12345',
     422                        'meta' => array(
     423                                'field_1' => 'Fozzie',
     424                                'meta1' => 'meta2',
     425                        ),
     426                );
     427                $s1 = BP_Signup::add( $args );
     428
     429                $args['meta']['field_1'] = 'Fozz';
     430                $s2 = BP_Signup::add( $args );
     431
     432                // Should find both.
     433                $found1 = BP_Signup::get( array(
     434                        'fields' => 'ids',
     435                        'number'  => -1,
     436                        'usersearch' => 'Fozz',
     437                ) );
     438                $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
     439
     440                BP_Signup::update( array(
     441                        'signup_id'  => $s1,
     442                        'meta'       => array(
     443                                'field_1' => 'Fonzie'
     444                        ),
     445                ) );
     446
     447                $found2 = BP_Signup::get( array(
     448                        'fields' => 'ids',
     449                        'number'  => -1,
     450                        'usersearch' => 'Fozz',
     451                ) );
     452
     453                $this->assertEqualSets( array( $s2 ), $found2['signups'] );
     454        }
     455
     456        /**
     457         * @group cache
     458         */
     459        public function test_get_query_caches_should_be_busted_by_delete() {
     460                global $wpdb;
     461                $time = bp_core_current_time();
     462
     463                $args = array(
     464                        'domain' => 'foo',
     465                        'path' => 'bar',
     466                        'title' => 'Foo bar',
     467                        'user_login' => 'user1',
     468                        'user_email' => 'user1@example.com',
     469                        'registered' => $time,
     470                        'activation_key' => '12345',
     471                        'meta' => array(
     472                                'field_1' => 'Fozzie',
     473                                'meta1' => 'meta2',
     474                        ),
     475                );
     476                $s1 = BP_Signup::add( $args );
     477
     478                $args['meta']['field_1'] = 'Fozz';
     479                $s2 = BP_Signup::add( $args );
     480
     481                // Should find both.
     482                $found1 = BP_Signup::get( array(
     483                        'fields' => 'ids',
     484                        'number'  => -1,
     485                        'usersearch' => 'Fozz',
     486                ) );
     487                $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
     488
     489                BP_Signup::delete( array( $s1 ) );
     490
     491                $found2 = BP_Signup::get( array(
     492                        'fields' => 'ids',
     493                        'number'  => -1,
     494                        'usersearch' => 'Fozz',
     495                ) );
     496
     497                $this->assertEqualSets( array( $s2 ), $found2['signups'] );
     498        }
     499
     500        /**
     501         * @group cache
     502         */
     503        public function test_get_query_caches_should_be_busted_by_activation() {
     504                $s1 = self::factory()->signup->create( array(
     505                        'user_login'     => 'accountone',
     506                        'user_email'     => 'accountone@example.com',
     507                        'activation_key' => 'activationkeyone',
     508                ) );
     509
     510                $s2 = self::factory()->signup->create( array(
     511                        'user_login'     => 'accounttwo',
     512                        'user_email'     => 'accounttwo@example.com',
     513                        'activation_key' => 'activationkeytwo',
     514                ) );
     515                $found1 = BP_Signup::get(
     516                        array(
     517                                'number' => -1,
     518                                'fields' => 'ids',
     519                        )
     520                );
     521                $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
     522
     523                $activate = BP_Signup::activate( (array) $s2 );
     524
     525                $found2 = BP_Signup::get(
     526                        array(
     527                                'number' => -1,
     528                                'fields' => 'ids',
     529                        )
     530                );
     531                $this->assertEqualSets( array( $s1 ), $found2['signups'] );
     532        }
     533
     534        /**
     535         * @group cache
     536         */
     537        public function test_objects_should_be_cached() {
     538                $s1 = self::factory()->signup->create( array(
     539                        'user_login'     => 'accountone',
     540                        'user_email'     => 'accountone@example.com',
     541                        'activation_key' => 'activationkeyone',
     542                ) );
     543
     544                $found1 = new BP_Signup( $s1 );
     545                $this->assertEquals( $s1, $found1->id );
     546
     547                $activate = BP_Signup::activate( (array) $s1 );
     548
     549                $found2 = new BP_Signup( $s1 );
     550                $this->assertEquals( 0, $found2->id );
     551        }
     552
     553        /**
     554         * @group cache
     555         */
     556        public function object_caches_should_be_busted_by_activation() {
     557                global $wpdb;
     558
     559                $s1 = self::factory()->signup->create( array(
     560                        'user_login'     => 'accountone',
     561                        'user_email'     => 'accountone@example.com',
     562                        'activation_key' => 'activationkeyone',
     563                ) );
     564
     565                $found1 = new BP_Signup( $s1 );
     566
     567                $num_queries = $wpdb->num_queries;
     568
     569                // Object should be rebuilt from cache.
     570                $found2 = new BP_Signup( $s1 );
     571
     572                // @TODO: This fails because "get_avatar()" in populate() results in db queries.
     573                $this->assertEquals( $found1, $found2 );
     574                $this->assertEquals( $num_queries, $wpdb->num_queries );
     575        }
    359576}