Skip to:
Content

BuddyPress.org

Changeset 8177


Ignore:
Timestamp:
03/27/2014 07:29:08 PM (12 years ago)
Author:
boonebgorges
Message:

Bail from bp_activity_remove_user_favorite() if the user has not previously favorited the item in question

The failure to do this check could result in the incorrect decrementing of the
item's favorite_count.

Fixes #5478

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-functions.php

    r8175 r8177  
    471471                $user_id = bp_loggedin_user_id();
    472472
    473         // Remove the fav from the user's favs
    474473        $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
    475474        $my_favs = array_flip( (array) $my_favs );
     475
     476        // Bail if the user has not previously favorited the item
     477        if ( ! isset( $my_favs[ $activity_id ] ) ) {
     478                return false;
     479        }
     480
     481        // Remove the fav from the user's favs
    476482        unset( $my_favs[$activity_id] );
    477483        $my_favs = array_unique( array_flip( $my_favs ) );
  • trunk/tests/testcases/activity/functions.php

    r8175 r8177  
    711711        }
    712712
     713        /**
     714         * @group favorites
     715         * @group bp_activity_remove_user_favorite
     716         */
     717        public function test_remove_user_favorite_bad_activity_id() {
     718                $u1 = $this->create_user();
     719                $u2 = $this->create_user();
     720                $a = $this->factory->activity->create();
     721
     722                // Only favorite for user 1
     723                bp_activity_add_user_favorite( $a, $u1 );
     724
     725                // Removing for user 2 should fail
     726                $this->assertFalse( bp_activity_remove_user_favorite( $a, $u2 ) );
     727                $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
     728        }
     729
    713730        public function check_activity_caches() {
    714731                foreach ( $this->acaches as $k => $v ) {
Note: See TracChangeset for help on using the changeset viewer.