Ticket #5705: 5705.01.patch
| File 5705.01.patch, 5.0 KB (added by , 12 years ago) |
|---|
-
src/bp-groups/bp-groups-template.php
1645 1645 /** 1646 1646 * Checks if a user is banned from a group. 1647 1647 * 1648 * If this function is invoked inside the groups template loop (e.g. the group directory), then 1649 * check $groups_template->group->is_banned instead of making another SQL query. 1650 * However, if used in a single group's pages, we must use groups_is_user_banned(). 1648 * If this function is invoked inside the groups template loop (e.g. the group 1649 * directory), then check $groups_template->group->is_banned instead of making 1650 * another SQL query. However, if used in a single group's pages, we must use 1651 * groups_is_user_banned(). 1652 * 1653 * @since BuddyPress (1.5.0) 1651 1654 * 1652 1655 * @global BP_Groups_Template $groups_template Group template loop object 1653 * @param object $group Group to check if user is banned from the group 1654 * @param int $user_id 1655 * @return bool If user is banned from the group or not 1656 * @since BuddyPress (1.5) 1656 * @param BP_Groups_Group $group Group to check if user is banned 1657 * @param int $user_id The user ID to check 1658 * @return mixed Boolean true or string '1' if user is banned. Boolean false 1659 * or string '0' if user isn't banned. null if user isn't a member of 1660 * the group when groups_is_user_banned() is called. 1657 1661 */ 1658 1662 function bp_group_is_user_banned( $group = false, $user_id = 0 ) { 1659 1663 global $groups_template; 1660 1664 1661 1665 // Site admins always have access 1662 if ( bp_current_user_can( 'bp_moderate' ) ) 1666 if ( bp_current_user_can( 'bp_moderate' ) ) { 1663 1667 return false; 1668 } 1664 1669 1665 if ( empty( $group ) ) { 1666 $group =& $groups_template->group; 1670 // check groups loop first 1671 // @see BP_Groups_Group::get_group_extras() 1672 if ( ! empty( $groups_template->in_the_loop ) && isset( $groups_template->group->is_banned ) ) { 1673 $retval = $groups_template->group->is_banned; 1667 1674 1668 if ( !$user_id && isset( $group->is_banned ) ) 1669 return apply_filters( 'bp_group_is_user_banned', $group->is_banned ); 1670 } 1675 // not in loop 1676 } else { 1677 // Default to not banned 1678 $retval = 0; 1671 1679 1672 if ( !$user_id ) 1673 $user_id = bp_loggedin_user_id(); 1680 if ( empty( $group ) ) { 1681 $group = $groups_template->group; 1682 } 1683 1684 if ( empty( $user_id ) ) { 1685 $user_id = bp_loggedin_user_id(); 1686 } 1687 1688 if ( ! empty( $user_id ) && ! empty( $group->id ) ) { 1689 $retval = groups_is_user_banned( $user_id, $group->id ); 1690 } 1691 } 1674 1692 1675 return apply_filters( 'bp_group_is_user_banned', groups_is_user_banned( $user_id, $group->id ));1693 return apply_filters( 'bp_group_is_user_banned', $retval ); 1676 1694 } 1677 1695 1678 1696 function bp_group_accept_invite_link() { -
tests/phpunit/testcases/groups/template.php
689 689 $this->assertEquals( $v, $requests_template->requests[0]->{$k} ); 690 690 } 691 691 } 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 } 692 761 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)