Skip to:
Content

BuddyPress.org

Ticket #6209: 6209-groups_leave_group.06.patch

File 6209-groups_leave_group.06.patch, 8.5 KB (added by dcavins, 5 years ago)

Change groups_leave_group() to use groups_remove_member(). Change groups_remove_member() to work in more cases.

  • src/bp-groups/bp-groups-functions.php

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index 8b19738..283392b 100644
    function groups_leave_group( $group_id, $user_id = 0 ) { 
    431431                }
    432432        }
    433433
    434         // This is exactly the same as deleting an invite, just is_confirmed = 1 NOT 0.
    435         if ( !groups_uninvite_user( $user_id, $group_id ) ) {
     434        if ( ! groups_remove_member( $user_id, $group_id ) ) {
    436435                return false;
    437436        }
    438437
    function groups_unban_member( $user_id, $group_id ) { 
    14011400 */
    14021401function groups_remove_member( $user_id, $group_id ) {
    14031402
    1404         if ( ! bp_is_item_admin() )
    1405                 return false;
     1403        if ( ! bp_is_item_admin() ) {
     1404                // bp_is_item_admin may not be set if this function is called outside of group context.
     1405                // Site admins and group admins can remove a member from a group.
     1406                // A member may also request to remove herself from a group.
     1407                if ( ! current_user_can( 'bp_moderate' )
     1408                        && ! groups_is_user_admin( bp_loggedin_user_id(), $group_id )
     1409                        && $user_id != bp_loggedin_user_id() ) {
     1410                                return false;
     1411                        }
     1412        }
    14061413
    14071414        $member = new BP_Groups_Member( $user_id, $group_id );
    14081415
  • tests/phpunit/testcases/groups/class-bp-groups-member.php

    diff --git tests/phpunit/testcases/groups/class-bp-groups-member.php tests/phpunit/testcases/groups/class-bp-groups-member.php
    index 135303f..0855b83 100644
    class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 
    513513                $u1_has_request = groups_check_for_membership_request( $u1, $g );
    514514                $this->assertTrue( is_numeric( $u1_has_request ) && $u1_has_request > 0 );
    515515        }
     516
     517        /**
     518         * @group groups_join_group
     519         */
     520        public function test_groups_join_group_basic_join() {
     521                $u1 = $this->factory->user->create();
     522                $g = $this->factory->group->create();
     523
     524                groups_join_group( $g, $u1 );
     525                $membership_id = groups_is_user_member( $u1, $g );
     526                $this->assertTrue( is_numeric( $membership_id ) && $membership_id > 0 );
     527        }
     528
     529        /**
     530         * @group groups_join_group
     531         */
     532        public function test_groups_join_group_basic_join_use_current_user() {
     533                $u1 = $this->factory->user->create();
     534                $g = $this->factory->group->create();
     535                $old_current_user = get_current_user_id();
     536                $this->set_current_user( $u1 );
     537
     538                groups_join_group( $g );
     539                $membership_id = groups_is_user_member( $u1, $g );
     540                $this->assertTrue( is_numeric( $membership_id ) && $membership_id > 0 );
     541                $this->set_current_user( $old_current_user );
     542        }
     543
     544        /**
     545         * @group groups_join_group
     546         */
     547        public function test_groups_join_group_already_member() {
     548                $u1 = $this->factory->user->create();
     549                $g = $this->factory->group->create();
     550                $this->add_user_to_group( $u1, $g );
     551
     552                $this->assertTrue( groups_join_group( $g, $u1 ) );
     553        }
     554
     555        /**
     556         * @group groups_join_group
     557         */
     558        public function test_groups_join_group_cleanup_invites() {
     559                $u1 = $this->factory->user->create();
     560                $u2 = $this->factory->user->create();
     561                $g = $this->factory->group->create();
     562                $this->add_user_to_group( $u1, $g );
     563
     564                $m1 = new BP_Groups_Member( $u1, $g );
     565                $m1->promote( 'admin' );
     566
     567                self::invite_user_to_group( $u2, $g, $u1 );
     568
     569                groups_join_group( $g, $u2 );
     570                // Upon joining the group, outstanding invitations should be cleaned up.
     571                $this->assertEquals( null, groups_check_user_has_invite( $u2, $g, 'any' ) );
     572        }
     573
     574        /**
     575         * @group groups_join_group
     576         */
     577        public function test_groups_join_group_cleanup_requests() {
     578                $u1 = $this->factory->user->create();
     579                $g = $this->factory->group->create();
     580                self::create_group_membership_request( $u1, $g );
     581
     582                groups_join_group( $g, $u1 );
     583                // Upon joining the group, outstanding requests should be cleaned up.
     584                $this->assertEquals( null, groups_check_for_membership_request( $u1, $g ) );
     585        }
     586
     587        /**
     588         * @group groups_leave_group
     589         */
     590        public function test_groups_leave_group_basic_leave_self_initiated() {
     591                $old_current_user = get_current_user_id();
     592                $u1 = $this->factory->user->create();
     593                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     594                $u2 = $this->factory->user->create();
     595                $this->add_user_to_group( $u2, $g );
     596
     597                $before = groups_get_total_member_count( $g );
     598                $this->set_current_user( $u2 );
     599                groups_leave_group( $g, $u2 );
     600                $after = groups_get_total_member_count( $g );
     601
     602                $this->assertEquals( $before - 1, $after );
     603                $this->set_current_user( $old_current_user );
     604        }
     605
     606        /**
     607         * @group groups_leave_group
     608         */
     609        public function test_groups_leave_group_basic_leave_use_current_user() {
     610                $old_current_user = get_current_user_id();
     611                $u1 = $this->factory->user->create();
     612                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     613                $u2 = $this->factory->user->create();
     614                $this->add_user_to_group( $u2, $g );
     615
     616                $before = groups_get_total_member_count( $g );
     617                $this->set_current_user( $u2 );
     618                groups_leave_group( $g );
     619                $after = groups_get_total_member_count( $g );
     620
     621                $this->assertEquals( $before - 1, $after );
     622                $this->set_current_user( $old_current_user );
     623        }
     624
     625        /**
     626         * @group groups_leave_group
     627         */
     628        public function test_groups_leave_group_basic_leave_group_admin_initiated() {
     629                $old_current_user = get_current_user_id();
     630                $u1 = $this->factory->user->create();
     631                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     632                $u2 = $this->factory->user->create();
     633                $this->add_user_to_group( $u2, $g );
     634
     635                $before = groups_get_total_member_count( $g );
     636                $this->set_current_user( $u1 );
     637                groups_leave_group( $g, $u2 );
     638                $after = groups_get_total_member_count( $g );
     639
     640                $this->assertEquals( $before - 1, $after );
     641                $this->set_current_user( $old_current_user );
     642        }
     643
     644        /**
     645         * @group groups_leave_group
     646         */
     647        public function test_groups_leave_group_basic_leave_site_admin_initiated() {
     648                $old_current_user = get_current_user_id();
     649                $u1 = $this->factory->user->create();
     650                $u1_siteadmin = new WP_User( $u1 );
     651                $u1_siteadmin->add_role( 'administrator' );
     652                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     653                $u2 = $this->factory->user->create();
     654                $this->add_user_to_group( $u2, $g );
     655
     656                $before = groups_get_total_member_count( $g );
     657                $this->set_current_user( $u1 );
     658                groups_leave_group( $g, $u2 );
     659                $after = groups_get_total_member_count( $g );
     660
     661                $this->assertEquals( $before - 1, $after );
     662                $this->set_current_user( $old_current_user );
     663        }
     664
     665
     666        /**
     667         * @group groups_leave_group
     668         */
     669        public function test_groups_leave_group_single_admin_prevent_leave() {
     670                $old_current_user = get_current_user_id();
     671                $u1 = $this->factory->user->create();
     672                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     673                $u2 = $this->factory->user->create();
     674                $this->add_user_to_group( $u2, $g );
     675
     676                $before = groups_get_total_member_count( $g );
     677                $this->set_current_user( $u1 );
     678                groups_leave_group( $g, $u1 );
     679                $after = groups_get_total_member_count( $g );
     680
     681                $this->assertEquals( $before, $after );
     682                $this->set_current_user( $old_current_user );
     683        }
     684
     685        /**
     686         * @group groups_leave_group
     687         */
     688        public function test_groups_leave_group_multiple_admins_allow_leave() {
     689                $old_current_user = get_current_user_id();
     690                $u1 = $this->factory->user->create();
     691                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     692                $u2 = $this->factory->user->create();
     693                $this->add_user_to_group( $u2, $g );
     694                $m2 = new BP_Groups_Member( $u2, $g );
     695                $m2->promote( 'admin' );
     696
     697                $before = groups_get_total_member_count( $g );
     698                $this->set_current_user( $u1 );
     699                groups_leave_group( $g, $u1 );
     700                $after = groups_get_total_member_count( $g );
     701
     702                $this->assertEquals( $before - 1, $after );
     703                $this->set_current_user( $old_current_user );
     704        }
    516705}
     706 No newline at end of file
  • tests/phpunit/testcases/groups/functions.php

    diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php
    index 04953c5..f73766b 100644
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    3737                groups_join_group( $g1, $u2 );
    3838                groups_join_group( $g2, $u2 );
    3939
    40                 // Test fails because of the change in groups_uninvite_user.
    41                 // @TODO: restore once groups_leave_group is fixed.
     40                // Set the current user so the leave group request goes through.
     41                $this->set_current_user( $u2 );
    4242                groups_leave_group( $g1, $u2 );
    43                 // $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
     43                $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
    4444        }
    4545
    4646        /**