[ 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_total_member_count( $g ) );
 188      }
 189  
 190      /**
 191       * @group total_member_count
 192       */
 193  	public function test_total_member_count_with_invalid_group() {
 194          $this->assertFalse( groups_get_total_member_count( 'invalid-group' ) );
 195          $this->assertFalse( groups_get_total_member_count( '' ) );
 196          $this->assertFalse( groups_get_total_member_count( 123456789 ) );
 197      }
 198  
 199      /**
 200       * @group total_member_count
 201       * @group groups_leave_group
 202       */
 203  	public function test_total_member_count_groups_leave_group() {
 204          $u1 = self::factory()->user->create();
 205          $u2 = self::factory()->user->create();
 206          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 207  
 208          groups_join_group( $g1, $u2 );
 209  
 210          $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
 211  
 212          groups_leave_group( $g1, $u2 );
 213  
 214          $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
 215      }
 216  
 217      /**
 218       * @group total_member_count
 219       * @group groups_ban_member
 220       */
 221  	public function test_total_member_count_groups_ban_member() {
 222          $u1 = self::factory()->user->create();
 223          $u2 = self::factory()->user->create();
 224          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 225          groups_join_group( $g1, $u2 );
 226  
 227          // Fool the admin check
 228          $this->set_current_user( $u1 );
 229          buddypress()->is_item_admin = true;
 230  
 231          $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
 232  
 233          groups_ban_member( $u2, $g1 );
 234  
 235          $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
 236      }
 237  
 238      /**
 239       * @group total_member_count
 240       * @group groups_unban_member
 241       */
 242  	public function test_total_member_count_groups_unban_member() {
 243          $u1 = self::factory()->user->create();
 244          $u2 = self::factory()->user->create();
 245          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 246          groups_join_group( $g1, $u2 );
 247  
 248          // Fool the admin check
 249          $this->set_current_user( $u1 );
 250          buddypress()->is_item_admin = true;
 251  
 252          groups_ban_member( $u2, $g1 );
 253  
 254          $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
 255  
 256          groups_unban_member( $u2, $g1 );
 257  
 258          $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
 259      }
 260  
 261      /**
 262       * @group total_member_count
 263       * @group groups_accept_invite
 264       */
 265  	public function test_total_member_count_groups_accept_invite() {
 266          $u1 = self::factory()->user->create();
 267          $u2 = self::factory()->user->create();
 268          $g = self::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u1 ) );
 269          groups_invite_user( array(
 270              'user_id'     => $u2,
 271              'group_id'    => $g,
 272              'inviter_id'  => $u1,
 273              'send_invite' => 1,
 274          ) );
 275  
 276          $this->assertEquals( 1, groups_get_total_member_count( $g ) );
 277  
 278          groups_accept_invite( $u2, $g );
 279  
 280          $this->assertEquals( 2, groups_get_total_member_count( $g ) );
 281      }
 282  
 283      /**
 284       * @group total_member_count
 285       * @group groups_accept_membership_request
 286       */
 287      public function test_total_member_count_groups_accept_membership_request() {
 288          $u1 = self::factory()->user->create();
 289          $u2 = self::factory()->user->create();
 290          $g = self::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u1 ) );
 291  
 292          groups_send_membership_request( array(
 293              'user_id'       => $u2,
 294              'group_id'      => $g,
 295          ) );
 296          groups_accept_membership_request( 0, $u2, $g );
 297  
 298          $this->assertEquals( 2, groups_get_total_member_count( $g ) );
 299      }
 300  
 301      /**
 302       * @group total_member_count
 303       * @group groups_remove_member
 304       */
 305  	public function test_total_member_count_groups_remove_member() {
 306          $u1 = self::factory()->user->create();
 307          $u2 = self::factory()->user->create();
 308          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 309          groups_join_group( $g1, $u2 );
 310  
 311          // Fool the admin check
 312          $this->set_current_user( $u1 );
 313          buddypress()->is_item_admin = true;
 314  
 315          groups_remove_member( $u2, $g1 );
 316  
 317          $this->assertEquals( 1, groups_get_total_member_count( $g1 ));
 318      }
 319  
 320      /**
 321       * @group total_member_count
 322       * @group groups_remove_member
 323       */
 324  	public function test_total_member_count_groups_delete_member() {
 325          $u1 = self::factory()->user->create();
 326          $u2 = self::factory()->user->create();
 327          $u3 = self::factory()->user->create();
 328          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 329  
 330          groups_join_group( $g1, $u2 );
 331          groups_join_group( $g1, $u3 );
 332  
 333          $this->assertEquals( 3, groups_get_total_member_count( $g1 ) );
 334          $this->assertEquals( 3, BP_Groups_Group::get_total_member_count( $g1 ) );
 335  
 336          add_filter( 'bp_remove_user_data_on_delete_user_hook', '__return_true' );
 337  
 338          // Delete user.
 339          wp_delete_user( $u2 );
 340  
 341          remove_filter( 'bp_remove_user_data_on_delete_user_hook', '__return_true' );
 342  
 343          $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
 344          $this->assertEquals( 2, BP_Groups_Group::get_total_member_count( $g1 ) );
 345      }
 346  
 347      /**
 348       * @group total_member_count
 349       * @group groups_create_group
 350       */
 351  	public function test_total_member_count_groups_create_group() {
 352          $u1 = self::factory()->user->create();
 353          $g = groups_create_group( array(
 354              'creator_id' => $u1,
 355              'name' => 'Boone Is Handsome',
 356              'description' => 'Yes',
 357              'slug' => 'boone-is-handsome',
 358              'status' => 'public',
 359              'enable_forum' => 0,
 360              'date_created' => bp_core_current_time(),
 361          ) );
 362  
 363          $this->assertEquals( 1, groups_get_total_member_count( $g ) );
 364      }
 365  
 366      /**
 367       * @group groups_create_group
 368       */
 369      public function test_groups_create_group_dont_delete_description_for_existing_group_when_no_description_is_passed() {
 370          $g = self::factory()->group->create();
 371  
 372          $group_before = groups_get_group( $g );
 373          groups_create_group( array(
 374              'group_id' => $g,
 375              'enable_forum' => 1,
 376          ) );
 377  
 378          $group_after = groups_get_group( $g );
 379          $this->assertSame( $group_before->description, $group_after->description );
 380      }
 381  
 382      /**
 383       * @group groupmeta
 384       * @ticket BP5180
 385       */
 386  	public function test_groups_update_groupmeta_with_line_breaks() {
 387          $g = self::factory()->group->create();
 388          $meta_value = 'Foo!
 389  
 390  Bar!';
 391          groups_update_groupmeta( $g, 'linebreak_test', $meta_value );
 392  
 393          $this->assertEquals( $meta_value, groups_get_groupmeta( $g, 'linebreak_test' ) );
 394      }
 395  
 396      /**
 397       * @group groupmeta
 398       */
 399  	public function test_groups_update_groupmeta_non_numeric_id() {
 400          $this->assertFalse( groups_update_groupmeta( 'foo', 'bar', 'baz' ) );
 401      }
 402  
 403      /**
 404       * @group groupmeta
 405       */
 406  	public function test_groups_update_groupmeta_stripslashes() {
 407          $g = self::factory()->group->create();
 408          $value = "This string is totally slashin\'!";
 409          groups_update_groupmeta( $g, 'foo', $value );
 410  
 411          $this->assertSame( stripslashes( $value ), groups_get_groupmeta( $g, 'foo' ) );
 412      }
 413  
 414      /**
 415       * @group groupmeta
 416       */
 417  	public function test_groups_update_groupmeta_new() {
 418          $g = self::factory()->group->create();
 419          $this->assertSame( '', groups_get_groupmeta( $g, 'foo' ), '"foo" meta should be empty for this group.' );
 420          $this->assertNotEmpty( groups_update_groupmeta( $g, 'foo', 'bar' ) );
 421          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) );
 422      }
 423  
 424      /**
 425       * @group groupmeta
 426       */
 427  	public function test_groups_update_groupmeta_existing() {
 428          $g = self::factory()->group->create();
 429          groups_update_groupmeta( $g, 'foo', 'bar' );
 430          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ), '"foo" meta should be set already for this group.' );
 431          $this->assertTrue( groups_update_groupmeta( $g, 'foo', 'baz' ) );
 432          $this->assertSame( 'baz', groups_get_groupmeta( $g, 'foo' ) );
 433      }
 434  
 435      /**
 436       * @group groupmeta
 437       */
 438      public function test_groups_update_groupmeta_existing_same_value() {
 439          $g = self::factory()->group->create();
 440          groups_update_groupmeta( $g, 'foo', 'bar' );
 441          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ), '"foo" meta should be set already for this group.' );
 442          $this->assertFalse( groups_update_groupmeta( $g, 'foo', 'bar' ) );
 443          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) );
 444      }
 445  
 446      /**
 447       * @group groupmeta
 448       * @group groups_update_groupmeta
 449       */
 450  	public function test_groups_update_groupmeta_prev_value() {
 451          $g = self::factory()->group->create();
 452          groups_add_groupmeta( $g, 'foo', 'bar' );
 453  
 454          // In earlier versions of WordPress, bp_activity_update_meta()
 455          // returns true even on failure. However, we know that in these
 456          // cases the update is failing as expected, so we skip this
 457          // assertion just to keep our tests passing
 458          // See https://core.trac.wordpress.org/ticket/24933
 459          if ( version_compare( $GLOBALS['wp_version'], '3.7', '>=' ) ) {
 460              $this->assertFalse( groups_update_groupmeta( $g, 'foo', 'bar2', 'baz' ) );
 461          }
 462  
 463          $this->assertTrue( groups_update_groupmeta( $g, 'foo', 'bar2', 'bar' ) );
 464      }
 465  
 466      /**
 467       * @group groupmeta
 468       * @group groups_get_groupmeta
 469       * @ticket BP5399
 470       */
 471      public function test_groups_get_groupmeta_with_illegal_key_characters() {
 472          $g = self::factory()->group->create();
 473          groups_update_groupmeta( $g, 'foo', 'bar' );
 474  
 475          $krazy_key = ' f!@#$%^o *(){}o?+';
 476          $this->assertSame( '', groups_get_groupmeta( $g, $krazy_key ) );
 477      }
 478  
 479      /**
 480       * @group groupmeta
 481       */
 482  	public function test_groups_get_groupmeta_all_metas() {
 483          $g = self::factory()->group->create();
 484          groups_update_groupmeta( $g, 'foo', 'bar' );
 485          groups_update_groupmeta( $g, 'Boone', 'is cool' );
 486  
 487          // There's likely some other keys (total_member_count etc)
 488          // Just check to make sure both of ours are there
 489          $metas = groups_get_groupmeta( $g );
 490          $count = count( $metas );
 491          $found = array_slice( $metas, $count - 2 );
 492  
 493          $expected = array(
 494              'foo' => array(
 495                  'bar',
 496              ),
 497              'Boone' => array(
 498                  'is cool',
 499              ),
 500          );
 501  
 502          $this->assertSame( $expected, $found );
 503      }
 504  
 505      /**
 506       * @group groupmeta
 507       */
 508  	public function test_groups_get_groupmeta_all_metas_empty() {
 509          $g = self::factory()->group->create();
 510  
 511          // Get rid of any auto-created values
 512          global $wpdb;
 513  
 514          $bp = buddypress();
 515          $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_groupmeta} WHERE group_id = %d", $g ) );
 516          wp_cache_delete( $g, 'group_meta' );
 517  
 518          $metas = groups_get_groupmeta( $g );
 519          $this->assertSame( array(), $metas );
 520      }
 521  
 522      /**
 523       * @group groupmeta
 524       */
 525  	public function test_groups_get_groupmeta_empty() {
 526          $g = self::factory()->group->create();
 527          $this->assertSame( '', groups_get_groupmeta( $g, 'foo' ) );
 528      }
 529  
 530      /**
 531       * @group groupmeta
 532       * @group groups_get_groupmeta
 533       */
 534  	public function test_bp_activity_get_meta_single_true() {
 535          $g = self::factory()->group->create();
 536          groups_add_groupmeta( $g, 'foo', 'bar' );
 537          groups_add_groupmeta( $g, 'foo', 'baz' );
 538          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) ); // default is true
 539          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo', true ) );
 540      }
 541  
 542      /**
 543       * @group groupmeta
 544       * @group groups_get_groupmeta
 545       */
 546  	public function test_bp_activity_get_meta_single_false() {
 547          $g = self::factory()->group->create();
 548          groups_add_groupmeta( $g, 'foo', 'bar' );
 549          groups_add_groupmeta( $g, 'foo', 'baz' );
 550          $this->assertSame( array( 'bar', 'baz' ), groups_get_groupmeta( $g, 'foo', false ) );
 551      }
 552  
 553      /**
 554       * @group groupmeta
 555       * @group groups_get_groupmeta
 556       * @group cache
 557       */
 558  	public function test_groups_get_groupmeta_cache_all_on_get() {
 559          $g = self::factory()->group->create();
 560          groups_add_groupmeta( $g, 'foo', 'bar' );
 561          groups_add_groupmeta( $g, 'foo1', 'baz' );
 562          $this->assertFalse( wp_cache_get( $g, 'group_meta' ) );
 563  
 564          // A single query should prime the whole meta cache
 565          groups_get_groupmeta( $g, 'foo' );
 566  
 567          $c = wp_cache_get( $g, 'group_meta' );
 568          $this->assertNotEmpty( $c['foo1'] );
 569      }
 570  
 571      /**
 572       * @group groupmeta
 573       */
 574  	public function test_groups_delete_groupmeta_non_numeric_id() {
 575          $this->assertFalse( groups_delete_groupmeta( 'foo', 'bar' ) );
 576      }
 577  
 578      /**
 579       * @group groupmeta
 580       * @group groups_delete_groupmeta
 581       * @ticket BP5399
 582       */
 583      public function test_groups_delete_groupmeta_with_illegal_key_characters() {
 584          $g = self::factory()->group->create();
 585          $this->assertNotEmpty( groups_update_groupmeta( $g, 'foo', 'bar' ), 'Value of "foo" should be set at this point.' );
 586  
 587          $krazy_key = ' f!@#$%^o *(){}o?+';
 588          $this->assertSame( 'bar', groups_get_groupmeta( $g, 'foo' ) );
 589      }
 590  
 591      /**
 592       * @group groupmeta
 593       * @group groups_delete_groupmeta
 594       * @ticket BP6326
 595       */
 596      public function test_groups_delete_groupmeta_with_no_meta_key_when_group_has_metadata() {
 597          global $wpdb;
 598  
 599          $g = self::factory()->group->create();
 600          $m = groups_get_groupmeta( $g );
 601          foreach ( $m as $mk => $mv ) {
 602              groups_delete_groupmeta( $g, $mk );
 603          }
 604  
 605          $found = groups_delete_groupmeta( $g );
 606          $this->assertTrue( $found );
 607      }
 608  
 609      /**
 610       * @group groupmeta
 611       * @group groups_delete_groupmeta
 612       */
 613      public function test_groups_delete_groupmeta_with_delete_all_but_no_meta_key() {
 614          // With no meta key, don't delete for all items - just delete
 615          // all for a single item
 616          $g1 = self::factory()->group->create();
 617          $g2 = self::factory()->group->create();
 618          groups_add_groupmeta( $g1, 'foo', 'bar' );
 619          groups_add_groupmeta( $g1, 'foo1', 'bar1' );
 620          groups_add_groupmeta( $g2, 'foo', 'bar' );
 621          groups_add_groupmeta( $g2, 'foo1', 'bar1' );
 622  
 623          $this->assertTrue( groups_delete_groupmeta( $g1, '', '', true ) );
 624          $this->assertEmpty( groups_get_groupmeta( $g1 ) );
 625          $this->assertSame( 'bar', groups_get_groupmeta( $g2, 'foo' ) );
 626          $this->assertSame( 'bar1', groups_get_groupmeta( $g2, 'foo1' ) );
 627      }
 628  
 629      /**
 630       * @group groupmeta
 631       * @group groups_delete_groupmeta
 632       */
 633  	public function test_groups_delete_groupmeta_with_delete_all() {
 634          // With no meta key, don't delete for all items - just delete
 635          // all for a single item
 636          $g1 = self::factory()->group->create();
 637          $g2 = self::factory()->group->create();
 638          groups_add_groupmeta( $g1, 'foo', 'bar' );
 639          groups_add_groupmeta( $g1, 'foo1', 'bar1' );
 640          groups_add_groupmeta( $g2, 'foo', 'bar' );
 641          groups_add_groupmeta( $g2, 'foo1', 'bar1' );
 642  
 643          $this->assertTrue( groups_delete_groupmeta( $g1, 'foo', '', true ) );
 644          $this->assertSame( '', groups_get_groupmeta( $g1, 'foo' ) );
 645          $this->assertSame( '', groups_get_groupmeta( $g2, 'foo' ) );
 646          $this->assertSame( 'bar1', groups_get_groupmeta( $g1, 'foo1' ) );
 647          $this->assertSame( 'bar1', groups_get_groupmeta( $g2, 'foo1' ) );
 648      }
 649  
 650      /**
 651       * @group groupmeta
 652       * @group groups_add_groupmeta
 653       */
 654  	public function test_groups_add_groupmeta_no_meta_key() {
 655          $this->assertFalse( groups_add_groupmeta( 1, '', 'bar' ) );
 656      }
 657  
 658      /**
 659       * @group groupmeta
 660       * @group groups_add_groupmeta
 661       */
 662  	public function test_groups_add_groupmeta_empty_object_id() {
 663          $this->assertFalse( groups_add_groupmeta( 0, 'foo', 'bar' ) );
 664      }
 665  
 666      /**
 667       * @group groupmeta
 668       * @group groups_add_groupmeta
 669       */
 670  	public function test_groups_add_groupmeta_existing_unique() {
 671          $g = self::factory()->group->create();
 672          groups_add_groupmeta( $g, 'foo', 'bar' );
 673          $this->assertFalse( groups_add_groupmeta( $g, 'foo', 'baz', true ) );
 674      }
 675  
 676      /**
 677       * @group groupmeta
 678       * @group groups_add_groupmeta
 679       */
 680  	public function test_groups_add_groupmeta_existing_not_unique() {
 681          $g = self::factory()->group->create();
 682          groups_add_groupmeta( $g, 'foo', 'bar' );
 683          $this->assertNotEmpty( groups_add_groupmeta( $g, 'foo', 'baz' ) );
 684      }
 685  
 686      /**
 687       * @group counts
 688       */
 689  	public function test_get_invite_count_for_user() {
 690          $u1 = self::factory()->user->create();
 691          $u2 = self::factory()->user->create();
 692          $g = self::factory()->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) );
 693  
 694          // create invitation
 695          groups_invite_user( array(
 696              'user_id'    => $u2,
 697              'group_id'   => $g,
 698              'inviter_id' => $u1,
 699              'send_invite' => 1
 700          ) );
 701  
 702          // assert invite count
 703          $this->assertEquals( 1, groups_get_invite_count_for_user( $u2 ) );
 704  
 705          // accept the invite and reassert
 706          groups_accept_invite( $u2, $g );
 707          $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) );
 708      }
 709  
 710      /**
 711       * @group hierarchical_groups
 712       */
 713      public function test_update_orphaned_groups_on_group_delete_top_level() {
 714          $g1 = self::factory()->group->create();
 715          $g2 = self::factory()->group->create( array(
 716              'parent_id' => $g1,
 717          ) );
 718  
 719          groups_delete_group( $g1 );
 720  
 721          $child = groups_get_group( array( 'group_id' => $g2 ) );
 722          $this->assertEquals( 0, $child->parent_id );
 723      }
 724  
 725      /**
 726       * @group hierarchical_groups
 727       */
 728      public function test_update_orphaned_groups_on_group_delete_two_levels() {
 729          $g1 = self::factory()->group->create();
 730          $g2 = self::factory()->group->create( array(
 731              'parent_id' => $g1,
 732          ) );
 733          $g3 = self::factory()->group->create( array(
 734              'parent_id' => $g2,
 735          ) );
 736  
 737          groups_delete_group( $g2 );
 738  
 739          $child = groups_get_group( array( 'group_id' => $g3 ) );
 740          $this->assertEquals( $g1, $child->parent_id );
 741      }
 742  
 743      /**
 744       * @group groups_get_group
 745        * @ticket BP7302
 746       */
 747  	public function test_groups_get_group_accept_integer() {
 748          $g1 = self::factory()->group->create();
 749          $group = groups_get_group( $g1 );
 750  
 751          $this->assertEquals( $g1, $group->id );
 752      }
 753  
 754      /**
 755       * @group groups_get_group
 756        * @ticket BP7302
 757       */
 758  	public function test_groups_get_group_accept_numeric() {
 759          $g1 = self::factory()->group->create();
 760          $group = groups_get_group( (string) $g1 );
 761  
 762          $this->assertEquals( $g1, $group->id );
 763      }
 764  
 765      /**
 766       * @group groups_get_group
 767        * @ticket BP7302
 768       */
 769  	public function test_groups_get_group_accept_array() {
 770          $g1 = self::factory()->group->create();
 771          $group = groups_get_group( array( 'group_id' => $g1 ) );
 772  
 773          $this->assertEquals( $g1, $group->id );
 774      }
 775  
 776      /**
 777       * @group groups_get_group
 778        * @ticket BP7302
 779       */
 780  	public function test_groups_get_group_accept_query_string() {
 781          $g1 = self::factory()->group->create();
 782          $group = groups_get_group( 'group_id=' . $g1 );
 783  
 784          $this->assertEquals( $g1, $group->id );
 785      }
 786  
 787      /**
 788       * @expectedDeprecated groups_edit_base_group_details
 789       * @group groups_edit_base_group_details
 790       */
 791      public function test_groups_edit_base_group_details_test_backcompat_arguments() {
 792          $g1 = self::factory()->group->create();
 793          $name = 'Great Scott';
 794          $description = 'A must-see in time for the holidays!';
 795          groups_edit_base_group_details( $g1, $name, $description, false );
 796  
 797          $expected = array(
 798              'id'          => $g1,
 799              'name'        => $name,
 800              'description' => $description
 801          );
 802          $updated_group_object = groups_get_group( $g1 );
 803          $updated = array(
 804              'id'          => $updated_group_object->id,
 805              'name'        => $updated_group_object->name,
 806              'description' => $updated_group_object->description
 807          );
 808  
 809          $this->assertEqualSets( $expected, $updated );
 810      }
 811  
 812      /**
 813       * @group groups_edit_base_group_details
 814       */
 815      public function test_groups_edit_base_group_details_test_new_arguments() {
 816          $g1 = self::factory()->group->create();
 817          $name = 'Great Scott';
 818          $slug = 'what-about-it';
 819          $description = 'A must-see in time for the holidays!';
 820          groups_edit_base_group_details( array(
 821                  'group_id'       => $g1,
 822                  'name'           => $name,
 823                  'slug'           => $slug,
 824                  'description'    => $description,
 825                  'notify_members' => false,
 826          ) );
 827  
 828          $expected = array(
 829              'id'          => $g1,
 830              'slug'        => $slug,
 831              'name'        => $name,
 832              'description' => $description
 833          );
 834          $updated_group_object = groups_get_group( $g1 );
 835          $updated = array(
 836              'id'          => $updated_group_object->id,
 837              'slug'        => $updated_group_object->slug,
 838              'name'        => $updated_group_object->name,
 839              'description' => $updated_group_object->description
 840          );
 841  
 842          $this->assertEqualSets( $expected, $updated );
 843      }
 844  
 845      /**
 846       * @group groups_edit_base_group_details
 847       */
 848      public function test_groups_edit_base_group_details_avoid_slug_collisions() {
 849          $slug = 'circe';
 850          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 851          $g2 = self::factory()->group->create( array( 'slug' => 'loom' ) );
 852  
 853          // Attempt to use a duplicate slug.
 854          groups_edit_base_group_details( array(
 855                  'group_id'       => $g2,
 856                  'slug'           => $slug,
 857          ) );
 858  
 859          $updated_group_object = groups_get_group( $g2 );
 860  
 861          $this->assertNotEquals( $slug, $updated_group_object->slug );
 862      }
 863  
 864      /**
 865       * @group groups_edit_base_group_details
 866       */
 867      public function test_groups_edit_base_group_details_slug_no_change() {
 868          $slug = 'circe';
 869          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 870  
 871          // Make sure the slug doesn't get incremented when there's no change.
 872          groups_edit_base_group_details( array(
 873                  'group_id'       => $g1,
 874                  'slug'           => $slug,
 875          ) );
 876  
 877          $updated_group_object = groups_get_group( $g1 );
 878  
 879          $this->assertEquals( $slug, $updated_group_object->slug );
 880      }
 881  
 882      /**
 883       * @group groups_edit_base_group_details
 884       */
 885      public function test_groups_edit_base_group_details_slug_null_value() {
 886          $slug = 'circe';
 887          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 888  
 889          // Make sure the slug doesn't get changed when null is passed.
 890          groups_edit_base_group_details( array(
 891                  'group_id'       => $g1,
 892                  'slug'           => null,
 893          ) );
 894  
 895          $updated_group_object = groups_get_group( $g1 );
 896  
 897          $this->assertEquals( $slug, $updated_group_object->slug );
 898      }
 899  
 900      /**
 901       * @group groups_get_id_by_previous_slug
 902       */
 903  	public function test_groups_get_id_by_previous_slug() {
 904          $slug = 'circe';
 905          $g1 = self::factory()->group->create( array( 'slug' => $slug ) );
 906          $g2 = self::factory()->group->create( array( 'slug' => 'loom' ) );
 907  
 908          groups_edit_base_group_details( array(
 909              'group_id'       => $g1,
 910              'slug'           => 'newslug',
 911          ) );
 912  
 913          // Function should return the group ID as an integer.
 914          $this->assertSame( $g1, groups_get_id_by_previous_slug( $slug ) );
 915      }
 916  
 917      /**
 918       * @group groups_get_id_by_previous_slug
 919       */
 920      public function test_groups_get_id_by_previous_slug_null_no_results() {
 921          $this->assertNull( groups_get_id_by_previous_slug( 'woohoo' ) );
 922      }
 923  
 924      /**
 925       * @ticket BP7820
 926       * @ticket BP7698
 927       */
 928      public function test_bp_groups_memberships_personal_data_exporter() {
 929          groups_join_group( self::$group_ids[0], self::$user_ids[0] );
 930  
 931          $test_user = new WP_User( self::$user_ids[0] );
 932  
 933          $actual = bp_groups_memberships_personal_data_exporter( $test_user->user_email, 1 );
 934  
 935          $this->assertTrue( $actual['done'] );
 936          $this->assertCount( 1, $actual['data'] );
 937          $this->assertSame( 'bp-group-membership-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 938      }
 939  
 940      /**
 941       * @ticket BP7820
 942       * @ticket BP7698
 943       */
 944      public function test_bp_groups_pending_requests_personal_data_exporter() {
 945          groups_send_membership_request( array(
 946              'user_id'       => self::$user_ids[0],
 947              'group_id'      => self::$group_ids[0],
 948          ) );
 949  
 950          $test_user = new WP_User( self::$user_ids[0] );
 951  
 952          $actual = bp_groups_pending_requests_personal_data_exporter( $test_user->user_email, 1 );
 953  
 954          $this->assertTrue( $actual['done'] );
 955          $this->assertCount( 1, $actual['data'] );
 956          $this->assertSame( 'bp-group-pending-request-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 957      }
 958  
 959      /**
 960       * @ticket BP7820
 961       * @ticket BP7698
 962       */
 963      public function test_bp_groups_pending_sent_invitations_personal_data_exporter() {
 964          groups_invite_user( array(
 965              'user_id'     => self::$user_ids[0],
 966              'group_id'    => self::$group_ids[0],
 967              'inviter_id'  => self::$user_ids[2],
 968              'send_invite' => 1,
 969          ) );
 970  
 971          $test_user = new WP_User( self::$user_ids[2] );
 972  
 973          $actual = bp_groups_pending_sent_invitations_personal_data_exporter( $test_user->user_email, 1 );
 974  
 975          $this->assertTrue( $actual['done'] );
 976          $this->assertCount( 1, $actual['data'] );
 977          $this->assertSame( 'bp-group-pending-sent-invitation-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 978      }
 979  
 980      /**
 981       * @ticket BP7820
 982       * @ticket BP7698
 983       */
 984      public function test_bp_groups_pending_received_invitations_personal_data_exporter() {
 985          groups_invite_user( array(
 986              'user_id'    => self::$user_ids[0],
 987              'group_id'   => self::$group_ids[0],
 988              'inviter_id' => self::$user_ids[2],
 989          ) );
 990  
 991          $test_user = new WP_User( self::$user_ids[0] );
 992  
 993          $actual = bp_groups_pending_received_invitations_personal_data_exporter( $test_user->user_email, 1 );
 994  
 995          $this->assertTrue( $actual['done'] );
 996          $this->assertCount( 1, $actual['data'] );
 997          $this->assertSame( 'bp-group-pending-received-invitation-' . self::$group_ids[0], $actual['data'][0]['item_id'] );
 998      }
 999  
1000      /**
1001       * @ticket BP8175
1002       */
1003      public function test_groups_data_should_be_deleted_on_user_delete_non_multisite() {
1004          if ( is_multisite() ) {
1005              $this->markTestSkipped( __METHOD__ . ' requires non-multisite.' );
1006          }
1007  
1008          $u = self::factory()->user->create();
1009  
1010          groups_join_group( self::$group_ids[0], $u );
1011  
1012          $this->assertNotEmpty( groups_is_user_member( $u, self::$group_ids[0] ) );
1013  
1014          wp_delete_user( $u );
1015  
1016          $this->assertFalse( groups_is_user_member( $u, self::$group_ids[0] ) );
1017      }
1018  
1019      /**
1020       * @ticket BP8175
1021       */
1022      public function test_groups_data_should_be_deleted_on_user_delete_multisite() {
1023          if ( ! is_multisite() ) {
1024              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
1025          }
1026  
1027          $u = self::factory()->user->create();
1028  
1029          groups_join_group( self::$group_ids[0], $u );
1030  
1031          $this->assertNotEmpty( groups_is_user_member( $u, self::$group_ids[0] ) );
1032  
1033          wpmu_delete_user( $u );
1034  
1035          $this->assertFalse( groups_is_user_member( $u, self::$group_ids[0] ) );
1036      }
1037  
1038      /**
1039       * @ticket BP8175
1040       */
1041      public function test_groups_data_should_not_be_deleted_on_wp_delete_user_multisite() {
1042          if ( ! is_multisite() ) {
1043              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
1044          }
1045  
1046          $u = self::factory()->user->create();
1047  
1048          groups_join_group( self::$group_ids[0], $u );
1049  
1050          $this->assertNotEmpty( groups_is_user_member( $u, self::$group_ids[0] ) );
1051  
1052          wp_delete_user( $u );
1053  
1054          $this->assertNotEmpty( groups_is_user_member( $u, self::$group_ids[0] ) );
1055      }
1056  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1