Skip to:
Content

BuddyPress.org

Ticket #6209: 6209-correct-delete-behavior.03.patch

File 6209-correct-delete-behavior.03.patch, 17.7 KB (added by dcavins, 5 years ago)

Correct invite/request delete behavior

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

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index 011231f..dc6c6b5 100644
    function groups_get_id( $group_slug ) { 
    420420 */
    421421function groups_leave_group( $group_id, $user_id = 0 ) {
    422422
    423         if ( empty( $user_id ) )
     423        if ( empty( $user_id ) ) {
    424424                $user_id = bp_loggedin_user_id();
     425        }
    425426
    426427        // Don't let single admins leave the group.
    427428        if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
    function groups_leave_group( $group_id, $user_id = 0 ) { 
    431432                }
    432433        }
    433434
    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 ) ) {
     435        if ( ! groups_remove_member( $user_id, $group_id ) ) {
    436436                return false;
    437437        }
    438438
    function groups_invite_user( $args = '' ) { 
    10831083 */
    10841084function groups_uninvite_user( $user_id, $group_id ) {
    10851085
    1086         if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
     1086        if ( !BP_Groups_Member::delete_invite( $user_id, $group_id ) )
    10871087                return false;
    10881088
    10891089        /**
    function groups_accept_invite( $user_id, $group_id ) { 
    11601160 * @return bool True on success, false on failure.
    11611161 */
    11621162function groups_reject_invite( $user_id, $group_id ) {
    1163         if ( ! BP_Groups_Member::delete( $user_id, $group_id ) )
     1163        if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) )
    11641164                return false;
    11651165
    11661166        /**
    function groups_unban_member( $user_id, $group_id ) { 
    14001400 * @return bool True on success, false on failure.
    14011401 */
    14021402function groups_remove_member( $user_id, $group_id ) {
    1403 
    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
    function groups_delete_membership_request( $membership_id, $user_id = 0, $group_ 
    15701577        else
    15711578                $membership = new BP_Groups_Member( false, false, $membership_id );
    15721579
    1573         if ( !BP_Groups_Member::delete( $membership->user_id, $membership->group_id ) )
     1580        if ( !BP_Groups_Member::delete_request( $membership->user_id, $membership->group_id ) )
    15741581                return false;
    15751582
    15761583        return $membership;
  • 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 5b1d319..1b21841 100644
    class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 
    253253        }
    254254
    255255        /**
     256         * @group groups_reject_membership_request
     257         */
     258        public function test_bp_groups_reject_membership_request_leave_memberships_intact() {
     259                $u1 = $this->factory->user->create();
     260                $g = $this->factory->group->create( array(
     261                        'status' => 'private',
     262                ) );
     263
     264                $now = time();
     265                $this->add_user_to_group( $u1, $g, array(
     266                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     267                ) );
     268
     269                // Confirmed memberships should be left intact.
     270                $u1_is_member_before = groups_is_user_member( $u1, $g );
     271                groups_reject_membership_request( null, $u1, $g );
     272                $u1_is_member_after = groups_is_user_member( $u1, $g );
     273                // If nothing changed, the before and after values will be the same.
     274                $this->assertEquals( $u1_is_member_before, $u1_is_member_after );
     275        }
     276
     277        /**
     278         * @group groups_reject_membership_request
     279         */
     280        public function test_bp_groups_reject_membership_request_leave_invites_intact() {
     281                $u1 = $this->factory->user->create();
     282                $u2 = $this->factory->user->create();
     283                $g = $this->factory->group->create( array(
     284                        'status' => 'private',
     285                ) );
     286
     287                $now = time();
     288                $this->add_user_to_group( $u1, $g, array(
     289                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     290                ) );
     291
     292                // Outstanding invitations should be left intact.
     293                self::invite_user_to_group( $u2, $g, $u1 );
     294                $u2_has_invite_before = groups_check_user_has_invite( $u2, $g );
     295                groups_reject_membership_request( null, $u2, $g );
     296                $u2_has_invite_after = groups_check_user_has_invite( $u2, $g );
     297                // If nothing changed, the before and after values will be the same.
     298                $this->assertEquals( $u2_has_invite_before, $u2_has_invite_after );
     299        }
     300
     301        /**
    256302         * @group groups_delete_membership_request
    257303         */
    258304        public function test_bp_groups_delete_membership_request_remove_request() {
    class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 
    269315        }
    270316
    271317        /**
     318         * @group groups_delete_membership_request
     319         */
     320        public function test_bp_groups_delete_membership_request_leave_memberships_intact() {
     321                $u1 = $this->factory->user->create();
     322                $g = $this->factory->group->create( array(
     323                        'status' => 'private',
     324                ) );
     325
     326                $now = time();
     327                $this->add_user_to_group( $u1, $g, array(
     328                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     329                ) );
     330
     331                // Confirmed memberships should be left intact.
     332                $u1_is_member_before = groups_is_user_member( $u1, $g );
     333                groups_delete_membership_request( null, $u1, $g );
     334                $u1_is_member_after = groups_is_user_member( $u1, $g );
     335                // If nothing changed, the before and after values will be the same.
     336                $this->assertEquals( $u1_is_member_before, $u1_is_member_after );
     337        }
     338
     339        /**
     340         * @group groups_delete_membership_request
     341         */
     342        public function test_bp_groups_delete_membership_request_leave_invites_intact() {
     343                $u1 = $this->factory->user->create();
     344                $u2 = $this->factory->user->create();
     345                $g = $this->factory->group->create( array(
     346                        'status' => 'private',
     347                ) );
     348
     349                $now = time();
     350                $this->add_user_to_group( $u1, $g, array(
     351                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     352                ) );
     353
     354                // Outstanding invitations should be left intact.
     355                self::invite_user_to_group( $u2, $g, $u1 );
     356                $u2_has_invite_before = groups_check_user_has_invite( $u2, $g );
     357                groups_delete_membership_request( null, $u2, $g );
     358                $u2_has_invite_after = groups_check_user_has_invite( $u2, $g );
     359                // If nothing changed, the before and after values will be the same.
     360                $this->assertEquals( $u2_has_invite_before, $u2_has_invite_after );
     361        }
     362
     363        /**
    272364         * @group groups_reject_invite
    273365         */
    274366        public function test_bp_groups_reject_invite_remove_invite() {
    class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 
    291383        }
    292384
    293385        /**
     386         * @group groups_reject_invite
     387         */
     388        public function test_bp_groups_reject_invite_leave_memberships_intact() {
     389                $u1 = $this->factory->user->create();
     390                $g = $this->factory->group->create( array(
     391                        'status' => 'private',
     392                ) );
     393
     394                $now = time();
     395                $this->add_user_to_group( $u1, $g, array(
     396                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     397                ) );
     398
     399                // Confirmed memberships should be left intact.
     400                $u1_is_member_before = groups_is_user_member( $u1, $g );
     401                groups_reject_invite( $u1, $g );
     402                $u1_is_member_after = groups_is_user_member( $u1, $g );
     403                // If nothing changed, the before and after values will be the same.
     404                $this->assertEquals( $u1_is_member_before, $u1_is_member_after );
     405        }
     406
     407        /**
     408         * @group groups_reject_invite
     409         */
     410        public function test_bp_groups_reject_invite_leave_requests_intact() {
     411                $u1 = $this->factory->user->create();
     412                $g = $this->factory->group->create( array(
     413                        'status' => 'private',
     414                ) );
     415
     416                // Membership requests should be left intact.
     417                self::create_group_membership_request( $u1, $g );
     418                $u1_has_request_before = groups_check_for_membership_request( $u1, $g );
     419                groups_reject_invite( $u1, $g );
     420                $u1_has_request_after = groups_check_for_membership_request( $u1, $g );
     421                // If nothing changed, the before and after values will be the same.
     422                $this->assertEquals( $u1_has_request_before, $u1_has_request_after );
     423        }
     424
     425        /**
    294426         * @group groups_delete_invite
    295427         */
    296428        public function test_bp_groups_delete_invite_remove_invite() {
    class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 
    335467        }
    336468
    337469        /**
     470         * @group groups_delete_invite
     471         */
     472        public function test_bp_groups_delete_invite_leave_requests_intact() {
     473                $u1 = $this->factory->user->create();
     474                $g = $this->factory->group->create( array(
     475                        'status' => 'private',
     476                ) );
     477
     478                // Membership requests should be left intact.
     479                self::create_group_membership_request( $u1, $g );
     480                $u1_has_request_before = groups_check_for_membership_request( $u1, $g );
     481                groups_delete_invite( $u1, $g );
     482                $u1_has_request_after = groups_check_for_membership_request( $u1, $g );
     483                // If nothing changed, the before and after values will be the same.
     484                $this->assertEquals( $u1_has_request_before, $u1_has_request_after );
     485        }
     486
     487        /**
    338488         * @group groups_uninvite_user
    339489         */
    340490        public function test_bp_groups_uninvite_user_remove_invite() {
    class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 
    355505                $u2_has_invite = groups_check_user_has_invite( $u2, $g );
    356506                $this->assertEquals( $u2_has_invite, 0 );
    357507        }
     508
     509        /**
     510         * @group groups_uninvite_user
     511         */
     512        public function test_bp_groups_uninvite_user_leave_memberships_intact() {
     513                $u1 = $this->factory->user->create();
     514                $g = $this->factory->group->create( array(
     515                        'status' => 'private',
     516                ) );
     517
     518                $now = time();
     519                $this->add_user_to_group( $u1, $g, array(
     520                        'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
     521                ) );
     522
     523                // Confirmed memberships should be left intact.
     524                $u1_is_member_before = groups_is_user_member( $u1, $g );
     525                groups_uninvite_user( $u1, $g );
     526                $u1_is_member_after = groups_is_user_member( $u1, $g );
     527                // If nothing changed, the before and after values will be the same.
     528                $this->assertEquals( $u1_is_member_before, $u1_is_member_after );
     529        }
     530
     531        /**
     532         * @group groups_uninvite_user
     533         */
     534        public function test_bp_groups_uninvite_user_leave_requests_intact() {
     535                $u1 = $this->factory->user->create();
     536                $g = $this->factory->group->create( array(
     537                        'status' => 'private',
     538                ) );
     539
     540                // Membership requests should be left intact.
     541                self::create_group_membership_request( $u1, $g );
     542                $u1_has_request_before = groups_check_for_membership_request( $u1, $g );
     543                groups_uninvite_user( $u1, $g );
     544                $u1_has_request_after = groups_check_for_membership_request( $u1, $g );
     545                // If nothing changed, the before and after values will be the same.
     546                $this->assertEquals( $u1_has_request_before, $u1_has_request_after );
     547        }
     548
     549        /**
     550         * @group groups_join_group
     551         */
     552        public function test_groups_join_group_basic_join() {
     553                $u1 = $this->factory->user->create();
     554                $g = $this->factory->group->create();
     555
     556                groups_join_group( $g, $u1 );
     557                $membership_id = groups_is_user_member( $u1, $g );
     558                $this->assertTrue( is_numeric( $membership_id ) && $membership_id > 0 );
     559        }
     560
     561        /**
     562         * @group groups_join_group
     563         */
     564        public function test_groups_join_group_basic_join_use_current_user() {
     565                $u1 = $this->factory->user->create();
     566                $g = $this->factory->group->create();
     567                $old_current_user = get_current_user_id();
     568                $this->set_current_user( $u1 );
     569
     570                groups_join_group( $g );
     571                $membership_id = groups_is_user_member( $u1, $g );
     572                $this->assertTrue( is_numeric( $membership_id ) && $membership_id > 0 );
     573                $this->set_current_user( $old_current_user );
     574        }
     575
     576        /**
     577         * @group groups_join_group
     578         */
     579        public function test_groups_join_group_already_member() {
     580                $u1 = $this->factory->user->create();
     581                $g = $this->factory->group->create();
     582                $this->add_user_to_group( $u1, $g );
     583
     584                $result = groups_join_group( $g, $u1 );
     585                $this->assertTrue( $result );
     586        }
     587
     588        /**
     589         * @group groups_join_group
     590         */
     591        public function test_groups_join_group_cleanup_invites() {
     592                $u1 = $this->factory->user->create();
     593                $u2 = $this->factory->user->create();
     594                $g = $this->factory->group->create();
     595                $this->add_user_to_group( $u1, $g );
     596
     597                $m1 = new BP_Groups_Member( $u1, $g );
     598                $m1->promote( 'admin' );
     599
     600                self::invite_user_to_group( $u2, $g, $u1 );
     601
     602                $result = groups_join_group( $g, $u2 );
     603                // Upon joining the group, outstanding invitations should be cleaned up.
     604                $this->assertEquals( null, groups_check_user_has_invite( $u2, $g, 'any' ) );
     605        }
     606
     607        /**
     608         * @group groups_join_group
     609         */
     610        public function test_groups_join_group_cleanup_requests() {
     611                $u1 = $this->factory->user->create();
     612                $g = $this->factory->group->create();
     613                self::create_group_membership_request( $u1, $g );
     614
     615                $result = groups_join_group( $g, $u1 );
     616                // Upon joining the group, outstanding requests should be cleaned up.
     617                $this->assertEquals( null, groups_check_for_membership_request( $u1, $g ) );
     618        }
     619
     620        /**
     621         * @group groups_leave_group
     622         */
     623        public function test_groups_leave_group_basic_leave_self_initiated() {
     624                $old_current_user = get_current_user_id();
     625                $u1 = $this->factory->user->create();
     626                // Specify the creator_id or the current user will be the creator,
     627                // and your counts will be off by one.
     628                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     629                $u2 = $this->factory->user->create();
     630                $this->add_user_to_group( $u2, $g );
     631
     632                $before = groups_get_total_member_count( $g );
     633                $this->set_current_user( $u2 );
     634                groups_leave_group( $g, $u2 );
     635                $after = groups_get_total_member_count( $g );
     636
     637                $this->assertEquals( $before - 1, $after );
     638                $this->set_current_user( $old_current_user );
     639        }
     640
     641        /**
     642         * @group groups_leave_group
     643         */
     644        public function test_groups_leave_group_basic_leave_use_current_user() {
     645                $old_current_user = get_current_user_id();
     646                $u1 = $this->factory->user->create();
     647                // Specify the creator_id or the current user will be the creator,
     648                // and your counts will be off by one.
     649                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     650                $u2 = $this->factory->user->create();
     651                $this->add_user_to_group( $u2, $g );
     652
     653                $before = groups_get_total_member_count( $g );
     654                $this->set_current_user( $u2 );
     655                groups_leave_group( $g );
     656                $after = groups_get_total_member_count( $g );
     657
     658                $this->assertEquals( $before - 1, $after );
     659                $this->set_current_user( $old_current_user );
     660        }
     661
     662        /**
     663         * @group groups_leave_group
     664         */
     665        public function test_groups_leave_group_basic_leave_group_admin_initiated() {
     666                $old_current_user = get_current_user_id();
     667                $u1 = $this->factory->user->create();
     668                // Specify the creator_id or the current user will be the creator,
     669                // and your counts will be off by one.
     670                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     671                $u2 = $this->factory->user->create();
     672                $this->add_user_to_group( $u2, $g );
     673
     674                $before = groups_get_total_member_count( $g );
     675                $this->set_current_user( $u1 );
     676                groups_leave_group( $g, $u2 );
     677                $after = groups_get_total_member_count( $g );
     678
     679                $this->assertEquals( $before - 1, $after );
     680                $this->set_current_user( $old_current_user );
     681        }
     682
     683        /**
     684         * @group groups_leave_group
     685         */
     686        public function test_groups_leave_group_basic_leave_site_admin_initiated() {
     687                $old_current_user = get_current_user_id();
     688                $u1 = $this->factory->user->create();
     689                $u1_siteadmin = new WP_User( $u1 );
     690                $u1_siteadmin->add_role( 'administrator' );
     691                // Specify the creator_id or the current user will be the creator,
     692                // and your counts will be off by one.
     693                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     694                $u2 = $this->factory->user->create();
     695                $this->add_user_to_group( $u2, $g );
     696
     697                $before = groups_get_total_member_count( $g );
     698                $this->set_current_user( $u1 );
     699                groups_leave_group( $g, $u2 );
     700                $after = groups_get_total_member_count( $g );
     701
     702                $this->assertEquals( $before - 1, $after );
     703                $this->set_current_user( $old_current_user );
     704        }
     705
     706
     707        /**
     708         * @group groups_leave_group
     709         */
     710        public function test_groups_leave_group_single_admin_prevent_leave() {
     711                $old_current_user = get_current_user_id();
     712                $u1 = $this->factory->user->create();
     713                // Specify the creator_id or the current user will be the creator,
     714                // and your counts will be off by one.
     715                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     716                $u2 = $this->factory->user->create();
     717                $this->add_user_to_group( $u2, $g );
     718
     719                $before = groups_get_total_member_count( $g );
     720                $this->set_current_user( $u1 );
     721                groups_leave_group( $g, $u1 );
     722                $after = groups_get_total_member_count( $g );
     723
     724                $this->assertEquals( $before, $after );
     725                $this->set_current_user( $old_current_user );
     726        }
     727
     728        /**
     729         * @group groups_leave_group
     730         */
     731        public function test_groups_leave_group_multiple_admins_allow_leave() {
     732                $old_current_user = get_current_user_id();
     733                $u1 = $this->factory->user->create();
     734                // Specify the creator_id or the current user will be the creator,
     735                // and your counts will be off by one.
     736                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     737                $u2 = $this->factory->user->create();
     738                $this->add_user_to_group( $u2, $g );
     739                $m2 = new BP_Groups_Member( $u2, $g );
     740                $m2->promote( 'admin' );
     741
     742                $before = groups_get_total_member_count( $g );
     743                $this->set_current_user( $u1 );
     744                groups_leave_group( $g, $u1 );
     745                $after = groups_get_total_member_count( $g );
     746
     747                $this->assertEquals( $before - 1, $after );
     748                $this->set_current_user( $old_current_user );
     749        }
    358750}
    359751
  • tests/phpunit/testcases/groups/functions.php

    diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php
    index 6e0add3..6bfad35 100644
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    4949                groups_join_group( $g1, $u2 );
    5050                groups_join_group( $g2, $u2 );
    5151
     52                // Set the current user so the leave group request goes through.
     53                $this->set_current_user( $u2 );
    5254                groups_leave_group( $g1, $u2 );
    5355                $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
    5456        }