[ 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          $g = self::factory()->group->create();
1376  
1377          $invite                = new BP_Groups_Member;
1378          $invite->group_id      = $g;
1379          $invite->user_id       = $u;
1380          $invite->date_modified = bp_core_current_time();
1381          $invite->invite_sent   = true;
1382          $invite->is_confirmed  = false;
1383          $invite->save();
1384  
1385          $paged_groups = array();
1386          $paged_groups[] = new stdClass;
1387          $paged_groups[0]->id = $g;
1388  
1389          $group_ids = array( $g );
1390  
1391          $expected = array();
1392          foreach ( $paged_groups as $key => $value ) {
1393              $expected[ $key ] = new stdClass;
1394              $expected[ $key ]->id = $value->id;
1395              $expected[ $key ]->is_member = '0';
1396              $expected[ $key ]->is_invited = '1';
1397              $expected[ $key ]->is_pending = '0';
1398              $expected[ $key ]->is_banned = false;
1399          }
1400  
1401          $old_user = get_current_user_id();
1402          $this->set_current_user( $u );
1403  
1404          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1405  
1406          $this->set_current_user( $old_user );
1407      }
1408  
1409      /**
1410       * @group get_group_extras
1411       */
1412  	public function test_get_group_extras_pending() {
1413          $u = self::factory()->user->create();
1414          $g = self::factory()->group->create();
1415  
1416          $invite                = new BP_Groups_Member;
1417          $invite->group_id      = $g;
1418          $invite->user_id       = $u;
1419          $invite->date_modified = bp_core_current_time();
1420          $invite->invite_sent   = false;
1421          $invite->is_confirmed  = false;
1422          $invite->save();
1423  
1424          $paged_groups = array();
1425          $paged_groups[] = new stdClass;
1426          $paged_groups[0]->id = $g;
1427  
1428          $group_ids = array( $g );
1429  
1430          $expected = array();
1431          foreach ( $paged_groups as $key => $value ) {
1432              $expected[ $key ] = new stdClass;
1433              $expected[ $key ]->id = $value->id;
1434              $expected[ $key ]->is_member = '0';
1435              $expected[ $key ]->is_invited = '0';
1436              $expected[ $key ]->is_pending = '1';
1437              $expected[ $key ]->is_banned = false;
1438          }
1439  
1440          $old_user = get_current_user_id();
1441          $this->set_current_user( $u );
1442  
1443          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1444  
1445          $this->set_current_user( $old_user );
1446      }
1447  
1448      /**
1449       * @group get_group_extras
1450       */
1451  	public function test_get_group_extras_banned() {
1452          $u = self::factory()->user->create();
1453          $g = self::factory()->group->create();
1454  
1455          $member                = new BP_Groups_Member;
1456          $member->group_id      = $g;
1457          $member->user_id       = $u;
1458          $member->date_modified = bp_core_current_time();
1459          $member->is_banned     = true;
1460          $member->save();
1461  
1462          $paged_groups = array();
1463          $paged_groups[] = new stdClass;
1464          $paged_groups[0]->id = $g;
1465  
1466          $group_ids = array( $g );
1467  
1468          $expected = array();
1469          foreach ( $paged_groups as $key => $value ) {
1470              $expected[ $key ] = new stdClass;
1471              $expected[ $key ]->id = $value->id;
1472              $expected[ $key ]->is_member = '0';
1473              $expected[ $key ]->is_invited = '0';
1474              $expected[ $key ]->is_pending = '0';
1475              $expected[ $key ]->is_banned = true;
1476          }
1477  
1478          $old_user = get_current_user_id();
1479          $this->set_current_user( $u );
1480  
1481          $this->assertEquals( $expected, BP_Groups_Group::get_group_extras( $paged_groups, $group_ids ) );
1482  
1483          $this->set_current_user( $old_user );
1484      }
1485  
1486      /**
1487       * @ticket BP5451
1488       */
1489  	public function test_admins_property() {
1490          $user_1 = self::factory()->user->create_and_get();
1491          $g = self::factory()->group->create( array(
1492              'creator_id' => $user_1->ID,
1493          ) );
1494  
1495          $group = new BP_Groups_Group( $g );
1496  
1497          $expected_admin_props = array(
1498              'user_id' => $user_1->ID,
1499              'user_login' => $user_1->user_login,
1500              'user_email' => $user_1->user_email,
1501              'user_nicename' => $user_1->user_nicename,
1502              'is_admin' => 1,
1503              'is_mod' => 0,
1504          );
1505  
1506          $found_admin = $group->admins[0];
1507          foreach ( $expected_admin_props as $prop => $value ) {
1508              $this->assertEquals( $value, $found_admin->{$prop} );
1509          }
1510      }
1511  
1512      /**
1513       * @ticket BP7497
1514       */
1515      public function test_admins_property_should_match_users_without_wp_role() {
1516          $user_1 = self::factory()->user->create_and_get();
1517          $g = self::factory()->group->create( array(
1518              'creator_id' => $user_1->ID,
1519          ) );
1520  
1521          $user_1->remove_all_caps();
1522  
1523          $group = new BP_Groups_Group( $g );
1524  
1525          $this->assertEqualSets( array( $user_1->ID ), wp_list_pluck( $group->admins, 'user_id' ) );
1526      }
1527  
1528      /**
1529       * @ticket BP7677
1530       */
1531  	public function test_demoting_sole_admin() {
1532          $user = self::factory()->user->create_and_get();
1533          $group = self::factory()->group->create_and_get( array(
1534              'creator_id' => $user->ID,
1535          ) );
1536          $member = new BP_Groups_Member( $user->ID, $group->id );
1537          $member->demote();
1538  
1539          $this->assertEmpty( $group->admins );
1540          $this->assertEmpty( $group->mods );
1541      }
1542  
1543      /**
1544       * @ticket BP5451
1545       */
1546  	public function test_mods_property() {
1547          $users = self::factory()->user->create_many( 2 );
1548          $user_1 = new WP_User( $users[0] );
1549          $user_2 = new WP_User( $users[1] );
1550  
1551          $g = self::factory()->group->create( array(
1552              'creator_id' => $user_1->ID,
1553          ) );
1554  
1555          $this->add_user_to_group( $user_2->ID, $g, array( 'is_mod' => 1 ) );
1556  
1557          $group = new BP_Groups_Group( $g );
1558  
1559          $expected_mod_props = array(
1560              'user_id' => $user_2->ID,
1561              'user_login' => $user_2->user_login,
1562              'user_email' => $user_2->user_email,
1563              'user_nicename' => $user_2->user_nicename,
1564              'is_admin' => 0,
1565              'is_mod' => 1,
1566          );
1567  
1568          $found_mod = $group->mods[0];
1569          foreach ( $expected_mod_props as $prop => $value ) {
1570              $this->assertEquals( $value, $found_mod->{$prop} );
1571          }
1572      }
1573  
1574      /**
1575       * @ticket BP5451
1576       */
1577  	public function test_is_member_property() {
1578          $users = self::factory()->user->create_many( 2 );
1579  
1580          $g = self::factory()->group->create( array(
1581              'creator_id' => $users[0],
1582          ) );
1583  
1584          wp_set_current_user( $users[1] );
1585  
1586          $group_a = new BP_Groups_Group( $g );
1587          $this->assertFalse( $group_a->is_member );
1588  
1589          $this->add_user_to_group( $users[1], $g );
1590          $group_b = new BP_Groups_Group( $g );
1591          $this->assertFalse( $group_b->is_member );
1592      }
1593  
1594      /**
1595       * @ticket BP5451
1596       */
1597  	public function test_is_invited_property() {
1598          $users = self::factory()->user->create_many( 2 );
1599  
1600          $g = self::factory()->group->create( array(
1601              'creator_id' => $users[0],
1602          ) );
1603  
1604          wp_set_current_user( $users[1] );
1605  
1606          $group_a = new BP_Groups_Group( $g );
1607          $this->assertFalse( $group_a->is_invited );
1608  
1609          $this->add_user_to_group( $users[1], $g, array(
1610              'invite_sent' => 1,
1611              'inviter_id' => $users[0],
1612              'is_confirmed' => 0,
1613          ) );
1614          $group_b = new BP_Groups_Group( $g );
1615          $this->assertFalse( $group_b->is_invited );
1616      }
1617  
1618      /**
1619       * @ticket BP5451
1620       */
1621  	public function test_is_pending_property() {
1622          $users = self::factory()->user->create_many( 2 );
1623  
1624          $g = self::factory()->group->create( array(
1625              'creator_id' => $users[0],
1626          ) );
1627  
1628          wp_set_current_user( $users[1] );
1629  
1630          $group_a = new BP_Groups_Group( $g );
1631          $this->assertFalse( $group_a->is_pending );
1632  
1633          $this->add_user_to_group( $users[1], $g, array(
1634              'is_confirmed' => 0,
1635              'invite_sent' => 0,
1636              'inviter_id' => 0,
1637          ) );
1638          $group_b = new BP_Groups_Group( $g );
1639          $this->assertFalse( $group_b->is_pending );
1640      }
1641  
1642  
1643      /**
1644       * @group group_types
1645       */
1646  	public function test_group_type_single_value() {
1647          $g1 = self::factory()->group->create();
1648          $g2 = self::factory()->group->create();
1649          bp_groups_register_group_type( 'foo' );
1650          bp_groups_register_group_type( 'bar' );
1651          bp_groups_set_group_type( $g1, 'foo' );
1652          bp_groups_set_group_type( $g2, 'bar' );
1653  
1654          $groups = BP_Groups_Group::get( array(
1655              'group_type' => 'foo',
1656          ) );
1657  
1658          $found = wp_list_pluck( $groups['groups'], 'id' );
1659          $this->assertEquals( array( $g1 ), $found );
1660      }
1661  
1662      /**
1663       * @group group_types
1664       */
1665  	public function test_group_type_array_with_single_value() {
1666          $g1 = self::factory()->group->create();
1667          $g2 = self::factory()->group->create();
1668          bp_groups_register_group_type( 'foo' );
1669          bp_groups_register_group_type( 'bar' );
1670          bp_groups_set_group_type( $g1, 'foo' );
1671          bp_groups_set_group_type( $g2, 'bar' );
1672  
1673          $groups = BP_Groups_Group::get( array(
1674              'group_type' => array( 'foo' ),
1675          ) );
1676  
1677          $found = wp_list_pluck( $groups['groups'], 'id' );
1678          $this->assertEquals( array( $g1 ), $found );
1679      }
1680  
1681      /**
1682       * @group group_types
1683       */
1684  	public function test_group_type_with_comma_separated_list() {
1685          $g1 = self::factory()->group->create();
1686          $g2 = self::factory()->group->create();
1687          bp_groups_register_group_type( 'foo' );
1688          bp_groups_register_group_type( 'bar' );
1689          bp_groups_set_group_type( $g1, 'foo' );
1690          bp_groups_set_group_type( $g2, 'bar' );
1691  
1692          $groups = BP_Groups_Group::get( array(
1693              'group_type' => 'foo, bar',
1694          ) );
1695  
1696          $found = wp_list_pluck( $groups['groups'], 'id' );
1697          $this->assertEqualSets( array( $g1, $g2 ), $found );
1698      }
1699  
1700      /**
1701       * @group group_types
1702       */
1703  	public function test_group_type_array_with_multiple_values() {
1704          $g1 = self::factory()->group->create();
1705          $g2 = self::factory()->group->create();
1706          bp_groups_register_group_type( 'foo' );
1707          bp_groups_register_group_type( 'bar' );
1708          bp_groups_set_group_type( $g1, 'foo' );
1709          bp_groups_set_group_type( $g2, 'bar' );
1710  
1711          $groups = BP_Groups_Group::get( array(
1712              'group_type' => array( 'foo', 'bar' ),
1713          ) );
1714  
1715          $found = wp_list_pluck( $groups['groups'], 'id' );
1716          $this->assertEqualSets( array( $g1, $g2 ), $found );
1717      }
1718  
1719      /**
1720       * @group group_types
1721       */
1722      public function test_group_type_should_discart_non_existing_types_in_comma_separated_value() {
1723          $g1 = self::factory()->group->create();
1724          $g2 = self::factory()->group->create();
1725          bp_groups_register_group_type( 'foo' );
1726          bp_groups_register_group_type( 'bar' );
1727          bp_groups_set_group_type( $g1, 'foo' );
1728          bp_groups_set_group_type( $g2, 'bar' );
1729  
1730          $groups = BP_Groups_Group::get( array(
1731              'group_type' => 'foo, baz',
1732          ) );
1733  
1734          $found = wp_list_pluck( $groups['groups'], 'id' );
1735          $this->assertEquals( array( $g1 ), $found );
1736      }
1737  
1738      /**
1739       * @group group_types
1740       */
1741      public function test_group_type_should_return_empty_when_no_groups_match_specified_types() {
1742          $g1 = self::factory()->group->create();
1743          $g2 = self::factory()->group->create();
1744  
1745          $groups = BP_Groups_Group::get( array(
1746              'group_type' => 'foo, baz',
1747          ) );
1748  
1749          $this->assertEmpty( $groups['groups'] );
1750      }
1751  
1752      /**
1753       * @group group_types
1754       */
1755  	public function test_group_type__in_single_value() {
1756          $g1 = self::factory()->group->create();
1757          $g2 = self::factory()->group->create();
1758          bp_groups_register_group_type( 'foo' );
1759          bp_groups_register_group_type( 'bar' );
1760          bp_groups_set_group_type( $g1, 'foo' );
1761          bp_groups_set_group_type( $g2, 'bar' );
1762  
1763          $groups = BP_Groups_Group::get( array(
1764              'group_type__in' => 'bar',
1765          ) );
1766  
1767          $found = wp_list_pluck( $groups['groups'], 'id' );
1768          $this->assertEquals( array( $g2 ), $found );
1769      }
1770  
1771      /**
1772       * @group group_types
1773       */
1774  	public function test_group_type__in_comma_separated_values() {
1775          $g1 = self::factory()->group->create();
1776          $g2 = self::factory()->group->create();
1777          bp_groups_register_group_type( 'foo' );
1778          bp_groups_register_group_type( 'bar' );
1779          bp_groups_set_group_type( $g1, 'foo' );
1780          bp_groups_set_group_type( $g2, 'bar' );
1781  
1782          $groups = BP_Groups_Group::get( array(
1783              'group_type__in' => 'foo, bar',
1784          ) );
1785  
1786          $found = wp_list_pluck( $groups['groups'], 'id' );
1787          $this->assertEqualSets( array( $g1, $g2 ), $found );
1788      }
1789  
1790      /**
1791       * @group group_types
1792       */
1793  	public function test_group_type__in_array_multiple_values() {
1794          $g1 = self::factory()->group->create();
1795          $g2 = self::factory()->group->create();
1796          bp_groups_register_group_type( 'foo' );
1797          bp_groups_register_group_type( 'bar' );
1798          bp_groups_set_group_type( $g1, 'foo' );
1799          bp_groups_set_group_type( $g2, 'bar' );
1800  
1801          $groups = BP_Groups_Group::get( array(
1802              'group_type__in' => array( 'foo', 'bar' ),
1803          ) );
1804  
1805          $found = wp_list_pluck( $groups['groups'], 'id' );
1806          $this->assertEqualSets( array( $g1, $g2 ), $found );
1807      }
1808  
1809      /**
1810       * @group group_types
1811       */
1812  	public function test_group_type__in_array_with_single_value() {
1813          $g1 = self::factory()->group->create();
1814          $g2 = self::factory()->group->create();
1815          bp_groups_register_group_type( 'foo' );
1816          bp_groups_register_group_type( 'bar' );
1817          bp_groups_set_group_type( $g1, 'foo' );
1818          bp_groups_set_group_type( $g2, 'bar' );
1819  
1820          $groups = BP_Groups_Group::get( array(
1821              'group_type__in' => array( 'foo' ),
1822          ) );
1823  
1824          $found = wp_list_pluck( $groups['groups'], 'id' );
1825          $this->assertEquals( array( $g1 ), $found );
1826      }
1827  
1828      /**
1829       * @group group_types
1830       */
1831      public function test_group_type__in_should_discart_non_existing_types_in_comma_separated_value() {
1832          $g1 = self::factory()->group->create();
1833          $g2 = self::factory()->group->create();
1834          bp_groups_register_group_type( 'foo' );
1835          bp_groups_register_group_type( 'bar' );
1836          bp_groups_set_group_type( $g1, 'foo' );
1837          bp_groups_set_group_type( $g2, 'bar' );
1838  
1839          $groups = BP_Groups_Group::get( array(
1840              'group_type__in' => 'foo, baz',
1841          ) );
1842  
1843          $found = wp_list_pluck( $groups['groups'], 'id' );
1844          $this->assertEquals( array( $g1 ), $found );
1845      }
1846  
1847      /**
1848       * @group group_types
1849       */
1850      public function test_group_type__in_should_return_empty_when_no_groups_match_specified_types() {
1851          $g1 = self::factory()->group->create();
1852          $g2 = self::factory()->group->create();
1853  
1854          $groups = BP_Groups_Group::get( array(
1855              'group_type__in' => 'foo, baz',
1856          ) );
1857  
1858          $this->assertEmpty( $groups['groups'] );
1859      }
1860  
1861      /**
1862       * @group group_types
1863       */
1864      public function test_group_type_should_take_precedence_over_group_type__in() {
1865          $g1 = self::factory()->group->create();
1866          $g2 = self::factory()->group->create();
1867          bp_groups_register_group_type( 'foo' );
1868          bp_groups_register_group_type( 'bar' );
1869          bp_groups_set_group_type( $g1, 'foo' );
1870          bp_groups_set_group_type( $g2, 'bar' );
1871  
1872          $groups = BP_Groups_Group::get( array(
1873              'group_type__in' => 'foo',
1874              'group_type' => 'bar',
1875          ) );
1876  
1877          $found = wp_list_pluck( $groups['groups'], 'id' );
1878          $this->assertEquals( array( $g2 ), $found );
1879      }
1880  
1881      /**
1882       * @group group_types
1883       */
1884      public function test_group_type__not_in_should_return_groups_with_types_and_without_types() {
1885          $g1 = self::factory()->group->create();
1886          $g2 = self::factory()->group->create();
1887          $g3 = self::factory()->group->create();
1888          bp_groups_register_group_type( 'foo' );
1889          bp_groups_register_group_type( 'bar' );
1890          bp_groups_set_group_type( $g1, 'foo' );
1891          bp_groups_set_group_type( $g2, 'bar' );
1892  
1893          $groups = BP_Groups_Group::get( array(
1894              'group_type__not_in' => 'foo',
1895          ) );
1896  
1897          $found = wp_list_pluck( $groups['groups'], 'id' );
1898          $this->assertEquals( array( $g2, $g3 ), $found );
1899      }
1900  
1901      /**
1902       * @group group_types
1903       */
1904      public function test_group_type__not_in_comma_separated_values() {
1905          $g1 = self::factory()->group->create();
1906          $g2 = self::factory()->group->create();
1907          $g3 = self::factory()->group->create();
1908          bp_groups_register_group_type( 'foo' );
1909          bp_groups_register_group_type( 'bar' );
1910          bp_groups_set_group_type( $g1, 'foo' );
1911          bp_groups_set_group_type( $g2, 'bar' );
1912          bp_groups_set_group_type( $g3, 'baz' );
1913  
1914          $groups = BP_Groups_Group::get( array(
1915              'group_type__not_in' => 'foo, bar',
1916          ) );
1917  
1918          $found = wp_list_pluck( $groups['groups'], 'id' );
1919          $this->assertEquals( array( $g3 ), $found );
1920      }
1921  
1922      /**
1923       * @group group_types
1924       */
1925      public function test_group_type__not_array_with_multiple_values() {
1926          $g1 = self::factory()->group->create();
1927          $g2 = self::factory()->group->create();
1928          $g3 = self::factory()->group->create();
1929          bp_groups_register_group_type( 'foo' );
1930          bp_groups_register_group_type( 'bar' );
1931          bp_groups_set_group_type( $g1, 'foo' );
1932          bp_groups_set_group_type( $g2, 'bar' );
1933          bp_groups_set_group_type( $g3, 'baz' );
1934  
1935          $groups = BP_Groups_Group::get( array(
1936              'group_type__not_in' => array( 'foo', 'bar' ),
1937          ) );
1938  
1939          $found = wp_list_pluck( $groups['groups'], 'id' );
1940          $this->assertEquals( array( $g3 ), $found );
1941      }
1942  
1943      /**
1944       * @group group_types
1945       */
1946      public function test_group_type__not_in_should_return_no_results_when_all_groups_mathc_sepecified_type() {
1947          $g1 = self::factory()->group->create();
1948          $g2 = self::factory()->group->create();
1949          $g3 = self::factory()->group->create();
1950          bp_groups_register_group_type( 'foo' );
1951          bp_groups_set_group_type( $g1, 'foo' );
1952          bp_groups_set_group_type( $g2, 'foo' );
1953          bp_groups_set_group_type( $g3, 'foo' );
1954  
1955          $groups = BP_Groups_Group::get( array(
1956              'group_type__not_in' => 'foo',
1957          ) );
1958  
1959          $this->assertEmpty( $groups['groups'] );
1960      }
1961  
1962      /**
1963       * @group group_types
1964       */
1965      public function test_group_type__not_in_takes_precedence_over_group_type() {
1966          $g1 = self::factory()->group->create();
1967          $g2 = self::factory()->group->create();
1968          $g3 = self::factory()->group->create();
1969          bp_groups_register_group_type( 'foo' );
1970          bp_groups_set_group_type( $g1, 'foo' );
1971          bp_groups_set_group_type( $g2, 'foo' );
1972          bp_groups_set_group_type( $g3, 'foo' );
1973  
1974          $groups = BP_Groups_Group::get( array(
1975              'group_type' => 'foo',
1976              'group_type__not_in' => 'foo',
1977          ) );
1978  
1979          $this->assertEmpty( $groups['groups'] );
1980      }
1981  
1982      /**
1983       * @group hierarchical_groups
1984       */
1985  	public function test_get_by_parent_id() {
1986          $g1 = self::factory()->group->create();
1987          $g2 = self::factory()->group->create( array(
1988              'parent_id' => $g1,
1989          ) );
1990          $g3 = self::factory()->group->create( array(
1991              'parent_id' => $g2,
1992          ) );
1993          $g4 = self::factory()->group->create();
1994  
1995          $groups = BP_Groups_Group::get( array(
1996              'parent_id' => $g1,
1997          ) );
1998  
1999          $found = wp_list_pluck( $groups['groups'], 'id' );
2000          $this->assertEquals( array( $g2 ), $found );
2001      }
2002  
2003      /**
2004       * @group hierarchical_groups
2005       */
2006  	public function test_get_by_parent_id_ignore_grandparent() {
2007          $g1 = self::factory()->group->create();
2008          $g2 = self::factory()->group->create( array(
2009              'parent_id' => $g1,
2010          ) );
2011          $g3 = self::factory()->group->create( array(
2012              'parent_id' => $g2,
2013          ) );
2014          $g4 = self::factory()->group->create();
2015  
2016          $groups = BP_Groups_Group::get( array(
2017              'parent_id' => $g2,
2018          ) );
2019  
2020          $found = wp_list_pluck( $groups['groups'], 'id' );
2021          $this->assertEquals( array( $g3 ), $found );
2022      }
2023  
2024      /**
2025       * @group hierarchical_groups
2026       */
2027  	public function test_get_by_parent_id_array() {
2028          $g1 = self::factory()->group->create();
2029          $g2 = self::factory()->group->create( array(
2030              'parent_id' => $g1,
2031          ) );
2032          $g3 = self::factory()->group->create( array(
2033              'parent_id' => $g2,
2034          ) );
2035          $g4 = self::factory()->group->create();
2036  
2037          $groups = BP_Groups_Group::get( array(
2038              'parent_id' => array( $g1, $g2 ),
2039          ) );
2040  
2041          $found = wp_list_pluck( $groups['groups'], 'id' );
2042          $this->assertEqualSets( array( $g2, $g3 ), $found );
2043      }
2044  
2045      /**
2046       * @group hierarchical_groups
2047       */
2048  	public function test_get_by_parent_id_comma_separated_string() {
2049          $g1 = self::factory()->group->create();
2050          $g2 = self::factory()->group->create( array(
2051              'parent_id' => $g1,
2052          ) );
2053          $g3 = self::factory()->group->create( array(
2054              'parent_id' => $g2,
2055          ) );
2056          $g4 = self::factory()->group->create();
2057  
2058          $groups = BP_Groups_Group::get( array(
2059              'parent_id' => "$g1, $g2",
2060          ) );
2061  
2062          $found = wp_list_pluck( $groups['groups'], 'id' );
2063          $this->assertEqualSets( array( $g2, $g3 ), $found );
2064      }
2065  
2066      /**
2067       * @group hierarchical_groups
2068       */
2069  	public function test_get_by_parent_id_top_level_groups() {
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' => 0,
2081          ) );
2082  
2083          $found = wp_list_pluck( $groups['groups'], 'id' );
2084          $this->assertEqualSets( array( $g1, $g4 ), $found );
2085      }
2086  
2087      /**
2088       * @group hierarchical_groups
2089       */
2090      public function test_get_by_parent_id_top_level_groups_using_false() {
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' => false,
2102          ) );
2103  
2104          $found = wp_list_pluck( $groups['groups'], 'id' );
2105          $this->assertEqualSets( array( $g1, $g4 ), $found );
2106      }
2107  
2108      /**
2109       * @group get_by_slug
2110       */
2111  	public function test_get_by_slug() {
2112          $g1 = self::factory()->group->create(array(
2113              'slug'      => 'apr'
2114          ) );
2115          $g2 = self::factory()->group->create( array(
2116              'slug'      => 'jan'
2117          ) );
2118          $g3 = self::factory()->group->create( array(
2119              'slug'      => 'mar'
2120          ) );
2121  
2122          $groups = BP_Groups_Group::get( array(
2123              'slug' => array( 'apr', 'mar' ),
2124          ) );
2125  
2126          $found = wp_list_pluck( $groups['groups'], 'id' );
2127          $this->assertEqualSets( array( $g1, $g3 ), $found );
2128      }
2129  
2130      /**
2131       * @group get_by_slug
2132       */
2133  	public function test_get_by_slug_accept_string() {
2134          $g1 = self::factory()->group->create(array(
2135              'slug'      => 'apr'
2136          ) );
2137          $g2 = self::factory()->group->create( array(
2138              'slug'      => 'jan'
2139          ) );
2140          $g3 = self::factory()->group->create( array(
2141              'slug'      => 'mar'
2142          ) );
2143  
2144          $groups = BP_Groups_Group::get( array(
2145              'slug' => 'jan',
2146          ) );
2147  
2148          $found = wp_list_pluck( $groups['groups'], 'id' );
2149          $this->assertEqualSets( array( $g2 ), $found );
2150      }
2151  
2152      /**
2153       * @group get_by_slug
2154       */
2155      public function test_get_by_slug_accept_comma_separated_string() {
2156          $g1 = self::factory()->group->create(array(
2157              'slug'      => 'apr'
2158          ) );
2159          $g2 = self::factory()->group->create( array(
2160              'slug'      => 'jan'
2161          ) );
2162          $g3 = self::factory()->group->create( array(
2163              'slug'      => 'mar'
2164          ) );
2165  
2166          $groups = BP_Groups_Group::get( array(
2167              'slug' => 'apr, mar',
2168          ) );
2169  
2170          $found = wp_list_pluck( $groups['groups'], 'id' );
2171          $this->assertEqualSets( array( $g1, $g3 ), $found );
2172      }
2173  
2174      /**
2175       * @group get_by_slug
2176       */
2177      public function test_get_by_slug_accept_space_separated_string() {
2178          $g1 = self::factory()->group->create(array(
2179              'slug'      => 'apr'
2180          ) );
2181          $g2 = self::factory()->group->create( array(
2182              'slug'      => 'jan'
2183          ) );
2184          $g3 = self::factory()->group->create( array(
2185              'slug'      => 'mar'
2186          ) );
2187  
2188          $groups = BP_Groups_Group::get( array(
2189              'slug' => 'apr mar',
2190          ) );
2191  
2192          $found = wp_list_pluck( $groups['groups'], 'id' );
2193          $this->assertEqualSets( array( $g1, $g3 ), $found );
2194      }
2195  
2196      /**
2197       * @group get_by_status
2198       */
2199  	public function test_get_by_status() {
2200          $g1 = self::factory()->group->create(array(
2201              'status'      => 'private'
2202          ) );
2203          $g2 = self::factory()->group->create( array(
2204              'status'      => 'public'
2205          ) );
2206          $g3 = self::factory()->group->create( array(
2207              'status'      => 'hidden'
2208          ) );
2209  
2210          $groups = BP_Groups_Group::get( array(
2211              'status' => array( 'private', 'hidden' ),
2212          ) );
2213  
2214          $found = wp_list_pluck( $groups['groups'], 'id' );
2215          $this->assertEqualSets( array( $g1, $g3 ), $found );
2216      }
2217  
2218      /**
2219       * @group get_by_status
2220       */
2221  	public function test_get_by_status_accept_string() {
2222          $g1 = self::factory()->group->create(array(
2223              'status'      => 'private'
2224          ) );
2225          $g2 = self::factory()->group->create( array(
2226              'status'      => 'public'
2227          ) );
2228          $g3 = self::factory()->group->create( array(
2229              'status'      => 'hidden'
2230          ) );
2231  
2232          $groups = BP_Groups_Group::get( array(
2233              'status' => 'public',
2234          ) );
2235  
2236          $found = wp_list_pluck( $groups['groups'], 'id' );
2237          $this->assertEqualSets( array( $g2 ), $found );
2238      }
2239  
2240      /**
2241       * @group get_by_status
2242       */
2243      public function test_get_by_status_accept_comma_separated_string() {
2244          $g1 = self::factory()->group->create(array(
2245              'status'      => 'private'
2246          ) );
2247          $g2 = self::factory()->group->create( array(
2248              'status'      => 'public'
2249          ) );
2250          $g3 = self::factory()->group->create( array(
2251              'status'      => 'hidden'
2252          ) );
2253  
2254          $groups = BP_Groups_Group::get( array(
2255              'status' => 'private, hidden',
2256          ) );
2257  
2258          $found = wp_list_pluck( $groups['groups'], 'id' );
2259          $this->assertEqualSets( array( $g1, $g3 ), $found );
2260      }
2261  
2262      /**
2263       * @group get_ids_only
2264       */
2265  	public function test_get_return_ids_only() {
2266          $now = time();
2267          $g1 = $this->factory->group->create( array(
2268              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
2269          ) );
2270          $g2 = $this->factory->group->create( array(
2271              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
2272          ) );
2273          $g3 = $this->factory->group->create( array(
2274              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*3 ),
2275          )  );
2276  
2277          $groups = BP_Groups_Group::get( array(
2278              'fields' => 'ids',
2279          ) );
2280  
2281          $this->assertSame( array( $g1, $g2, $g3 ), $groups['groups'] );
2282      }
2283  
2284  }
2285  
2286  /**
2287   * Stub class for accessing protected methods
2288   */
2289  class _BP_Groups_Group extends BP_Groups_Group {
2290  	public static function _convert_type_to_order_orderby( $type ) {
2291          return self::convert_type_to_order_orderby( $type );
2292      }
2293  
2294  	public static function _convert_orderby_to_order_by_term( $term ) {
2295          return self::convert_orderby_to_order_by_term( $term );
2296      }
2297  }


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1