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


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1