Ticket #6327: 6327.2.patch
File 6327.2.patch, 35.5 KB (added by , 10 years ago) |
---|
-
src/bp-groups/bp-groups-cache.php
diff --git src/bp-groups/bp-groups-cache.php src/bp-groups/bp-groups-cache.php index 10ba004..44aeaf6 100644
function groups_clear_group_administrator_cache_on_member_save( BP_Groups_Member 202 202 } 203 203 add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' ); 204 204 205 /** 206 * Clear caches on membership save. 207 * 208 * @since BuddyPress (2.3.0) 209 */ 210 function bp_groups_clear_user_group_cache_on_membership_save( BP_Groups_Member $member ) { 211 wp_cache_delete( $member->user_id, 'bp_groups_memberships_for_user' ); 212 wp_cache_delete( $member->id, 'bp_groups_memberships' ); 213 } 214 add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' ); 215 add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' ); 216 217 /** 218 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook. 219 * 220 * @since BuddyPress (2.3.0) 221 */ 222 function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) { 223 wp_cache_delete( $user_id, 'bp_groups_memberships_for_user' ); 224 225 $membership = new BP_Groups_Member( $user_id, $group_id ); 226 wp_cache_delete( $membership->id, 'bp_groups_memberships' ); 227 } 228 add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 ); 229 205 230 /* List actions to clear super cached pages on, if super cache is installed */ 206 231 add_action( 'groups_join_group', 'bp_core_clear_cache' ); 207 232 add_action( 'groups_leave_group', 'bp_core_clear_cache' ); -
src/bp-groups/bp-groups-functions.php
diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php index 2aed47e..5a060fa 100644
function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) { 758 758 } 759 759 760 760 /** 761 * Get a list of groups of which the specified user is a member. 762 * 763 * @since BuddyPress (2.3.0) 764 * 765 * @param int $user_id ID of the user. 766 * @param array $args { 767 * Array of optional args. 768 * @param bool|null $is_confirmed Whether to return only confirmed memberships. Pass `null` to disable this 769 * filter. Default: true. 770 * @param bool|null $is_banned Whether to return only banned memberships. Pass `null` to disable this filter. 771 * Default: false. 772 * @param bool|null $is_admin Whether to return only admin memberships. Pass `null` to disable this filter. 773 * Default: false. 774 * @param bool|null $is_mod Whether to return only mod memberships. Pass `null` to disable this filter. 775 * Default: false. 776 * @param bool|null $invite_sent Whether to return only memberships with 'invite_sent'. Pass `null` to disable 777 * this filter. Default: false. 778 * @param string $orderby Field to order by. Accepts 'id' (membership ID), 'group_id', 'date_modified'. 779 * Default: 'group_id'. 780 * @param string $order Sort order. Accepts 'ASC' or 'DESC'. Default: 'ASC'. 781 * } 782 * @return array Array of matching group memberships, keyed by group ID. 783 */ 784 function bp_get_user_groups( $user_id, $args = array() ) { 785 $r = bp_parse_args( $args, array( 786 'is_confirmed' => true, 787 'is_banned' => false, 788 'is_admin' => false, 789 'is_mod' => false, 790 'invite_sent' => null, 791 'orderby' => 'group_id', 792 'order' => 'ASC', 793 ), 'get_user_groups' ); 794 795 $user_id = intval( $user_id ); 796 797 $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' ); 798 if ( false === $membership_ids ) { 799 $membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id ); 800 wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' ); 801 } 802 803 // Prime the membership cache. 804 $uncached_membership_ids = bp_get_non_cached_ids( $membership_ids, 'bp_groups_memberships' ); 805 if ( ! empty( $uncached_membership_ids ) ) { 806 $uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids ); 807 808 foreach ( $uncached_memberships as $uncached_membership ) { 809 wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' ); 810 } 811 } 812 813 // Populate group membership array from cache. 814 $groups = array(); 815 foreach ( $membership_ids as $membership_id ) { 816 $membership = wp_cache_get( $membership_id, 'bp_groups_memberships' ); 817 818 // Sanity check. 819 if ( ! isset( $membership->group_id ) ) { 820 continue; 821 } 822 823 $group_id = (int) $membership->group_id; 824 825 $groups[ $group_id ] = $membership; 826 } 827 828 // Normalize group data. 829 foreach ( $groups as &$group ) { 830 // Integer values. 831 $group_id = intval( $group->group_id ); 832 foreach ( array( 'id', 'group_id', 'user_id', 'inviter_id' ) as $index ) { 833 $group->{$index} = intval( $group->{$index} ); 834 } 835 836 // Boolean values. 837 foreach ( array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' ) as $index ) { 838 $group->{$index} = (bool) $group->{$index}; 839 } 840 } 841 842 // Assemble filter array for use in `wp_list_filter()`. 843 $filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) ); 844 foreach ( $filters as $filter_name => $filter_value ) { 845 if ( is_null( $filter_value ) ) { 846 unset( $filters[ $filter_name ] ); 847 } 848 } 849 850 if ( ! empty( $filters ) ) { 851 $groups = wp_list_filter( $groups, $filters ); 852 } 853 854 // By default, results are ordered by membership id. 855 if ( 'group_id' === $r['orderby'] ) { 856 ksort( $groups ); 857 } elseif ( in_array( $r['orderby'], array( 'id', 'date_modified' ) ) ) { 858 $groups = bp_sort_by_key( $groups, $r['orderby'] ); 859 } 860 861 // By default, results are ordered ASC. 862 if ( 'DESC' === strtoupper( $r['order'] ) ) { 863 // `true` to preserve keys. 864 $groups = array_reverse( $groups, true ); 865 } 866 867 return $groups; 868 } 869 870 /** 761 871 * Get the count of groups of which the specified user is a member. 762 872 * 763 873 * @param int $user_id Optional. Default: ID of the displayed user. … … function groups_avatar_upload_dir( $group_id = 0 ) { 851 961 * @param int|null ID of the membership if the user is an admin, otherwise null. 852 962 */ 853 963 function groups_is_user_admin( $user_id, $group_id ) { 854 return BP_Groups_Member::check_is_admin( $user_id, $group_id ); 964 $is_admin = false; 965 966 $user_groups = bp_get_user_groups( $user_id, array( 967 'is_admin' => true, 968 ) ); 969 970 if ( isset( $user_groups[ $group_id ] ) ) { 971 $is_admin = $user_groups[ $group_id ]->id; 972 } 973 974 return $is_admin; 855 975 } 856 976 857 977 /** … … function groups_is_user_admin( $user_id, $group_id ) { 862 982 * @param int|null ID of the membership if the user is a mod, otherwise null. 863 983 */ 864 984 function groups_is_user_mod( $user_id, $group_id ) { 865 return BP_Groups_Member::check_is_mod( $user_id, $group_id ); 985 $is_mod = false; 986 987 $user_groups = bp_get_user_groups( $user_id, array( 988 'is_mod' => true, 989 ) ); 990 991 if ( isset( $user_groups[ $group_id ] ) ) { 992 $is_mod = $user_groups[ $group_id ]->id; 993 } 994 995 return $is_mod; 866 996 } 867 997 868 998 /** … … function groups_is_user_mod( $user_id, $group_id ) { 873 1003 * @param int|null ID of the membership if the user is a member, otherwise null. 874 1004 */ 875 1005 function groups_is_user_member( $user_id, $group_id ) { 876 return BP_Groups_Member::check_is_member( $user_id, $group_id ); 1006 $is_member = false; 1007 1008 $user_groups = bp_get_user_groups( $user_id, array( 1009 'is_admin' => null, 1010 'is_mod' => null, 1011 ) ); 1012 1013 if ( isset( $user_groups[ $group_id ] ) ) { 1014 $is_member = $user_groups[ $group_id ]->id; 1015 } 1016 1017 return $is_member; 877 1018 } 878 1019 879 1020 function groups_is_user_banned( $user_id, $group_id ) { 880 return BP_Groups_Member::check_is_banned( $user_id, $group_id ); 1021 $is_banned = false; 1022 1023 $user_groups = bp_get_user_groups( $user_id, array( 1024 'is_confirmed' => null, 1025 'is_banned' => true, 1026 ) ); 1027 1028 if ( isset( $user_groups[ $group_id ] ) ) { 1029 $is_banned = $user_groups[ $group_id ]->id; 1030 } 1031 1032 return $is_banned; 1033 } 1034 1035 /** 1036 * Check whether a user has an outstanding invitation to a group. 1037 * 1038 * @since BuddyPress (2.3.0) 1039 * 1040 * @param int $user_id ID of the user. 1041 * @param int $group_id ID of the group. 1042 * @return int|null ID of the membership if found. 1043 */ 1044 function groups_is_user_invited( $user_id, $group_id ) { 1045 $is_invited = false; 1046 1047 $user_groups = bp_get_user_groups( $user_id, array( 1048 'invite_sent' => true, 1049 'is_confirmed' => false, 1050 ) ); 1051 1052 if ( isset( $user_groups[ $group_id ] ) ) { 1053 $is_invited = $user_groups[ $group_id ]->id; 1054 } 1055 1056 return $is_invited; 1057 } 1058 1059 /** 1060 * Check whether a user has a pending membership request for a group. 1061 * 1062 * @since BuddyPress (2.3.0) 1063 * 1064 * @param int $user_id ID of the user. 1065 * @param int $group_id ID of the group. 1066 * @return int|null ID of the membership if found. 1067 */ 1068 function groups_is_user_pending( $user_id, $group_id ) { 1069 $is_pending = false; 1070 1071 $user_groups = bp_get_user_groups( $user_id, array( 1072 'invite_sent' => false, 1073 'is_confirmed' => false, 1074 ) ); 1075 1076 if ( isset( $user_groups[ $group_id ] ) ) { 1077 $is_pending = $user_groups[ $group_id ]->id; 1078 } 1079 1080 return $is_pending; 881 1081 } 882 1082 883 1083 /** -
src/bp-groups/bp-groups-loader.php
diff --git src/bp-groups/bp-groups-loader.php src/bp-groups/bp-groups-loader.php index 14d43c6..b0ce70e 100644
class BP_Groups_Component extends BP_Component { 741 741 'bp_groups', 742 742 'bp_group_admins', 743 743 'bp_group_invite_count', 744 'bp_groups_memberships', 745 'bp_groups_memberships_for_user', 744 746 'group_meta' 745 747 ) ); 746 748 -
src/bp-groups/classes/class-bp-groups-group.php
diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php index 2ccb2a6..47b5f8a 100644
class BP_Groups_Group { 1362 1362 * @return array $paged_groups 1363 1363 */ 1364 1364 public static function get_group_extras( &$paged_groups, &$group_ids, $type = false ) { 1365 global $wpdb; 1366 1367 if ( empty( $group_ids ) ) 1368 return $paged_groups; 1369 1370 $bp = buddypress(); 1371 1372 // Sanitize group IDs 1373 $group_ids = implode( ',', wp_parse_id_list( $group_ids ) ); 1374 1375 // Fetch the logged-in user's status within each group 1376 if ( is_user_logged_in() ) { 1377 $user_status_results = $wpdb->get_results( $wpdb->prepare( "SELECT group_id, is_confirmed, invite_sent FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id IN ( {$group_ids} ) AND is_banned = 0", bp_loggedin_user_id() ) ); 1378 } else { 1379 $user_status_results = array(); 1380 } 1381 1382 // Reindex 1383 $user_status = array(); 1384 foreach ( $user_status_results as $user_status_result ) { 1385 $user_status[ $user_status_result->group_id ] = $user_status_result; 1386 } 1387 1388 for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) { 1389 $is_member = $is_invited = $is_pending = '0'; 1390 $gid = $paged_groups[ $i ]->id; 1391 1392 if ( isset( $user_status[ $gid ] ) ) { 1393 1394 // is_confirmed means the user is a member 1395 if ( $user_status[ $gid ]->is_confirmed ) { 1396 $is_member = '1'; 1365 $user_id = bp_loggedin_user_id(); 1397 1366 1398 // invite_sent means the user has been invited 1399 } elseif ( $user_status[ $gid ]->invite_sent ) { 1400 $is_invited = '1'; 1401 1402 // User has sent request, but has not been confirmed 1403 } else { 1404 $is_pending = '1'; 1405 } 1406 } 1407 1408 $paged_groups[ $i ]->is_member = $is_member; 1409 $paged_groups[ $i ]->is_invited = $is_invited; 1410 $paged_groups[ $i ]->is_pending = $is_pending; 1411 } 1412 1413 if ( is_user_logged_in() ) { 1414 $user_banned = $wpdb->get_col( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_members} WHERE is_banned = 1 AND user_id = %d AND group_id IN ( {$group_ids} )", bp_loggedin_user_id() ) ); 1415 } else { 1416 $user_banned = array(); 1417 } 1418 1419 for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) { 1420 $paged_groups[$i]->is_banned = false; 1421 1422 foreach ( (array) $user_banned as $group_id ) { 1423 if ( $group_id == $paged_groups[$i]->id ) { 1424 $paged_groups[$i]->is_banned = true; 1425 } 1426 } 1367 foreach ( $paged_groups as &$group ) { 1368 $group->is_member = groups_is_user_member( $user_id, $group->id ) ? '1' : '0'; 1369 $group->is_invited = groups_is_user_invited( $user_id, $group->id ) ? '1' : '0'; 1370 $group->is_pending = groups_is_user_pending( $user_id, $group->id ) ? '1' : '0'; 1371 $group->is_banned = (bool) groups_is_user_banned( $user_id, $group->id ); 1427 1372 } 1428 1373 1429 1374 return $paged_groups; -
src/bp-groups/classes/class-bp-groups-member.php
diff --git src/bp-groups/classes/class-bp-groups-member.php src/bp-groups/classes/class-bp-groups-member.php index 06a0705..e3a9aef 100644
class BP_Groups_Member { 939 939 } 940 940 941 941 /** 942 * Get group membership objects by ID (or an array of IDs). 943 * 944 * @since BuddyPress (2.3.0) 945 * 946 * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs. 947 * @return array 948 */ 949 public static function get_memberships_by_id( $membership_ids ) { 950 global $wpdb; 951 952 $bp = buddypress(); 953 954 $membership_ids = implode( ',', wp_parse_id_list( $membership_ids ) ); 955 return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" ); 956 } 957 958 /** 942 959 * Get the IDs users with outstanding membership requests to the group. 943 960 * 944 961 * @param int $group_id ID of the group. … … class BP_Groups_Member { 1037 1054 } 1038 1055 1039 1056 /** 1057 * Get all membership IDs for a user. 1058 * 1059 * @since BuddyPress (2.3.0) 1060 * 1061 * @param int $user_id ID of the user. 1062 * @return array 1063 */ 1064 public static function get_membership_ids_for_user( $user_id ) { 1065 global $wpdb; 1066 1067 $bp = buddypress(); 1068 1069 $group_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d ORDER BY id ASC", $user_id ) ); 1070 1071 return $group_ids; 1072 } 1073 1074 /** 1040 1075 * Delete all memberships for a given group. 1041 1076 * 1042 1077 * @param int $group_id ID of the group. -
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 7289c59..6e65525 100644
class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 215 215 216 216 $this->set_current_user( $old_current_user ); 217 217 } 218 219 /** 220 * @group get_memberships_by_id 221 */ 222 public function test_get_memberships_by_id_with_single_id() { 223 $users = $this->factory->user->create_many( 2 ); 224 $groups = $this->factory->group->create_many( 2 ); 225 226 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 227 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 228 229 $found = BP_Groups_Member::get_memberships_by_id( $m0 ); 230 231 $this->assertSame( 1, count( $found ) ); 232 $this->assertEquals( $m0, $found[0]->id ); 233 } 234 235 /** 236 * @group get_memberships_by_id 237 */ 238 public function test_get_memberships_by_id_with_multiple_ids() { 239 $users = $this->factory->user->create_many( 2 ); 240 $groups = $this->factory->group->create_many( 2 ); 241 242 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 243 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 244 245 $found = BP_Groups_Member::get_memberships_by_id( array( $m0, $m1 ) ); 246 247 $this->assertSame( 2, count( $found ) ); 248 $this->assertEqualSets( array( $m0, $m1 ), wp_list_pluck( $found, 'id' ) ); 249 } 218 250 } 219 251 -
new file tests/phpunit/testcases/groups/functions/bpGetUserGroups.php
diff --git tests/phpunit/testcases/groups/functions/bpGetUserGroups.php tests/phpunit/testcases/groups/functions/bpGetUserGroups.php new file mode 100644 index 0000000..12197a3
- + 1 <?php 2 3 /** 4 * @group groups 5 */ 6 class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase { 7 static $user; 8 static $admin_user; 9 static $groups; 10 11 public function setUp() { 12 parent::setUp(); 13 groups_remove_member( self::$user, self::$groups[2] ); 14 } 15 16 public static function setUpBeforeClass() { 17 $f = new BP_UnitTest_Factory(); 18 19 self::$user = $f->user->create( array( 20 'user_login' => 'bp_get_user_groups_user', 21 'user_email' => 'bp_get_user_groups_user@example.com', 22 ) ); 23 self::$admin_user = $f->user->create( array( 24 'user_login' => 'bp_get_user_groups_admin_user', 25 'user_email' => 'bp_get_user_groups_admin_user@example.com', 26 ) ); 27 self::$groups = $f->group->create_many( 4, array( 28 'creator_id' => self::$admin_user, 29 ) ); 30 31 $now = time(); 32 self::add_user_to_group( self::$user, self::$groups[1], array( 33 'date_modified' => date( 'Y-m-d H:i:s', $now - 10 ), 34 ) ); 35 self::add_user_to_group( self::$user, self::$groups[0], array( 36 'date_modified' => date( 'Y-m-d H:i:s', $now ), 37 ) ); 38 39 self::commit_transaction(); 40 } 41 42 public static function tearDownAfterClass() { 43 foreach ( self::$groups as $group ) { 44 groups_delete_group( $group ); 45 } 46 47 if ( is_multisite() ) { 48 wpmu_delete_user( self::$user ); 49 wpmu_delete_user( self::$admin_user ); 50 } else { 51 wp_delete_user( self::$user ); 52 wp_delete_user( self::$admin_user ); 53 } 54 55 self::commit_transaction(); 56 } 57 58 public function test_default_params() { 59 $found = bp_get_user_groups( self::$user ); 60 61 $this->assertEqualSets( array( self::$groups[0], self::$groups[1] ), wp_list_pluck( $found, 'group_id' ) ); 62 63 foreach ( $found as $index => $f ) { 64 $this->assertInternalType( 'int', $index ); 65 $this->assertInternalType( 'object', $f ); 66 $this->assertInternalType( 'int', $f->group_id ); 67 $this->assertSame( $index, $f->group_id ); 68 } 69 } 70 71 public function test_is_confirmed_true() { 72 $this->add_user_to_group( self::$user, self::$groups[2], array( 73 'is_confirmed' => false, 74 ) ); 75 76 $expected = array( self::$groups[0], self::$groups[1] ); 77 $found = bp_get_user_groups( self::$user, array( 78 'is_confirmed' => true, 79 ) ); 80 81 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 82 } 83 84 public function test_is_confirmed_false() { 85 $this->add_user_to_group( self::$user, self::$groups[2], array( 86 'is_confirmed' => false, 87 ) ); 88 89 $expected = array( self::$groups[2] ); 90 $found = bp_get_user_groups( self::$user, array( 91 'is_confirmed' => false, 92 ) ); 93 94 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 95 } 96 97 public function test_is_confirmed_null() { 98 $this->add_user_to_group( self::$user, self::$groups[2], array( 99 'is_confirmed' => false, 100 ) ); 101 102 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 103 $found = bp_get_user_groups( self::$user, array( 104 'is_confirmed' => null, 105 ) ); 106 107 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 108 } 109 110 public function test_is_banned_true() { 111 $this->add_user_to_group( self::$user, self::$groups[2] ); 112 $member = new BP_Groups_Member( self::$user, self::$groups[2] ); 113 $member->ban(); 114 115 $expected = array( self::$groups[2] ); 116 $found = bp_get_user_groups( self::$user, array( 117 'is_banned' => true, 118 ) ); 119 120 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 121 } 122 123 public function test_is_banned_false() { 124 $this->add_user_to_group( self::$user, self::$groups[2] ); 125 $member = new BP_Groups_Member( self::$user, self::$groups[2] ); 126 $member->ban(); 127 128 $expected = array( self::$groups[0], self::$groups[1] ); 129 $found = bp_get_user_groups( self::$user, array( 130 'is_banned' => false, 131 ) ); 132 133 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 134 } 135 136 public function test_is_banned_null() { 137 $this->add_user_to_group( self::$user, self::$groups[2] ); 138 $member = new BP_Groups_Member( self::$user, self::$groups[2] ); 139 $member->ban(); 140 141 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 142 $found = bp_get_user_groups( self::$user, array( 143 'is_banned' => null, 144 ) ); 145 146 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 147 } 148 149 public function test_is_admin_true() { 150 $this->add_user_to_group( self::$user, self::$groups[2], array( 151 'is_admin' => true, 152 ) ); 153 154 $expected = array( self::$groups[2] ); 155 $found = bp_get_user_groups( self::$user, array( 156 'is_admin' => true, 157 ) ); 158 159 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 160 } 161 162 public function test_is_admin_false() { 163 $this->add_user_to_group( self::$user, self::$groups[2], array( 164 'is_admin' => true, 165 ) ); 166 167 $expected = array( self::$groups[0], self::$groups[1] ); 168 $found = bp_get_user_groups( self::$user, array( 169 'is_admin' => false, 170 ) ); 171 172 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 173 } 174 175 public function test_is_admin_null() { 176 $this->add_user_to_group( self::$user, self::$groups[2], array( 177 'is_admin' => false, 178 ) ); 179 180 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 181 $found = bp_get_user_groups( self::$user, array( 182 'is_admin' => null, 183 ) ); 184 185 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 186 } 187 188 public function test_is_mod_true() { 189 $this->add_user_to_group( self::$user, self::$groups[2], array( 190 'is_mod' => true, 191 ) ); 192 193 $expected = array( self::$groups[2] ); 194 $found = bp_get_user_groups( self::$user, array( 195 'is_mod' => true, 196 ) ); 197 198 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 199 } 200 201 public function test_is_mod_false() { 202 $this->add_user_to_group( self::$user, self::$groups[2], array( 203 'is_mod' => true, 204 ) ); 205 206 $expected = array( self::$groups[0], self::$groups[1] ); 207 $found = bp_get_user_groups( self::$user, array( 208 'is_mod' => false, 209 ) ); 210 211 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 212 } 213 214 public function test_is_mod_null() { 215 $this->add_user_to_group( self::$user, self::$groups[2], array( 216 'is_mod' => false, 217 ) ); 218 219 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 220 $found = bp_get_user_groups( self::$user, array( 221 'is_mod' => null, 222 ) ); 223 224 $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) ); 225 } 226 227 public function test_orderby_should_default_to_group_id() { 228 $expected = bp_get_user_groups( self::$user ); 229 $found = bp_get_user_groups( self::$user, array( 230 'orderby' => 'group_id', 231 ) ); 232 233 $this->assertEquals( $expected, $found ); 234 } 235 236 public function test_orderby_group_id() { 237 $expected = array( self::$groups[0], self::$groups[1] ); 238 $found = bp_get_user_groups( self::$user, array( 239 'orderby' => 'group_id', 240 ) ); 241 242 $this->assertSame( $expected, array_keys( $found ) ); 243 } 244 245 public function test_orderby_id() { 246 $expected = array( self::$groups[1], self::$groups[0] ); 247 $found = bp_get_user_groups( self::$user, array( 248 'orderby' => 'id', 249 ) ); 250 251 $this->assertSame( $expected, array_values( wp_list_pluck( $found, 'group_id' ) ) ); 252 } 253 254 public function test_orderby_date_modified() { 255 $this->add_user_to_group( self::$user, self::$groups[2], array( 256 'date_modified' => '2015-03-14 09:26:53', 257 ) ); 258 259 $expected = array( self::$groups[2], self::$groups[1], self::$groups[0] ); 260 $found = bp_get_user_groups( self::$user, array( 261 'orderby' => 'date_modified', 262 ) ); 263 264 $this->assertSame( $expected, array_values( wp_list_pluck( $found, 'group_id' ) ) ); 265 } 266 267 public function test_orderby_group_id_with_order_desc() { 268 $expected = array( self::$groups[1], self::$groups[0] ); 269 $found = bp_get_user_groups( self::$user, array( 270 'orderby' => 'group_id', 271 'order' => 'DESC', 272 ) ); 273 274 $this->assertSame( $expected, array_keys( $found ) ); 275 } 276 277 public function test_orderby_date_modified_with_order_desc() { 278 $this->add_user_to_group( self::$user, self::$groups[2], array( 279 'date_modified' => '2015-03-14 09:26:53', 280 ) ); 281 282 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 283 $found = bp_get_user_groups( self::$user, array( 284 'orderby' => 'date_modified', 285 'order' => 'DESC', 286 ) ); 287 288 $this->assertSame( $expected, array_values( wp_list_pluck( $found, 'group_id' ) ) ); 289 } 290 291 /** 292 * @group cache 293 */ 294 public function test_results_should_be_cached() { 295 global $wpdb; 296 297 $g1 = bp_get_user_groups( self::$user ); 298 299 $num_queries = $wpdb->num_queries; 300 $g2 = bp_get_user_groups( self::$user ); 301 302 $this->assertSame( $num_queries, $wpdb->num_queries ); 303 $this->assertEquals( $g1, $g2 ); 304 } 305 306 /** 307 * @group cache 308 */ 309 public function test_cache_should_be_invalidated_on_group_join() { 310 // Populate cache. 311 $g1 = bp_get_user_groups( self::$user ); 312 313 groups_join_group( self::$groups[2], self::$user ); 314 315 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 316 $found = bp_get_user_groups( self::$user ); 317 318 $this->assertEqualSets( $expected, array_keys( $found ) ); 319 } 320 321 /** 322 * @group cache 323 */ 324 public function test_cache_should_be_invalidated_on_group_leave() { 325 // Populate cache. 326 $g1 = bp_get_user_groups( self::$user ); 327 328 groups_leave_group( self::$groups[1], self::$user ); 329 330 $expected = array( self::$groups[0] ); 331 $found = bp_get_user_groups( self::$user ); 332 333 $this->assertEqualSets( $expected, array_keys( $found ) ); 334 } 335 336 /** 337 * @group cache 338 */ 339 public function test_cache_should_be_invalidated_on_group_remove() { 340 // Populate cache. 341 $g1 = bp_get_user_groups( self::$user ); 342 343 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 344 $m->remove(); 345 346 $expected = array( self::$groups[0] ); 347 $found = bp_get_user_groups( self::$user ); 348 349 $this->assertEqualSets( $expected, array_keys( $found ) ); 350 } 351 352 /** 353 * @group cache 354 */ 355 public function test_cache_should_be_invalidated_on_group_ban() { 356 // Populate cache. 357 $g1 = bp_get_user_groups( self::$user ); 358 359 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 360 $m->ban(); 361 362 $expected = array( self::$groups[0] ); 363 $found = bp_get_user_groups( self::$user ); 364 365 $this->assertEqualSets( $expected, array_keys( $found ) ); 366 } 367 368 /** 369 * @group cache 370 */ 371 public function test_cache_should_be_invalidated_on_group_unban() { 372 $m1 = new BP_Groups_Member( self::$user, self::$groups[1] ); 373 $m1->ban(); 374 375 // Populate cache. 376 $g1 = bp_get_user_groups( self::$user ); 377 378 $m2 = new BP_Groups_Member( self::$user, self::$groups[1] ); 379 $m2->unban(); 380 381 $expected = array( self::$groups[0], self::$groups[1] ); 382 $found = bp_get_user_groups( self::$user ); 383 384 $this->assertEqualSets( $expected, array_keys( $found ) ); 385 } 386 387 /** 388 * @group cache 389 */ 390 public function test_cache_should_be_invalidated_on_group_invite() { 391 // Populate cache. 392 $g1 = bp_get_user_groups( self::$user, array( 393 'is_confirmed' => null, 394 ) ); 395 396 groups_invite_user( array( 397 'user_id' => self::$user, 398 'group_id' => self::$groups[2], 399 'inviter_id' => self::$admin_user, 400 ) ); 401 402 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 403 $found = bp_get_user_groups( self::$user, array( 404 'is_confirmed' => null, 405 ) ); 406 407 $this->assertEqualSets( $expected, array_keys( $found ) ); 408 } 409 410 /** 411 * @group cache 412 */ 413 public function test_cache_should_be_invalidated_on_group_uninvite() { 414 groups_invite_user( array( 415 'user_id' => self::$user, 416 'group_id' => self::$groups[2], 417 'inviter_id' => self::$admin_user, 418 ) ); 419 420 // Populate cache. 421 $g1 = bp_get_user_groups( self::$user, array( 422 'is_confirmed' => null, 423 ) ); 424 425 groups_uninvite_user( self::$user, self::$groups[2] ); 426 427 $expected = array( self::$groups[0], self::$groups[1] ); 428 $found = bp_get_user_groups( self::$user, array( 429 'is_confirmed' => null, 430 ) ); 431 432 $this->assertEqualSets( $expected, array_keys( $found ) ); 433 } 434 435 /** 436 * @group cache 437 */ 438 public function test_cache_should_be_invalidated_on_group_invite_acceptance() { 439 groups_invite_user( array( 440 'user_id' => self::$user, 441 'group_id' => self::$groups[2], 442 'inviter_id' => self::$admin_user, 443 ) ); 444 445 // Populate cache. 446 $g1 = bp_get_user_groups( self::$user ); 447 448 groups_accept_invite( self::$user, self::$groups[2] ); 449 450 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 451 $found = bp_get_user_groups( self::$user ); 452 453 $this->assertEqualSets( $expected, array_keys( $found ) ); 454 } 455 456 /** 457 * @group cache 458 */ 459 public function test_cache_should_be_invalidated_on_group_invite_reject() { 460 groups_invite_user( array( 461 'user_id' => self::$user, 462 'group_id' => self::$groups[2], 463 'inviter_id' => self::$admin_user, 464 ) ); 465 466 // Populate cache. 467 $g1 = bp_get_user_groups( self::$user, array( 468 'is_confirmed' => null, 469 ) ); 470 471 groups_reject_invite( self::$user, self::$groups[2] ); 472 473 $expected = array( self::$groups[0], self::$groups[1] ); 474 $found = bp_get_user_groups( self::$user, array( 475 'is_confirmed' => null, 476 ) ); 477 478 $this->assertEqualSets( $expected, array_keys( $found ) ); 479 } 480 481 /** 482 * @group cache 483 */ 484 public function test_cache_should_be_invalidated_on_group_invite_delete() { 485 groups_invite_user( array( 486 'user_id' => self::$user, 487 'group_id' => self::$groups[2], 488 'inviter_id' => self::$admin_user, 489 ) ); 490 491 // Populate cache. 492 $g1 = bp_get_user_groups( self::$user, array( 493 'is_confirmed' => null, 494 ) ); 495 496 groups_delete_invite( self::$user, self::$groups[2] ); 497 498 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 499 $found = bp_get_user_groups( self::$user, array( 500 'is_confirmed' => null, 501 ) ); 502 503 $this->assertEqualSets( $expected, array_keys( $found ) ); 504 } 505 506 /** 507 * @group cache 508 */ 509 public function test_cache_should_be_invalidated_on_group_request() { 510 // Populate cache. 511 $g1 = bp_get_user_groups( self::$user, array( 512 'is_confirmed' => null, 513 ) ); 514 515 // For `wp_mail()`. 516 $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null; 517 $_SERVER['SERVER_NAME'] = ''; 518 519 groups_send_membership_request( self::$user, self::$groups[2] ); 520 521 // For `wp_mail()`. 522 if ( is_null( $server_name ) ) { 523 unset( $_SERVER['SERVER_NAME'] ); 524 } else { 525 $_SERVER['SERVER_NAME'] = $server_name; 526 } 527 528 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 529 $found = bp_get_user_groups( self::$user, array( 530 'is_confirmed' => null, 531 ) ); 532 533 $this->assertEqualSets( $expected, array_keys( $found ) ); 534 } 535 536 /** 537 * @group cache 538 */ 539 public function test_cache_should_be_invalidated_on_group_request_acceptance() { 540 // For `wp_mail()`. 541 $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null; 542 $_SERVER['SERVER_NAME'] = ''; 543 544 groups_send_membership_request( self::$user, self::$groups[2] ); 545 546 // Populate cache. 547 $g1 = bp_get_user_groups( self::$user ); 548 549 $m = new BP_Groups_Member( self::$user, self::$groups[2] ); 550 551 groups_accept_membership_request( $m->id, self::$user, self::$groups[2] ); 552 553 // For `wp_mail()`. 554 if ( is_null( $server_name ) ) { 555 unset( $_SERVER['SERVER_NAME'] ); 556 } else { 557 $_SERVER['SERVER_NAME'] = $server_name; 558 } 559 560 $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] ); 561 $found = bp_get_user_groups( self::$user ); 562 563 $this->assertEqualSets( $expected, array_keys( $found ) ); 564 } 565 } -
tests/phpunit/testcases/groups/functions/groupsIsUser.php
diff --git tests/phpunit/testcases/groups/functions/groupsIsUser.php tests/phpunit/testcases/groups/functions/groupsIsUser.php index 5a2abc3..b40df66 100644
class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 54 54 'is_admin' => true, 55 55 ) ); 56 56 57 $this->assert Equals( true,groups_is_user_admin( self::$user, self::$groups[1] ) );57 $this->assertNotEmpty( groups_is_user_admin( self::$user, self::$groups[1] ) ); 58 58 } 59 59 60 60 public function test_groups_is_user_admin_expected_false() { … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 76 76 'is_mod' => true, 77 77 ) ); 78 78 79 $this->assert Equals( true,groups_is_user_mod( self::$user, self::$groups[1] ) );79 $this->assertNotEmpty( groups_is_user_mod( self::$user, self::$groups[1] ) ); 80 80 } 81 81 82 82 public function test_groups_is_user_mod_expected_false() { … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 107 107 public function test_groups_is_user_member_expected_true() { 108 108 $this->add_user_to_group( self::$user, self::$groups[1] ); 109 109 110 $this->assertEquals( true, groups_is_user_member( self::$user, self::$groups[1] ) ); 110 $this->assertNotEmpty( groups_is_user_member( self::$user, self::$groups[1] ) ); 111 } 112 113 public function test_groups_is_user_member_should_return_true_for_admin() { 114 $this->add_user_to_group( self::$user, self::$groups[1], array( 115 'is_admin' => true, 116 ) ); 117 118 $this->assertNotEmpty( groups_is_user_member( self::$user, self::$groups[1] ) ); 119 } 120 121 public function test_groups_is_user_member_should_return_true_for_mod() { 122 $this->add_user_to_group( self::$user, self::$groups[1], array( 123 'is_mod' => true, 124 ) ); 125 126 $this->assertNotEmpty( groups_is_user_member( self::$user, self::$groups[1] ) ); 111 127 } 112 128 113 129 public function test_groups_is_user_member_expected_false() { … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 143 159 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 144 160 $m->ban(); 145 161 146 $this->assertEquals( true, groups_is_user_banned( self::$user, self::$groups[1] ) ); 162 $this->assertNotEmpty( groups_is_user_banned( self::$user, self::$groups[1] ) ); 163 } 164 165 public function test_groups_is_user_invited_should_return_false_for_confirmed_member() { 166 $this->add_user_to_group( self::$user, self::$groups[1] ); 167 $this->assertEquals( false, groups_is_user_invited( self::$user, self::$groups[1] ) ); 168 } 169 170 public function test_groups_is_user_invited_should_return_false_for_uninvited_member() { 171 $this->assertEquals( false, groups_is_user_invited( self::$user, self::$groups[1] ) ); 172 } 173 174 public function test_groups_is_user_invited_should_return_true_for_invited_member() { 175 $i = groups_invite_user( array( 176 'user_id' => self::$user, 177 'group_id' => self::$groups[1], 178 'inviter_id' => 123, 179 ) ); 180 181 // Send invite. 182 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 183 $m->invite_sent = 1; 184 $m->save(); 185 186 $this->assertNotEmpty( groups_is_user_invited( self::$user, self::$groups[1] ) ); 187 } 188 189 public function test_groups_is_user_pending_should_return_false_for_pending_member() { 190 groups_invite_user( array( 191 'user_id' => self::$user, 192 'group_id' => self::$groups[1], 193 'inviter_id' => 123, 194 ) ); 195 196 // Send invite. 197 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 198 $m->invite_sent = 1; 199 $m->save(); 200 201 $this->assertEquals( false, groups_is_user_pending( self::$user, self::$groups[1] ) ); 202 } 203 204 public function test_groups_is_user_pending_should_return_false_for_member_with_no_request() { 205 $this->assertEquals( false, groups_is_user_pending( self::$user, self::$groups[1] ) ); 206 } 207 208 public function test_groups_is_user_pending_should_return_true_for_pending_member() { 209 210 $m = new BP_Groups_Member; 211 $m->group_id = self::$groups[1]; 212 $m->user_id = self::$user; 213 $m->inviter_id = 0; 214 $m->is_admin = 0; 215 $m->user_title = ''; 216 $m->date_modified = bp_core_current_time(); 217 $m->is_confirmed = 0; 218 $m->comments = 'request'; 219 $m->save(); 220 221 $this->assertNotEmpty( groups_is_user_pending( self::$user, self::$groups[1] ) ); 147 222 } 148 223 }