[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/groups/ -> functions.php (source)

   1  <?php
   2  
   3  /**
   4   * @group groups
   5   * @group functions
   6   */
   7  class BP_Tests_Groups_Functions extends BP_UnitTestCase {
   8      static public $group_ids;
   9      static public $user_ids;
  10  
  11  	static public function wpSetUpBeforeClass( $factory ) {
  12          self::$user_ids  = $factory->user->create_many( 3 );
  13          self::$group_ids = $factory->group->create_many( 2, array(
  14              'creator_id' => self::$user_ids[2],
  15          ) );
  16      }
  17  
  18  	static public 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_creating_new_group_as_authenticated_user() {
  24          $u = self::factory()->user->create();
  25          wp_set_current_user( $u );
  26  
  27          self::factory()->group->create();
  28  
  29          // Remove the following lines when you implement this test.
  30          $this->markTestIncomplete(
  31              'This test has not been implemented yet.'
  32          );
  33      }
  34  
  35      /**
  36       * @group total_group_count
  37       * @group groups_join_group
  38       */
  39  	public function test_total_group_count_groups_join_group() {
  40          $u1 = self::factory()->user->create();
  41          $u2 = self::factory()->user->create();
  42          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
  43  
  44          groups_join_group( $g, $u2 );
  45          $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
  46      }
  47  
  48      /**
  49       * @group total_group_count
  50       * @group groups_leave_group
  51       */
  52  	public function test_total_group_count_groups_leave_group() {
  53          $u1 = self::factory()->user->create();
  54          $u2 = self::factory()->user->create();
  55          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
  56          $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
  57          groups_join_group( $g1, $u2 );
  58          groups_join_group( $g2, $u2 );
  59  
  60          // Set the current user so the leave group request goes through.
  61          $this->set_current_user( $u2 );
  62          groups_leave_group( $g1, $u2 );
  63          $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
  64      }
  65  
  66      /**
  67       * @group total_group_count
  68       * @group groups_ban_member
  69       */
  70  	public function test_total_group_count_groups_ban_member() {
  71          $u1 = self::factory()->user->create();
  72          $u2 = self::factory()->user->create();
  73          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
  74          $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
  75          groups_join_group( $g1, $u2 );
  76          groups_join_group( $g2, $u2 );
  77  
  78          // Fool the admin check
  79          $this->set_current_user( $u1 );
  80          buddypress()->is_item_admin = true;
  81  
  82          groups_ban_member( $u2, $g1 );
  83  
  84          $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
  85      }
  86  
  87      /**
  88       * @group total_group_count
  89       * @group groups_unban_member
  90       */
  91  	public function test_total_group_count_groups_unban_member() {
  92          $u1 = self::factory()->user->create();
  93          $u2 = self::factory()->user->create();
  94          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
  95          $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
  96          groups_join_group( $g1, $u2 );
  97          groups_join_group( $g2, $u2 );
  98  
  99          // Fool the admin check
 100          $this->set_current_user( $u1 );
 101          buddypress()->is_item_admin = true;
 102  
 103          groups_ban_member( $u2, $g1 );
 104  
 105          groups_unban_member( $u2, $g1 );
 106  
 107          $this->assertEquals( 2, bp_get_user_meta( $u2, 'total_group_count', true ) );
 108      }
 109  
 110      /**
 111       * @group total_group_count
 112       * @group groups_accept_invite
 113       */
 114  	public function test_total_group_count_groups_accept_invite() {
 115          $u1 = self::factory()->user->create();
 116          $u2 = self::factory()->user->create();
 117          $g = self::factory()->group->create();
 118          groups_invite_user( array(
 119              'user_id' => $u1,
 120              'group_id' => $g,
 121              'inviter_id' => $u2,
 122          ) );
 123  
 124          groups_accept_invite( $u2, $g );
 125  
 126          $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
 127      }
 128  
 129      /**
 130       * @group total_group_count
 131       * @group groups_accept_membership_request
 132       */
 133      public function test_total_group_count_groups_accept_membership_request() {
 134          $u1 = self::factory()->user->create();
 135          $u2 = self::factory()->user->create();
 136  
 137          $current_user = bp_loggedin_user_id();
 138          $this->set_current_user( $u2 );
 139  
 140          $g = self::factory()->group->create();
 141          groups_send_membership_request( $u1, $g );
 142  
 143          groups_accept_membership_request( 0, $u1, $g );
 144  
 145          $this->assertEquals( 1, bp_get_user_meta( $u1, 'total_group_count', true ) );
 146  
 147          $this->set_current_user( $current_user );
 148      }
 149  
 150      /**
 151       * @group total_group_count
 152       * @group groups_remove_member
 153       */
 154  	public function test_total_group_count_groups_remove_member() {
 155          $u1 = self::factory()->user->create();
 156          $u2 = self::factory()->user->create();
 157          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 158          $g2 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 159          groups_join_group( $g1, $u2 );
 160          groups_join_group( $g2, $u2 );
 161  
 162          // Fool the admin check
 163          $this->set_current_user( $u1 );
 164          buddypress()->is_item_admin = true;
 165  
 166          groups_remove_member( $u2, $g1 );
 167  
 168          $this->assertEquals( 1, bp_get_user_meta( $u2, 'total_group_count', true ) );
 169      }
 170  
 171      /**
 172       * @group total_member_count
 173       * @group groups_join_group
 174       */
 175  	public function test_total_member_count_groups_join_group() {
 176          $u1 = self::factory()->user->create();
 177          $u2 = self::factory()->user->create();
 178          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 179  
 180          groups_join_group( $g, $u2 );
 181          $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
 182      }
 183  
 184      /**
 185       * @group total_member_count
 186       * @group groups_leave_group
 187       */
 188  	public function test_total_member_count_groups_leave_group() {
 189          $u1 = self::factory()->user->create();
 190          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 191          groups_join_group( $g1, $u1 );
 192  
 193          groups_leave_group( $g1, $u1 );
 194          $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
 195      }
 196  
 197      /**
 198       * @group total_member_count
 199       * @group groups_ban_member
 200       */
 201  	public function test_total_member_count_groups_ban_member() {
 202          $u1 = self::factory()->user->create();
 203          $u2 = self::factory()->user->create();
 204          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 205          groups_join_group( $g1, $u2 );
 206  
 207          // Fool the admin check
 208          $this->set_current_user( $u1 );
 209          buddypress()->is_item_admin = true;
 210  
 211          groups_ban_member( $u2, $g1 );
 212  
 213          $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
 214      }
 215  
 216      /**
 217       * @group total_member_count
 218       * @group groups_unban_member
 219       */
 220  	public function test_total_member_count_groups_unban_member() {
 221          $u1 = self::factory()->user->create();
 222          $u2 = self::factory()->user->create();
 223          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 224          groups_join_group( $g1, $u2 );
 225  
 226          // Fool the admin check
 227          $this->set_current_user( $u1 );
 228          buddypress()->is_item_admin = true;
 229  
 230          groups_ban_member( $u2, $g1 );
 231  
 232          groups_unban_member( $u2, $g1 );
 233  
 234          $this->assertEquals( 2, groups_get_groupmeta( $g1, 'total_member_count' ) );
 235      }
 236  
 237      /**
 238       * @group total_member_count
 239       * @group groups_accept_invite
 240       */
 241  	public function test_total_member_count_groups_accept_invite() {
 242          $u1 = self::factory()->user->create();
 243          $u2 = self::factory()->user->create();
 244          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 245          groups_invite_user( array(
 246              'user_id' => $u1,
 247              'group_id' => $g,
 248              'inviter_id' => $u2,
 249          ) );
 250  
 251          groups_accept_invite( $u2, $g );
 252  
 253          $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
 254      }
 255  
 256      /**
 257       * @group total_member_count
 258       * @group groups_accept_membership_request
 259       */
 260      public function test_total_member_count_groups_accept_membership_request() {
 261          $u1 = self::factory()->user->create();
 262          $u2 = self::factory()->user->create();
 263          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 264  
 265          groups_send_membership_request( $u2, $g );
 266          groups_accept_membership_request( 0, $u2, $g );
 267  
 268          $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
 269      }
 270  
 271      /**
 272       * @group total_member_count
 273       * @group groups_remove_member
 274       */
 275  	public function test_total_member_count_groups_remove_member() {
 276          $u1 = self::factory()->user->create();
 277          $u2 = self::factory()->user->create();
 278          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 279          groups_join_group( $g1, $u2 );
 280  
 281          // Fool the admin check
 282          $this->set_current_user( $u1 );
 283          buddypress()->is_item_admin = true;
 284  
 285          groups_remove_member( $u2, $g1 );
 286  
 287          $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
 288      }
 289  
 290      /**
 291       * @group total_member_count
 292       * @group groups_create_group
 293       */
 294  	public function test_total_member_count_groups_create_group() {
 295          $u1 = self::factory()->user->create();
 296          $g = groups_create_group( array(
 297              'creator_id' => $u1,
 298              'name' => 'Boone Is Handsome',
 299              'description' => 'Yes',
 300              'slug' => 'boone-is-handsome',
 301              'status' => 'public',
 302              'enable_forum' => 0,
 303              'date_created' => bp_core_current_time(),
 304          ) );
 305  
 306          $this->assertEquals( 1, groups_get_groupmeta( $g, 'total_member_count' ) );
 307      }
 308  
 309      /**
 310       * @group groups_create_group
 311       */
 312      public function test_groups_create_group_dont_delete_description_for_existing_group_when_no_description_is_passed() {
 313          $g = self::factory()->group->create();
 314  
 315          $group_before = groups_get_group( $g );
 316          groups_create_group( array(
 317              'group_id' => $g,
 318              'enable_forum' => 1,
 319          ) );
 320  
 321          $group_after = groups_get_group( $g );
 322          $this->assertSame( $group_before->description, $group_after->description );
 323      }
 324  
 325      /**
 326       * @group groupmeta
 327       * @ticket BP5180
 328       */
 329  	public function test_groups_update_groupmeta_with_line_breaks() {
 330          $g = self::factory()->group->create();
 331          $meta_value = 'Foo!
 332  
 333  Bar!';
 334          groups_update_groupmeta( $g, 'linebreak_test', $meta_value );
 335  
 336          $this->assertEquals( $meta_value, groups_get_groupmeta( $g, 'linebreak_test' ) );
 337      }
 338  
 339      /**
 340       * @group groupmeta
 341       */
 342  	public function test_groups_update_groupmeta_non_numeric_id() {
 343          $this->assertFalse( groups_update_groupmeta( 'foo', 'bar', 'baz' ) );
 344      }
 345  
 346      /**
 347       * @group groupmeta
 348       */
 349  	public function test_groups_update_groupmeta_stripslashes() {
 350          $g = self::factory()->group->create();
 351          $value = "This string is totally slashin\'!";
 352          groups_update_groupmeta( $g, 'foo', $value );
 353  
 354          $this->assertSame( stripslashes( $value ), groups_get_groupmeta( $g, 'foo' ) );
 355      }
 356  
 357      /**
 358       * @group groupmeta
 359       */
 360  	public function test_groups_update_groupmeta_new() {
 361          $g = self::factory()->group->create();
 362          $this->assertSame( '', groups_get_groupmeta( $g, 'foo' ), '"foo" meta should be empty for this group.' );
 363          $this->assertNotEmpty( groups_update_groupmeta( $g, 'foo', 'bar' ) );
 364          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) );
 365      }
 366  
 367      /**
 368       * @group groupmeta
 369       */
 370  	public function test_groups_update_groupmeta_existing() {
 371          $g = self::factory()->group->create();
 372          groups_update_groupmeta( $g, 'foo', 'bar' );
 373          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ), '"foo" meta should be set already for this group.' );
 374          $this->assertTrue( groups_update_groupmeta( $g, 'foo', 'baz' ) );
 375          $this->assertSame( 'baz', groups_get_groupmeta( $g, 'foo' ) );
 376      }
 377  
 378      /**
 379       * @group groupmeta
 380       */
 381      public function test_groups_update_groupmeta_existing_same_value() {
 382          $g = self::factory()->group->create();
 383          groups_update_groupmeta( $g, 'foo', 'bar' );
 384          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ), '"foo" meta should be set already for this group.' );
 385          $this->assertFalse( groups_update_groupmeta( $g, 'foo', 'bar' ) );
 386          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) );
 387      }
 388  
 389      /**
 390       * @group groupmeta
 391       * @group groups_update_groupmeta
 392       */
 393  	public function test_groups_update_groupmeta_prev_value() {
 394          $g = self::factory()->group->create();
 395          groups_add_groupmeta( $g, 'foo', 'bar' );
 396  
 397          // In earlier versions of WordPress, bp_activity_update_meta()
 398          // returns true even on failure. However, we know that in these
 399          // cases the update is failing as expected, so we skip this
 400          // assertion just to keep our tests passing
 401          // See https://core.trac.wordpress.org/ticket/24933
 402          if ( version_compare( $GLOBALS['wp_version'], '3.7', '>=' ) ) {
 403              $this->assertFalse( groups_update_groupmeta( $g, 'foo', 'bar2', 'baz' ) );
 404          }
 405  
 406          $this->assertTrue( groups_update_groupmeta( $g, 'foo', 'bar2', 'bar' ) );
 407      }
 408  
 409      /**
 410       * @group groupmeta
 411       * @group groups_get_groupmeta
 412       * @ticket BP5399
 413       */
 414      public function test_groups_get_groupmeta_with_illegal_key_characters() {
 415          $g = self::factory()->group->create();
 416          groups_update_groupmeta( $g, 'foo', 'bar' );
 417  
 418          $krazy_key = ' f!@#$%^o *(){}o?+';
 419          $this->assertSame( '', groups_get_groupmeta( $g, $krazy_key ) );
 420      }
 421  
 422      /**
 423       * @group groupmeta
 424       */
 425  	public function test_groups_get_groupmeta_all_metas() {
 426          $g = self::factory()->group->create();
 427          groups_update_groupmeta( $g, 'foo', 'bar' );
 428          groups_update_groupmeta( $g, 'Boone', 'is cool' );
 429  
 430          // There's likely some other keys (total_member_count etc)
 431          // Just check to make sure both of ours are there
 432          $metas = groups_get_groupmeta( $g );
 433          $count = count( $metas );
 434          $found = array_slice( $metas, $count - 2 );
 435  
 436          $expected = array(
 437              'foo' => array(
 438                  'bar',
 439              ),
 440              'Boone' => array(
 441                  'is cool',
 442              ),
 443          );
 444  
 445          $this->assertSame( $expected, $found );
 446      }
 447  
 448      /**
 449       * @group groupmeta
 450       */
 451  	public function test_groups_get_groupmeta_all_metas_empty() {
 452          $g = self::factory()->group->create();
 453  
 454          // Get rid of any auto-created values
 455          global $wpdb;
 456  
 457          $bp = buddypress();
 458          $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_groupmeta} WHERE group_id = %d", $g ) );
 459          wp_cache_delete( $g, 'group_meta' );
 460  
 461          $metas = groups_get_groupmeta( $g );
 462          $this->assertSame( array(), $metas );
 463      }
 464  
 465      /**
 466       * @group groupmeta
 467       */
 468  	public function test_groups_get_groupmeta_empty() {
 469          $g = self::factory()->group->create();
 470          $this->assertSame( '', groups_get_groupmeta( $g, 'foo' ) );
 471      }
 472  
 473      /**
 474       * @group groupmeta
 475       * @group groups_get_groupmeta
 476       */
 477  	public function test_bp_activity_get_meta_single_true() {
 478          $g = self::factory()->group->create();
 479          groups_add_groupmeta( $g, 'foo', 'bar' );
 480          groups_add_groupmeta( $g, 'foo', 'baz' );
 481          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) ); // default is true
 482          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo', true ) );
 483      }
 484  
 485      /**
 486       * @group groupmeta
 487       * @group groups_get_groupmeta
 488       */
 489  	public function test_bp_activity_get_meta_single_false() {
 490          $g = self::factory()->group->create();
 491          groups_add_groupmeta( $g, 'foo', 'bar' );
 492          groups_add_groupmeta( $g, 'foo', 'baz' );
 493          $this->assertSame( array( 'bar', 'baz' ), groups_get_groupmeta( $g, 'foo', false ) );
 494      }
 495  
 496      /**
 497       * @group groupmeta
 498       * @group groups_get_groupmeta
 499       * @group cache
 500       */
 501  	public function test_groups_get_groupmeta_cache_all_on_get() {
 502          $g = self::factory()->group->create();
 503          groups_add_groupmeta( $g, 'foo', 'bar' );
 504          groups_add_groupmeta( $g, 'foo1', 'baz' );
 505          $this->assertFalse( wp_cache_get( $g, 'group_meta' ) );
 506  
 507          // A single query should prime the whole meta cache
 508          groups_get_groupmeta( $g, 'foo' );
 509  
 510          $c = wp_cache_get( $g, 'group_meta' );
 511          $this->assertNotEmpty( $c['foo1'] );
 512      }
 513  
 514      /**
 515       * @group groupmeta
 516       */
 517  	public function test_groups_delete_groupmeta_non_numeric_id() {
 518          $this->assertFalse( groups_delete_groupmeta( 'foo', 'bar' ) );
 519      }
 520  
 521      /**
 522       * @group groupmeta
 523       * @group groups_delete_groupmeta
 524       * @ticket BP5399
 525       */
 526      public function test_groups_delete_groupmeta_with_illegal_key_characters() {
 527          $g = self::factory()->group->create();
 528          $this->assertNotEmpty( groups_update_groupmeta( $g, 'foo', 'bar' ), 'Value of "foo" should be set at this point.' );
 529  
 530          $krazy_key = ' f!@#$%^o *(){}o?+';
 531          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) );
 532      }
 533  
 534      /**
 535       * @group groupmeta
 536       * @group groups_delete_groupmeta
 537       * @ticket BP6326
 538       */
 539      public function test_groups_delete_groupmeta_with_no_meta_key_when_group_has_metadata() {
 540          global $wpdb;
 541  
 542          $g = self::factory()->group->create();
 543          $m = groups_get_groupmeta( $g );
 544          foreach ( $m as $mk => $mv ) {
 545              groups_delete_groupmeta( $g, $mk );
 546          }
 547  
 548          $found = groups_delete_groupmeta( $g );
 549          $this->assertTrue( $found );
 550      }
 551  
 552      /**
 553       * @group groupmeta
 554       * @group groups_delete_groupmeta
 555       */
 556      public function test_groups_delete_groupmeta_with_delete_all_but_no_meta_key() {
 557          // With no meta key, don't delete for all items - just delete
 558          // all for a single item
 559          $g1 = self::factory()->group->create();
 560          $g2 = self::factory()->group->create();
 561          groups_add_groupmeta( $g1, 'foo', 'bar' );
 562          groups_add_groupmeta( $g1, 'foo1', 'bar1' );
 563          groups_add_groupmeta( $g2, 'foo', 'bar' );
 564          groups_add_groupmeta( $g2, 'foo1', 'bar1' );
 565  
 566          $this->assertTrue( groups_delete_groupmeta( $g1, '', '', true ) );
 567          $this->assertEmpty( groups_get_groupmeta( $g1 ) );
 568          $this->assertSame( 'bar', groups_get_groupmeta( $g2, 'foo' ) );
 569          $this->assertSame( 'bar1', groups_get_groupmeta( $g2, 'foo1' ) );
 570      }
 571  
 572      /**
 573       * @group groupmeta
 574       * @group groups_delete_groupmeta
 575       */
 576  	public function test_groups_delete_groupmeta_with_delete_all() {
 577          // With no meta key, don't delete for all items - just delete
 578          // all for a single item
 579          $g1 = self::factory()->group->create();
 580          $g2 = self::factory()->group->create();
 581          groups_add_groupmeta( $g1, 'foo', 'bar' );
 582          groups_add_groupmeta( $g1, 'foo1', 'bar1' );
 583          groups_add_groupmeta( $g2, 'foo', 'bar' );
 584          groups_add_groupmeta( $g2, 'foo1', 'bar1' );
 585  
 586          $this->assertTrue( groups_delete_groupmeta( $g1, 'foo', '', true ) );
 587          $this->assertSame( '', groups_get_groupmeta( $g1, 'foo' ) );
 588          $this->assertSame( '', groups_get_groupmeta( $g2, 'foo' ) );
 589          $this->assertSame( 'bar1', groups_get_groupmeta( $g1, 'foo1' ) );
 590          $this->assertSame( 'bar1', groups_get_groupmeta( $g2, 'foo1' ) );
 591      }
 592  
 593      /**
 594       * @group groupmeta
 595       * @group groups_add_groupmeta
 596       */
 597  	public function test_groups_add_groupmeta_no_meta_key() {
 598          $this->assertFalse( groups_add_groupmeta( 1, '', 'bar' ) );
 599      }
 600  
 601      /**
 602       * @group groupmeta
 603       * @group groups_add_groupmeta
 604       */
 605  	public function test_groups_add_groupmeta_empty_object_id() {
 606          $this->assertFalse( groups_add_groupmeta( 0, 'foo', 'bar' ) );
 607      }
 608  
 609      /**
 610       * @group groupmeta
 611       * @group groups_add_groupmeta
 612       */
 613  	public function test_groups_add_groupmeta_existing_unique() {
 614          $g = self::factory()->group->create();
 615          groups_add_groupmeta( $g, 'foo', 'bar' );
 616          $this->assertFalse( groups_add_groupmeta( $g, 'foo', 'baz', true ) );
 617      }
 618  
 619      /**
 620       * @group groupmeta
 621       * @group groups_add_groupmeta
 622       */
 623  	public function test_groups_add_groupmeta_existing_not_unique() {
 624          $g = self::factory()->group->create();
 625          groups_add_groupmeta( $g, 'foo', 'bar' );
 626          $this->assertNotEmpty( groups_add_groupmeta( $g, 'foo', 'baz' ) );
 627      }
 628  
 629      /**
 630       * @group counts
 631       */
 632  	public function test_get_invite_count_for_user() {
 633          $u1 = self::factory()->user->create();
 634          $u2 = self::factory()->user->create();
 635          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 636  
 637          // create invitation
 638          groups_invite_user( array(
 639              'user_id'    => $u2,
 640              'group_id'   => $g,
 641              'inviter_id' => $u1,
 642          ) );
 643  
 644          // send the invite
 645          // this function is imperative to set the 'invite_sent' flag in the DB
 646          // why is this separated from groups_invite_user()?
 647          // @see groups_screen_group_invite()
 648          groups_send_invites( $u1, $g );
 649  
 650          // assert invite count
 651          $this->assertEquals( 1, groups_get_invite_count_for_user( $u2 ) );
 652  
 653          // accept the invite and reassert
 654          groups_accept_invite( $u2, $g );
 655          $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) );
 656      }
 657  
 658      /**
 659       * @group hierarchical_groups
 660       */
 661      public function test_update_orphaned_groups_on_group_delete_top_level() {
 662          $g1 = self::factory()->group->create();
 663          $g2 = self::factory()->group->create( array(
 664              'parent_id' => $g1,
 665          ) );
 666  
 667          groups_delete_group( $g1 );
 668  
 669          $child = groups_get_group( array( 'group_id' => $g2 ) );
 670          $this->assertEquals( 0, $child->parent_id );
 671      }
 672  
 673      /**
 674       * @group hierarchical_groups
 675       */
 676      public function test_update_orphaned_groups_on_group_delete_two_levels() {
 677          $g1 = self::factory()->group->create();
 678          $g2 = self::factory()->group->create( array(
 679              'parent_id' => $g1,
 680          ) );
 681          $g3 = self::factory()->group->create( array(
 682              'parent_id' => $g2,
 683          ) );
 684  
 685          groups_delete_group( $g2 );
 686  
 687          $child = groups_get_group( array( 'group_id' => $g3 ) );
 688          $this->assertEquals( $g1, $child->parent_id );
 689      }
 690  
 691      /**
 692       * @group groups_get_group
 693        * @ticket BP7302
 694       */
 695  	public function test_groups_get_group_accept_integer() {
 696          $g1 = self::factory()->group->create();
 697          $group = groups_get_group( $g1 );
 698  
 699          $this->assertEquals( $g1, $group->id );
 700      }
 701  
 702      /**
 703       * @group groups_get_group
 704        * @ticket BP7302
 705       */
 706  	public function test_groups_get_group_accept_numeric() {
 707          $g1 = self::factory()->group->create();
 708          $group = groups_get_group( (string) $g1 );
 709  
 710          $this->assertEquals( $g1, $group->id );
 711      }
 712  
 713      /**
 714       * @group groups_get_group
 715        * @ticket BP7302
 716       */
 717  	public function test_groups_get_group_accept_array() {
 718          $g1 = self::factory()->group->create();
 719          $group = groups_get_group( array( 'group_id' => $g1 ) );
 720  
 721          $this->assertEquals( $g1, $group->id );
 722      }
 723  
 724      /**
 725       * @group groups_get_group
 726        * @ticket BP7302
 727       */
 728  	public function test_groups_get_group_accept_query_string() {
 729          $g1 = self::factory()->group->create();
 730          $group = groups_get_group( 'group_id=' . $g1 );
 731  
 732          $this->assertEquals( $g1, $group->id );
 733      }
 734  
 735      /**
 736       * @expectedDeprecated groups_edit_base_group_details
 737       * @group groups_edit_base_group_details
 738       */
 739      public function test_groups_edit_base_group_details_test_backcompat_arguments() {
 740          $g1 = self::factory()->group->create();
 741          $name = 'Great Scott';
 742          $description = 'A must-see in time for the holidays!';
 743          groups_edit_base_group_details( $g1, $name, $description, false );
 744  
 745          $expected = array(
 746              'id'          => $g1,
 747              'name'        => $name,
 748              'description' => $description
 749          );
 750          $updated_group_object = groups_get_group( $g1 );
 751          $updated = array(
 752              'id'          => $updated_group_object->id,
 753              'name'        => $updated_group_object->name,
 754              'description' => $updated_group_object->description
 755          );
 756  
 757          $this->assertEqualSets( $expected, $updated );
 758      }
 759  
 760      /**
 761       * @group groups_edit_base_group_details
 762       */
 763      public function test_groups_edit_base_group_details_test_new_arguments() {
 764          $g1 = self::factory()->group->create();
 765          $name = 'Great Scott';
 766          $slug = 'what-about-it';
 767          $description = 'A must-see in time for the holidays!';
 768          groups_edit_base_group_details( array(
 769                  'group_id'       => $g1,
 770                  'name'           => $name,
 771                  'slug'           => $slug,
 772                  'description'    => $description,
 773                  'notify_members' => false,
 774          ) );
 775  
 776          $expected = array(
 777              'id'          => $g1,
 778              'slug'        => $slug,
 779              'name'        => $name,
 780              'description' => $description
 781          );
 782          $updated_group_object = groups_get_group( $g1 );
 783          $updated = array(
 784              'id'          => $updated_group_object->id,
 785              'slug'        => $updated_group_object->slug,
 786              'name'        => $updated_group_object->name,
 787              'description' => $updated_group_object->description
 788          );
 789  
 790          $this->assertEqualSets( $expected, $updated );
 791      }
 792  
 793      /**
 794       * @group groups_edit_base_group_details
 795       */
 796      public function test_groups_edit_base_group_details_avoid_slug_collisions() {
 797          $slug = 'circe';
 798          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 799          $g2 = self::factory()->group->create( array( 'slug' => 'loom' ) );
 800  
 801          // Attempt to use a duplicate slug.
 802          groups_edit_base_group_details( array(
 803                  'group_id'       => $g2,
 804                  'slug'           => $slug,
 805          ) );
 806  
 807          $updated_group_object = groups_get_group( $g2 );
 808  
 809          $this->assertNotEquals( $slug, $updated_group_object->slug );
 810      }
 811  
 812      /**
 813       * @group groups_edit_base_group_details
 814       */
 815      public function test_groups_edit_base_group_details_slug_no_change() {
 816          $slug = 'circe';
 817          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 818  
 819          // Make sure the slug doesn't get incremented when there's no change.
 820          groups_edit_base_group_details( array(
 821                  'group_id'       => $g1,
 822                  'slug'           => $slug,
 823          ) );
 824  
 825          $updated_group_object = groups_get_group( $g1 );
 826  
 827          $this->assertEquals( $slug, $updated_group_object->slug );
 828      }
 829  
 830      /**
 831       * @group groups_edit_base_group_details
 832       */
 833      public function test_groups_edit_base_group_details_slug_null_value() {
 834          $slug = 'circe';
 835          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 836  
 837          // Make sure the slug doesn't get changed when null is passed.
 838          groups_edit_base_group_details( array(
 839                  'group_id'       => $g1,
 840                  'slug'           => null,
 841          ) );
 842  
 843          $updated_group_object = groups_get_group( $g1 );
 844  
 845          $this->assertEquals( $slug, $updated_group_object->slug );
 846      }
 847  
 848      /**
 849       * @group groups_get_id_by_previous_slug
 850       */
 851  	public function test_groups_get_id_by_previous_slug() {
 852          $slug = 'circe';
 853          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 854          $g2 = self::factory()->group->create( array( 'slug' => 'loom' ) );
 855  
 856          groups_edit_base_group_details( array(
 857              'group_id'       => $g1,
 858              'slug'           => 'newslug',
 859          ) );
 860  
 861          // Function should return the group ID as an integer.
 862          $this->assertSame( $g1, groups_get_id_by_previous_slug( $slug ) );
 863      }
 864  
 865      /**
 866       * @group groups_get_id_by_previous_slug
 867       */
 868      public function test_groups_get_id_by_previous_slug_null_no_results() {
 869          $this->assertNull( groups_get_id_by_previous_slug( 'woohoo' ) );
 870      }
 871  
 872      /**
 873       * @ticket BP7820
 874       * @ticket BP7698
 875       */
 876      public function test_bp_groups_memberships_personal_data_exporter() {
 877          groups_join_group( self::$group_ids[0], self::$user_ids[0] );
 878  
 879          $test_user = new WP_User( self::$user_ids[0] );
 880  
 881          $actual = bp_groups_memberships_personal_data_exporter( $test_user->user_email, 1 );
 882  
 883          $this->assertTrue( $actual['done'] );
 884          $this->assertCount( 1, $actual['data'] );
 885          $this->assertSame( 'bp-group-membership-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 886      }
 887  
 888      /**
 889       * @ticket BP7820
 890       * @ticket BP7698
 891       */
 892      public function test_bp_groups_pending_requests_personal_data_exporter() {
 893          groups_send_membership_request( self::$user_ids[0], self::$group_ids[0] );
 894  
 895          $test_user = new WP_User( self::$user_ids[0] );
 896  
 897          $actual = bp_groups_pending_requests_personal_data_exporter( $test_user->user_email, 1 );
 898  
 899          $this->assertTrue( $actual['done'] );
 900          $this->assertCount( 1, $actual['data'] );
 901          $this->assertSame( 'bp-group-pending-request-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 902      }
 903  
 904      /**
 905       * @ticket BP7820
 906       * @ticket BP7698
 907       */
 908      public function test_bp_groups_pending_sent_invitations_personal_data_exporter() {
 909          groups_invite_user( array(
 910              'user_id'    => self::$user_ids[1],
 911              'group_id'   => self::$group_ids[0],
 912              'inviter_id' => self::$user_ids[0],
 913          ) );
 914  
 915          $test_user = new WP_User( self::$user_ids[0] );
 916  
 917          $actual = bp_groups_pending_sent_invitations_personal_data_exporter( $test_user->user_email, 1 );
 918  
 919          $this->assertTrue( $actual['done'] );
 920          $this->assertCount( 1, $actual['data'] );
 921          $this->assertSame( 'bp-group-pending-sent-invitation-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 922      }
 923  
 924      /**
 925       * @ticket BP7820
 926       * @ticket BP7698
 927       */
 928      public function test_bp_groups_pending_received_invitations_personal_data_exporter() {
 929          groups_invite_user( array(
 930              'user_id'    => self::$user_ids[0],
 931              'group_id'   => self::$group_ids[0],
 932              'inviter_id' => self::$user_ids[1],
 933          ) );
 934  
 935          $test_user = new WP_User( self::$user_ids[0] );
 936  
 937          $actual = bp_groups_pending_received_invitations_personal_data_exporter( $test_user->user_email, 1 );
 938  
 939          $this->assertTrue( $actual['done'] );
 940          $this->assertCount( 1, $actual['data'] );
 941          $this->assertSame( 'bp-group-pending-received-invitation-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 942      }
 943  }


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1