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


Generated: Sat Sep 21 01:01:46 2019 Cross-referenced by PHPXref 0.7.1