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


Generated: Sat Sep 18 01:01:37 2021 Cross-referenced by PHPXref 0.7.1