[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @group groups
   4   * @group BP_Groups_Group
   5   */
   6  class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase {
   7  
   8      /** __construct()  ***************************************************/
   9  
  10      /**
  11       * @group __construct
  12       */
  13  	public function test_non_existent_group() {
  14          $group = new BP_Groups_Group( 123456789 );
  15          $this->assertSame( 0, $group->id );
  16      }
  17  
  18      /**
  19       * @group __construct
  20       * @expectedDeprecated BP_Groups_Group::__construct
  21       */
  22  	public function test_deprecated_arg() {
  23          $group = new BP_Groups_Group( 123456789, array( 'populate_extras' => true ) );
  24          $this->assertSame( 0, $group->id );
  25      }
  26  
  27      /** get() ************************************************************/
  28  
  29      /**
  30       * @group get
  31       */
  32  	public function test_get_group_id_with_slug() {
  33          $slug     = 'group-test';
  34          $g1       = self::factory()->group->create( array( 'slug' => $slug ) );
  35          $group_id = BP_Groups_Group::group_exists( $slug );
  36  
  37          $this->assertSame( $g1, $group_id );
  38      }
  39  
  40      /**
  41       * @group get
  42       */
  43  	public function test_get_group_id_with_empty_slug() {
  44          $this->assertFalse( BP_Groups_Group::group_exists( '' ) );
  45      }
  46  
  47      /**
  48       * @group get
  49       */
  50  	public function test_get_group_id_from_slug_with_empty_slug() {
  51          $this->assertFalse( BP_Groups_Group::get_id_from_slug( '' ) );
  52      }
  53  
  54      /**
  55       * @group get
  56       */
  57  	public function test_get_group_id_from_slug() {
  58          $slug     = 'group-test';
  59          $g1       = self::factory()->group->create( array( 'slug' => $slug ) );
  60          $group_id = BP_Groups_Group::get_id_from_slug( $slug );
  61  
  62          $this->assertSame( $g1, $group_id );
  63      }
  64  
  65      /**
  66       * @group get
  67       * @expectedDeprecated BP_Groups_Group::group_exists
  68       */
  69  	public function test_get_group_with_slug_with_deprecated_args() {
  70          $slug     = 'group-test';
  71          $g1       = self::factory()->group->create( array( 'slug' => $slug ) );
  72          $group_id = BP_Groups_Group::group_exists( $slug, 'random-name' );
  73  
  74          $this->assertSame( $g1, $group_id );
  75      }
  76  
  77      /**
  78       * @group get
  79       */
  80  	public function test_get_with_exclude() {
  81          $g1 = self::factory()->group->create();
  82          $g2 = self::factory()->group->create();
  83          groups_update_groupmeta( $g1, 'foo', 'bar' );
  84  
  85          $groups = BP_Groups_Group::get( array(
  86              'exclude' => array(
  87                  $g1,
  88                  'foobar',
  89              ),
  90          ) );
  91          $ids = wp_list_pluck( $groups['groups'], 'id' );
  92          $this->assertEquals( $ids, array( $g2 ) );
  93      }
  94  
  95      /**
  96       * @group get
  97       */
  98  	public function test_get_with_include() {
  99          $g1 = self::factory()->group->create();
 100          $g2 = self::factory()->group->create();
 101          groups_update_groupmeta( $g1, 'foo', 'bar' );
 102  
 103          $groups = BP_Groups_Group::get( array(
 104              'include' => array(
 105                  $g1,
 106                  'foobar',
 107              ),
 108          ) );
 109          $ids = wp_list_pluck( $groups['groups'], 'id' );
 110          $this->assertEquals( $ids, array( $g1 ) );
 111      }
 112  
 113      /**
 114       * @group get
 115       * @group group_meta_query
 116       */
 117  	public function test_get_with_meta_query() {
 118          $g1 = self::factory()->group->create();
 119          $g2 = self::factory()->group->create();
 120          groups_update_groupmeta( $g1, 'foo', 'bar' );
 121  
 122          $groups = BP_Groups_Group::get( array(
 123              'meta_query' => array(
 124                  array(
 125                      'key' => 'foo',
 126                      'value' => 'bar',
 127                  ),
 128              ),
 129          ) );
 130          $ids = wp_list_pluck( $groups['groups'], 'id' );
 131          $this->assertEquals( $ids, array( $g1 ) );
 132      }
 133  
 134      /**
 135       * @group get
 136       * @group group_meta_query
 137       */
 138  	public function test_get_empty_meta_query() {
 139          $g1 = self::factory()->group->create();
 140          $g2 = self::factory()->group->create();
 141          groups_update_groupmeta( $g1, 'foo', 'bar' );
 142  
 143          $groups = BP_Groups_Group::get( array(
 144              'meta_query' => array(),
 145          ) );
 146          $ids = wp_list_pluck( $groups['groups'], 'id' );
 147          $this->assertEquals( $ids, array( $g1, $g2, ) );
 148      }
 149  
 150      /**
 151       * @group get
 152       * @group group_meta_query
 153       */
 154  	public function test_get_with_meta_query_multiple_clauses() {
 155          $now = time();
 156          $g1 = self::factory()->group->create( array(
 157              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
 158          ) );
 159          $g2 = self::factory()->group->create( array(
 160              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
 161          ) );
 162          $g3 = self::factory()->group->create( array(
 163              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*3 ),
 164          ) );
 165          groups_update_groupmeta( $g1, 'foo', 'bar' );
 166          groups_update_groupmeta( $g2, 'foo', 'bar' );
 167          groups_update_groupmeta( $g1, 'bar', 'barry' );
 168  
 169          $groups = BP_Groups_Group::get( array(
 170              'meta_query' => array(
 171                  'relation' => 'AND',
 172                  array(
 173                      'key' => 'foo',
 174                      'value' => 'bar',
 175                  ),
 176                  array(
 177                      'key' => 'bar',
 178                      'value' => 'barry',
 179                  ),
 180              ),
 181          ) );
 182          $ids = wp_list_pluck( $groups['groups'], 'id' );
 183          $this->assertEquals( $ids, array( $g1 ) );
 184          $this->assertEquals( 1, $groups['total'] );
 185      }
 186  
 187      /**
 188       * @group get
 189       * @group group_meta_query
 190       */
 191      public function test_get_with_meta_query_multiple_clauses_relation_or() {
 192          $now = time();
 193          $g1 = self::factory()->group->create( array(
 194              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
 195          ) );
 196          $g2 = self::factory()->group->create( array(
 197              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
 198          ) );
 199          $g3 = self::factory()->group->create( array(
 200              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*3 ),
 201          ) );
 202          groups_update_groupmeta( $g1, 'foo', 'bar' );
 203          groups_update_groupmeta( $g2, 'foo', 'baz' );
 204          groups_update_groupmeta( $g3, 'bar', 'barry' );
 205  
 206          $groups = BP_Groups_Group::get( array(
 207              'meta_query' => array(
 208                  'relation' => 'OR',
 209                  array(
 210                      'key' => 'foo',
 211                      'value' => 'bar',
 212                  ),
 213                  array(
 214                      'key' => 'bar',
 215                      'value' => 'barry',
 216                  ),
 217              ),
 218          ) );
 219          $ids = wp_list_pluck( $groups['groups'], 'id' );
 220          $this->assertEqualSets( array( $g1, $g3 ), $ids );
 221          $this->assertEquals( 2, $groups['total'] );
 222      }
 223  
 224      /**
 225       * @group get
 226       * @group group_meta_query
 227       * @ticket BP5874
 228       */
 229      public function test_get_with_meta_query_multiple_clauses_relation_or_shared_meta_key() {
 230          $now = time();
 231          $g1 = self::factory()->group->create( array(
 232              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
 233          ) );
 234          $g2 = self::factory()->group->create( array(
 235              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
 236          ) );
 237          $g3 = self::factory()->group->create( array(
 238              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*3 ),
 239          ) );
 240          groups_update_groupmeta( $g1, 'foo', 'bar' );
 241          groups_update_groupmeta( $g2, 'foo', 'baz' );
 242          groups_update_groupmeta( $g3, 'foo', 'barry' );
 243  
 244          $groups = BP_Groups_Group::get( array(
 245              'meta_query' => array(
 246                  'relation' => 'OR',
 247                  array(
 248                      'key' => 'foo',
 249                      'value' => 'bar',
 250                  ),
 251                  array(
 252                      'key' => 'foo',
 253                      'value' => 'baz',
 254                  ),
 255              ),
 256          ) );
 257          $ids = wp_list_pluck( $groups['groups'], 'id' );
 258          $this->assertEqualSets( array( $g1, $g2 ), $ids );
 259          $this->assertEquals( 2, $groups['total'] );
 260      }
 261  
 262      /**
 263       * @group get
 264       * @group group_meta_query
 265       * @ticket BP5824
 266       */
 267      public function test_get_with_meta_query_multiple_keys_with_same_value() {
 268          $now = time();
 269          $g1 = self::factory()->group->create( array(
 270              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
 271          ) );
 272          $g2 = self::factory()->group->create( array(
 273              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
 274          ) );
 275          groups_update_groupmeta( $g1, 'foo', 'bar' );
 276          groups_update_groupmeta( $g2, 'foo2', 'bar' );
 277  
 278          $groups = BP_Groups_Group::get( array(
 279              'meta_query' => array(
 280                  array(
 281                      'key' => 'foo',
 282                      'value' => 'bar',
 283                      'compare' => '=',
 284                  ),
 285              ),
 286          ) );
 287          $ids = wp_list_pluck( $groups['groups'], 'id' );
 288          $this->assertEquals( $ids, array( $g1 ) );
 289          $this->assertEquals( 1, $groups['total'] );
 290      }
 291  
 292      /**
 293       * @group get
 294       * @group date_query
 295       */
 296  	public function test_get_with_date_query_before() {
 297          $u1 = self::factory()->group->create( array(
 298              'last_activity' => date( 'Y-m-d H:i:s', time() ),
 299          ) );
 300          $u2 = self::factory()->group->create( array(
 301              'last_activity' => '2008-03-25 17:13:55',
 302          ) );
 303          $u3 = self::factory()->group->create( array(
 304              'last_activity' => '2010-01-01 12:00',
 305          ) );
 306  
 307          // 'date_query' before test
 308          $groups = BP_Groups_Group::get( array(
 309              'type' => 'active',
 310              'date_query' => array( array(
 311                  'before' => array(
 312                      'year'  => 2010,
 313                      'month' => 1,
 314                      'day'   => 1,
 315                  ),
 316              ) )
 317          ) );
 318  
 319          $this->assertEquals( [ $u2 ], wp_list_pluck( $groups['groups'], 'id' ) );
 320      }
 321  
 322      /**
 323       * @group get
 324       * @group date_query
 325       */
 326  	public function test_get_with_date_query_range() {
 327          $u1 = self::factory()->group->create( array(
 328              'last_activity' => date( 'Y-m-d H:i:s', time() ),
 329          ) );
 330          $u2 = self::factory()->group->create( array(
 331              'last_activity' => '2008-03-25 17:13:55',
 332          ) );
 333          $u3 = self::factory()->group->create( array(
 334              'last_activity' => '2001-01-01 12:00',
 335          ) );
 336  
 337          // 'date_query' range test
 338          $groups = BP_Groups_Group::get( array(
 339              'type' => 'active',
 340              'date_query' => array( array(
 341                  'after'  => 'January 2nd, 2001',
 342                  'before' => array(
 343                      'year'  => 2010,
 344                      'month' => 1,
 345                      'day'   => 1,
 346                  ),
 347                  'inclusive' => true,
 348              ) )
 349          ) );
 350  
 351          $this->assertEquals( [ $u2 ], wp_list_pluck( $groups['groups'], 'id' ) );
 352      }
 353  
 354      /**
 355       * @group get
 356       * @group date_query
 357       */
 358  	public function test_get_with_date_query_after() {
 359          $u1 = self::factory()->group->create( array(
 360              'last_activity' => date( 'Y-m-d H:i:s', time() ),
 361          ) );
 362          $u2 = self::factory()->group->create( array(
 363              'last_activity' => '2008-03-25 17:13:55',
 364          ) );
 365          $u3 = self::factory()->group->create( array(
 366              'last_activity' => '2001-01-01 12:00',
 367          ) );
 368  
 369          // 'date_query' after and relative test
 370          $groups = BP_Groups_Group::get( array(
 371              'type' => 'active',
 372              'date_query' => array( array(
 373                  'after' => '1 day ago'
 374              ) )
 375          ) );
 376  
 377          $this->assertEquals( [ $u1 ], wp_list_pluck( $groups['groups'], 'id' ) );
 378      }
 379  
 380      /**
 381       * @group get
 382       */
 383  	public function test_get_normal_search() {
 384          $g1 = self::factory()->group->create( array(
 385              'name' => 'Cool Group',
 386              'description' => 'This is one cool group',
 387          ) );
 388          $g2 = self::factory()->group->create();
 389  
 390          $groups = BP_Groups_Group::get( array(
 391              'search_terms' => 'Cool',
 392          ) );
 393  
 394          $found = wp_list_pluck( $groups['groups'], 'id' );
 395          $this->assertEquals( array( $g1 ), $found );
 396      }
 397  
 398      /**
 399       * @group get
 400       */
 401  	public function test_get_search_with_underscores() {
 402          $g1 = self::factory()->group->create( array(
 403              'name' => 'Cool Group',
 404              'description' => '_cool_ dude',
 405          ) );
 406          $g2 = self::factory()->group->create();
 407  
 408          $groups = BP_Groups_Group::get( array(
 409              'search_terms' => '_cool_',
 410          ) );
 411  
 412          $found = wp_list_pluck( $groups['groups'], 'id' );
 413          $this->assertEquals( array( $g1 ), $found );
 414      }
 415  
 416      /**
 417       * @group get
 418       */
 419  	public function test_get_search_with_percent_sign() {
 420          $g1 = self::factory()->group->create( array(
 421              'name' => 'Cool Group',
 422              'description' => '100% awesome',
 423          ) );
 424          $g2 = self::factory()->group->create();
 425  
 426          $groups = BP_Groups_Group::get( array(
 427              'search_terms' => '100%',
 428          ) );
 429  
 430          $found = wp_list_pluck( $groups['groups'], 'id' );
 431          $this->assertEquals( array( $g1 ), $found );
 432      }
 433  
 434      /**
 435       * @group get
 436       */
 437  	public function test_get_search_with_quotes() {
 438          $g1 = self::factory()->group->create( array(
 439              'name' => 'Cool Group',
 440              'description' => "'tis sweet",
 441          ) );
 442          $g2 = self::factory()->group->create();
 443  
 444          $groups = BP_Groups_Group::get( array(
 445              'search_terms' => "'tis ",
 446          ) );
 447  
 448          $found = wp_list_pluck( $groups['groups'], 'id' );
 449  
 450          $this->assertEquals( array( $g1 ), $found );
 451      }
 452  
 453      /**
 454       * @group get
 455       */
 456  	public function test_get_search_with_left_wildcard() {
 457          $g1 = self::factory()->group->create( array(
 458              'name' => 'Ye Lads',
 459              'description' => "My Bonnie lies over the ocean",
 460          ) );
 461          $g2 = self::factory()->group->create();
 462  
 463          $groups = BP_Groups_Group::get( array(
 464              'search_terms' => "*ads",
 465          ) );
 466  
 467          $found = wp_list_pluck( $groups['groups'], 'id' );
 468  
 469          $this->assertEquals( array( $g1 ), $found );
 470      }
 471  
 472      /**
 473       * @group get
 474       */
 475      public function test_get_search_with_left_wildcard_should_miss() {
 476          $g1 = self::factory()->group->create( array(
 477              'name' => 'Ye Lads',
 478              'description' => "My Bonnie lies over the ocean",
 479          ) );
 480          $g2 = self::factory()->group->create();
 481  
 482          $groups = BP_Groups_Group::get( array(
 483              'search_terms' => "*la",
 484          ) );
 485  
 486          $found = wp_list_pluck( $groups['groups'], 'id' );
 487  
 488          $this->assertEquals( array(), $found );
 489      }
 490  
 491      /**
 492       * @group get
 493       */
 494  	public function test_get_search_with_right_wildcard() {
 495          $g1 = self::factory()->group->create( array(
 496              'name' => 'Ye Lads',
 497              'description' => "My Bonnie lies over the ocean",
 498          ) );
 499          $g2 = self::factory()->group->create();
 500  
 501          $groups = BP_Groups_Group::get( array(
 502              'search_terms' => "Ye*",
 503          ) );
 504  
 505          $found = wp_list_pluck( $groups['groups'], 'id' );
 506  
 507          $this->assertEquals( array( $g1 ), $found );
 508      }
 509  
 510      /**
 511       * @group get
 512       */
 513      public function test_get_search_with_right_wildcard_should_miss() {
 514          $g1 = self::factory()->group->create( array(
 515              'name' => 'Ye Lads',
 516              'description' => "My Bonnie lies over the ocean",
 517          ) );
 518          $g2 = self::factory()->group->create();
 519  
 520          $groups = BP_Groups_Group::get( array(
 521              'search_terms' => "la*",
 522          ) );
 523  
 524          $found = wp_list_pluck( $groups['groups'], 'id' );
 525  
 526          $this->assertEquals( array(), $found );
 527      }
 528  
 529      /**
 530       * @group get
 531       */
 532  	public function test_get_search_with_both_wildcard() {
 533          $g1 = self::factory()->group->create( array(
 534              'name' => 'Ye Lads',
 535              'description' => "My Bonnie lies over the ocean",
 536          ) );
 537          $g2 = self::factory()->group->create();
 538  
 539          $groups = BP_Groups_Group::get( array(
 540              'search_terms' => "*la*",
 541          ) );
 542  
 543          $found = wp_list_pluck( $groups['groups'], 'id' );
 544  
 545          $this->assertEquals( array( $g1 ), $found );
 546      }
 547  
 548      /**
 549       * @group get
 550       */
 551  	public function test_get_search_limited_to_name_column() {
 552          $g1 = self::factory()->group->create( array(
 553              'name' => 'Ye Lads',
 554              'description' => "My Bonnie lies over the ocean",
 555          ) );
 556          $g2 = self::factory()->group->create();
 557          $g3 = self::factory()->group->create( array(
 558              'name' => 'Bonnie Lasses',
 559              'description' => "That lad is unknown to me",
 560          ) );
 561  
 562          $groups = BP_Groups_Group::get( array(
 563              'search_terms'   => "lad",
 564              'search_columns' => array( 'name' ),
 565          ) );
 566  
 567          $found = wp_list_pluck( $groups['groups'], 'id' );
 568  
 569          $this->assertEquals( array( $g1 ), $found );
 570      }
 571  
 572      /**
 573       * @group get
 574       */
 575  	public function test_get_search_limited_to_description_column() {
 576          $g1 = self::factory()->group->create( array(
 577              'name' => 'Ye Lads',
 578              'description' => "My Bonnie lies over the ocean",
 579          ) );
 580          $g2 = self::factory()->group->create();
 581          $g3 = self::factory()->group->create( array(
 582              'name' => 'Bonnie Lasses',
 583              'description' => "That lad is unknown to me",
 584          ) );
 585  
 586          $groups = BP_Groups_Group::get( array(
 587              'search_terms'   => "lad",
 588              'search_columns' => array( 'description' ),
 589          ) );
 590  
 591          $found = wp_list_pluck( $groups['groups'], 'id' );
 592  
 593          $this->assertEquals( array( $g3 ), $found );
 594      }
 595  
 596      /**
 597       * BP 1.8 will change the default 'type' param in favor of default
 598       * 'order' and 'orderby'. This is to make sure that existing plugins
 599       * will work appropriately
 600       *
 601       * @group get
 602       */
 603      public function test_get_with_default_type_value_should_be_newest() {
 604          $g1 = self::factory()->group->create( array(
 605              'name' => 'A Group',
 606              'date_created' => bp_core_current_time(),
 607          ) );
 608          $g2 = self::factory()->group->create( array(
 609              'name' => 'D Group',
 610              'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
 611          ) );
 612          $g3 = self::factory()->group->create( array(
 613              'name' => 'B Group',
 614              'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100000 ),
 615          ) );
 616          $g4 = self::factory()->group->create( array(
 617              'name' => 'C Group',
 618              'date_created' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
 619          ) );
 620  
 621          $found = BP_Groups_Group::get();
 622  
 623          $this->assertEquals( BP_Groups_Group::get( array( 'type' => 'newest' ) ), $found );
 624      }
 625  
 626      /**
 627       * @group get
 628       */
 629  	public function test_get_with_type_newest() {
 630          $time = time();
 631          $g1 = self::factory()->group->create( array(
 632              'name' => 'A Group',
 633              'date_created' => bp_core_current_time(),
 634          ) );
 635          $g2 = self::factory()->group->create( array(
 636              'name' => 'D Group',
 637              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
 638          ) );
 639          $g3 = self::factory()->group->create( array(
 640              'name' => 'B Group',
 641              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
 642          ) );
 643          $g4 = self::factory()->group->create( array(
 644              'name' => 'C Group',
 645              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
 646          ) );
 647  
 648          $groups = BP_Groups_Group::get( array( 'type' => 'newest' ) );
 649          $found = wp_parse_id_list( wp_list_pluck( $groups['groups'], 'id' ) );
 650          $this->assertEquals( array( $g1, $g2, $g4, $g3 ), $found );
 651      }
 652  
 653      /**
 654       * @group get
 655       */
 656  	public function test_get_with_type_popular() {
 657          $time = time();
 658          $g1 = self::factory()->group->create( array(
 659              'name' => 'A Group',
 660              'date_created' => bp_core_current_time(),
 661          ) );
 662          $g2 = self::factory()->group->create( array(
 663              'name' => 'D Group',
 664              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
 665          ) );
 666          $g3 = self::factory()->group->create( array(
 667              'name' => 'B Group',
 668              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
 669          ) );
 670          $g4 = self::factory()->group->create( array(
 671              'name' => 'C Group',
 672              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
 673          ) );
 674  
 675          groups_update_groupmeta( $g1, 'total_member_count', 1 );
 676          groups_update_groupmeta( $g2, 'total_member_count', 4 );
 677          groups_update_groupmeta( $g3, 'total_member_count', 2 );
 678          groups_update_groupmeta( $g4, 'total_member_count', 3 );
 679  
 680          $groups = BP_Groups_Group::get( array( 'type' => 'popular' ) );
 681          $found = wp_parse_id_list( wp_list_pluck( $groups['groups'], 'id' ) );
 682          $this->assertEquals( array( $g2, $g4, $g3, $g1 ), $found );
 683      }
 684  
 685      /**
 686       * @group get
 687       */
 688  	public function test_get_with_type_alphabetical() {
 689          $time = time();
 690          $g1 = self::factory()->group->create( array(
 691              'name' => 'A Group',
 692              'date_created' => bp_core_current_time(),
 693          ) );
 694          $g2 = self::factory()->group->create( array(
 695              'name' => 'D Group',
 696              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
 697          ) );
 698          $g3 = self::factory()->group->create( array(
 699              'name' => 'B Group',
 700              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
 701          ) );
 702          $g4 = self::factory()->group->create( array(
 703              'name' => 'C Group',
 704              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
 705          ) );
 706  
 707          $groups = BP_Groups_Group::get( array( 'type' => 'alphabetical' ) );
 708          $found = wp_parse_id_list( wp_list_pluck( $groups['groups'], 'id' ) );
 709          $this->assertEquals( array( $g1, $g3, $g4, $g2 ), $found );
 710      }
 711  
 712      /**
 713       * @group get
 714       * @group group_meta_query
 715       * @ticket BP5099
 716       */
 717  	public function test_meta_query_and_total_groups() {
 718          $time = time();
 719  
 720          $g1 = self::factory()->group->create( array(
 721              'name' => 'A Group',
 722              'date_created' => bp_core_current_time(),
 723          ) );
 724          $g2 = self::factory()->group->create( array(
 725              'name' => 'D Group',
 726              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
 727          ) );
 728          $g3 = self::factory()->group->create( array(
 729              'name' => 'B Group',
 730              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
 731          ) );
 732          $g4 = self::factory()->group->create( array(
 733              'name' => 'C Group',
 734              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
 735          ) );
 736  
 737          // mark one group with the metakey 'supergroup'
 738          groups_update_groupmeta( $g1, 'supergroup', 1 );
 739  
 740          // fetch groups with our 'supergroup' metakey
 741          $groups = BP_Groups_Group::get( array(
 742              'meta_query' => array(
 743                  array(
 744                      'key'     => 'supergroup',
 745                      'compare' => 'EXISTS',
 746                  )
 747              )
 748          ) );
 749  
 750          // group total should match 1
 751          $this->assertEquals( '1', $groups['total'] );
 752      }
 753  
 754      /**
 755       * @group get
 756       * @ticket BP5477
 757       */
 758  	public function test_get_groups_page_perpage_params() {
 759          // Create more than 20 groups (20 is the default per_page number)
 760          $group_ids = array();
 761  
 762          for ( $i = 1; $i <= 25; $i++ ) {
 763              $group_ids[] = self::factory()->group->create();
 764          }
 765  
 766          // Tests
 767          // Passing false to 'per_page' and 'page' should result in pagination not being applied
 768          $groups = BP_Groups_Group::get( array(
 769              'per_page' => false,
 770              'page'     => false
 771          ) );
 772  
 773          // Should return all groups; "paged" group total should be 25
 774          $this->assertEquals( count( $group_ids ), count( $groups['groups'] ) );
 775  
 776          unset( $groups );
 777  
 778          // Passing 'per_page' => -1 should result in pagination not being applied.
 779          $groups = BP_Groups_Group::get( array(
 780              'per_page' => -1
 781          ) );
 782  
 783          // Should return all groups; "paged" group total should match 25
 784          $this->assertEquals( count( $group_ids ), count( $groups['groups'] ) );
 785  
 786          unset( $groups );
 787  
 788          // If "per_page" and "page" are both set, should result in pagination being applied.
 789          $groups = BP_Groups_Group::get( array(
 790              'per_page' => 12,
 791              'page'     => 1
 792          ) );
 793  
 794          // Should return top 12 groups only
 795          $this->assertEquals( '12', count( $groups['groups'] ) );
 796      }
 797  
 798      /**
 799       * @group cache
 800       * @ticket BP5451
 801       * @ticket BP6643
 802       */
 803  	public function test_get_queries_should_be_cached() {
 804          global $wpdb;
 805  
 806          $g = self::factory()->group->create();
 807  
 808          $found1 = BP_Groups_Group::get();
 809  
 810          $num_queries = $wpdb->num_queries;
 811  
 812          $found2 = BP_Groups_Group::get();
 813  
 814          $this->assertEqualSets( $found1, $found2 );
 815          $this->assertSame( $num_queries, $wpdb->num_queries );
 816      }
 817  
 818      /**
 819       * @group cache
 820       * @ticket BP5451
 821       * @ticket BP6643
 822       */
 823      public function test_get_query_caches_should_be_busted_by_groupmeta_update() {
 824          global $wpdb;
 825  
 826          $groups = self::factory()->group->create_many( 2 );
 827          groups_update_groupmeta( $groups[0], 'foo', 'bar' );
 828          groups_update_groupmeta( $groups[1], 'foo', 'bar' );
 829  
 830          $found1 = BP_Groups_Group::get( array(
 831              'meta_query' => array(
 832                  array(
 833                      'key' => 'foo',
 834                      'value' => 'bar',
 835                  ),
 836              ),
 837          ) );
 838  
 839          $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found1['groups'], 'id' ) );
 840  
 841          groups_update_groupmeta( $groups[1], 'foo', 'baz' );
 842  
 843          $found2 = BP_Groups_Group::get( array(
 844              'meta_query' => array(
 845                  array(
 846                      'key' => 'foo',
 847                      'value' => 'bar',
 848                  ),
 849              ),
 850          ) );
 851  
 852          $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found2['groups'], 'id' ) );
 853      }
 854  
 855      /**
 856       * @group cache
 857       * @group group_types
 858       * @ticket BP5451
 859       * @ticket BP6643
 860       */
 861      public function test_get_query_caches_should_be_busted_by_group_term_change() {
 862          global $wpdb;
 863  
 864          bp_groups_register_group_type( 'foo' );
 865          bp_groups_register_group_type( 'bar' );
 866  
 867          $groups = self::factory()->group->create_many( 2 );
 868          bp_groups_set_group_type( $groups[0], 'foo' );
 869          bp_groups_set_group_type( $groups[1], 'bar' );
 870  
 871          $found1 = BP_Groups_Group::get( array(
 872              'group_type' => 'foo',
 873          ) );
 874  
 875          $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found1['groups'], 'id' ) );
 876  
 877          bp_groups_set_group_type( $groups[1], 'foo' );
 878  
 879          $found2 = BP_Groups_Group::get( array(
 880              'group_type' => 'foo',
 881          ) );
 882  
 883          $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found2['groups'], 'id' ) );
 884      }
 885  
 886      /**
 887       * @group cache
 888       * @group group_types
 889       * @ticket BP5451
 890       * @ticket BP6643
 891       */
 892      public function test_get_query_caches_should_be_busted_by_group_term_removal() {
 893          global $wpdb;
 894  
 895          bp_groups_register_group_type( 'foo' );
 896  
 897          $groups = self::factory()->group->create_many( 2 );
 898          bp_groups_set_group_type( $groups[0], 'foo' );
 899          bp_groups_set_group_type( $groups[1], 'foo' );
 900  
 901          $found1 = BP_Groups_Group::get( array(
 902              'group_type' => 'foo',
 903          ) );
 904  
 905          $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found1['groups'], 'id' ) );
 906  
 907          bp_groups_remove_group_type( $groups[1], 'foo' );
 908  
 909          $found2 = BP_Groups_Group::get( array(
 910              'group_type' => 'foo',
 911          ) );
 912  
 913          $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found2['groups'], 'id' ) );
 914      }
 915  
 916      /**
 917       * @group cache
 918       * @ticket BP5451
 919       * @ticket BP6643
 920       */
 921      public function test_get_query_caches_should_be_busted_by_group_save() {
 922          global $wpdb;
 923  
 924          $groups = self::factory()->group->create_many( 2 );
 925          groups_update_groupmeta( $groups[0], 'foo', 'bar' );
 926          groups_update_groupmeta( $groups[1], 'foo', 'bar' );
 927  
 928          $found1 = BP_Groups_Group::get( array(
 929              'search_terms' => 'Foo',
 930          ) );
 931  
 932          $this->assertEmpty( $found1['groups'] );
 933  
 934          $group0 = groups_get_group( $groups[0] );
 935          $group0->name = 'Foo';
 936          $group0->save();
 937  
 938          $found2 = BP_Groups_Group::get( array(
 939              'search_terms' => 'Foo',
 940          ) );
 941  
 942          $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found2['groups'], 'id' ) );
 943      }
 944  
 945      /**
 946       * @group cache
 947       * @ticket BP5451
 948       * @ticket BP6643
 949       */
 950      public function test_get_query_caches_should_be_busted_by_group_delete() {
 951          global $wpdb;
 952  
 953          $groups = self::factory()->group->create_many( 2 );
 954  
 955          $found1 = BP_Groups_Group::get();
 956  
 957          $this->assertEqualSets( $groups, wp_list_pluck( $found1['groups'], 'id' ) );
 958  
 959          $group0 = groups_get_group( $groups[0] );
 960          $group0->delete();
 961  
 962          $found2 = BP_Groups_Group::get();
 963  
 964          $this->assertEqualSets( array( $groups[1] ), wp_list_pluck( $found2['groups'], 'id' ) );
 965      }
 966  
 967      /**
 968       * @ticket BP5451
 969       */
 970      public function test_bp_groups_group_magic_isset_with_empty_check() {
 971          $this->old_current_user = get_current_user_id();
 972  
 973          $u = self::factory()->user->create();
 974          $g = self::factory()->group->create( array( 'creator_id' => $u ) );
 975  
 976          // Instantiate group object.
 977          $this->set_current_user( $u );
 978          $group = new BP_Groups_Group( $g );
 979  
 980          // Assert ! empty() check is not false.
 981          $this->assertTrue( ! empty( $group->is_member ) );
 982  
 983          $this->set_current_user( $this->old_current_user );
 984      }
 985  
 986      /** convert_type_to_order_orderby() **********************************/
 987  
 988      /**
 989       * @group convert_type_to_order_orderby
 990       */
 991  	public function test_convert_type_to_order_orderby_newest() {
 992          $expected = array(
 993              'order' => 'DESC',
 994              'orderby' => 'date_created',
 995          );
 996          $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'newest' ) );
 997      }
 998  
 999      /**
1000       * @group convert_type_to_order_orderby
1001       */
1002  	public function test_convert_type_to_order_orderby_active() {
1003          $expected = array(
1004              'order' => 'DESC',
1005              'orderby' => 'last_activity',
1006          );
1007          $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'active' ) );
1008      }
1009  
1010      /**
1011       * @group convert_type_to_order_orderby
1012       */
1013  	public function test_convert_type_to_order_orderby_popular() {
1014          $expected = array(
1015              'order' => 'DESC',
1016              'orderby' => 'total_member_count',
1017          );
1018          $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'popular' ) );
1019      }
1020  
1021      /**
1022       * @group convert_type_to_order_orderby
1023       */
1024      public function test_convert_type_to_order_orderby_alphabetical() {
1025          $expected = array(
1026              'order' => 'ASC',
1027              'orderby' => 'name',
1028          );
1029          $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'alphabetical' ) );
1030      }
1031  
1032      /**
1033       * @group convert_type_to_order_orderby
1034       */
1035  	public function test_convert_type_to_order_orderby_random() {
1036          $expected = array(
1037              // order gets thrown out
1038              'order' => '',
1039              'orderby' => 'random',
1040          );
1041          $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'random' ) );
1042      }
1043  
1044      /**
1045       * @group convert_type_to_order_orderby
1046       */
1047  	public function test_convert_type_to_order_orderby_invalid() {
1048          $expected = array(
1049              'order' => '',
1050              'orderby' => '',
1051          );
1052          $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'foooooooooooooooobar' ) );
1053      }
1054  
1055      /** convert_orderby_to_order_by_term() **********************************/
1056  
1057      /**
1058       * @group convert_orderby_to_order_by_term
1059       */
1060      public function test_convert_orderby_to_order_by_term_date_created() {
1061          $this->assertEquals( 'g.date_created', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'date_created' ) );
1062      }
1063  
1064      /**
1065       * @group convert_orderby_to_order_by_term
1066       */
1067      public function test_convert_orderby_to_order_by_term_last_activity() {
1068          $c = new _BP_Groups_Group();
1069          $this->assertEquals( 'gm_last_activity.meta_value', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'last_activity' ) );
1070      }
1071  
1072      /**
1073       * @group convert_orderby_to_order_by_term
1074       */
1075      public function test_convert_orderby_to_order_by_term_total_member_count() {
1076          $c = new _BP_Groups_Group();
1077          $this->assertEquals( 'CONVERT(gm_total_member_count.meta_value, SIGNED)', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'total_member_count' ) );
1078      }
1079  
1080      /**
1081       * @group convert_orderby_to_order_by_term
1082       */
1083  	public function test_convert_orderby_to_order_by_term_name() {
1084          $c = new _BP_Groups_Group();
1085          $this->assertEquals( 'g.name', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'name' ) );
1086      }
1087  
1088      /**
1089       * @group convert_orderby_to_order_by_term
1090       */
1091  	public function test_convert_orderby_to_order_by_term_random() {
1092          $c = new _BP_Groups_Group();
1093          $this->assertEquals( 'rand()', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'random' ) );
1094      }
1095  
1096      /**
1097       * @group convert_orderby_to_order_by_term
1098       */
1099      public function test_convert_orderby_to_order_by_term_invalid_fallback_to_date_created() {
1100          $c = new _BP_Groups_Group();
1101          $this->assertEquals( _BP_Groups_Group::_convert_orderby_to_order_by_term( 'date_created' ), _BP_Groups_Group::_convert_orderby_to_order_by_term( 'I am a bad boy' ) );
1102      }
1103  
1104      /**
1105       * @group groups_get_orderby_meta_id
1106       */
1107  	public function test_get_orderby_meta_id() {
1108          $g1 = self::factory()->group->create();
1109          $g2 = self::factory()->group->create();
1110          $g3 = self::factory()->group->create();
1111  
1112          groups_update_groupmeta( $g2, 'orderup', 'sammy' );
1113          groups_update_groupmeta( $g1, 'orderup', 'sammy' );
1114  
1115          $args = array(
1116              'meta_query'         => array(
1117                  array(
1118                      'key'   => 'orderup',
1119                      'value' => 'sammy'
1120                  ),
1121              ),
1122              'orderby'           => 'meta_id',
1123              'order'             => 'ASC',
1124          );
1125          $groups = BP_Groups_Group::get( $args );
1126  
1127          $found = wp_list_pluck( $groups['groups'], 'id' );
1128          $this->assertEquals( array( $g2, $g1 ), $found );
1129      }
1130  
1131      /**
1132       * @group groups_get_orderby_meta_id
1133       */
1134      public function test_get_orderby_meta_id_invalid_fallback_to_date_created() {
1135          $time = time();
1136          $g1 = self::factory()->group->create( array(
1137              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 10000 ),
1138          ) );
1139          $g2 = self::factory()->group->create( array(
1140              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
1141          ) );
1142          $g3 = self::factory()->group->create( array(
1143              'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
1144          ) );
1145  
1146          $args = array(
1147              'orderby' => 'meta_id',
1148          );
1149          $groups = BP_Groups_Group::get( $args );
1150  
1151          // Orderby meta_id should be ignored if no meta query is present.
1152          $found = wp_list_pluck( $groups['groups'], 'id' );
1153          $this->assertEquals( array( $g3, $g2, $g1 ), $found );
1154      }
1155  
1156  	public function test_filter_user_groups_normal_search() {
1157          $g1 = self::factory()->group->create( array(
1158              'name' => 'Cool Group',
1159              'description' => 'This is one cool group',
1160          ) );
1161          $g2 = self::factory()->group->create();
1162          $u = self::factory()->user->create();
1163          self::add_user_to_group( $u, $g1 );
1164  
1165          $groups = BP_Groups_Group::filter_user_groups( 'Cool', $u );
1166  
1167          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1168          $this->assertEquals( array( $g1 ), $found );
1169      }
1170  
1171      public function test_filter_user_groups_normal_search_middle_of_string() {
1172          $g1 = self::factory()->group->create( array(
1173              'name' => 'Cool Group',
1174              'description' => 'This group is for mandocellos and oboes.',
1175          ) );
1176          $g2 = self::factory()->group->create();
1177          $u = self::factory()->user->create();
1178          self::add_user_to_group( $u, $g1 );
1179  
1180          $groups = BP_Groups_Group::filter_user_groups( 'cello', $u );
1181  
1182          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1183          $this->assertEquals( array( $g1 ), $found );
1184      }
1185  
1186      public function test_filter_user_groups_search_with_underscores() {
1187          $g1 = self::factory()->group->create( array(
1188              'name' => 'Cool Group',
1189              'description' => '_cool_ dude',
1190          ) );
1191          $g2 = self::factory()->group->create();
1192  
1193          $u = self::factory()->user->create();
1194          self::add_user_to_group( $u, $g1 );
1195          self::add_user_to_group( $u, $g2 );
1196  
1197          $groups = BP_Groups_Group::filter_user_groups( '_cool_', $u );
1198  
1199          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1200          $this->assertEquals( array( $g1 ), $found );
1201      }
1202  
1203      public function test_filter_user_groups_search_with_percent_sign() {
1204          $g1 = self::factory()->group->create( array(
1205              'name' => 'Cool Group',
1206              'description' => '100% awesome',
1207          ) );
1208          $g2 = self::factory()->group->create();
1209  
1210          $u = self::factory()->user->create();
1211          self::add_user_to_group( $u, $g1 );
1212          self::add_user_to_group( $u, $g2 );
1213  
1214          $groups = BP_Groups_Group::filter_user_groups( '100%', $u );
1215  
1216          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1217          $this->assertEquals( array( $g1 ), $found );
1218      }
1219  
1220  	public function test_filter_user_groups_search_with_quotes() {
1221          $g1 = self::factory()->group->create( array(
1222              'name' => 'Cool Group',
1223              'description' => "'tis sweet",
1224          ) );
1225          $g2 = self::factory()->group->create();
1226  
1227          $u = self::factory()->user->create();
1228          self::add_user_to_group( $u, $g1 );
1229          self::add_user_to_group( $u, $g2 );
1230  
1231          $groups = BP_Groups_Group::filter_user_groups( "'tis ", $u );
1232  
1233          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1234  
1235          // @todo
1236          //$this->assertEquals( array( $g1->id ), $found );
1237  
1238          // Remove the following lines when you implement this test.
1239          $this->markTestIncomplete(
1240              'This test has not been implemented yet.'
1241          );
1242      }
1243  
1244  	public function test_search_groups_normal_search() {
1245          $g1 = self::factory()->group->create( array(
1246              'name' => 'Cool Group',
1247              'description' => 'This is one cool group',
1248          ) );
1249          $g2 = self::factory()->group->create();
1250  
1251          $groups = BP_Groups_Group::search_groups( 'Cool' );
1252  
1253          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1254          $this->assertEquals( array( $g1 ), $found );
1255      }
1256  
1257  	public function test_search_groups_search_with_underscores() {
1258          $g1 = self::factory()->group->create( array(
1259              'name' => 'Cool Group',
1260              'description' => '_cool_ dude',
1261          ) );
1262          $g2 = self::factory()->group->create();
1263  
1264          $groups = BP_Groups_Group::search_groups( '_cool_' );
1265  
1266          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1267          $this->assertEquals( array( $g1 ), $found );
1268      }
1269  
1270  	public function test_search_groups_search_with_percent_sign() {
1271          $g1 = self::factory()->group->create( array(
1272              'name' => 'Cool Group',
1273              'description' => '100% awesome',
1274          ) );
1275          $g2 = self::factory()->group->create();
1276  
1277          $groups = BP_Groups_Group::search_groups( '100%' );
1278  
1279          $found = wp_list_pluck( $groups['groups'], 'group_id' );
1280          $this->assertEquals( array( $g1 ), $found );
1281      }
1282  
1283  	public function test_search_groups_search_with_quotes() {
1284          $g1 = self::factory()->group->create( array(
1285              'name' => 'Cool Group',
1286              'description' => "'tis sweet",
1287          ) );
1288          $g2 = self::factory()->group->create();
1289  
1290          $groups = BP_Groups_Group::search_groups( "'tis " );
1291          $found  = wp_list_pluck( $groups['groups'], 'group_id' );
1292  
1293          $this->assertEquals( array( $g1 ), $found );
1294          $this->assertNotContains( $g2, $found );
1295      }
1296  
1297      /**
1298       * @expectedDeprecated BP_Groups_Group::get_by_letter
1299       */
1300  	public function test_get_by_letter_with_deprecated_arg() {
1301          $g1 = self::factory()->group->create( array(
1302              'name'        => 'Awesome Cool Group',
1303              'description' => 'Neat',
1304          ) );
1305          $g2 = self::factory()->group->create();
1306  
1307          $groups = BP_Groups_Group::get_by_letter( 'A', null, null, false );
1308          $found  = wp_list_pluck( $groups['groups'], 'id' );
1309  
1310          $this->assertEquals( array( $g1 ), $found );
1311          $this->assertNotContains( $g2, $found );
1312      }
1313  
1314  	public function test_get_by_letter_typical_use() {
1315          $g1 = self::factory()->group->create( array(
1316              'name'        => 'Awesome Cool Group',
1317              'description' => 'Neat',
1318          ) );
1319          $g2 = self::factory()->group->create();
1320  
1321          $groups = BP_Groups_Group::get_by_letter( 'A' );
1322          $found  = wp_list_pluck( $groups['groups'], 'id' );
1323  
1324          $this->assertEquals( array( $g1 ), $found );
1325          $this->assertNotContains( $g2, $found );
1326      }
1327  
1328  	public function test_get_by_letter_with_exclude() {
1329          $g1 = self::factory()->group->create( array(
1330              'name' => 'Awesome Cool Group',
1331              'description' => 'Neat',
1332          ) );
1333          $g2 = self::factory()->group->create( array(
1334              'name' => 'Another Cool Group',
1335              'description' => 'Awesome',
1336          ) );
1337  
1338          $groups = BP_Groups_Group::get_by_letter( 'A', null, null, true, array( $g1, 'stringthatshouldberemoved' ) );
1339          $found  = wp_list_pluck( $groups['groups'], 'id' );
1340  
1341          $this->assertEquals( array( $g2 ), $found );
1342  
1343      }
1344  
1345  	public function test_get_by_letter_starts_with_apostrophe() {
1346          $g1 = self::factory()->group->create( array(
1347              'name' => "'Tis Sweet",
1348              'description' => 'Neat',
1349          ) );
1350          $g2 = self::factory()->group->create( array(
1351              'name' => 'Another Cool Group',
1352              'description' => 'Awesome',
1353          ) );
1354  
1355          $groups = BP_Groups_Group::get_by_letter( "'" );
1356  
1357          $found = wp_list_pluck( $groups['groups'], 'id' );
1358  
1359          // @todo
1360          // The test fails but at least it's sanitized
1361          //$this->assertEquals( array( $g1->id ), $found );
1362  
1363          // Remove the following lines when you implement this test.
1364          $this->markTestIncomplete(
1365              'This test has not been implemented yet.'
1366          );
1367      }
1368  
1369      /**
1370       * @expectedDeprecated BP_Groups_Group::get_random
1371       */
1372  	public function test_get_random_with_deprecated_arg() {
1373          $g1 = self::factory()->group->create();
1374          $g2 = self::factory()->group->create();
1375  
1376          // There are only two groups, so excluding one should give us the other
1377          $groups = BP_Groups_Group::get_random( null, null, 0, false, false, array( $g1, 'ignore this' ) );
1378          $found  = wp_list_pluck( $groups['groups'], 'id' );
1379  
1380          $this->assertEquals( array( $g2 ), $found );
1381      }
1382  
1383  	public function test_get_random_with_exclude() {
1384          $g1 = self::factory()->group->create();
1385          $g2 = self::factory()->group->create();
1386  
1387          // There are only two groups, so excluding one should give us the other
1388          $groups = BP_Groups_Group::get_random( null, null, 0, false, true, array( $g1, 'ignore this' ) );
1389  
1390          $found = wp_list_pluck( $groups['groups'], 'id' );
1391  
1392          $this->assertEquals( array( $g2 ), $found );
1393      }
1394  
1395  	public function test_get_random_with_search_terms() {
1396          $g1 = self::factory()->group->create( array(
1397              'name' => 'Bodacious',
1398          ) );
1399          $g2 = self::factory()->group->create( array(
1400              'name' => 'Crummy group',
1401          ) );
1402  
1403          // Only one group will match, so the random part doesn't matter
1404          $groups = BP_Groups_Group::get_random( null, null, 0, 'daci' );
1405          $found  = wp_list_pluck( $groups['groups'], 'id' );
1406  
1407          $this->assertEquals( array( $g1 ), $found );
1408          $this->assertNotContains( $g2, $found );
1409      }
1410  
1411      /**
1412       * @group delete
1413       * @group cache
1414       */
1415  	public function test_delete_clear_cache() {
1416          $g = self::factory()->group->create();
1417  
1418          // Prime cache
1419          groups_get_group( $g );
1420  
1421          $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
1422  
1423          $group = new BP_Groups_Group( $g );
1424          $group->delete();
1425  
1426          $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
1427      }
1428  
1429      /**
1430       * @group save
1431       * @group cache
1432       */
1433  	public function test_save_clear_cache() {
1434          $g = self::factory()->group->create();
1435  
1436          // Prime cache
1437          groups_get_group( $g );
1438  
1439          $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
1440  
1441          $group = new BP_Groups_Group( $g );
1442          $group->name = 'Foo';
1443          $group->save();
1444  
1445          $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
1446      }
1447      /**
1448       * @group get_group_extras
1449       */
1450  	public function test_get_group_extras_non_logged_in() {
1451          $paged_groups = array();
1452          $paged_groups[] = new stdClass;
1453          $paged_groups[] = new stdClass;
1454  
1455          $paged_groups[0]->id = 5;
1456          $paged_groups[1]->id = 10;
1457  
1458          $group_ids = array( 5, 10 );
1459  
1460          $expected = array();
1461          foreach ( $paged_groups as $key => $value ) {
1462              $expected[ $key ] = new stdClass;
1463              $expected[ $key ]->id = $value->id;
1464              $expected[ $key ]->is_member = '0';
1465              $expected[ $key ]->is_invited = '0';
1466              $expected[ $key ]->is_pending = '0';
1467              $expected[ $key ]->is_banned = false;
1468          }
1469  
1470          $old_user = get_current_user_id();
1471          $this->set_current_user( 0 );
1472  
1473          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1474  
1475          $this->set_current_user( $old_user );
1476      }
1477  
1478      /**
1479       * @group get_group_extras
1480       */
1481  	public function test_get_group_extras_non_member() {
1482          $u = self::factory()->user->create();
1483          $g = self::factory()->group->create();
1484  
1485          $paged_groups = array();
1486          $paged_groups[] = new stdClass;
1487          $paged_groups[0]->id = $g;
1488  
1489          $group_ids = array( $g );
1490  
1491          $expected = array();
1492          foreach ( $paged_groups as $key => $value ) {
1493              $expected[ $key ] = new stdClass;
1494              $expected[ $key ]->id = $value->id;
1495              $expected[ $key ]->is_member = '0';
1496              $expected[ $key ]->is_invited = '0';
1497              $expected[ $key ]->is_pending = '0';
1498              $expected[ $key ]->is_banned = false;
1499          }
1500  
1501          $old_user = get_current_user_id();
1502          $this->set_current_user( $u );
1503  
1504          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1505  
1506          $this->set_current_user( $old_user );
1507      }
1508  
1509      /**
1510       * @group get_group_extras
1511       */
1512  	public function test_get_group_extras_member() {
1513          $u = self::factory()->user->create();
1514          $g = self::factory()->group->create();
1515          $this->add_user_to_group( $u, $g );
1516  
1517          $paged_groups = array();
1518          $paged_groups[] = new stdClass;
1519          $paged_groups[0]->id = $g;
1520  
1521          $group_ids = array( $g );
1522  
1523          $expected = array();
1524          foreach ( $paged_groups as $key => $value ) {
1525              $expected[ $key ] = new stdClass;
1526              $expected[ $key ]->id = $value->id;
1527              $expected[ $key ]->is_member = '1';
1528              $expected[ $key ]->is_invited = '0';
1529              $expected[ $key ]->is_pending = '0';
1530              $expected[ $key ]->is_banned = false;
1531          }
1532  
1533          $old_user = get_current_user_id();
1534          $this->set_current_user( $u );
1535  
1536          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1537  
1538          $this->set_current_user( $old_user );
1539      }
1540  
1541      /**
1542       * @group get_group_extras
1543       */
1544  	public function test_get_group_extras_invited() {
1545          $u = self::factory()->user->create();
1546          $u2 = self::factory()->user->create();
1547          $g = self::factory()->group->create( array( 'creator_id' => $u2, 'status' => 'private' ) );
1548  
1549          // Outstanding invitations should be left intact.
1550          groups_invite_user( array(
1551              'user_id' => $u,
1552              'group_id' => $g,
1553              'inviter_id' => $u2,
1554              'send_invite' => 1,
1555          ) );
1556  
1557          $paged_groups = array();
1558          $paged_groups[] = new stdClass;
1559          $paged_groups[0]->id = $g;
1560  
1561          $group_ids = array( $g );
1562  
1563          $expected = array();
1564          foreach ( $paged_groups as $key => $value ) {
1565              $expected[ $key ] = new stdClass;
1566              $expected[ $key ]->id = $value->id;
1567              $expected[ $key ]->is_member = '0';
1568              $expected[ $key ]->is_invited = '1';
1569              $expected[ $key ]->is_pending = '0';
1570              $expected[ $key ]->is_banned = false;
1571          }
1572  
1573          $old_user = get_current_user_id();
1574          $this->set_current_user( $u );
1575  
1576          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1577  
1578          $this->set_current_user( $old_user );
1579      }
1580  
1581      /**
1582       * @group get_group_extras
1583       */
1584  	public function test_get_group_extras_pending() {
1585          $u = self::factory()->user->create();
1586          $g = self::factory()->group->create( array( 'status' => 'private' ) );
1587  
1588          // Create membership request
1589          groups_send_membership_request( array(
1590              'user_id'       => $u,
1591              'group_id'      => $g,
1592          ) );
1593  
1594          $paged_groups = array();
1595          $paged_groups[] = new stdClass;
1596          $paged_groups[0]->id = $g;
1597  
1598          $group_ids = array( $g );
1599  
1600          $expected = array();
1601          foreach ( $paged_groups as $key => $value ) {
1602              $expected[ $key ] = new stdClass;
1603              $expected[ $key ]->id = $value->id;
1604              $expected[ $key ]->is_member = '0';
1605              $expected[ $key ]->is_invited = '0';
1606              $expected[ $key ]->is_pending = '1';
1607              $expected[ $key ]->is_banned = false;
1608          }
1609  
1610          $old_user = get_current_user_id();
1611          $this->set_current_user( $u );
1612  
1613          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1614  
1615          $this->set_current_user( $old_user );
1616      }
1617  
1618      /**
1619       * @group get_group_extras
1620       */
1621  	public function test_get_group_extras_banned() {
1622          $u = self::factory()->user->create();
1623          $g = self::factory()->group->create();
1624  
1625          $member                = new BP_Groups_Member;
1626          $member->group_id      = $g;
1627          $member->user_id       = $u;
1628          $member->date_modified = bp_core_current_time();
1629          $member->is_banned     = true;
1630          $member->save();
1631  
1632          $paged_groups = array();
1633          $paged_groups[] = new stdClass;
1634          $paged_groups[0]->id = $g;
1635  
1636          $group_ids = array( $g );
1637  
1638          $expected = array();
1639          foreach ( $paged_groups as $key => $value ) {
1640              $expected[ $key ] = new stdClass;
1641              $expected[ $key ]->id = $value->id;
1642              $expected[ $key ]->is_member = '0';
1643              $expected[ $key ]->is_invited = '0';
1644              $expected[ $key ]->is_pending = '0';
1645              $expected[ $key ]->is_banned = true;
1646          }
1647  
1648          $old_user = get_current_user_id();
1649          $this->set_current_user( $u );
1650  
1651          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1652  
1653          $this->set_current_user( $old_user );
1654      }
1655  
1656      /**
1657       * @ticket BP5451
1658       */
1659  	public function test_admins_property() {
1660          $user_1 = self::factory()->user->create_and_get();
1661          $g = self::factory()->group->create( array(
1662              'creator_id' => $user_1->ID,
1663          ) );
1664  
1665          $group = new BP_Groups_Group( $g );
1666  
1667          $expected_admin_props = array(
1668              'user_id' => $user_1->ID,
1669              'user_login' => $user_1->user_login,
1670              'user_email' => $user_1->user_email,
1671              'user_nicename' => $user_1->user_nicename,
1672              'is_admin' => 1,
1673              'is_mod' => 0,
1674          );
1675  
1676          $found_admin = $group->admins[0];
1677          foreach ( $expected_admin_props as $prop => $value ) {
1678              $this->assertEquals( $value, $found_admin->{$prop} );
1679          }
1680      }
1681  
1682      /**
1683       * @ticket BP7497
1684       */
1685      public function test_admins_property_should_match_users_without_wp_role() {
1686          $user_1 = self::factory()->user->create_and_get();
1687          $g = self::factory()->group->create( array(
1688              'creator_id' => $user_1->ID,
1689          ) );
1690  
1691          $user_1->remove_all_caps();
1692  
1693          $group = new BP_Groups_Group( $g );
1694  
1695          $this->assertEqualSets( array( $user_1->ID ), wp_list_pluck( $group->admins, 'user_id' ) );
1696      }
1697  
1698      /**
1699       * @ticket BP7677
1700       */
1701  	public function test_demoting_sole_admin() {
1702          $user = self::factory()->user->create_and_get();
1703          $group = self::factory()->group->create_and_get( array(
1704              'creator_id' => $user->ID,
1705          ) );
1706          $member = new BP_Groups_Member( $user->ID, $group->id );
1707          $member->demote();
1708  
1709          $this->assertEmpty( $group->admins );
1710          $this->assertEmpty( $group->mods );
1711      }
1712  
1713      /**
1714       * @ticket BP5451
1715       */
1716  	public function test_mods_property() {
1717          $users = self::factory()->user->create_many( 2 );
1718          $user_1 = new WP_User( $users[0] );
1719          $user_2 = new WP_User( $users[1] );
1720  
1721          $g = self::factory()->group->create( array(
1722              'creator_id' => $user_1->ID,
1723          ) );
1724  
1725          $this->add_user_to_group( $user_2->ID, $g, array( 'is_mod' => 1 ) );
1726  
1727          $group = new BP_Groups_Group( $g );
1728  
1729          $expected_mod_props = array(
1730              'user_id' => $user_2->ID,
1731              'user_login' => $user_2->user_login,
1732              'user_email' => $user_2->user_email,
1733              'user_nicename' => $user_2->user_nicename,
1734              'is_admin' => 0,
1735              'is_mod' => 1,
1736          );
1737  
1738          $found_mod = $group->mods[0];
1739          foreach ( $expected_mod_props as $prop => $value ) {
1740              $this->assertEquals( $value, $found_mod->{$prop} );
1741          }
1742      }
1743  
1744      /**
1745       * @ticket BP5451
1746       */
1747  	public function test_is_member_property() {
1748          $users = self::factory()->user->create_many( 2 );
1749  
1750          $g = self::factory()->group->create( array(
1751              'creator_id' => $users[0],
1752          ) );
1753  
1754          wp_set_current_user( $users[1] );
1755  
1756          $group_a = new BP_Groups_Group( $g );
1757          $this->assertFalse( $group_a->is_member );
1758  
1759          $this->add_user_to_group( $users[1], $g );
1760          $group_b = new BP_Groups_Group( $g );
1761          $this->assertFalse( $group_b->is_member );
1762      }
1763  
1764      /**
1765       * @ticket BP5451
1766       */
1767  	public function test_is_invited_property() {
1768          $users = self::factory()->user->create_many( 2 );
1769  
1770          $g = self::factory()->group->create( array(
1771              'creator_id' => $users[0],
1772          ) );
1773  
1774          wp_set_current_user( $users[1] );
1775  
1776          $group_a = new BP_Groups_Group( $g );
1777          $this->assertFalse( $group_a->is_invited );
1778  
1779          groups_invite_user( array(
1780              'user_id'    => $users[1],
1781              'group_id'   => $g,
1782              'inviter_id' => $users[0],
1783              'send_invite' => 1
1784          ) );
1785  
1786          $group_b = new BP_Groups_Group( $g );
1787          $this->assertFalse( $group_b->is_invited );
1788      }
1789  
1790      /**
1791       * @ticket BP5451
1792       */
1793  	public function test_is_pending_property() {
1794          $users = self::factory()->user->create_many( 2 );
1795  
1796          $g = self::factory()->group->create( array(
1797              'creator_id' => $users[0],
1798          ) );
1799  
1800          wp_set_current_user( $users[1] );
1801  
1802          $group_a = new BP_Groups_Group( $g );
1803          $this->assertFalse( $group_a->is_pending );
1804  
1805          groups_send_membership_request( array(
1806              'user_id' => $users[1],
1807              'group_id' => $g
1808          ) );
1809  
1810          $group_b = new BP_Groups_Group( $g );
1811          $this->assertFalse( $group_b->is_pending );
1812      }
1813  
1814  
1815      /**
1816       * @group group_types
1817       */
1818  	public function test_group_type_single_value() {
1819          $g1 = self::factory()->group->create();
1820          $g2 = self::factory()->group->create();
1821          bp_groups_register_group_type( 'foo' );
1822          bp_groups_register_group_type( 'bar' );
1823          bp_groups_set_group_type( $g1, 'foo' );
1824          bp_groups_set_group_type( $g2, 'bar' );
1825  
1826          $groups = BP_Groups_Group::get( array(
1827              'group_type' => 'foo',
1828          ) );
1829  
1830          $found = wp_list_pluck( $groups['groups'], 'id' );
1831          $this->assertEquals( array( $g1 ), $found );
1832      }
1833  
1834      /**
1835       * @group group_types
1836       */
1837  	public function test_group_type_array_with_single_value() {
1838          $g1 = self::factory()->group->create();
1839          $g2 = self::factory()->group->create();
1840          bp_groups_register_group_type( 'foo' );
1841          bp_groups_register_group_type( 'bar' );
1842          bp_groups_set_group_type( $g1, 'foo' );
1843          bp_groups_set_group_type( $g2, 'bar' );
1844  
1845          $groups = BP_Groups_Group::get( array(
1846              'group_type' => array( 'foo' ),
1847          ) );
1848  
1849          $found = wp_list_pluck( $groups['groups'], 'id' );
1850          $this->assertEquals( array( $g1 ), $found );
1851      }
1852  
1853      /**
1854       * @group group_types
1855       */
1856  	public function test_group_type_with_comma_separated_list() {
1857          $g1 = self::factory()->group->create();
1858          $g2 = self::factory()->group->create();
1859          bp_groups_register_group_type( 'foo' );
1860          bp_groups_register_group_type( 'bar' );
1861          bp_groups_set_group_type( $g1, 'foo' );
1862          bp_groups_set_group_type( $g2, 'bar' );
1863  
1864          $groups = BP_Groups_Group::get( array(
1865              'group_type' => 'foo, bar',
1866          ) );
1867  
1868          $found = wp_list_pluck( $groups['groups'], 'id' );
1869          $this->assertEqualSets( array( $g1, $g2 ), $found );
1870      }
1871  
1872      /**
1873       * @group group_types
1874       */
1875  	public function test_group_type_array_with_multiple_values() {
1876          $g1 = self::factory()->group->create();
1877          $g2 = self::factory()->group->create();
1878          bp_groups_register_group_type( 'foo' );
1879          bp_groups_register_group_type( 'bar' );
1880          bp_groups_set_group_type( $g1, 'foo' );
1881          bp_groups_set_group_type( $g2, 'bar' );
1882  
1883          $groups = BP_Groups_Group::get( array(
1884              'group_type' => array( 'foo', 'bar' ),
1885          ) );
1886  
1887          $found = wp_list_pluck( $groups['groups'], 'id' );
1888          $this->assertEqualSets( array( $g1, $g2 ), $found );
1889      }
1890  
1891      /**
1892       * @group group_types
1893       */
1894      public function test_group_type_should_discart_non_existing_types_in_comma_separated_value() {
1895          $g1 = self::factory()->group->create();
1896          $g2 = self::factory()->group->create();
1897          bp_groups_register_group_type( 'foo' );
1898          bp_groups_register_group_type( 'bar' );
1899          bp_groups_set_group_type( $g1, 'foo' );
1900          bp_groups_set_group_type( $g2, 'bar' );
1901  
1902          $groups = BP_Groups_Group::get( array(
1903              'group_type' => 'foo, baz',
1904          ) );
1905  
1906          $found = wp_list_pluck( $groups['groups'], 'id' );
1907          $this->assertEquals( array( $g1 ), $found );
1908      }
1909  
1910      /**
1911       * @group group_types
1912       */
1913      public function test_group_type_should_return_empty_when_no_groups_match_specified_types() {
1914          $g1 = self::factory()->group->create();
1915          $g2 = self::factory()->group->create();
1916  
1917          $groups = BP_Groups_Group::get( array(
1918              'group_type' => 'foo, baz',
1919          ) );
1920  
1921          $this->assertEmpty( $groups['groups'] );
1922      }
1923  
1924      /**
1925       * @group group_types
1926       */
1927  	public function test_group_type__in_single_value() {
1928          $g1 = self::factory()->group->create();
1929          $g2 = self::factory()->group->create();
1930          bp_groups_register_group_type( 'foo' );
1931          bp_groups_register_group_type( 'bar' );
1932          bp_groups_set_group_type( $g1, 'foo' );
1933          bp_groups_set_group_type( $g2, 'bar' );
1934  
1935          $groups = BP_Groups_Group::get( array(
1936              'group_type__in' => 'bar',
1937          ) );
1938  
1939          $found = wp_list_pluck( $groups['groups'], 'id' );
1940          $this->assertEquals( array( $g2 ), $found );
1941      }
1942  
1943      /**
1944       * @group group_types
1945       */
1946  	public function test_group_type__in_comma_separated_values() {
1947          $g1 = self::factory()->group->create();
1948          $g2 = self::factory()->group->create();
1949          bp_groups_register_group_type( 'foo' );
1950          bp_groups_register_group_type( 'bar' );
1951          bp_groups_set_group_type( $g1, 'foo' );
1952          bp_groups_set_group_type( $g2, 'bar' );
1953  
1954          $groups = BP_Groups_Group::get( array(
1955              'group_type__in' => 'foo, bar',
1956          ) );
1957  
1958          $found = wp_list_pluck( $groups['groups'], 'id' );
1959          $this->assertEqualSets( array( $g1, $g2 ), $found );
1960      }
1961  
1962      /**
1963       * @group group_types
1964       */
1965  	public function test_group_type__in_array_multiple_values() {
1966          $g1 = self::factory()->group->create();
1967          $g2 = self::factory()->group->create();
1968          bp_groups_register_group_type( 'foo' );
1969          bp_groups_register_group_type( 'bar' );
1970          bp_groups_set_group_type( $g1, 'foo' );
1971          bp_groups_set_group_type( $g2, 'bar' );
1972  
1973          $groups = BP_Groups_Group::get( array(
1974              'group_type__in' => array( 'foo', 'bar' ),
1975          ) );
1976  
1977          $found = wp_list_pluck( $groups['groups'], 'id' );
1978          $this->assertEqualSets( array( $g1, $g2 ), $found );
1979      }
1980  
1981      /**
1982       * @group group_types
1983       */
1984  	public function test_group_type__in_array_with_single_value() {
1985          $g1 = self::factory()->group->create();
1986          $g2 = self::factory()->group->create();
1987          bp_groups_register_group_type( 'foo' );
1988          bp_groups_register_group_type( 'bar' );
1989          bp_groups_set_group_type( $g1, 'foo' );
1990          bp_groups_set_group_type( $g2, 'bar' );
1991  
1992          $groups = BP_Groups_Group::get( array(
1993              'group_type__in' => array( 'foo' ),
1994          ) );
1995  
1996          $found = wp_list_pluck( $groups['groups'], 'id' );
1997          $this->assertEquals( array( $g1 ), $found );
1998      }
1999  
2000      /**
2001       * @group group_types
2002       */
2003      public function test_group_type__in_should_discart_non_existing_types_in_comma_separated_value() {
2004          $g1 = self::factory()->group->create();
2005          $g2 = self::factory()->group->create();
2006          bp_groups_register_group_type( 'foo' );
2007          bp_groups_register_group_type( 'bar' );
2008          bp_groups_set_group_type( $g1, 'foo' );
2009          bp_groups_set_group_type( $g2, 'bar' );
2010  
2011          $groups = BP_Groups_Group::get( array(
2012              'group_type__in' => 'foo, baz',
2013          ) );
2014  
2015          $found = wp_list_pluck( $groups['groups'], 'id' );
2016          $this->assertEquals( array( $g1 ), $found );
2017      }
2018  
2019      /**
2020       * @group group_types
2021       */
2022      public function test_group_type__in_should_return_empty_when_no_groups_match_specified_types() {
2023          $g1 = self::factory()->group->create();
2024          $g2 = self::factory()->group->create();
2025  
2026          $groups = BP_Groups_Group::get( array(
2027              'group_type__in' => 'foo, baz',
2028          ) );
2029  
2030          $this->assertEmpty( $groups['groups'] );
2031      }
2032  
2033      /**
2034       * @group group_types
2035       */
2036      public function test_group_type_should_take_precedence_over_group_type__in() {
2037          $g1 = self::factory()->group->create();
2038          $g2 = self::factory()->group->create();
2039          bp_groups_register_group_type( 'foo' );
2040          bp_groups_register_group_type( 'bar' );
2041          bp_groups_set_group_type( $g1, 'foo' );
2042          bp_groups_set_group_type( $g2, 'bar' );
2043  
2044          $groups = BP_Groups_Group::get( array(
2045              'group_type__in' => 'foo',
2046              'group_type' => 'bar',
2047          ) );
2048  
2049          $found = wp_list_pluck( $groups['groups'], 'id' );
2050          $this->assertEquals( array( $g2 ), $found );
2051      }
2052  
2053      /**
2054       * @group group_types
2055       */
2056      public function test_group_type__not_in_should_return_groups_with_types_and_without_types() {
2057          $g1 = self::factory()->group->create();
2058          $g2 = self::factory()->group->create();
2059          $g3 = self::factory()->group->create();
2060          bp_groups_register_group_type( 'foo' );
2061          bp_groups_register_group_type( 'bar' );
2062          bp_groups_set_group_type( $g1, 'foo' );
2063          bp_groups_set_group_type( $g2, 'bar' );
2064  
2065          $groups = BP_Groups_Group::get( array(
2066              'group_type__not_in' => 'foo',
2067          ) );
2068  
2069          $found = wp_list_pluck( $groups['groups'], 'id' );
2070          $this->assertEquals( array( $g2, $g3 ), $found );
2071      }
2072  
2073      /**
2074       * @group group_types
2075       */
2076      public function test_group_type__not_in_comma_separated_values() {
2077          $g1 = self::factory()->group->create();
2078          $g2 = self::factory()->group->create();
2079          $g3 = self::factory()->group->create();
2080          bp_groups_register_group_type( 'foo' );
2081          bp_groups_register_group_type( 'bar' );
2082          bp_groups_set_group_type( $g1, 'foo' );
2083          bp_groups_set_group_type( $g2, 'bar' );
2084          bp_groups_set_group_type( $g3, 'baz' );
2085  
2086          $groups = BP_Groups_Group::get( array(
2087              'group_type__not_in' => 'foo, bar',
2088          ) );
2089  
2090          $found = wp_list_pluck( $groups['groups'], 'id' );
2091          $this->assertEquals( array( $g3 ), $found );
2092      }
2093  
2094      /**
2095       * @group group_types
2096       */
2097      public function test_group_type__not_array_with_multiple_values() {
2098          $g1 = self::factory()->group->create();
2099          $g2 = self::factory()->group->create();
2100          $g3 = self::factory()->group->create();
2101          bp_groups_register_group_type( 'foo' );
2102          bp_groups_register_group_type( 'bar' );
2103          bp_groups_set_group_type( $g1, 'foo' );
2104          bp_groups_set_group_type( $g2, 'bar' );
2105          bp_groups_set_group_type( $g3, 'baz' );
2106  
2107          $groups = BP_Groups_Group::get( array(
2108              'group_type__not_in' => array( 'foo', 'bar' ),
2109          ) );
2110  
2111          $found = wp_list_pluck( $groups['groups'], 'id' );
2112          $this->assertEquals( array( $g3 ), $found );
2113      }
2114  
2115      /**
2116       * @group group_types
2117       */
2118      public function test_group_type__not_in_should_return_no_results_when_all_groups_mathc_sepecified_type() {
2119          $g1 = self::factory()->group->create();
2120          $g2 = self::factory()->group->create();
2121          $g3 = self::factory()->group->create();
2122          bp_groups_register_group_type( 'foo' );
2123          bp_groups_set_group_type( $g1, 'foo' );
2124          bp_groups_set_group_type( $g2, 'foo' );
2125          bp_groups_set_group_type( $g3, 'foo' );
2126  
2127          $groups = BP_Groups_Group::get( array(
2128              'group_type__not_in' => 'foo',
2129          ) );
2130  
2131          $this->assertEmpty( $groups['groups'] );
2132      }
2133  
2134      /**
2135       * @group group_types
2136       */
2137      public function test_group_type__not_in_takes_precedence_over_group_type() {
2138          $g1 = self::factory()->group->create();
2139          $g2 = self::factory()->group->create();
2140          $g3 = self::factory()->group->create();
2141          bp_groups_register_group_type( 'foo' );
2142          bp_groups_set_group_type( $g1, 'foo' );
2143          bp_groups_set_group_type( $g2, 'foo' );
2144          bp_groups_set_group_type( $g3, 'foo' );
2145  
2146          $groups = BP_Groups_Group::get( array(
2147              'group_type' => 'foo',
2148              'group_type__not_in' => 'foo',
2149          ) );
2150  
2151          $this->assertEmpty( $groups['groups'] );
2152      }
2153  
2154      /**
2155       * @group hierarchical_groups
2156       */
2157  	public function test_get_by_parent_id() {
2158          $g1 = self::factory()->group->create();
2159          $g2 = self::factory()->group->create( array(
2160              'parent_id' => $g1,
2161          ) );
2162          $g3 = self::factory()->group->create( array(
2163              'parent_id' => $g2,
2164          ) );
2165          $g4 = self::factory()->group->create();
2166  
2167          $groups = BP_Groups_Group::get( array(
2168              'parent_id' => $g1,
2169          ) );
2170  
2171          $found = wp_list_pluck( $groups['groups'], 'id' );
2172          $this->assertEquals( array( $g2 ), $found );
2173      }
2174  
2175      /**
2176       * @group hierarchical_groups
2177       */
2178  	public function test_get_by_parent_id_ignore_grandparent() {
2179          $g1 = self::factory()->group->create();
2180          $g2 = self::factory()->group->create( array(
2181              'parent_id' => $g1,
2182          ) );
2183          $g3 = self::factory()->group->create( array(
2184              'parent_id' => $g2,
2185          ) );
2186          $g4 = self::factory()->group->create();
2187  
2188          $groups = BP_Groups_Group::get( array(
2189              'parent_id' => $g2,
2190          ) );
2191  
2192          $found = wp_list_pluck( $groups['groups'], 'id' );
2193          $this->assertEquals( array( $g3 ), $found );
2194      }
2195  
2196      /**
2197       * @group hierarchical_groups
2198       */
2199  	public function test_get_by_parent_id_array() {
2200          $g1 = self::factory()->group->create();
2201          $g2 = self::factory()->group->create( array(
2202              'parent_id' => $g1,
2203          ) );
2204          $g3 = self::factory()->group->create( array(
2205              'parent_id' => $g2,
2206          ) );
2207          $g4 = self::factory()->group->create();
2208  
2209          $groups = BP_Groups_Group::get( array(
2210              'parent_id' => array( $g1, $g2 ),
2211          ) );
2212  
2213          $found = wp_list_pluck( $groups['groups'], 'id' );
2214          $this->assertEqualSets( array( $g2, $g3 ), $found );
2215      }
2216  
2217      /**
2218       * @group hierarchical_groups
2219       */
2220  	public function test_get_by_parent_id_comma_separated_string() {
2221          $g1 = self::factory()->group->create();
2222          $g2 = self::factory()->group->create( array(
2223              'parent_id' => $g1,
2224          ) );
2225          $g3 = self::factory()->group->create( array(
2226              'parent_id' => $g2,
2227          ) );
2228          $g4 = self::factory()->group->create();
2229  
2230          $groups = BP_Groups_Group::get( array(
2231              'parent_id' => "$g1, $g2",
2232          ) );
2233  
2234          $found = wp_list_pluck( $groups['groups'], 'id' );
2235          $this->assertEqualSets( array( $g2, $g3 ), $found );
2236      }
2237  
2238      /**
2239       * @group hierarchical_groups
2240       */
2241  	public function test_get_by_parent_id_top_level_groups() {
2242          $g1 = self::factory()->group->create();
2243          $g2 = self::factory()->group->create( array(
2244              'parent_id' => $g1,
2245          ) );
2246          $g3 = self::factory()->group->create( array(
2247              'parent_id' => $g2,
2248          ) );
2249          $g4 = self::factory()->group->create();
2250  
2251          $groups = BP_Groups_Group::get( array(
2252              'parent_id' => 0,
2253          ) );
2254  
2255          $found = wp_list_pluck( $groups['groups'], 'id' );
2256          $this->assertEqualSets( array( $g1, $g4 ), $found );
2257      }
2258  
2259      /**
2260       * @group hierarchical_groups
2261       */
2262      public function test_get_by_parent_id_top_level_groups_using_false() {
2263          $g1 = self::factory()->group->create();
2264          $g2 = self::factory()->group->create( array(
2265              'parent_id' => $g1,
2266          ) );
2267          $g3 = self::factory()->group->create( array(
2268              'parent_id' => $g2,
2269          ) );
2270          $g4 = self::factory()->group->create();
2271  
2272          $groups = BP_Groups_Group::get( array(
2273              'parent_id' => false,
2274          ) );
2275  
2276          $found = wp_list_pluck( $groups['groups'], 'id' );
2277          $this->assertEqualSets( array( $g1, $g4 ), $found );
2278      }
2279  
2280      /**
2281       * @group get_by_slug
2282       */
2283  	public function test_get_by_slug() {
2284          $g1 = self::factory()->group->create(array(
2285              'slug'      => 'apr'
2286          ) );
2287          $g2 = self::factory()->group->create( array(
2288              'slug'      => 'jan'
2289          ) );
2290          $g3 = self::factory()->group->create( array(
2291              'slug'      => 'mar'
2292          ) );
2293  
2294          $groups = BP_Groups_Group::get( array(
2295              'slug' => array( 'apr', 'mar' ),
2296          ) );
2297  
2298          $found = wp_list_pluck( $groups['groups'], 'id' );
2299          $this->assertEqualSets( array( $g1, $g3 ), $found );
2300      }
2301  
2302      /**
2303       * @group get_by_slug
2304       */
2305  	public function test_get_by_slug_accept_string() {
2306          $g1 = self::factory()->group->create(array(
2307              'slug'      => 'apr'
2308          ) );
2309          $g2 = self::factory()->group->create( array(
2310              'slug'      => 'jan'
2311          ) );
2312          $g3 = self::factory()->group->create( array(
2313              'slug'      => 'mar'
2314          ) );
2315  
2316          $groups = BP_Groups_Group::get( array(
2317              'slug' => 'jan',
2318          ) );
2319  
2320          $found = wp_list_pluck( $groups['groups'], 'id' );
2321          $this->assertEqualSets( array( $g2 ), $found );
2322      }
2323  
2324      /**
2325       * @group get_by_slug
2326       */
2327      public function test_get_by_slug_accept_comma_separated_string() {
2328          $g1 = self::factory()->group->create(array(
2329              'slug'      => 'apr'
2330          ) );
2331          $g2 = self::factory()->group->create( array(
2332              'slug'      => 'jan'
2333          ) );
2334          $g3 = self::factory()->group->create( array(
2335              'slug'      => 'mar'
2336          ) );
2337  
2338          $groups = BP_Groups_Group::get( array(
2339              'slug' => 'apr, mar',
2340          ) );
2341  
2342          $found = wp_list_pluck( $groups['groups'], 'id' );
2343          $this->assertEqualSets( array( $g1, $g3 ), $found );
2344      }
2345  
2346      /**
2347       * @group get_by_slug
2348       */
2349      public function test_get_by_slug_accept_space_separated_string() {
2350          $g1 = self::factory()->group->create(array(
2351              'slug'      => 'apr'
2352          ) );
2353          $g2 = self::factory()->group->create( array(
2354              'slug'      => 'jan'
2355          ) );
2356          $g3 = self::factory()->group->create( array(
2357              'slug'      => 'mar'
2358          ) );
2359  
2360          $groups = BP_Groups_Group::get( array(
2361              'slug' => 'apr mar',
2362          ) );
2363  
2364          $found = wp_list_pluck( $groups['groups'], 'id' );
2365          $this->assertEqualSets( array( $g1, $g3 ), $found );
2366      }
2367  
2368      /**
2369       * @group get_by_status
2370       */
2371  	public function test_get_by_status() {
2372          $g1 = self::factory()->group->create(array(
2373              'status'      => 'private'
2374          ) );
2375          $g2 = self::factory()->group->create( array(
2376              'status'      => 'public'
2377          ) );
2378          $g3 = self::factory()->group->create( array(
2379              'status'      => 'hidden'
2380          ) );
2381  
2382          $groups = BP_Groups_Group::get( array(
2383              'status' => array( 'private', 'hidden' ),
2384          ) );
2385  
2386          $found = wp_list_pluck( $groups['groups'], 'id' );
2387          $this->assertEqualSets( array( $g1, $g3 ), $found );
2388      }
2389  
2390      /**
2391       * @group get_by_status
2392       */
2393  	public function test_get_by_status_accept_string() {
2394          $g1 = self::factory()->group->create(array(
2395              'status'      => 'private'
2396          ) );
2397          $g2 = self::factory()->group->create( array(
2398              'status'      => 'public'
2399          ) );
2400          $g3 = self::factory()->group->create( array(
2401              'status'      => 'hidden'
2402          ) );
2403  
2404          $groups = BP_Groups_Group::get( array(
2405              'status' => 'public',
2406          ) );
2407  
2408          $found = wp_list_pluck( $groups['groups'], 'id' );
2409          $this->assertEqualSets( array( $g2 ), $found );
2410      }
2411  
2412      /**
2413       * @group get_by_status
2414       */
2415      public function test_get_by_status_accept_comma_separated_string() {
2416          $g1 = self::factory()->group->create(array(
2417              'status'      => 'private'
2418          ) );
2419          $g2 = self::factory()->group->create( array(
2420              'status'      => 'public'
2421          ) );
2422          $g3 = self::factory()->group->create( array(
2423              'status'      => 'hidden'
2424          ) );
2425  
2426          $groups = BP_Groups_Group::get( array(
2427              'status' => 'private, hidden',
2428          ) );
2429  
2430          $found = wp_list_pluck( $groups['groups'], 'id' );
2431          $this->assertEqualSets( array( $g1, $g3 ), $found );
2432      }
2433  
2434      /**
2435       * @group get_ids_only
2436       */
2437  	public function test_get_return_ids_only() {
2438          $now = time();
2439          $g1 = $this->factory->group->create( array(
2440              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
2441          ) );
2442          $g2 = $this->factory->group->create( array(
2443              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
2444          ) );
2445          $g3 = $this->factory->group->create( array(
2446              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*3 ),
2447          )  );
2448  
2449          $groups = BP_Groups_Group::get( array(
2450              'fields' => 'ids',
2451          ) );
2452  
2453          $this->assertSame( array( $g1, $g2, $g3 ), $groups['groups'] );
2454      }
2455  
2456  }
2457  
2458  /**
2459   * Stub class for accessing protected methods
2460   */
2461  class _BP_Groups_Group extends BP_Groups_Group {
2462  	public static function _convert_type_to_order_orderby( $type ) {
2463          return self::convert_type_to_order_orderby( $type );
2464      }
2465  
2466  	public static function _convert_orderby_to_order_by_term( $term ) {
2467          return self::convert_orderby_to_order_by_term( $term );
2468      }
2469  }


Generated: Mon Jan 24 01:01:00 2022 Cross-referenced by PHPXref 0.7.1