[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @group groups 4 * @group BP_Groups_Member 5 */ 6 class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 7 static public $user_ids; 8 static public $group_ids; 9 10 public static function wpSetUpBeforeClass( $factory ) { 11 global $wpdb, $bp; 12 self::$user_ids = $factory->user->create_many( 4 ); 13 self::$group_ids = $factory->group->create_many( 3, array( 14 'creator_id' => self::$user_ids[3], 15 ) ); 16 } 17 18 public static function wpTearDownAfterClass() { 19 array_map( array( __CLASS__, 'delete_user' ), self::$user_ids ); 20 array_map( 'groups_delete_group', self::$group_ids ); 21 } 22 23 public function test_get_recently_joined_with_filter() { 24 $g1 = self::factory()->group->create( array( 25 'name' => 'Tab', 26 ) ); 27 $g2 = self::factory()->group->create( array( 28 'name' => 'Diet Rite', 29 ) ); 30 31 $u = self::factory()->user->create(); 32 self::add_user_to_group( $u, $g1 ); 33 self::add_user_to_group( $u, $g2 ); 34 35 $groups = BP_Groups_Member::get_recently_joined( $u, false, false, 'Rite' ); 36 37 $ids = wp_list_pluck( $groups['groups'], 'id' ); 38 $this->assertEquals( $ids, array( $g2 ) ); 39 } 40 41 public function test_get_is_admin_of_with_filter() { 42 $g1 = self::factory()->group->create( array( 43 'name' => 'RC Cola', 44 ) ); 45 $g2 = self::factory()->group->create( array( 46 'name' => 'Pepsi', 47 ) ); 48 49 $u = self::factory()->user->create(); 50 self::add_user_to_group( $u, $g1 ); 51 self::add_user_to_group( $u, $g2 ); 52 53 $m1 = new BP_Groups_Member( $u, $g1 ); 54 $m1->promote( 'admin' ); 55 $m2 = new BP_Groups_Member( $u, $g2 ); 56 $m2->promote( 'admin' ); 57 58 $groups = BP_Groups_Member::get_is_admin_of( $u, false, false, 'eps' ); 59 60 $ids = wp_list_pluck( $groups['groups'], 'id' ); 61 $this->assertEquals( $ids, array( $g2 ) ); 62 } 63 64 public function test_get_is_mod_of_with_filter() { 65 $g1 = self::factory()->group->create( array( 66 'name' => 'RC Cola', 67 ) ); 68 $g2 = self::factory()->group->create( array( 69 'name' => 'Pepsi', 70 ) ); 71 72 $u = self::factory()->user->create(); 73 self::add_user_to_group( $u, $g1 ); 74 self::add_user_to_group( $u, $g2 ); 75 76 $m1 = new BP_Groups_Member( $u, $g1 ); 77 $m1->promote( 'mod' ); 78 $m2 = new BP_Groups_Member( $u, $g2 ); 79 $m2->promote( 'mod' ); 80 81 $groups = BP_Groups_Member::get_is_mod_of( $u, false, false, 'eps' ); 82 83 $ids = wp_list_pluck( $groups['groups'], 'id' ); 84 $this->assertEquals( $ids, array( $g2 ) ); 85 } 86 87 public function test_get_is_banned_of_with_filter() { 88 $g1 = self::factory()->group->create( array( 89 'name' => 'RC Cola', 90 ) ); 91 $g2 = self::factory()->group->create( array( 92 'name' => 'Pepsi', 93 ) ); 94 95 $u = self::factory()->user->create(); 96 self::add_user_to_group( $u, $g1 ); 97 self::add_user_to_group( $u, $g2 ); 98 99 $m1 = new BP_Groups_Member( $u, $g1 ); 100 $m1->ban(); 101 $m2 = new BP_Groups_Member( $u, $g2 ); 102 $m2->ban(); 103 104 $groups = BP_Groups_Member::get_is_banned_of( $u, false, false, 'eps' ); 105 106 $ids = wp_list_pluck( $groups['groups'], 'id' ); 107 $this->assertEquals( $ids, array( $g2 ) ); 108 } 109 110 public function test_get_invites_with_exclude() { 111 $u1 = self::factory()->user->create(); 112 $u2 = self::factory()->user->create(); 113 $g1 = self::factory()->group->create( array( 114 'status' => 'private', 115 'creator_id' => $u1 116 ) ); 117 $g2 = self::factory()->group->create( array( 118 'status' => 'private', 119 'creator_id' => $u1 120 ) ); 121 122 groups_invite_user( array( 123 'user_id' => $u2, 124 'group_id' => $g1, 125 'inviter_id' => $u1, 126 'send_invite' => 1, 127 ) ); 128 groups_invite_user( array( 129 'user_id' => $u2, 130 'group_id' => $g2, 131 'inviter_id' => $u1, 132 'send_invite' => 1, 133 ) ); 134 135 $groups = BP_Groups_Member::get_invites( $u2, false, false, array( 'awesome', $g1 ) ); 136 137 $ids = wp_list_pluck( $groups['groups'], 'id' ); 138 $this->assertEquals( $ids, array( $g2 ) ); 139 } 140 141 /** 142 * @expectedDeprecated BP_Groups_Member::get_all_for_group 143 */ 144 public function test_get_all_for_group_with_exclude() { 145 $g1 = self::factory()->group->create(); 146 147 $u1 = self::factory()->user->create(); 148 $u2 = self::factory()->user->create(); 149 self::add_user_to_group( $u1, $g1 ); 150 self::add_user_to_group( $u2, $g1 ); 151 152 $members = BP_Groups_Member::get_all_for_group( $g1, false, false, true, true, array( $u1 ) ); 153 154 $mm = (array) $members['members']; 155 $ids = wp_list_pluck( $mm, 'user_id' ); 156 $this->assertEquals( array( $u2 ), $ids ); 157 } 158 159 /** 160 * @group bp_groups_user_can_send_invites 161 */ 162 public function test_bp_groups_user_can_send_invites() { 163 $u_nonmembers = self::factory()->user->create(); 164 $u_members = self::factory()->user->create(); 165 $u_mods = self::factory()->user->create(); 166 $u_admins = self::factory()->user->create(); 167 $u_siteadmin = self::factory()->user->create(); 168 169 $user_siteadmin = new WP_User( $u_siteadmin ); 170 $user_siteadmin->add_role( 'administrator' ); 171 172 $g = self::factory()->group->create(); 173 174 $time = time() - 60; 175 $old_current_user = get_current_user_id(); 176 177 // Create member-level user 178 $this->add_user_to_group( $u_members, $g, array( 179 'date_modified' => date( 'Y-m-d H:i:s', $time ), 180 ) ); 181 182 // Create mod-level user 183 $this->add_user_to_group( $u_mods, $g, array( 184 'date_modified' => date( 'Y-m-d H:i:s', $time ), 185 ) ); 186 $m_mod = new BP_Groups_Member( $u_mods, $g ); 187 $m_mod->promote( 'mod' ); 188 189 // Create admin-level user 190 $this->add_user_to_group( $u_admins, $g, array( 191 'date_modified' => date( 'Y-m-d H:i:s', $time ), 192 ) ); 193 $m_admin = new BP_Groups_Member( $u_admins, $g ); 194 $m_admin->promote( 'admin' ); 195 196 // Test with no status 197 // In bp_group_get_invite_status(), no status falls back to "members" 198 $this->assertTrue( '' == groups_get_groupmeta( $g, 'invite_status' ) ); 199 $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_nonmembers ) ); 200 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_members ) ); 201 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_mods ) ); 202 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) ); 203 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) ); 204 205 // Test with members status 206 groups_update_groupmeta( $g, 'invite_status', 'members' ); 207 $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_nonmembers ) ); 208 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_members ) ); 209 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_mods ) ); 210 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) ); 211 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) ); 212 // Falling back to current user 213 $this->set_current_user( $u_members ); 214 $this->assertTrue( bp_groups_user_can_send_invites( $g, null ) ); 215 216 // Test with mod status 217 groups_update_groupmeta( $g, 'invite_status', 'mods' ); 218 $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_nonmembers ) ); 219 $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_members ) ); 220 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_mods ) ); 221 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) ); 222 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) ); 223 // Falling back to current user 224 $this->set_current_user( $u_members ); 225 $this->assertFalse( bp_groups_user_can_send_invites( $g, null ) ); 226 $this->set_current_user( $u_mods ); 227 $this->assertTrue( bp_groups_user_can_send_invites( $g, null ) ); 228 229 // Test with admin status 230 groups_update_groupmeta( $g, 'invite_status', 'admins' ); 231 $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_nonmembers ) ); 232 $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_members ) ); 233 $this->assertFalse( bp_groups_user_can_send_invites( $g, $u_mods ) ); 234 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_admins ) ); 235 $this->assertTrue( bp_groups_user_can_send_invites( $g, $u_siteadmin ) ); 236 // Falling back to current user 237 $this->set_current_user( $u_mods ); 238 $this->assertFalse( bp_groups_user_can_send_invites( $g, null ) ); 239 $this->set_current_user( $u_admins ); 240 $this->assertTrue( bp_groups_user_can_send_invites( $g, null ) ); 241 242 // Bad or null parameters 243 $this->assertFalse( bp_groups_user_can_send_invites( 59876454257, $u_members ) ); 244 $this->assertFalse( bp_groups_user_can_send_invites( $g, 958647515 ) ); 245 // Not in group context 246 $this->assertFalse( bp_groups_user_can_send_invites( null, $u_members ) ); 247 // In group context 248 $g_obj = groups_get_group( $g ); 249 $this->go_to( bp_get_group_permalink( $g_obj ) ); 250 groups_update_groupmeta( $g, 'invite_status', 'mods' ); 251 $this->assertFalse( bp_groups_user_can_send_invites( null, $u_nonmembers ) ); 252 $this->assertFalse( bp_groups_user_can_send_invites( null, $u_members ) ); 253 $this->assertTrue( bp_groups_user_can_send_invites( null, $u_mods ) ); 254 255 $this->set_current_user( $old_current_user ); 256 } 257 258 /** 259 * @group groups_reject_membership_request 260 * @group group_membership_requests 261 * @group group_membership 262 */ 263 public function test_bp_groups_reject_membership_request_remove_request() { 264 $u1 = self::factory()->user->create(); 265 $g = self::factory()->group->create( array( 266 'status' => 'private', 267 ) ); 268 269 // Membership requests should be removed. 270 groups_send_membership_request( array( 271 'user_id' => $u1, 272 'group_id' => $g 273 ) ); 274 275 groups_reject_membership_request( null, $u1, $g ); 276 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 277 $this->assertEquals( 0, $u1_has_request ); 278 } 279 280 /** 281 * @group groups_reject_membership_request 282 * @group group_membership_requests 283 * @group group_membership 284 */ 285 public function test_bp_groups_reject_membership_request_leave_memberships_intact() { 286 $u1 = self::factory()->user->create(); 287 $g = self::factory()->group->create( array( 288 'status' => 'private', 289 ) ); 290 291 $this->add_user_to_group( $u1, $g ); 292 293 // Confirmed memberships should be left intact. 294 groups_reject_membership_request( null, $u1, $g ); 295 $u1_is_member = groups_is_user_member( $u1, $g ); 296 $this->assertTrue( is_numeric( $u1_is_member ) && $u1_is_member > 0 ); 297 } 298 299 /** 300 * @group groups_reject_membership_request 301 * @group group_membership_requests 302 * @group group_membership 303 */ 304 public function test_bp_groups_reject_membership_request_leave_invites_intact() { 305 $u1 = self::factory()->user->create(); 306 $u2 = self::factory()->user->create(); 307 $g = self::factory()->group->create( array( 308 'status' => 'private', 309 ) ); 310 311 $time = time() - 60; 312 $this->add_user_to_group( $u1, $g, array( 313 'date_modified' => date( 'Y-m-d H:i:s', $time ), 314 ) ); 315 316 // Outstanding invitations should be left intact. 317 groups_invite_user( array( 318 'user_id' => $u2, 319 'group_id' => $g, 320 'inviter_id' => $u1, 321 'send_invite' => 1, 322 ) ); 323 groups_reject_membership_request( null, $u2, $g ); 324 $u2_has_invite = groups_check_user_has_invite( $u2, $g ); 325 $this->assertTrue( is_numeric( $u2_has_invite ) && $u2_has_invite > 0 ); 326 } 327 328 /** 329 * @group groups_delete_membership_request 330 * @group group_membership_requests 331 * @group group_membership 332 */ 333 public function test_bp_groups_delete_membership_request_remove_request() { 334 $u1 = self::factory()->user->create(); 335 $g = self::factory()->group->create( array( 336 'status' => 'private', 337 ) ); 338 339 // Membership requests should be removed. 340 groups_send_membership_request( array( 341 'user_id' => $u1, 342 'group_id' => $g 343 ) ); 344 groups_delete_membership_request( null, $u1, $g ); 345 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 346 $this->assertEquals( 0, $u1_has_request ); 347 } 348 349 /** 350 * @group groups_delete_membership_request 351 * @group group_membership_requests 352 * @group group_membership 353 */ 354 public function test_bp_groups_delete_membership_request_leave_memberships_intact() { 355 $u1 = self::factory()->user->create(); 356 $g = self::factory()->group->create( array( 357 'status' => 'private', 358 ) ); 359 360 $this->add_user_to_group( $u1, $g ); 361 362 // Confirmed memberships should be left intact. 363 groups_delete_membership_request( null, $u1, $g ); 364 $u1_is_member = groups_is_user_member( $u1, $g ); 365 $this->assertTrue( is_numeric( $u1_is_member ) && $u1_is_member > 0 ); 366 } 367 368 /** 369 * @group groups_delete_membership_request 370 * @group group_membership_requests 371 * @group group_membership 372 */ 373 public function test_bp_groups_delete_membership_request_leave_invites_intact() { 374 $u1 = self::factory()->user->create(); 375 $u2 = self::factory()->user->create(); 376 $g = self::factory()->group->create( array( 377 'status' => 'private', 378 ) ); 379 380 $time = time() - 60; 381 $this->add_user_to_group( $u1, $g, array( 382 'date_modified' => date( 'Y-m-d H:i:s', $time ), 383 ) ); 384 385 // Outstanding invitations should be left intact. 386 groups_invite_user( array( 387 'user_id' => $u2, 388 'group_id' => $g, 389 'inviter_id' => $u1, 390 'send_invite' => 1, 391 ) ); 392 393 groups_delete_membership_request( null, $u2, $g ); 394 $u2_has_invite = groups_check_user_has_invite( $u2, $g ); 395 $this->assertTrue( is_numeric( $u2_has_invite ) && $u2_has_invite > 0 ); 396 } 397 398 /** 399 * @group groups_reject_invite 400 * @group group_invitations 401 * @group group_membership 402 */ 403 public function test_bp_groups_reject_invite_remove_invite() { 404 $u1 = self::factory()->user->create(); 405 $u2 = self::factory()->user->create(); 406 $g = self::factory()->group->create( array( 407 'status' => 'private', 408 ) ); 409 410 $time = time() - 60; 411 $this->add_user_to_group( $u1, $g, array( 412 'date_modified' => date( 'Y-m-d H:i:s', $time ), 413 ) ); 414 415 // The invitation should be removed. 416 groups_invite_user( array( 417 'user_id' => $u2, 418 'group_id' => $g, 419 'inviter_id' => $u1, 420 'send_invite' => 1, 421 ) ); 422 423 groups_reject_invite( $u2, $g ); 424 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 425 $this->assertEquals( 0, $u2_has_invite ); 426 } 427 428 /** 429 * @group groups_reject_invite 430 * @group group_invitations 431 * @group group_membership 432 */ 433 public function test_bp_groups_reject_invite_leave_memberships_intact() { 434 $u1 = self::factory()->user->create(); 435 $g = self::factory()->group->create( array( 436 'status' => 'private', 437 ) ); 438 439 $time = time() - 60; 440 $this->add_user_to_group( $u1, $g, array( 441 'date_modified' => date( 'Y-m-d H:i:s', $time ), 442 ) ); 443 444 // Confirmed memberships should be left intact. 445 groups_reject_invite( $u1, $g ); 446 $u1_is_member = groups_is_user_member( $u1, $g ); 447 $this->assertTrue( is_numeric( $u1_is_member ) && $u1_is_member > 0 ); 448 } 449 450 /** 451 * @group groups_reject_invite 452 * @group group_invitations 453 * @group group_membership 454 */ 455 public function test_bp_groups_reject_invite_leave_requests_intact() { 456 $u1 = self::factory()->user->create(); 457 $g = self::factory()->group->create( array( 458 'status' => 'private', 459 ) ); 460 461 // Membership requests should be left intact. 462 groups_send_membership_request( array( 463 'user_id' => $u1, 464 'group_id' => $g 465 ) ); 466 groups_reject_invite( $u1, $g ); 467 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 468 $this->assertTrue( is_numeric( $u1_has_request ) && $u1_has_request > 0 ); 469 } 470 471 /** 472 * @group groups_delete_invite 473 * @group group_invitations 474 * @group group_membership 475 */ 476 public function test_bp_groups_delete_invite_remove_invite() { 477 $u1 = self::factory()->user->create(); 478 $u2 = self::factory()->user->create(); 479 $g = self::factory()->group->create( array( 480 'status' => 'private', 481 ) ); 482 483 $time = time() - 60; 484 $this->add_user_to_group( $u1, $g, array( 485 'date_modified' => date( 'Y-m-d H:i:s', $time ), 486 ) ); 487 488 // The invitation should be removed. 489 groups_invite_user( array( 490 'user_id' => $u2, 491 'group_id' => $g, 492 'inviter_id' => $u1, 493 'send_invite' => 1, 494 ) ); 495 496 groups_delete_invite( $u2, $g ); 497 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 498 $this->assertEquals( 0, $u2_has_invite ); 499 } 500 501 /** 502 * @group groups_delete_invite 503 * @group group_invitations 504 * @group group_membership 505 */ 506 public function test_bp_groups_delete_invite_remove_draft_invite() { 507 $u1 = self::factory()->user->create(); 508 $u2 = self::factory()->user->create(); 509 $g = self::factory()->group->create( array( 510 'status' => 'private', 511 ) ); 512 513 $time = time() - 60; 514 $this->add_user_to_group( $u1, $g, array( 515 'date_modified' => date( 'Y-m-d H:i:s', $time ), 516 ) ); 517 518 // Create the draft invitation. 519 groups_invite_user( array( 520 'user_id' => $u2, 521 'group_id' => $g, 522 'inviter_id' => $u1 523 ) ); 524 525 // Check that the invite got created. 526 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 527 $this->assertTrue( is_numeric( $u2_has_invite ) && $u2_has_invite > 0 ); 528 529 // The invitation should be removed. 530 groups_delete_invite( $u2, $g ); 531 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 532 $this->assertEquals( 0, $u2_has_invite ); 533 } 534 535 /** 536 * @group groups_delete_invite 537 * @group group_invitations 538 * @group group_membership 539 */ 540 public function test_bp_groups_delete_invite_leave_memberships_intact() { 541 $u1 = self::factory()->user->create(); 542 $g = self::factory()->group->create( array( 543 'status' => 'private', 544 ) ); 545 546 $time = time() - 60; 547 $this->add_user_to_group( $u1, $g, array( 548 'date_modified' => date( 'Y-m-d H:i:s', $time ), 549 ) ); 550 551 groups_delete_invite( $u1, $g ); 552 $u1_is_member = groups_is_user_member( $u1, $g ); 553 $this->assertTrue( is_numeric( $u1_is_member ) && $u1_is_member > 0 ); 554 } 555 556 /** 557 * @group groups_delete_invite 558 * @group group_invitations 559 * @group group_membership 560 */ 561 public function test_bp_groups_delete_invite_leave_requests_intact() { 562 $u1 = self::factory()->user->create(); 563 $g = self::factory()->group->create( array( 564 'status' => 'private', 565 ) ); 566 567 // Membership requests should be left intact. 568 groups_send_membership_request( array( 569 'user_id' => $u1, 570 'group_id' => $g 571 ) ); 572 groups_delete_invite( $u1, $g ); 573 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 574 $this->assertTrue( is_numeric( $u1_has_request ) && $u1_has_request > 0 ); 575 } 576 577 /** 578 * @group groups_uninvite_user 579 * @group group_invitations 580 * @group group_membership 581 */ 582 public function test_bp_groups_uninvite_user_remove_invite() { 583 $u1 = self::factory()->user->create(); 584 $u2 = self::factory()->user->create(); 585 $g = self::factory()->group->create( array( 586 'status' => 'private', 587 ) ); 588 589 $time = time() - 60; 590 $this->add_user_to_group( $u1, $g, array( 591 'date_modified' => date( 'Y-m-d H:i:s', $time ), 592 ) ); 593 594 // The invitation should be removed. 595 groups_invite_user( array( 596 'user_id' => $u2, 597 'group_id' => $g, 598 'inviter_id' => $u1, 599 'send_invite' => 1, 600 ) ); 601 groups_uninvite_user( $u2, $g ); 602 $u2_has_invite = groups_check_user_has_invite( $u2, $g, 'all' ); 603 $this->assertEquals( 0, $u2_has_invite ); 604 } 605 606 /** 607 * @group groups_uninvite_user 608 * @group group_invitations 609 * @group group_membership 610 */ 611 public function test_bp_groups_uninvite_user_leave_memberships_intact() { 612 $u1 = self::factory()->user->create(); 613 $g = self::factory()->group->create( array( 614 'status' => 'private', 615 ) ); 616 617 $time = time() - 60; 618 $this->add_user_to_group( $u1, $g, array( 619 'date_modified' => date( 'Y-m-d H:i:s', $time ), 620 ) ); 621 622 // Confirmed memberships should be left intact. 623 groups_is_user_member( $u1, $g ); 624 groups_uninvite_user( $u1, $g ); 625 $u1_is_member = groups_is_user_member( $u1, $g ); 626 $this->assertTrue( is_numeric( $u1_is_member ) && $u1_is_member > 0 ); 627 } 628 629 /** 630 * @group groups_uninvite_user 631 * @group group_invitations 632 * @group group_membership 633 */ 634 public function test_bp_groups_uninvite_user_leave_requests_intact() { 635 $u1 = self::factory()->user->create(); 636 $g = self::factory()->group->create( array( 637 'status' => 'private', 638 ) ); 639 640 // Membership requests should be left intact. 641 groups_send_membership_request( array( 642 'user_id' => $u1, 643 'group_id' => $g 644 ) ); 645 groups_uninvite_user( $u1, $g ); 646 $u1_has_request = groups_check_for_membership_request( $u1, $g ); 647 $this->assertTrue( is_numeric( $u1_has_request ) && $u1_has_request > 0 ); 648 } 649 650 /** 651 * @group groups_join_group 652 * @group group_membership 653 */ 654 public function test_groups_join_group_basic_join() { 655 $u1 = self::factory()->user->create(); 656 $g = self::factory()->group->create(); 657 658 groups_join_group( $g, $u1 ); 659 $membership_id = groups_is_user_member( $u1, $g ); 660 $this->assertTrue( is_numeric( $membership_id ) && $membership_id > 0 ); 661 } 662 663 /** 664 * @group groups_join_group 665 * @group group_membership 666 */ 667 public function test_groups_join_group_basic_join_use_current_user() { 668 $u1 = self::factory()->user->create(); 669 $g = self::factory()->group->create(); 670 $old_current_user = get_current_user_id(); 671 $this->set_current_user( $u1 ); 672 673 groups_join_group( $g ); 674 $membership_id = groups_is_user_member( $u1, $g ); 675 $this->assertTrue( is_numeric( $membership_id ) && $membership_id > 0 ); 676 $this->set_current_user( $old_current_user ); 677 } 678 679 /** 680 * @group groups_join_group 681 * @group group_membership 682 */ 683 public function test_groups_join_group_already_member() { 684 $u1 = self::factory()->user->create(); 685 $g = self::factory()->group->create(); 686 $this->add_user_to_group( $u1, $g ); 687 688 $this->assertTrue( groups_join_group( $g, $u1 ) ); 689 } 690 691 /** 692 * @group groups_join_group 693 * @group group_membership 694 */ 695 public function test_groups_join_group_cleanup_invites() { 696 $u1 = self::factory()->user->create(); 697 $u2 = self::factory()->user->create(); 698 $g = self::factory()->group->create(); 699 $this->add_user_to_group( $u1, $g ); 700 701 $m1 = new BP_Groups_Member( $u1, $g ); 702 $m1->promote( 'admin' ); 703 704 groups_invite_user( array( 705 'user_id' => $u2, 706 'group_id' => $g, 707 'inviter_id' => $u1, 708 'send_invite' => 1, 709 ) ); 710 groups_join_group( $g, $u2 ); 711 // Upon joining the group, outstanding invitations should be cleaned up. 712 $this->assertEquals( null, groups_check_user_has_invite( $u2, $g, 'any' ) ); 713 } 714 715 /** 716 * @group groups_join_group 717 * @group group_membership 718 */ 719 public function test_groups_join_group_cleanup_requests() { 720 $u1 = self::factory()->user->create(); 721 $g = self::factory()->group->create(); 722 723 groups_send_membership_request( array( 724 'user_id' => $u1, 725 'group_id' => $g 726 ) ); 727 728 groups_join_group( $g, $u1 ); 729 // Upon joining the group, outstanding requests should be cleaned up. 730 $this->assertEquals( null, groups_check_for_membership_request( $u1, $g ) ); 731 } 732 733 /** 734 * @group groups_leave_group 735 * @group group_membership 736 */ 737 public function test_groups_leave_group_basic_leave_self_initiated() { 738 $old_current_user = get_current_user_id(); 739 $u1 = self::factory()->user->create(); 740 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 741 $u2 = self::factory()->user->create(); 742 $this->add_user_to_group( $u2, $g ); 743 744 $before = groups_get_total_member_count( $g ); 745 $this->set_current_user( $u2 ); 746 groups_leave_group( $g, $u2 ); 747 $after = groups_get_total_member_count( $g ); 748 749 $this->assertEquals( $before - 1, $after ); 750 $this->set_current_user( $old_current_user ); 751 } 752 753 /** 754 * @group groups_leave_group 755 * @group group_membership 756 */ 757 public function test_groups_leave_group_basic_leave_use_current_user() { 758 $old_current_user = get_current_user_id(); 759 $u1 = self::factory()->user->create(); 760 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 761 $u2 = self::factory()->user->create(); 762 $this->add_user_to_group( $u2, $g ); 763 764 $before = groups_get_total_member_count( $g ); 765 $this->set_current_user( $u2 ); 766 groups_leave_group( $g ); 767 $after = groups_get_total_member_count( $g ); 768 769 $this->assertEquals( $before - 1, $after ); 770 $this->set_current_user( $old_current_user ); 771 } 772 773 /** 774 * @group groups_leave_group 775 * @group group_membership 776 */ 777 public function test_groups_leave_group_basic_leave_group_admin_initiated() { 778 $old_current_user = get_current_user_id(); 779 $u1 = self::factory()->user->create(); 780 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 781 $u2 = self::factory()->user->create(); 782 $this->add_user_to_group( $u2, $g ); 783 784 $before = groups_get_total_member_count( $g ); 785 $this->set_current_user( $u1 ); 786 groups_leave_group( $g, $u2 ); 787 $after = groups_get_total_member_count( $g ); 788 789 $this->assertEquals( $before - 1, $after ); 790 $this->set_current_user( $old_current_user ); 791 } 792 793 /** 794 * @group groups_leave_group 795 * @group group_membership 796 */ 797 public function test_groups_leave_group_basic_leave_site_admin_initiated() { 798 $old_current_user = get_current_user_id(); 799 $u1 = self::factory()->user->create(); 800 $u1_siteadmin = new WP_User( $u1 ); 801 $u1_siteadmin->add_role( 'administrator' ); 802 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 803 $u2 = self::factory()->user->create(); 804 $this->add_user_to_group( $u2, $g ); 805 806 $before = groups_get_total_member_count( $g ); 807 $this->set_current_user( $u1 ); 808 groups_leave_group( $g, $u2 ); 809 $after = groups_get_total_member_count( $g ); 810 811 $this->assertEquals( $before - 1, $after ); 812 $this->set_current_user( $old_current_user ); 813 } 814 815 /** 816 * @group groups_leave_group 817 * @group group_membership 818 */ 819 public function test_groups_leave_group_single_admin_prevent_leave() { 820 $old_current_user = get_current_user_id(); 821 $u1 = self::factory()->user->create(); 822 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 823 $u2 = self::factory()->user->create(); 824 $this->add_user_to_group( $u2, $g ); 825 826 $before = groups_get_total_member_count( $g ); 827 $this->set_current_user( $u1 ); 828 groups_leave_group( $g, $u1 ); 829 $after = groups_get_total_member_count( $g ); 830 831 $this->assertEquals( $before, $after ); 832 $this->set_current_user( $old_current_user ); 833 } 834 835 /** 836 * @group groups_leave_group 837 * @group group_membership 838 */ 839 public function test_groups_leave_group_multiple_admins_allow_leave() { 840 $old_current_user = get_current_user_id(); 841 $u1 = self::factory()->user->create(); 842 $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); 843 $u2 = self::factory()->user->create(); 844 $this->add_user_to_group( $u2, $g ); 845 $m2 = new BP_Groups_Member( $u2, $g ); 846 $m2->promote( 'admin' ); 847 848 $before = groups_get_total_member_count( $g ); 849 $this->set_current_user( $u1 ); 850 groups_leave_group( $g, $u1 ); 851 $after = groups_get_total_member_count( $g ); 852 853 $this->assertEquals( $before - 1, $after ); 854 $this->set_current_user( $old_current_user ); 855 } 856 857 /** 858 * @group groups_get_invites_for_user 859 * @group group_invitations 860 * @group group_membership 861 */ 862 public function test_groups_get_invites_for_user() { 863 $u1 = self::factory()->user->create(); 864 $u2 = self::factory()->user->create(); 865 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 866 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 867 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 868 869 groups_invite_user( array( 870 'user_id' => $u2, 871 'group_id' => $g1, 872 'inviter_id' => $u1, 873 'send_invite' => 1, 874 ) ); 875 groups_invite_user( array( 876 'user_id' => $u2, 877 'group_id' => $g2, 878 'inviter_id' => $u1, 879 'send_invite' => 1, 880 ) ); 881 groups_invite_user( array( 882 'user_id' => $u2, 883 'group_id' => $g3, 884 'inviter_id' => $u1, 885 'send_invite' => 1, 886 ) ); 887 $groups = groups_get_invites_for_user( $u2 ); 888 889 $this->assertEqualSets( array( $g1, $g2, $g3 ), wp_list_pluck( $groups['groups'], 'id' ) ); 890 $this->assertEquals( 3, $groups['total'] ); 891 } 892 893 /** 894 * @group groups_get_invites_for_user 895 * @group group_invitations 896 * @group group_membership 897 */ 898 public function test_groups_get_invites_for_user_infer_user() { 899 $old_current_user = get_current_user_id(); 900 901 $u1 = self::factory()->user->create(); 902 $u2 = self::factory()->user->create(); 903 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 904 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 905 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 906 907 groups_invite_user( array( 908 'user_id' => $u2, 909 'group_id' => $g1, 910 'inviter_id' => $u1, 911 'send_invite' => 1, 912 ) ); 913 groups_invite_user( array( 914 'user_id' => $u2, 915 'group_id' => $g2, 916 'inviter_id' => $u1, 917 'send_invite' => 1, 918 ) ); 919 groups_invite_user( array( 920 'user_id' => $u2, 921 'group_id' => $g3, 922 'inviter_id' => $u1, 923 'send_invite' => 1, 924 ) ); 925 926 $this->set_current_user( $u2 ); 927 $groups = groups_get_invites_for_user(); 928 $this->assertEqualSets( array( $g1, $g2, $g3 ), wp_list_pluck( $groups['groups'], 'id' ) ); 929 930 $this->set_current_user( $old_current_user ); 931 } 932 933 /** 934 * @group groups_get_invites_for_user 935 * @group group_invitations 936 * @group group_membership 937 */ 938 public function test_groups_get_invites_for_user_with_exclude() { 939 $u1 = self::factory()->user->create(); 940 $u2 = self::factory()->user->create(); 941 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 942 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 943 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 944 945 groups_invite_user( array( 946 'user_id' => $u2, 947 'group_id' => $g1, 948 'inviter_id' => $u1, 949 'send_invite' => 1, 950 ) ); 951 groups_invite_user( array( 952 'user_id' => $u2, 953 'group_id' => $g2, 954 'inviter_id' => $u1, 955 'send_invite' => 1, 956 ) ); 957 groups_invite_user( array( 958 'user_id' => $u2, 959 'group_id' => $g3, 960 'inviter_id' => $u1, 961 'send_invite' => 1, 962 ) ); 963 964 $groups = groups_get_invites_for_user( $u2, false, false, array( $g2 ) ); 965 $this->assertEqualSets( array( $g1, $g3 ), wp_list_pluck( $groups['groups'], 'id' ) ); 966 $this->assertEquals( 2, $groups['total'] ); 967 } 968 969 /** 970 * @group groups_get_invite_count_for_user 971 * @group group_invitations 972 * @group group_membership 973 */ 974 public function test_groups_get_invite_count_for_user() { 975 $u1 = self::factory()->user->create(); 976 $u2 = self::factory()->user->create(); 977 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 978 $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 979 $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 980 981 groups_invite_user( array( 982 'user_id' => $u2, 983 'group_id' => $g1, 984 'inviter_id' => $u1, 985 'send_invite' => 1, 986 ) ); 987 groups_invite_user( array( 988 'user_id' => $u2, 989 'group_id' => $g2, 990 'inviter_id' => $u1, 991 'send_invite' => 1, 992 ) ); 993 groups_invite_user( array( 994 'user_id' => $u2, 995 'group_id' => $g3, 996 'inviter_id' => $u1, 997 'send_invite' => 1, 998 ) ); 999 1000 $this->assertEquals( 3, groups_get_invite_count_for_user( $u2 ) ); 1001 } 1002 1003 /** 1004 * @group groups_get_invite_count_for_user 1005 * @group group_invitations 1006 * @group group_membership 1007 */ 1008 public function test_groups_get_invite_count_for_user_ignore_drafts() { 1009 $u1 = self::factory()->user->create(); 1010 $u2 = self::factory()->user->create(); 1011 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1012 1013 // Create draft invitation. 1014 groups_invite_user( array( 1015 'user_id' => $u2, 1016 'group_id' => $g1, 1017 'inviter_id' => $u1, 1018 'date_modified' => bp_core_current_time(), 1019 'is_confirmed' => 0 1020 ) ); 1021 1022 // groups_get_invite_count_for_user should ignore draft invitations. 1023 $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) ); 1024 } 1025 1026 /** 1027 * @group groups_invite_user 1028 * @group group_invitations 1029 * @group group_membership 1030 */ 1031 public function test_groups_invite_user() { 1032 $u1 = self::factory()->user->create(); 1033 $u2 = self::factory()->user->create(); 1034 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1035 1036 // Create draft invitation 1037 groups_invite_user( array( 1038 'user_id' => $u2, 1039 'group_id' => $g1, 1040 'inviter_id' => $u1, 1041 'date_modified' => bp_core_current_time(), 1042 'is_confirmed' => 0 1043 ) ); 1044 1045 // Check that the draft invitation has been created. 1046 $draft = groups_check_user_has_invite( $u2, $g1, 'all' ); 1047 $this->assertTrue( is_numeric( $draft ) && $draft > 0 ); 1048 } 1049 1050 /** 1051 * @group groups_send_invites 1052 * @group group_invitations 1053 * @group group_membership 1054 */ 1055 public function test_groups_send_invites() { 1056 $u1 = self::factory()->user->create(); 1057 $u2 = self::factory()->user->create(); 1058 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1059 1060 // Create draft invitation 1061 groups_invite_user( array( 1062 'user_id' => $u2, 1063 'group_id' => $g1, 1064 'inviter_id' => $u1, 1065 'date_modified' => bp_core_current_time(), 1066 'is_confirmed' => 0 1067 ) ); 1068 1069 // Send the invitation 1070 groups_send_invites( array( 1071 'group_id' => $g1, 1072 'inviter_id' => $u1, 1073 ) ); 1074 1075 // Check that the invitation has been sent. 1076 $sent = groups_check_user_has_invite( $u2, $g1, $type = 'sent' ); 1077 $this->assertTrue( is_numeric( $sent ) && $sent > 0 ); 1078 } 1079 1080 /** 1081 * @group groups_send_invites 1082 * @group group_invitations 1083 * @group group_membership 1084 * @expectedDeprecated groups_send_invites 1085 */ 1086 public function test_groups_send_invites_deprecated_args() { 1087 $u1 = self::factory()->user->create(); 1088 $u2 = self::factory()->user->create(); 1089 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1090 1091 // Create draft invitation 1092 groups_invite_user( array( 1093 'user_id' => $u2, 1094 'group_id' => $g1, 1095 'inviter_id' => $u1, 1096 'date_modified' => bp_core_current_time(), 1097 'is_confirmed' => 0 1098 ) ); 1099 1100 // Send the invitation 1101 groups_send_invites( $u1, $g1 ); 1102 1103 // Check that the invitation has been sent. 1104 $sent = groups_check_user_has_invite( $u2, $g1, $type = 'sent' ); 1105 $this->assertTrue( is_numeric( $sent ) && $sent > 0 ); 1106 } 1107 1108 /** 1109 * @group groups_accept_invite 1110 * @group group_invitations 1111 * @group group_membership 1112 */ 1113 public function test_groups_accept_invite() { 1114 $u1 = self::factory()->user->create(); 1115 $u2 = self::factory()->user->create(); 1116 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1117 1118 // Create draft invitation 1119 groups_invite_user( array( 1120 'user_id' => $u2, 1121 'group_id' => $g1, 1122 'inviter_id' => $u1, 1123 'date_modified' => bp_core_current_time(), 1124 'is_confirmed' => 0, 1125 'send_invite' => 1 1126 ) ); 1127 1128 // Accept the invitation 1129 groups_accept_invite( $u2, $g1 ); 1130 1131 // Check that the user is a member of the group. 1132 $member = groups_is_user_member( $u2, $g1 ); 1133 $this->assertTrue( is_numeric( $member ) && $member > 0 ); 1134 // Check that the invite has been removed. 1135 $invite = groups_check_user_has_invite( $u2, $g1, 'all' ); 1136 $this->assertFalse( $invite ); 1137 } 1138 1139 /** 1140 * @group groups_accept_invite 1141 * @group group_invitations 1142 * @group group_membership 1143 */ 1144 public function test_groups_accept_invite_removes_membership_requests() { 1145 $u1 = self::factory()->user->create(); 1146 $u2 = self::factory()->user->create(); 1147 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1148 1149 // Create draft invitation 1150 groups_invite_user( array( 1151 'user_id' => $u2, 1152 'group_id' => $g1, 1153 'inviter_id' => $u1, 1154 'date_modified' => bp_core_current_time(), 1155 'is_confirmed' => 0 1156 ) ); 1157 1158 // Create membership request 1159 $request_id = groups_send_membership_request( array( 1160 'user_id' => $u2, 1161 'group_id' => $g1, 1162 ) ); 1163 1164 $request = groups_check_for_membership_request( $u2, $g1 ); 1165 1166 $this->assertTrue( is_numeric( $request ) && $request > 0 ); 1167 1168 // Send the invitation 1169 groups_send_invites( array( 1170 'group_id' => $g1, 1171 'inviter_id' => $u1, 1172 ) ); 1173 1174 // Accept the invitation 1175 groups_accept_invite( $u2, $g1 ); 1176 1177 // Check that the membership request has been removed. 1178 $this->assertTrue( 0 == groups_check_for_membership_request( $u2, $g1 ) ); 1179 } 1180 1181 /** 1182 * @group groups_send_invites 1183 * @group group_invitations 1184 * @group group_membership_requests 1185 * @group group_membership 1186 */ 1187 public function test_groups_sent_invite_plus_request_equals_member() { 1188 $u1 = self::factory()->user->create(); 1189 $u2 = self::factory()->user->create(); 1190 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1191 1192 // Create draft invitation 1193 groups_invite_user( array( 1194 'user_id' => $u2, 1195 'group_id' => $g1, 1196 'inviter_id' => $u1, 1197 'date_modified' => bp_core_current_time(), 1198 'is_confirmed' => 0, 1199 'send_invite' => 1 1200 ) ); 1201 1202 // Create membership request 1203 groups_send_membership_request( array( 1204 'user_id' => $u2, 1205 'group_id' => $g1 1206 ) ); 1207 1208 // User should now be a group member 1209 $member = groups_is_user_member( $u2, $g1 ); 1210 $this->assertTrue( is_numeric( $member ) && $member > 0 ); 1211 } 1212 1213 /** 1214 * @group groups_delete_all_group_invites 1215 * @group group_invitations 1216 * @group group_membership 1217 */ 1218 public function test_groups_delete_all_group_invites() { 1219 $u1 = self::factory()->user->create(); 1220 $u2 = self::factory()->user->create(); 1221 $u3 = self::factory()->user->create(); 1222 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1223 1224 groups_invite_user( array( 1225 'user_id' => $u2, 1226 'group_id' => $g1, 1227 'inviter_id' => $u1, 1228 'send_invite' => 1, 1229 ) ); 1230 groups_invite_user( array( 1231 'user_id' => $u3, 1232 'group_id' => $g1, 1233 'inviter_id' => $u1, 1234 'send_invite' => 1, 1235 ) ); 1236 1237 groups_delete_all_group_invites( $g1 ); 1238 1239 // Get group invitations of any type, from any user in the group. 1240 1241 $invitees = groups_get_invites( array( 1242 'group_id' => $g1, 1243 'invite_sent' => 'all', 1244 ) ); 1245 1246 $this->assertTrue( empty( $invitees ) ); 1247 } 1248 1249 /** 1250 * @group groups_invite_user 1251 * @group group_invitations 1252 * @group group_membership 1253 */ 1254 public function test_groups_send_invites_fail_on_empty_group_id() { 1255 $u1 = self::factory()->user->create(); 1256 $u2 = self::factory()->user->create(); 1257 1258 // Create draft invitation with empty inviter_id 1259 $invite_created = groups_invite_user( array( 1260 'user_id' => $u2, 1261 'group_id' => 0, 1262 'inviter_id' => $u1, 1263 'date_modified' => bp_core_current_time(), 1264 'is_confirmed' => 0 1265 ) ); 1266 1267 $this->assertFalse( $invite_created ); 1268 } 1269 1270 /** 1271 * @group groups_invite_user 1272 * @group group_invitations 1273 * @group group_membership 1274 */ 1275 public function test_groups_send_invites_fail_on_empty_user_id() { 1276 $u1 = self::factory()->user->create(); 1277 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1278 1279 // Create draft invitation with empty inviter_id 1280 $invite_created = groups_invite_user( array( 1281 'user_id' => 0, 1282 'group_id' => $g1, 1283 'inviter_id' => $u1, 1284 'date_modified' => bp_core_current_time(), 1285 'is_confirmed' => 0 1286 ) ); 1287 1288 $this->assertFalse( $invite_created ); 1289 } 1290 1291 /** 1292 * @group groups_invite_user 1293 * @group group_invitations 1294 * @group group_membership 1295 */ 1296 public function test_groups_send_invites_fail_on_empty_inviter_id() { 1297 $u1 = self::factory()->user->create(); 1298 $u2 = self::factory()->user->create(); 1299 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1300 1301 // Create draft invitation with empty inviter_id 1302 $invite_created = groups_invite_user( array( 1303 'user_id' => $u2, 1304 'group_id' => $g1, 1305 'inviter_id' => 0, 1306 'date_modified' => bp_core_current_time(), 1307 'is_confirmed' => 0 1308 ) ); 1309 1310 $this->assertFalse( $invite_created ); 1311 } 1312 1313 /** 1314 * @group groups_get_invites_for_group 1315 * @group group_send_invites 1316 * @group group_invitations 1317 * @group group_membership 1318 */ 1319 public function test_groups_get_invites_for_group_with_sent_parameter() { 1320 $u1 = self::factory()->user->create(); 1321 $u2 = self::factory()->user->create(); 1322 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1323 1324 // Create draft invitation 1325 groups_invite_user( array( 1326 'user_id' => $u2, 1327 'group_id' => $g1, 1328 'inviter_id' => $u1, 1329 'date_modified' => bp_core_current_time(), 1330 'is_confirmed' => 0, 1331 'send_invite' => 1 1332 ) ); 1333 1334 // Default groups_get_invites_for_group() call 1335 $i = groups_get_invites_for_group( $u1, $g1 ); 1336 $this->assertEqualSets( array( $u2 ), $i ); 1337 1338 // Fetch users whose invites have been sent out; should be the same as above. 1339 $i = groups_get_invites_for_group( $u1, $g1 ); 1340 $this->assertEqualSets( array( $u2 ), $i ); 1341 1342 // Fetch users whose invites haven't been sent yet. 1343 $i = groups_get_invites_for_group( $u1, $g1, 0 ); 1344 $this->assertEmpty( $i ); 1345 } 1346 1347 /** 1348 * @group groups_send_membership_request 1349 * @group group_membership_requests 1350 * @group group_membership 1351 */ 1352 public function test_groups_send_membership_request() { 1353 $u1 = self::factory()->user->create(); 1354 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1355 1356 // Create membership request 1357 groups_send_membership_request( array( 1358 'user_id' => $u1, 1359 'group_id' => $g1 1360 ) ); 1361 1362 $request = groups_check_for_membership_request( $u1, $g1 ); 1363 $this->assertTrue( is_numeric( $request ) && $request > 0 ); 1364 } 1365 1366 /** 1367 * @group groups_send_membership_request 1368 * @group group_membership_requests 1369 * @group group_membership 1370 * @expectedDeprecated groups_send_membership_request 1371 */ 1372 public function test_groups_send_membership_request_deprecated_args() { 1373 $u1 = self::factory()->user->create(); 1374 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1375 1376 // Create membership request 1377 groups_send_membership_request( $u1, $g1 ); 1378 1379 $request = groups_check_for_membership_request( $u1, $g1 ); 1380 $this->assertTrue( is_numeric( $request ) && $request > 0 ); 1381 } 1382 1383 /** 1384 * @group groups_accept_membership_request 1385 * @group group_membership_requests 1386 * @group group_membership 1387 */ 1388 public function test_groups_accept_membership_request_by_membership_id() { 1389 $u1 = self::factory()->user->create(); 1390 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1391 1392 // Create membership request 1393 groups_send_membership_request( array( 1394 'user_id' => $u1, 1395 'group_id' => $g1 1396 ) ); 1397 1398 // Get group invitations of any type, from any user in the group. 1399 $member = new BP_Groups_Member( $u1, $g1 ); 1400 1401 groups_accept_membership_request( false, $u1, $g1 ); 1402 1403 // User should now be a group member. 1404 $member = groups_is_user_member( $u1, $g1 ); 1405 1406 $this->assertTrue( is_numeric( $member ) && $member > 0 ); 1407 } 1408 1409 /** 1410 * @group groups_accept_membership_request 1411 * @group group_membership_requests 1412 * @group group_membership 1413 */ 1414 public function test_groups_accept_membership_request_by_user_id_group_id() { 1415 $u1 = self::factory()->user->create(); 1416 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1417 1418 // Create membership request 1419 groups_send_membership_request( array( 1420 'user_id' => $u1, 1421 'group_id' => $g1 1422 ) ); 1423 1424 groups_accept_membership_request( null, $u1, $g1 ); 1425 1426 // User should now be a group member 1427 $member = groups_is_user_member( $u1, $g1 ); 1428 $this->assertTrue( is_numeric( $member ) && $member > 0 ); 1429 } 1430 1431 /** 1432 * @group groups_send_invites 1433 * @group group_invitations 1434 * @group group_membership_requests 1435 * @group group_membership 1436 */ 1437 public function test_groups_membership_request_plus_invite_equals_member() { 1438 $u1 = self::factory()->user->create(); 1439 $u2 = self::factory()->user->create(); 1440 $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 1441 1442 // Create membership request 1443 groups_send_membership_request( array( 1444 'user_id' => $u2, 1445 'group_id' => $g1 1446 ) ); 1447 1448 // Create draft invitation 1449 groups_invite_user( array( 1450 'user_id' => $u2, 1451 'group_id' => $g1, 1452 'inviter_id' => $u1, 1453 'date_modified' => bp_core_current_time(), 1454 'is_confirmed' => 0, 1455 'send_invite' => 1 1456 ) ); 1457 1458 // User should now be a group member 1459 $member = groups_is_user_member( $u2, $g1 ); 1460 $this->assertTrue( is_numeric( $member ) && $member > 0 ); 1461 } 1462 1463 /** 1464 * @group groups_accept_all_pending_membership_requests 1465 * @group group_membership_requests 1466 * @group group_membership 1467 */ 1468 public function test_groups_accept_all_pending_membership_requests() { 1469 $u1 = self::factory()->user->create(); 1470 $u2 = self::factory()->user->create(); 1471 $u3 = self::factory()->user->create(); 1472 $g1 = self::factory()->group->create( array( 'status' => 'private' ) ); 1473 1474 // Create membership request 1475 groups_send_membership_request( array( 1476 'user_id' => $u1, 1477 'group_id' => $g1 1478 ) ); 1479 groups_send_membership_request( array( 1480 'user_id' => $u2, 1481 'group_id' => $g1 1482 ) ); 1483 groups_send_membership_request( array( 1484 'user_id' => $u3, 1485 'group_id' => $g1 1486 ) ); 1487 1488 groups_accept_all_pending_membership_requests( $g1 ); 1489 1490 // All users should now be group members. 1491 $members = new BP_Group_Member_Query( array( 'group_id' => $g1 ) ); 1492 $this->assertEqualSets( array( $u1, $u2, $u3 ), $members->user_ids ); 1493 } 1494 1495 /** 1496 * @group total_group_count 1497 * @ticket BP6813 1498 */ 1499 public function test_total_group_count_should_return_integer() { 1500 $this->assertInternalType( 'int', BP_Groups_Member::total_group_count( 123 ) ); 1501 } 1502 1503 /** 1504 * @group get_memberships_by_id 1505 */ 1506 public function test_get_memberships_by_id_with_single_id() { 1507 $users = self::factory()->user->create_many( 2 ); 1508 $groups = self::factory()->group->create_many( 2 ); 1509 1510 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 1511 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 1512 1513 $found = BP_Groups_Member::get_memberships_by_id( $m0 ); 1514 1515 $this->assertSame( 1, count( $found ) ); 1516 $this->assertEquals( $m0, $found[0]->id ); 1517 } 1518 1519 /** 1520 * @group get_memberships_by_id 1521 */ 1522 public function test_get_memberships_by_id_with_multiple_ids() { 1523 $users = self::factory()->user->create_many( 2 ); 1524 $groups = self::factory()->group->create_many( 2 ); 1525 1526 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 1527 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 1528 1529 $found = BP_Groups_Member::get_memberships_by_id( array( $m0, $m1 ) ); 1530 1531 $this->assertSame( 2, count( $found ) ); 1532 $this->assertEqualSets( array( $m0, $m1 ), wp_list_pluck( $found, 'id' ) ); 1533 } 1534 1535 /** 1536 * @ticket BP7382 1537 */ 1538 public function test_user_property_should_be_accessible() { 1539 $user = self::factory()->user->create(); 1540 $group = self::factory()->group->create(); 1541 1542 $this->add_user_to_group( $user, $group ); 1543 1544 $membership = new BP_Groups_Member( $user, $group ); 1545 1546 $user_obj = $membership->user; 1547 1548 $this->assertInstanceOf( 'BP_Core_User', $user_obj ); 1549 $this->assertEquals( $user, $user_obj->id ); 1550 } 1551 1552 /** 1553 * @group get_group_moderator_ids 1554 */ 1555 public function test_groups_get_group_mods_bad_id() { 1556 $mods = groups_get_group_mods( null ); 1557 1558 $this->assertTrue( is_array( $mods ) && empty( $mods ) ); 1559 } 1560 1561 /** 1562 * @group get_group_moderator_ids 1563 */ 1564 public function test_groups_get_group_admins_bad_id() { 1565 $admins = groups_get_group_admins( null ); 1566 1567 $this->assertTrue( is_array( $admins ) && empty( $admins ) ); 1568 } 1569 1570 /** 1571 * @ticket BP7859 1572 */ 1573 public function test_get_user_memberships_type_membership() { 1574 groups_join_group( self::$group_ids[0], self::$user_ids[0] ); 1575 1576 $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array( 1577 'type' => 'membership', 1578 ) ); 1579 1580 $this->assertCount( 1, $memberships ); 1581 $this->assertSame( self::$group_ids[0], $memberships[0]->group_id ); 1582 } 1583 1584 /** 1585 * @ticket BP7476 1586 */ 1587 public function test_delete_all_for_user() { 1588 $new_user = self::factory()->user->create(); 1589 1590 $admin_users = get_users( array( 1591 'blog_id' => bp_get_root_blog_id(), 1592 'fields' => 'id', 1593 'number' => 1, 1594 'orderby' => 'ID', 1595 'role' => 'administrator', 1596 ) ); 1597 1598 $admin_user = (int) $admin_users[0]; 1599 1600 // Sole admin of group. 1601 $new_group = self::factory()->group->create( array( 1602 'creator_id' => $new_user, 1603 ) ); 1604 1605 // One of two group admins. 1606 groups_join_group( self::$group_ids[0], $new_user ); 1607 $m1 = new BP_Groups_Member( $new_user, self::$group_ids[0] ); 1608 $m1->promote( 'admin' ); 1609 1610 // Not an admin. 1611 groups_join_group( self::$group_ids[1], $new_user ); 1612 $m2 = new BP_Groups_Member( $new_user, self::$group_ids[1] ); 1613 1614 BP_Groups_Member::delete_all_for_user( $new_user ); 1615 1616 $new_group_members = BP_Groups_Member::get_group_administrator_ids( $new_group ); 1617 $this->assertSame( array( $admin_user ), wp_list_pluck( $new_group_members, 'user_id' ) ); 1618 1619 $g0_members = BP_Groups_Member::get_group_administrator_ids( self::$group_ids[0] ); 1620 $this->assertSame( array( self::$user_ids[3] ), wp_list_pluck( $g0_members, 'user_id' ) ); 1621 1622 $g1_members = BP_Groups_Member::get_group_administrator_ids( self::$group_ids[1] ); 1623 $this->assertSame( array( self::$user_ids[3] ), wp_list_pluck( $g1_members, 'user_id' ) ); 1624 } 1625 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Dec 14 01:00:55 2024 | Cross-referenced by PHPXref 0.7.1 |