Ticket #6327: 6327.2.refreshed.patch
File 6327.2.refreshed.patch, 35.4 KB (added by , 9 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 3d09916..074b13d 100644
function groups_clear_group_administrator_cache_on_member_save( BP_Groups_Member 212 212 } 213 213 add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' ); 214 214 215 /** 216 * Clear caches on membership save. 217 * 218 * @since 2.6.0 219 */ 220 function bp_groups_clear_user_group_cache_on_membership_save( BP_Groups_Member $member ) { 221 wp_cache_delete( $member->user_id, 'bp_groups_memberships_for_user' ); 222 wp_cache_delete( $member->id, 'bp_groups_memberships' ); 223 } 224 add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' ); 225 add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' ); 226 227 /** 228 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook. 229 * 230 * @since 2.6.0 231 */ 232 function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) { 233 wp_cache_delete( $user_id, 'bp_groups_memberships_for_user' ); 234 235 $membership = new BP_Groups_Member( $user_id, $group_id ); 236 wp_cache_delete( $membership->id, 'bp_groups_memberships' ); 237 } 238 add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 ); 239 215 240 /* List actions to clear super cached pages on, if super cache is installed */ 216 241 add_action( 'groups_join_group', 'bp_core_clear_cache' ); 217 242 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 c5cdc1f..fce345e 100644
function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) { 784 784 } 785 785 786 786 /** 787 * Get a list of groups of which the specified user is a member. 788 * 789 * @since 2.6.0 790 * 791 * @param int $user_id ID of the user. 792 * @param array $args { 793 * Array of optional args. 794 * @param bool|null $is_confirmed Whether to return only confirmed memberships. Pass `null` to disable this 795 * filter. Default: true. 796 * @param bool|null $is_banned Whether to return only banned memberships. Pass `null` to disable this filter. 797 * Default: false. 798 * @param bool|null $is_admin Whether to return only admin memberships. Pass `null` to disable this filter. 799 * Default: false. 800 * @param bool|null $is_mod Whether to return only mod memberships. Pass `null` to disable this filter. 801 * Default: false. 802 * @param bool|null $invite_sent Whether to return only memberships with 'invite_sent'. Pass `null` to disable 803 * this filter. Default: false. 804 * @param string $orderby Field to order by. Accepts 'id' (membership ID), 'group_id', 'date_modified'. 805 * Default: 'group_id'. 806 * @param string $order Sort order. Accepts 'ASC' or 'DESC'. Default: 'ASC'. 807 * } 808 * @return array Array of matching group memberships, keyed by group ID. 809 */ 810 function bp_get_user_groups( $user_id, $args = array() ) { 811 $r = bp_parse_args( $args, array( 812 'is_confirmed' => true, 813 'is_banned' => false, 814 'is_admin' => false, 815 'is_mod' => false, 816 'invite_sent' => null, 817 'orderby' => 'group_id', 818 'order' => 'ASC', 819 ), 'get_user_groups' ); 820 821 $user_id = intval( $user_id ); 822 823 $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' ); 824 if ( false === $membership_ids ) { 825 $membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id ); 826 wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' ); 827 } 828 829 // Prime the membership cache. 830 $uncached_membership_ids = bp_get_non_cached_ids( $membership_ids, 'bp_groups_memberships' ); 831 if ( ! empty( $uncached_membership_ids ) ) { 832 $uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids ); 833 834 foreach ( $uncached_memberships as $uncached_membership ) { 835 wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' ); 836 } 837 } 838 839 // Populate group membership array from cache. 840 $groups = array(); 841 foreach ( $membership_ids as $membership_id ) { 842 $membership = wp_cache_get( $membership_id, 'bp_groups_memberships' ); 843 844 // Sanity check. 845 if ( ! isset( $membership->group_id ) ) { 846 continue; 847 } 848 849 $group_id = (int) $membership->group_id; 850 851 $groups[ $group_id ] = $membership; 852 } 853 854 // Normalize group data. 855 foreach ( $groups as &$group ) { 856 // Integer values. 857 $group_id = intval( $group->group_id ); 858 foreach ( array( 'id', 'group_id', 'user_id', 'inviter_id' ) as $index ) { 859 $group->{$index} = intval( $group->{$index} ); 860 } 861 862 // Boolean values. 863 foreach ( array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' ) as $index ) { 864 $group->{$index} = (bool) $group->{$index}; 865 } 866 } 867 868 // Assemble filter array for use in `wp_list_filter()`. 869 $filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) ); 870 foreach ( $filters as $filter_name => $filter_value ) { 871 if ( is_null( $filter_value ) ) { 872 unset( $filters[ $filter_name ] ); 873 } 874 } 875 876 if ( ! empty( $filters ) ) { 877 $groups = wp_list_filter( $groups, $filters ); 878 } 879 880 // By default, results are ordered by membership id. 881 if ( 'group_id' === $r['orderby'] ) { 882 ksort( $groups ); 883 } elseif ( in_array( $r['orderby'], array( 'id', 'date_modified' ) ) ) { 884 $groups = bp_sort_by_key( $groups, $r['orderby'] ); 885 } 886 887 // By default, results are ordered ASC. 888 if ( 'DESC' === strtoupper( $r['order'] ) ) { 889 // `true` to preserve keys. 890 $groups = array_reverse( $groups, true ); 891 } 892 893 return $groups; 894 } 895 896 /** 787 897 * Get the count of groups of which the specified user is a member. 788 898 * 789 899 * @since 1.0.0 … … function groups_avatar_upload_dir( $group_id = 0 ) { 882 992 * @return bool 883 993 */ 884 994 function groups_is_user_admin( $user_id, $group_id ) { 885 return BP_Groups_Member::check_is_admin( $user_id, $group_id ); 995 $is_admin = false; 996 997 $user_groups = bp_get_user_groups( $user_id, array( 998 'is_admin' => true, 999 ) ); 1000 1001 if ( isset( $user_groups[ $group_id ] ) ) { 1002 $is_admin = $user_groups[ $group_id ]->id; 1003 } 1004 1005 return $is_admin; 886 1006 } 887 1007 888 1008 /** … … function groups_is_user_admin( $user_id, $group_id ) { 895 1015 * @return bool 896 1016 */ 897 1017 function groups_is_user_mod( $user_id, $group_id ) { 898 return BP_Groups_Member::check_is_mod( $user_id, $group_id ); 1018 $is_mod = false; 1019 1020 $user_groups = bp_get_user_groups( $user_id, array( 1021 'is_mod' => true, 1022 ) ); 1023 1024 if ( isset( $user_groups[ $group_id ] ) ) { 1025 $is_mod = $user_groups[ $group_id ]->id; 1026 } 1027 1028 return $is_mod; 899 1029 } 900 1030 901 1031 /** … … function groups_is_user_mod( $user_id, $group_id ) { 908 1038 * @return bool 909 1039 */ 910 1040 function groups_is_user_member( $user_id, $group_id ) { 911 return BP_Groups_Member::check_is_member( $user_id, $group_id ); 1041 $is_member = false; 1042 1043 $user_groups = bp_get_user_groups( $user_id, array( 1044 'is_admin' => null, 1045 'is_mod' => null, 1046 ) ); 1047 1048 if ( isset( $user_groups[ $group_id ] ) ) { 1049 $is_member = $user_groups[ $group_id ]->id; 1050 } 1051 1052 return $is_member; 912 1053 } 913 1054 914 1055 /** … … function groups_is_user_member( $user_id, $group_id ) { 922 1063 * @return bool 923 1064 */ 924 1065 function groups_is_user_banned( $user_id, $group_id ) { 925 return BP_Groups_Member::check_is_banned( $user_id, $group_id ); 1066 $is_banned = false; 1067 1068 $user_groups = bp_get_user_groups( $user_id, array( 1069 'is_confirmed' => null, 1070 'is_banned' => true, 1071 ) ); 1072 1073 if ( isset( $user_groups[ $group_id ] ) ) { 1074 $is_banned = $user_groups[ $group_id ]->id; 1075 } 1076 1077 return $is_banned; 1078 } 1079 1080 /** 1081 * Check whether a user has an outstanding invitation to a group. 1082 * 1083 * @since 2.6.0 1084 * 1085 * @param int $user_id ID of the user. 1086 * @param int $group_id ID of the group. 1087 * @return int|null ID of the membership if found. 1088 */ 1089 function groups_is_user_invited( $user_id, $group_id ) { 1090 $is_invited = false; 1091 1092 $user_groups = bp_get_user_groups( $user_id, array( 1093 'invite_sent' => true, 1094 'is_confirmed' => false, 1095 ) ); 1096 1097 if ( isset( $user_groups[ $group_id ] ) ) { 1098 $is_invited = $user_groups[ $group_id ]->id; 1099 } 1100 1101 return $is_invited; 1102 } 1103 1104 /** 1105 * Check whether a user has a pending membership request for a group. 1106 * 1107 * @since 2.6.0 1108 * 1109 * @param int $user_id ID of the user. 1110 * @param int $group_id ID of the group. 1111 * @return int|null ID of the membership if found. 1112 */ 1113 function groups_is_user_pending( $user_id, $group_id ) { 1114 $is_pending = false; 1115 1116 $user_groups = bp_get_user_groups( $user_id, array( 1117 'invite_sent' => false, 1118 'is_confirmed' => false, 1119 ) ); 1120 1121 if ( isset( $user_groups[ $group_id ] ) ) { 1122 $is_pending = $user_groups[ $group_id ]->id; 1123 } 1124 1125 return $is_pending; 926 1126 } 927 1127 928 1128 /** -
src/bp-groups/classes/class-bp-groups-component.php
diff --git src/bp-groups/classes/class-bp-groups-component.php src/bp-groups/classes/class-bp-groups-component.php index 355ab8c..6ab8124 100644
class BP_Groups_Component extends BP_Component { 827 827 'bp_groups', 828 828 'bp_group_admins', 829 829 'bp_group_invite_count', 830 'group_meta' 830 'group_meta', 831 'bp_groups_memberships', 832 'bp_groups_memberships_for_user', 831 833 ) ); 832 834 833 835 parent::setup_cache_groups(); -
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 8d29b0e..d01312e 100644
class BP_Groups_Group { 1395 1395 * @return array $paged_groups 1396 1396 */ 1397 1397 public static function get_group_extras( &$paged_groups, &$group_ids, $type = false ) { 1398 global $wpdb; 1399 1400 if ( empty( $group_ids ) ) 1401 return $paged_groups; 1402 1403 $bp = buddypress(); 1404 1405 // Sanitize group IDs. 1406 $group_ids = implode( ',', wp_parse_id_list( $group_ids ) ); 1407 1408 // Fetch the logged-in user's status within each group. 1409 if ( is_user_logged_in() ) { 1410 $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() ) ); 1411 } else { 1412 $user_status_results = array(); 1413 } 1414 1415 // Reindex. 1416 $user_status = array(); 1417 foreach ( $user_status_results as $user_status_result ) { 1418 $user_status[ $user_status_result->group_id ] = $user_status_result; 1419 } 1420 1421 for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) { 1422 $is_member = $is_invited = $is_pending = '0'; 1423 $gid = $paged_groups[ $i ]->id; 1424 1425 if ( isset( $user_status[ $gid ] ) ) { 1426 1427 // The is_confirmed means the user is a member. 1428 if ( $user_status[ $gid ]->is_confirmed ) { 1429 $is_member = '1'; 1430 1431 // The invite_sent means the user has been invited. 1432 } elseif ( $user_status[ $gid ]->invite_sent ) { 1433 $is_invited = '1'; 1434 1435 // User has sent request, but has not been confirmed. 1436 } else { 1437 $is_pending = '1'; 1438 } 1439 } 1440 1441 $paged_groups[ $i ]->is_member = $is_member; 1442 $paged_groups[ $i ]->is_invited = $is_invited; 1443 $paged_groups[ $i ]->is_pending = $is_pending; 1444 } 1445 1446 if ( is_user_logged_in() ) { 1447 $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() ) ); 1448 } else { 1449 $user_banned = array(); 1450 } 1451 1452 for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) { 1453 $paged_groups[$i]->is_banned = false; 1454 1455 foreach ( (array) $user_banned as $group_id ) { 1456 if ( $group_id == $paged_groups[$i]->id ) { 1457 $paged_groups[$i]->is_banned = true; 1458 } 1459 } 1460 } 1398 $user_id = bp_loggedin_user_id(); 1399 1400 foreach ( $paged_groups as &$group ) { 1401 $group->is_member = groups_is_user_member( $user_id, $group->id ) ? '1' : '0'; 1402 $group->is_invited = groups_is_user_invited( $user_id, $group->id ) ? '1' : '0'; 1403 $group->is_pending = groups_is_user_pending( $user_id, $group->id ) ? '1' : '0'; 1404 $group->is_banned = (bool) groups_is_user_banned( $user_id, $group->id ); 1405 } 1461 1406 1462 1407 return $paged_groups; 1463 1408 } -
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 68d315e..9737a25 100644
class BP_Groups_Member { 1053 1053 } 1054 1054 1055 1055 /** 1056 * Get group membership objects by ID (or an array of IDs). 1057 * 1058 * @since 2.6.0 1059 * 1060 * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs. 1061 * @return array 1062 */ 1063 public static function get_memberships_by_id( $membership_ids ) { 1064 global $wpdb; 1065 1066 $bp = buddypress(); 1067 1068 $membership_ids = implode( ',', wp_parse_id_list( $membership_ids ) ); 1069 return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" ); 1070 } 1071 1072 /** 1056 1073 * Get the IDs users with outstanding membership requests to the group. 1057 1074 * 1058 1075 * @since 1.6.0 … … class BP_Groups_Member { 1161 1178 } 1162 1179 1163 1180 /** 1181 * Get all membership IDs for a user. 1182 * 1183 * @since 2.6.0 1184 * 1185 * @param int $user_id ID of the user. 1186 * @return array 1187 */ 1188 public static function get_membership_ids_for_user( $user_id ) { 1189 global $wpdb; 1190 1191 $bp = buddypress(); 1192 1193 $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 ) ); 1194 1195 return $group_ids; 1196 } 1197 1198 /** 1164 1199 * Delete all memberships for a given group. 1165 1200 * 1166 1201 * @since 1.6.0 -
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 7aea071..5447ced 100644
class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1268 1268 public function test_total_group_count_should_return_integer() { 1269 1269 $this->assertInternalType( 'int', BP_Groups_Member::total_group_count( 123 ) ); 1270 1270 } 1271 1272 /** 1273 * @group get_memberships_by_id 1274 */ 1275 public function test_get_memberships_by_id_with_single_id() { 1276 $users = $this->factory->user->create_many( 2 ); 1277 $groups = $this->factory->group->create_many( 2 ); 1278 1279 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 1280 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 1281 1282 $found = BP_Groups_Member::get_memberships_by_id( $m0 ); 1283 1284 $this->assertSame( 1, count( $found ) ); 1285 $this->assertEquals( $m0, $found[0]->id ); 1286 } 1287 1288 /** 1289 * @group get_memberships_by_id 1290 */ 1291 public function test_get_memberships_by_id_with_multiple_ids() { 1292 $users = $this->factory->user->create_many( 2 ); 1293 $groups = $this->factory->group->create_many( 2 ); 1294 1295 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 1296 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 1297 1298 $found = BP_Groups_Member::get_memberships_by_id( array( $m0, $m1 ) ); 1299 1300 $this->assertSame( 2, count( $found ) ); 1301 $this->assertEqualSets( array( $m0, $m1 ), wp_list_pluck( $found, 'id' ) ); 1302 } 1271 1303 } -
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 }