[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/groups/ -> class-bp-groups-member.php (source)

   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      }
 891  
 892      /**
 893       * @group groups_get_invites_for_user
 894       * @group group_invitations
 895       * @group group_membership
 896       */
 897  	public function test_groups_get_invites_for_user_infer_user() {
 898          $old_current_user = get_current_user_id();
 899  
 900          $u1 = self::factory()->user->create();
 901          $u2 = self::factory()->user->create();
 902          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 903          $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 904          $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 905  
 906          groups_invite_user( array(
 907              'user_id' => $u2,
 908              'group_id' => $g1,
 909              'inviter_id' => $u1,
 910              'send_invite' => 1,
 911          ) );
 912          groups_invite_user( array(
 913              'user_id' => $u2,
 914              'group_id' => $g2,
 915              'inviter_id' => $u1,
 916              'send_invite' => 1,
 917          ) );
 918          groups_invite_user( array(
 919              'user_id' => $u2,
 920              'group_id' => $g3,
 921              'inviter_id' => $u1,
 922              'send_invite' => 1,
 923          ) );
 924  
 925          $this->set_current_user( $u2 );
 926          $groups = groups_get_invites_for_user();
 927          $this->assertEqualSets( array( $g1, $g2, $g3 ), wp_list_pluck( $groups['groups'], 'id' ) );
 928  
 929          $this->set_current_user( $old_current_user );
 930      }
 931  
 932      /**
 933       * @group groups_get_invites_for_user
 934       * @group group_invitations
 935       * @group group_membership
 936       */
 937  	public function test_groups_get_invites_for_user_with_exclude() {
 938          $u1 = self::factory()->user->create();
 939          $u2 = self::factory()->user->create();
 940          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 941          $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 942          $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 943  
 944          groups_invite_user( array(
 945              'user_id' => $u2,
 946              'group_id' => $g1,
 947              'inviter_id' => $u1,
 948              'send_invite' => 1,
 949          ) );
 950          groups_invite_user( array(
 951              'user_id' => $u2,
 952              'group_id' => $g2,
 953              'inviter_id' => $u1,
 954              'send_invite' => 1,
 955          ) );
 956          groups_invite_user( array(
 957              'user_id' => $u2,
 958              'group_id' => $g3,
 959              'inviter_id' => $u1,
 960              'send_invite' => 1,
 961          ) );
 962  
 963          $groups = groups_get_invites_for_user( $u2, false, false, array( $g2 ) );
 964          $this->assertEqualSets( array( $g1, $g3 ), wp_list_pluck( $groups['groups'], 'id' ) );
 965      }
 966  
 967      /**
 968       * @group groups_get_invite_count_for_user
 969       * @group group_invitations
 970       * @group group_membership
 971       */
 972  	public function test_groups_get_invite_count_for_user() {
 973          $u1 = self::factory()->user->create();
 974          $u2 = self::factory()->user->create();
 975          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 976          $g2 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 977          $g3 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 978  
 979          groups_invite_user( array(
 980              'user_id' => $u2,
 981              'group_id' => $g1,
 982              'inviter_id' => $u1,
 983              'send_invite' => 1,
 984          ) );
 985          groups_invite_user( array(
 986              'user_id' => $u2,
 987              'group_id' => $g2,
 988              'inviter_id' => $u1,
 989              'send_invite' => 1,
 990          ) );
 991          groups_invite_user( array(
 992              'user_id' => $u2,
 993              'group_id' => $g3,
 994              'inviter_id' => $u1,
 995              'send_invite' => 1,
 996          ) );
 997  
 998          $this->assertEquals( 3, groups_get_invite_count_for_user( $u2 ) );
 999      }
