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


Generated: Sun Nov 17 01:01:36 2019 Cross-referenced by PHPXref 0.7.1