Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/15/2014 08:00:53 PM (10 years ago)
Author:
r-a-y
Message:

Check groups loop data when using bp_group_is_user_banned().

Previously, `bp_group_is_user_banned() did not reference the data already
queried in the groups loop via BP_Groups_Group::get_group_extras(). As a
result, if a user without the 'bp_moderate' cap was logged in and viewing
the group directory, this added twenty additional DB queries to the page.

Commit also standardizes the return value of bp_group_is_user_banned() to
a boolean. Previously, it was possible for the function to return either a
string of the integer, null or a boolean.

Fixes #5705.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/groups/template.php

    r8419 r8531  
    690690        }
    691691    }
     692
     693    /**
     694     * @group bp_group_is_user_banned
     695     */
     696    public function test_bp_group_is_user_banned_in_groups_loop() {
     697        $u1 = $this->create_user();
     698        $u2 = $this->create_user();
     699        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     700        $g2 = $this->factory->group->create( array( 'creator_id' => $u2 ) );
     701        groups_join_group( $g1, $u2 );
     702        groups_join_group( $g2, $u2 );
     703        groups_join_group( $g2, $u1 );
     704
     705        // Ban user 1 from group 2
     706        // Fool the admin check
     707        $old_user = get_current_user_id();
     708        $this->set_current_user( $u2 );
     709        buddypress()->is_item_admin = true;
     710        groups_ban_member( $u1, $g2 );
     711
     712        // Start the groups loop
     713        $this->set_current_user( $u1 );
     714        if ( bp_has_groups() ) : while ( bp_groups() ) : bp_the_group();
     715            $found[] = bp_group_is_user_banned();
     716        endwhile; endif;
     717
     718        // Assert
     719        $expected = array( 0, 1 );
     720        $this->assertEquals( $expected, $found );
     721
     722        // Clean up
     723        $GLOBALS['groups_template'] = null;
     724        $this->set_current_user( $old_user );
     725    }
     726
     727    /**
     728     * @group bp_group_is_user_banned
     729     */
     730    public function test_bp_group_is_user_banned_not_in_groups_loop() {
     731        $u1 = $this->create_user();
     732        $u2 = $this->create_user();
     733        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     734        $g2 = $this->factory->group->create( array( 'creator_id' => $u2 ) );
     735        groups_join_group( $g1, $u2 );
     736        groups_join_group( $g2, $u2 );
     737        groups_join_group( $g2, $u1 );
     738
     739        // Ban user 1 from group 2
     740        // Fool the admin check
     741        $old_user = get_current_user_id();
     742        $this->set_current_user( $u2 );
     743        buddypress()->is_item_admin = true;
     744        groups_ban_member( $u1, $g2 );
     745
     746        // Do group ban checks
     747        $group1 = new BP_Groups_Group( $g1 );
     748        $group2 = new BP_Groups_Group( $g2 );
     749
     750        $found = array();
     751        $found[] = bp_group_is_user_banned( $group1, $u1 );
     752        $found[] = bp_group_is_user_banned( $group2, $u1 );
     753
     754        // Assert
     755        $expected = array( 0, 1 );
     756        $this->assertEquals( $expected, $found );
     757
     758        // Clean up
     759        $this->set_current_user( $old_user );
     760    }
    692761}
Note: See TracChangeset for help on using the changeset viewer.