1000  
1001      /**
1002       * @group groups_get_invite_count_for_user
1003       * @group group_invitations
1004       * @group group_membership
1005       */
1006      public function test_groups_get_invite_count_for_user_ignore_drafts() {
1007          $u1 = self::factory()->user->create();
1008          $u2 = self::factory()->user->create();
1009          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1010  
1011          // Create draft invitation.
1012          groups_invite_user( array(
1013              'user_id'       => $u2,
1014              'group_id'      => $g1,
1015              'inviter_id'    => $u1,
1016              'date_modified' => bp_core_current_time(),
1017              'is_confirmed'  => 0
1018          ) );
1019  
1020          // groups_get_invite_count_for_user should ignore draft invitations.
1021          $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) );
1022      }
1023  
1024      /**
1025       * @group groups_invite_user
1026       * @group group_invitations
1027       * @group group_membership
1028       */
1029  	public function test_groups_invite_user() {
1030          $u1 = self::factory()->user->create();
1031          $u2 = self::factory()->user->create();
1032          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1033  
1034          // Create draft invitation
1035          groups_invite_user( array(
1036              'user_id'       => $u2,
1037              'group_id'      => $g1,
1038              'inviter_id'    => $u1,
1039              'date_modified' => bp_core_current_time(),
1040              'is_confirmed'  => 0
1041          ) );
1042  
1043          // Check that the draft invitation has been created.
1044          $draft = groups_check_user_has_invite( $u2, $g1, 'all' );
1045          $this->assertTrue( is_numeric( $draft ) && $draft > 0 );
1046      }
1047  
1048      /**
1049       * @group groups_send_invites
1050       * @group group_invitations
1051       * @group group_membership
1052       */
1053  	public function test_groups_send_invites() {
1054          $u1 = self::factory()->user->create();
1055          $u2 = self::factory()->user->create();
1056          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1057  
1058          // Create draft invitation
1059          groups_invite_user( array(
1060              'user_id'       => $u2,
1061              'group_id'      => $g1,
1062              'inviter_id'    => $u1,
1063              'date_modified' => bp_core_current_time(),
1064              'is_confirmed'  => 0
1065          ) );
1066  
1067          // Send the invitation
1068          groups_send_invites( array(
1069              'group_id'   => $g1,
1070              'inviter_id' => $u1,
1071          ) );
1072  
1073          // Check that the invitation has been sent.
1074          $sent = groups_check_user_has_invite( $u2, $g1, $type = 'sent' );
1075          $this->assertTrue( is_numeric( $sent ) && $sent > 0 );
1076      }
1077  
1078      /**
1079       * @group groups_send_invites
1080       * @group group_invitations
1081       * @group group_membership
1082       * @expectedDeprecated groups_send_invites
1083       */
1084  	public function test_groups_send_invites_deprecated_args() {
1085          $u1 = self::factory()->user->create();
1086          $u2 = self::factory()->user->create();
1087          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1088  
1089          // Create draft invitation
1090          groups_invite_user( array(
1091              'user_id'       => $u2,
1092              'group_id'      => $g1,
1093              'inviter_id'    => $u1,
1094              'date_modified' => bp_core_current_time(),
1095              'is_confirmed'  => 0
1096          ) );
1097  
1098          // Send the invitation
1099          groups_send_invites( $u1, $g1 );
1100  
1101          // Check that the invitation has been sent.
1102          $sent = groups_check_user_has_invite( $u2, $g1, $type = 'sent' );
1103          $this->assertTrue( is_numeric( $sent ) && $sent > 0 );
1104      }
1105  
1106      /**
1107       * @group groups_accept_invite
1108       * @group group_invitations
1109       * @group group_membership
1110       */
1111  	public function test_groups_accept_invite() {
1112          $u1 = self::factory()->user->create();
1113          $u2 = self::factory()->user->create();
1114          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1115  
1116          // Create draft invitation
1117          groups_invite_user( array(
1118              'user_id'       => $u2,
1119              'group_id'      => $g1,
1120              'inviter_id'    => $u1,
1121              'date_modified' => bp_core_current_time(),
1122              'is_confirmed'  => 0,
1123              'send_invite'   => 1
1124          ) );
1125  
1126          // Accept the invitation
1127          groups_accept_invite( $u2, $g1 );
1128  
1129          // Check that the user is a member of the group.
1130          $member = groups_is_user_member( $u2, $g1 );
1131          $this->assertTrue( is_numeric( $member ) && $member > 0 );
1132          // Check that the invite has been removed.
1133          $invite = groups_check_user_has_invite( $u2, $g1, 'all' );
1134          $this->assertFalse( $invite );
1135      }
1136  
1137      /**
1138       * @group groups_accept_invite
1139       * @group group_invitations
1140       * @group group_membership
1141       */
1142      public function test_groups_accept_invite_removes_membership_requests() {
1143          $u1 = self::factory()->user->create();
1144          $u2 = self::factory()->user->create();
1145          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1146  
1147          // Create draft invitation
1148          groups_invite_user( array(
1149              'user_id'       => $u2,
1150              'group_id'      => $g1,
1151              'inviter_id'    => $u1,
1152              'date_modified' => bp_core_current_time(),
1153              'is_confirmed'  => 0
1154          ) );
1155  
1156          // Create membership request
1157          $request_id = groups_send_membership_request( array(
1158              'user_id'       => $u2,
1159              'group_id'      => $g1,
1160          ) );
1161  
1162          $request = groups_check_for_membership_request( $u2, $g1 );
1163  
1164          $this->assertTrue( is_numeric( $request ) && $request > 0 );
1165  
1166          // Send the invitation
1167          groups_send_invites( array(
1168              'group_id'   => $g1,
1169              'inviter_id' => $u1,
1170          ) );
1171  
1172          // Accept the invitation
1173          groups_accept_invite( $u2, $g1 );
1174  
1175          // Check that the membership request has been removed.
1176          $this->assertTrue( 0 == groups_check_for_membership_request( $u2, $g1 ) );
1177      }
1178  
1179      /**
1180       * @group groups_send_invites
1181       * @group group_invitations
1182       * @group group_membership_requests
1183       * @group group_membership
1184       */
1185      public function test_groups_sent_invite_plus_request_equals_member() {
1186          $u1 = self::factory()->user->create();
1187          $u2 = self::factory()->user->create();
1188          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1189  
1190          // Create draft invitation
1191          groups_invite_user( array(
1192              'user_id'       => $u2,
1193              'group_id'      => $g1,
1194              'inviter_id'    => $u1,
1195              'date_modified' => bp_core_current_time(),
1196              'is_confirmed'  => 0,
1197              'send_invite'   => 1
1198          ) );
1199  
1200          // Create membership request
1201          groups_send_membership_request( array(
1202              'user_id' => $u2,
1203              'group_id' => $g1
1204          ) );
1205  
1206          // User should now be a group member
1207          $member = groups_is_user_member( $u2, $g1 );
1208          $this->assertTrue( is_numeric( $member ) && $member > 0 );
1209      }
1210  
1211      /**
1212       * @group groups_delete_all_group_invites
1213       * @group group_invitations
1214       * @group group_membership
1215       */
1216  	public function test_groups_delete_all_group_invites() {
1217          $u1 = self::factory()->user->create();
1218          $u2 = self::factory()->user->create();
1219          $u3 = self::factory()->user->create();
1220          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1221  
1222          groups_invite_user( array(
1223              'user_id' => $u2,
1224              'group_id' => $g1,
1225              'inviter_id' => $u1,
1226              'send_invite' => 1,
1227          ) );
1228          groups_invite_user( array(
1229              'user_id' => $u3,
1230              'group_id' => $g1,
1231              'inviter_id' => $u1,
1232              'send_invite' => 1,
1233          ) );
1234  
1235          groups_delete_all_group_invites( $g1 );
1236  
1237          // Get group invitations of any type, from any user in the group.
1238  
1239          $invitees = groups_get_invites(    array(
1240              'group_id'     => $g1,
1241              'invite_sent'  => 'all',
1242          ) );
1243  
1244          $this->assertTrue( empty( $invitees ) );
1245      }
1246  
1247      /**
1248       * @group groups_invite_user
1249       * @group group_invitations
1250       * @group group_membership
1251       */
1252      public function test_groups_send_invites_fail_on_empty_group_id() {
1253          $u1 = self::factory()->user->create();
1254          $u2 = self::factory()->user->create();
1255  
1256          // Create draft invitation with empty inviter_id
1257          $invite_created = groups_invite_user( array(
1258              'user_id'       => $u2,
1259              'group_id'      => 0,
1260              'inviter_id'    => $u1,
1261              'date_modified' => bp_core_current_time(),
1262              'is_confirmed'  => 0
1263          ) );
1264  
1265          $this->assertFalse( $invite_created );
1266      }
1267  
1268      /**
1269       * @group groups_invite_user
1270       * @group group_invitations
1271       * @group group_membership
1272       */
1273      public function test_groups_send_invites_fail_on_empty_user_id() {
1274          $u1 = self::factory()->user->create();
1275          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1276  
1277          // Create draft invitation with empty inviter_id
1278          $invite_created = groups_invite_user( array(
1279              'user_id'       => 0,
1280              'group_id'      => $g1,
1281              'inviter_id'    => $u1,
1282              'date_modified' => bp_core_current_time(),
1283              'is_confirmed'  => 0
1284          ) );
1285  
1286          $this->assertFalse( $invite_created );
1287      }
1288  
1289      /**
1290       * @group groups_invite_user
1291       * @group group_invitations
1292       * @group group_membership
1293       */
1294      public function test_groups_send_invites_fail_on_empty_inviter_id() {
1295          $u1 = self::factory()->user->create();
1296          $u2 = self::factory()->user->create();
1297          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1298  
1299          // Create draft invitation with empty inviter_id
1300          $invite_created = groups_invite_user( array(
1301              'user_id'       => $u2,
1302              'group_id'      => $g1,
1303              'inviter_id'    => 0,
1304              'date_modified' => bp_core_current_time(),
1305              'is_confirmed'  => 0
1306          ) );
1307  
1308          $this->assertFalse( $invite_created );
1309      }
1310  
1311      /**
1312       * @group groups_get_invites_for_group
1313       * @group group_send_invites
1314       * @group group_invitations
1315       * @group group_membership
1316       */
1317      public function test_groups_get_invites_for_group_with_sent_parameter() {
1318          $u1 = self::factory()->user->create();
1319          $u2 = self::factory()->user->create();
1320          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1321  
1322          // Create draft invitation
1323          groups_invite_user( array(
1324              'user_id'       => $u2,
1325              'group_id'      => $g1,
1326              'inviter_id'    => $u1,
1327              'date_modified' => bp_core_current_time(),
1328              'is_confirmed'  => 0,
1329              'send_invite'   => 1
1330          ) );
1331  
1332          // Default groups_get_invites_for_group() call
1333          $i = groups_get_invites_for_group( $u1, $g1 );
1334          $this->assertEqualSets( array( $u2 ), $i );
1335  
1336          // Fetch users whose invites have been sent out; should be the same as above.
1337          $i = groups_get_invites_for_group( $u1, $g1 );
1338          $this->assertEqualSets( array( $u2 ), $i );
1339  
1340          // Fetch users whose invites haven't been sent yet.
1341          $i = groups_get_invites_for_group( $u1, $g1, 0 );
1342          $this->assertEmpty( $i );
1343      }
1344  
1345      /**
1346       * @group groups_send_membership_request
1347       * @group group_membership_requests
1348       * @group group_membership
1349       */
1350  	public function test_groups_send_membership_request() {
1351          $u1 = self::factory()->user->create();
1352          $g1 = self::factory()->group->create( array( 'status' => 'private' ) );
1353  
1354          // Create membership request
1355          groups_send_membership_request( array(
1356              'user_id' => $u1,
1357              'group_id' => $g1
1358          ) );
1359  
1360          $request = groups_check_for_membership_request( $u1, $g1 );
1361          $this->assertTrue( is_numeric( $request ) && $request > 0 );
1362      }
1363  
1364      /**
1365       * @group groups_send_membership_request
1366       * @group group_membership_requests
1367       * @group group_membership
1368       * @expectedDeprecated groups_send_membership_request
1369       */
1370      public function test_groups_send_membership_request_deprecated_args() {
1371          $u1 = self::factory()->user->create();
1372          $g1 = self::factory()->group->create( array( 'status' => 'private' ) );
1373  
1374          // Create membership request
1375          groups_send_membership_request( $u1, $g1 );
1376  
1377          $request = groups_check_for_membership_request( $u1, $g1 );
1378          $this->assertTrue( is_numeric( $request ) && $request > 0 );
1379      }
1380  
1381      /**
1382       * @group groups_accept_membership_request
1383       * @group group_membership_requests
1384       * @group group_membership
1385       */
1386      public function test_groups_accept_membership_request_by_membership_id() {
1387          $u1 = self::factory()->user->create();
1388          $g1 = self::factory()->group->create( array( 'status' => 'private' ) );
1389  
1390          // Create membership request
1391          groups_send_membership_request( array(
1392              'user_id' => $u1,
1393              'group_id' => $g1
1394          ) );
1395  
1396          // Get group invitations of any type, from any user in the group.
1397          $member = new BP_Groups_Member( $u1, $g1 );
1398  
1399          groups_accept_membership_request( false, $u1, $g1 );
1400  
1401          // User should now be a group member.
1402          $member = groups_is_user_member( $u1, $g1 );
1403  
1404          $this->assertTrue( is_numeric( $member ) && $member > 0 );
1405      }
1406  
1407      /**
1408       * @group groups_accept_membership_request
1409       * @group group_membership_requests
1410       * @group group_membership
1411       */
1412      public function test_groups_accept_membership_request_by_user_id_group_id() {
1413          $u1 = self::factory()->user->create();
1414          $g1 = self::factory()->group->create( array( 'status' => 'private' ) );
1415  
1416          // Create membership request
1417          groups_send_membership_request( array(
1418              'user_id' => $u1,
1419              'group_id' => $g1
1420          ) );
1421  
1422          groups_accept_membership_request( null, $u1, $g1 );
1423  
1424          // User should now be a group member
1425          $member = groups_is_user_member( $u1, $g1 );
1426          $this->assertTrue( is_numeric( $member ) && $member > 0 );
1427      }
1428  
1429      /**
1430       * @group groups_send_invites
1431       * @group group_invitations
1432       * @group group_membership_requests
1433       * @group group_membership
1434       */
1435      public function test_groups_membership_request_plus_invite_equals_member() {
1436          $u1 = self::factory()->user->create();
1437          $u2 = self::factory()->user->create();
1438          $g1 = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
1439  
1440          // Create membership request
1441          groups_send_membership_request( array(
1442              'user_id' => $u2,
1443              'group_id' => $g1
1444          ) );
1445  
1446          // Create draft invitation
1447          groups_invite_user( array(
1448              'user_id'       => $u2,
1449              'group_id'      => $g1,
1450              'inviter_id'    => $u1,
1451              'date_modified' => bp_core_current_time(),
1452              'is_confirmed'  => 0,
1453              'send_invite'   => 1
1454          ) );
1455  
1456          // User should now be a group member
1457          $member = groups_is_user_member( $u2, $g1 );
1458          $this->assertTrue( is_numeric( $member ) && $member > 0 );
1459      }
1460  
1461      /**
1462       * @group groups_accept_all_pending_membership_requests
1463       * @group group_membership_requests
1464       * @group group_membership
1465       */
1466      public function test_groups_accept_all_pending_membership_requests() {
1467          $u1 = self::factory()->user->create();
1468          $u2 = self::factory()->user->create();
1469          $u3 = self::factory()->user->create();
1470          $g1 = self::factory()->group->create( array( 'status' => 'private' ) );
1471  
1472          // Create membership request
1473          groups_send_membership_request( array(
1474              'user_id' => $u1,
1475              'group_id' => $g1
1476          ) );
1477          groups_send_membership_request( array(
1478              'user_id' => $u2,
1479              'group_id' => $g1
1480          ) );
1481          groups_send_membership_request( array(
1482              'user_id' => $u3,
1483              'group_id' => $g1
1484          ) );
1485  
1486          groups_accept_all_pending_membership_requests( $g1 );
1487  
1488          // All users should now be group members.
1489          $members = new BP_Group_Member_Query( array( 'group_id' => $g1 ) );
1490          $this->assertEqualSets( array( $u1, $u2, $u3 ), $members->user_ids );
1491      }
1492  
1493      /**
1494       * @group total_group_count
1495       * @ticket BP6813
1496       */
1497  	public function test_total_group_count_should_return_integer() {
1498          $this->assertInternalType( 'int', BP_Groups_Member::total_group_count( 123 ) );
1499      }
1500  
1501      /**
1502       * @group get_memberships_by_id
1503       */
1504  	public function test_get_memberships_by_id_with_single_id() {
1505          $users = self::factory()->user->create_many( 2 );
1506          $groups = self::factory()->group->create_many( 2 );
1507  
1508          $m0 = $this->add_user_to_group( $users[0], $groups[0] );
1509          $m1 = $this->add_user_to_group( $users[1], $groups[1] );
1510  
1511          $found = BP_Groups_Member::get_memberships_by_id( $m0 );
1512  
1513          $this->assertSame( 1, count( $found ) );
1514          $this->assertEquals( $m0, $found[0]->id );
1515      }
1516  
1517      /**
1518       * @group get_memberships_by_id
1519       */
1520  	public function test_get_memberships_by_id_with_multiple_ids() {
1521          $users = self::factory()->user->create_many( 2 );
1522          $groups = self::factory()->group->create_many( 2 );
1523  
1524          $m0 = $this->add_user_to_group( $users[0], $groups[0] );
1525          $m1 = $this->add_user_to_group( $users[1], $groups[1] );
1526  
1527          $found = BP_Groups_Member::get_memberships_by_id( array( $m0, $m1 ) );
1528  
1529          $this->assertSame( 2, count( $found ) );
1530          $this->assertEqualSets( array( $m0, $m1 ), wp_list_pluck( $found, 'id' ) );
1531      }
1532  
1533      /**
1534       * @ticket BP7382
1535       */
1536  	public function test_user_property_should_be_accessible() {
1537          $user = self::factory()->user->create();
1538          $group = self::factory()->group->create();
1539  
1540          $this->add_user_to_group( $user, $group );
1541  
1542          $membership = new BP_Groups_Member( $user, $group );
1543  
1544          $user_obj = $membership->user;
1545  
1546          $this->assertInstanceOf( 'BP_Core_User', $user_obj );
1547          $this->assertEquals( $user, $user_obj->id );
1548      }
1549  
1550      /**
1551       * @group get_group_moderator_ids
1552       */
1553  	public function test_groups_get_group_mods_bad_id() {
1554          $mods = groups_get_group_mods( null );
1555  
1556          $this->assertTrue( is_array( $mods ) && empty( $mods ) );
1557      }
1558  
1559      /**
1560       * @group get_group_moderator_ids
1561       */
1562  	public function test_groups_get_group_admins_bad_id() {
1563          $admins = groups_get_group_admins( null );
1564  
1565          $this->assertTrue( is_array( $admins ) && empty( $admins ) );
1566      }
1567  
1568      /**
1569       * @ticket BP7859
1570       */
1571  	public function test_get_user_memberships_type_membership() {
1572          groups_join_group( self::$group_ids[0], self::$user_ids[0] );
1573  
1574          $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(
1575              'type' => 'membership',
1576          ) );
1577  
1578          $this->assertCount( 1, $memberships );
1579          $this->assertSame( self::$group_ids[0], $memberships[0]->group_id );
1580      }
1581  
1582      /**
1583       * @ticket BP7476
1584       */
1585  	public function test_delete_all_for_user() {
1586          $new_user = self::factory()->user->create();
1587  
1588          $admin_users = get_users( array(
1589              'blog_id' => bp_get_root_blog_id(),
1590              'fields'  => 'id',
1591              'number'  => 1,
1592              'orderby' => 'ID',
1593              'role'    => 'administrator',
1594          ) );
1595  
1596          $admin_user = (int) $admin_users[0];
1597  
1598          // Sole admin of group.
1599          $new_group = self::factory()->group->create( array(
1600              'creator_id' => $new_user,
1601          ) );
1602  
1603          // One of two group admins.
1604          groups_join_group( self::$group_ids[0], $new_user );
1605          $m1 = new BP_Groups_Member( $new_user, self::$group_ids[0] );
1606          $m1->promote( 'admin' );
1607  
1608          // Not an admin.
1609          groups_join_group( self::$group_ids[1], $new_user );
1610          $m2 = new BP_Groups_Member( $new_user, self::$group_ids[1] );
1611  
1612          BP_Groups_Member::delete_all_for_user( $new_user );
1613  
1614          $new_group_members = BP_Groups_Member::get_group_administrator_ids( $new_group );
1615          $this->assertSame( array( $admin_user ), wp_list_pluck( $new_group_members, 'user_id' ) );
1616  
1617          $g0_members = BP_Groups_Member::get_group_administrator_ids( self::$group_ids[0] );
1618          $this->assertSame( array( self::$user_ids[3] ), wp_list_pluck( $g0_members, 'user_id' ) );
1619  
1620          $g1_members = BP_Groups_Member::get_group_administrator_ids( self::$group_ids[1] );
1621          $this->assertSame( array( self::$user_ids[3] ), wp_list_pluck( $g1_members, 'user_id' ) );
1622      }
1623  }


Generated: Mon Oct 21 01:01:35 2019 Cross-referenced by PHPXref 0.7.1