Skip to:
Content

BuddyPress.org

Changeset 7182


Ignore:
Timestamp:
06/07/2013 02:06:51 AM (12 years ago)
Author:
boonebgorges
Message:

Improvements to how group total_member_count is refreshed on membership events

  • Use an actual member count when refreshing, rather than incrementing
  • Centralize all of the updating in the database methods, rather than having the updates spread throughout the groups component

Also adds integration tests for total_member_count as relates to the groups_*
functions.

See #5018

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups/bp-groups-classes.php

    r7180 r7182  
    12261226        self::refresh_total_group_count_for_user( $this->user_id );
    12271227
     1228        // Update the group's member count
     1229        self::refresh_total_member_count_for_group( $this->group_id );
     1230
    12281231        do_action_ref_array( 'groups_member_after_save', array( &$this ) );
    12291232
     
    12561259
    12571260    function ban() {
    1258 
    12591261        if ( !empty( $this->is_admin ) )
    12601262            return false;
     
    12631265        $this->is_banned = 1;
    12641266
    1265         groups_update_groupmeta( $this->group_id, 'total_member_count', ( (int) groups_get_groupmeta( $this->group_id, 'total_member_count' ) - 1 ) );
    1266 
    12671267        return $this->save();
    12681268    }
    12691269
    12701270    function unban() {
    1271 
    12721271        if ( !empty( $this->is_admin ) )
    12731272            return false;
    12741273
    12751274        $this->is_banned = 0;
    1276 
    1277         groups_update_groupmeta( $this->group_id, 'total_member_count', ( (int) groups_get_groupmeta( $this->group_id, 'total_member_count' ) + 1 ) );
    12781275
    12791276        return $this->save();
     
    12991296            return false;
    13001297
    1301         groups_update_groupmeta( $this->group_id, 'total_member_count', ( (int) groups_get_groupmeta( $this->group_id, 'total_member_count' ) - 1 ) );
    1302 
    13031298        // Update the user's group count
    13041299        self::refresh_total_group_count_for_user( $this->user_id );
    13051300
     1301        // Update the group's member count
     1302        self::refresh_total_member_count_for_group( $this->group_id );
     1303
    13061304        return $result;
    13071305    }
     
    13091307    /** Static Methods ********************************************************/
    13101308
     1309    /**
     1310     * Refresh the total_group_count for a user
     1311     *
     1312     * @since BuddyPress (1.8)
     1313     * @param int $user_id
     1314     * @return bool True on success
     1315     */
    13111316    public static function refresh_total_group_count_for_user( $user_id ) {
    1312         bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
     1317        return bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
     1318    }
     1319
     1320    /**
     1321     * Refresh the total_member_count for a group
     1322     *
     1323     * @since BuddyPress (1.8)
     1324     * @param int $group_id
     1325     * @return bool True on success
     1326     */
     1327    public static function refresh_total_member_count_for_group( $group_id ) {
     1328        return groups_update_groupmeta( $group_id, 'total_member_count', (int) BP_Groups_Group::get_total_member_count( $group_id ) );
    13131329    }
    13141330
     
    13201336        // Update the user's group count
    13211337        self::refresh_total_group_count_for_user( $user_id );
     1338
     1339        // Update the group's member count
     1340        self::refresh_total_member_count_for_group( $group_id );
    13221341
    13231342        return $remove;
  • trunk/bp-groups/bp-groups-functions.php

    r7179 r7182  
    124124        $member->save();
    125125
    126         groups_update_groupmeta( $group->id, 'total_member_count', 1 );
    127126        groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() );
    128127
     
    299298    if ( !groups_uninvite_user( $user_id, $group_id ) )
    300299        return false;
    301 
    302     // Modify group member count
    303     groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 );
    304300
    305301    /**
     
    362358
    363359    // Modify group meta
    364     groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
    365360    groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    366361
     
    722717
    723718    // Modify group meta
    724     groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
    725719    groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    726720
     
    920914        groups_delete_invite( $membership->user_id, $membership->group_id );
    921915
    922     // Modify group member count
    923     groups_update_groupmeta( $membership->group_id, 'total_member_count', (int) groups_get_groupmeta( $membership->group_id, 'total_member_count') + 1 );
    924 
    925916    // Record this in activity streams
    926917    $group = groups_get_group( array( 'group_id' => $membership->group_id ) );
  • trunk/tests/testcases/groups/functions.php

    r7179 r7182  
    5858    /**
    5959     * @group total_group_count
    60      * @group BP_Groups_Member
    61      * @group unban
    62      */
    63     public function test_total_group_count_BP_Groups_Member_unban() {
     60     * @group groups_unban_member
     61     */
     62    public function test_total_group_count_groups_unban_member() {
    6463        $u1 = $this->create_user();
    6564        $u2 = $this->create_user();
     
    113112    }
    114113
     114    /**
     115     * @group total_group_count
     116     * @group groups_remove_member
     117     */
    115118    public function test_total_group_count_groups_remove_member() {
    116119        $u1 = $this->create_user();
     
    129132        $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
    130133    }
     134
     135    /**
     136     * @group total_member_count
     137     * @group groups_join_group
     138     */
     139    public function test_total_member_count_groups_join_group() {
     140        $u1 = $this->create_user();
     141        $u2 = $this->create_user();
     142        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     143
     144        groups_join_group( $g, $u2 );
     145        $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
     146    }
     147
     148    /**
     149     * @group total_member_count
     150     * @group groups_leave_group
     151     */
     152    public function test_total_member_count_groups_leave_group() {
     153        $u1 = $this->create_user();
     154        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     155        groups_join_group( $g1, $u2 );
     156
     157        groups_leave_group( $g1, $u2 );
     158        $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
     159    }
     160
     161    /**
     162     * @group total_member_count
     163     * @group groups_ban_member
     164     */
     165    public function test_total_member_count_groups_ban_member() {
     166        $u1 = $this->create_user();
     167        $u2 = $this->create_user();
     168        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     169        groups_join_group( $g1, $u2 );
     170
     171        // Fool the admin check
     172        $this->set_current_user( $u1 );
     173        buddypress()->is_item_admin = true;
     174
     175        groups_ban_member( $u2, $g1 );
     176
     177        $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
     178    }
     179
     180    /**
     181     * @group total_member_count
     182     * @group groups_unban_member
     183     */
     184    public function test_total_member_count_groups_unban_member() {
     185        $u1 = $this->create_user();
     186        $u2 = $this->create_user();
     187        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     188        groups_join_group( $g1, $u2 );
     189
     190        // Fool the admin check
     191        $this->set_current_user( $u1 );
     192        buddypress()->is_item_admin = true;
     193
     194        groups_ban_member( $u2, $g1 );
     195
     196        groups_unban_member( $u2, $g1 );
     197
     198        $this->assertEquals( 2, groups_get_groupmeta( $g1, 'total_member_count' ) );
     199    }
     200
     201    /**
     202     * @group total_member_count
     203     * @group groups_accept_invite
     204     */
     205    public function test_total_member_count_groups_accept_invite() {
     206        $u1 = $this->create_user();
     207        $u2 = $this->create_user();
     208        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     209        groups_invite_user( array(
     210            'user_id' => $u1,
     211            'group_id' => $g,
     212            'inviter_id' => $u2,
     213        ) );
     214
     215        groups_accept_invite( $u2, $g );
     216
     217        $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
     218    }
     219
     220    /**
     221     * @group total_member_count
     222     * @group groups_accept_membership_request
     223     */
     224    public function test_total_member_count_groups_accept_membership_request() {
     225        $u1 = $this->create_user();
     226        $u2 = $this->create_user();
     227        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     228        groups_send_membership_request( $u2, $g );
     229
     230        groups_accept_membership_request( 0, $u2, $g );
     231
     232        $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
     233    }
     234
     235    /**
     236     * @group total_member_count
     237     * @group groups_remove_member
     238     */
     239    public function test_total_member_count_groups_remove_member() {
     240        $u1 = $this->create_user();
     241        $u2 = $this->create_user();
     242        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     243        groups_join_group( $g1, $u2 );
     244
     245        // Fool the admin check
     246        $this->set_current_user( $u1 );
     247        buddypress()->is_item_admin = true;
     248
     249        groups_remove_member( $u2, $g1 );
     250
     251        $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
     252    }
     253
     254    /**
     255     * @group total_member_count
     256     * @group groups_create_group
     257     */
     258    public function test_total_member_count_groups_create_group() {
     259        $u1 = $this->create_user();
     260        $g = groups_create_group( array(
     261            'creator_id' => $u1,
     262            'name' => 'Boone Is Handsome',
     263            'description' => 'Yes',
     264            'slug' => 'boone-is-handsome',
     265            'status' => 'public',
     266            'enable_forum' => 0,
     267            'date_created' => bp_core_current_time(),
     268        ) );
     269
     270        $this->assertEquals( 1, groups_get_groupmeta( $g, 'total_member_count' ) );
     271    }
    131272}
Note: See TracChangeset for help on using the changeset viewer.