[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/groups/ -> template.php (source)

   1  <?php
   2  /**
   3   * @group groups
   4   * @group template
   5   */
   6  class BP_Tests_Groups_Template extends BP_UnitTestCase {
   7  
   8  	public function setUp() {
   9          parent::setUp();
  10  
  11          if ( isset( $GLOBALS['groups_template'] ) ) {
  12              $this->groups_template = $GLOBALS['groups_template'];
  13          }
  14      }
  15  
  16  	public function tearDown() {
  17          if ( $this->groups_template ) {
  18              $GLOBALS['groups_template'] = $this->groups_template;
  19          }
  20  
  21          parent::tearDown();
  22      }
  23  
  24      /**
  25       * Integration test to make sure meta_query is getting passed through
  26       *
  27       * @group bp_has_groups
  28       */
  29  	public function test_bp_has_groups_with_meta_query() {
  30          $g1 = self::factory()->group->create();
  31          $g2 = self::factory()->group->create();
  32          groups_update_groupmeta( $g1, 'foo', 'bar' );
  33  
  34          global $groups_template;
  35          bp_has_groups( array(
  36              'meta_query' => array(
  37                  array(
  38                      'key' => 'foo',
  39                      'value' => 'bar',
  40                  ),
  41              ),
  42          ) );
  43  
  44          $ids = wp_list_pluck( $groups_template->groups, 'id' );
  45          $this->assertEquals( $ids, array( $g1, ) );
  46      }
  47  
  48      /**
  49       * Integration test to make sure order and orderby are interpreted when
  50       * no 'type' value has been passed
  51       *
  52       * @group bp_has_groups
  53       */
  54      public function test_bp_has_groups_with_order_orderby_with_null_type() {
  55          $now = time();
  56          $g1 = self::factory()->group->create( array(
  57              'name' => 'AAAAA',
  58              'date_created' => gmdate( 'Y-m-d H:i:s', $now - 100 ),
  59              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 1000000 ),
  60          ) );
  61          $g2 = self::factory()->group->create( array(
  62              'name' => 'BBBBB',
  63              'date_created' => gmdate( 'Y-m-d H:i:s', $now - 1000000 ),
  64              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 10000 ),
  65          ) );
  66          $g3 = self::factory()->group->create( array(
  67              'name' => 'CCCCC',
  68              'date_created' => gmdate( 'Y-m-d H:i:s', $now - 10000 ),
  69              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 10 ),
  70          ) );
  71  
  72          global $groups_template;
  73          bp_has_groups( array(
  74              'order' => 'ASC',
  75              'orderby' => 'name',
  76          ) );
  77  
  78          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
  79          $this->assertEquals( array( $g1, $g2, $g3, ), $ids );
  80      }
  81  
  82      /**
  83       * Integration test to make sure 'order' is set to 'DESC' and 'orderby'
  84       * to 'last_activity' when no type or order/orderby params are passed.
  85       * This ensures backpat with the old system, where 'active' was the
  86       * default type param, and there were no order/orderby params.
  87       *
  88       * @group bp_has_groups
  89       */
  90      public function test_bp_has_groups_defaults_to_DESC_last_activity_for_default_type_active_backpat() {
  91          $now = time();
  92          $g1 = self::factory()->group->create( array(
  93              'name' => 'AAAAA',
  94              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 100 ),
  95          ) );
  96          $g2 = self::factory()->group->create( array(
  97              'name' => 'BBBBB',
  98              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 1000000 ),
  99          ) );
 100          $g3 = self::factory()->group->create( array(
 101              'name' => 'CCCCC',
 102              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 10000 ),
 103          ) );
 104  
 105          global $groups_template;
 106          bp_has_groups();
 107  
 108          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 109          $this->assertEquals( array( $g1, $g3, $g2, ), $ids );
 110      }
 111  
 112      /**
 113       * Test using the 'slug' parameter in bp_has_groups()
 114       *
 115       * Note: The 'slug' parameter currently also requires the 'type' to be set
 116       * to 'single-group'.
 117       *
 118       * @group bp_has_groups
 119       */
 120  	public function test_bp_has_groups_single_group_with_slug() {
 121          $g1 = self::factory()->group->create( array(
 122              'name' => 'Test Group',
 123              'slug' => 'test-group',
 124              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
 125          ) );
 126  
 127          global $groups_template;
 128          bp_has_groups( array(
 129              'type' => 'single-group',
 130              'slug' => 'test-group',
 131          ) );
 132  
 133          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 134          $this->assertEquals( array( $g1 ), $ids );
 135  
 136          $this->assertEquals( 1, $groups_template->group_count );
 137      }
 138  
 139      /**
 140       * Test using the 'status' parameter in bp_has_groups()
 141       *
 142       * @group bp_has_groups
 143       */
 144  	public function test_bp_has_groups_status() {
 145          $g1 = self::factory()->group->create( array(
 146              'status' => 'public',
 147          ) );
 148          $g2 = self::factory()->group->create( array(
 149              'status' => 'private',
 150          ) );
 151          $g3 = self::factory()->group->create( array(
 152              'status' => 'hidden',
 153          ) );
 154  
 155          global $groups_template;
 156          bp_has_groups( array(
 157              'status' => 'private',
 158          ) );
 159  
 160          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 161          $this->assertEqualSets( array( $g2 ), $ids );
 162  
 163          $this->assertEquals( 1, $groups_template->group_count );
 164      }
 165  
 166      /**
 167       * @group hierarchical_groups
 168       */
 169  	public function test_bp_has_groups_parent_id() {
 170          $g1 = self::factory()->group->create();
 171          $g2 = self::factory()->group->create( array(
 172              'parent_id' => $g1,
 173          ) );
 174          $g3 = self::factory()->group->create( array(
 175              'parent_id' => $g2,
 176          ) );
 177          $g4 = self::factory()->group->create();
 178  
 179          global $groups_template;
 180          bp_has_groups( array(
 181              'parent_id' => $g1,
 182          ) );
 183  
 184          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 185          $this->assertEquals( array( $g2 ), $ids );
 186      }
 187  
 188      /**
 189       * @group hierarchical_groups
 190       */
 191  	public function test_bp_has_groups_parent_id_array() {
 192          $g1 = self::factory()->group->create();
 193          $g2 = self::factory()->group->create( array(
 194              'parent_id' => $g1,
 195          ) );
 196          $g3 = self::factory()->group->create( array(
 197              'parent_id' => $g2,
 198          ) );
 199          $g4 = self::factory()->group->create();
 200  
 201          global $groups_template;
 202          bp_has_groups( array(
 203              'parent_id' => array( $g1, $g2 ),
 204          ) );
 205  
 206          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 207          $this->assertEqualSets( array( $g2, $g3 ), $ids );
 208      }
 209  
 210      /**
 211       * @group hierarchical_groups
 212       */
 213  	public function test_bp_has_groups_parent_id_comma_separated() {
 214          $g1 = self::factory()->group->create();
 215          $g2 = self::factory()->group->create( array(
 216              'parent_id' => $g1,
 217          ) );
 218          $g3 = self::factory()->group->create( array(
 219              'parent_id' => $g2,
 220          ) );
 221          $g4 = self::factory()->group->create();
 222  
 223          global $groups_template;
 224          bp_has_groups( array(
 225              'parent_id' => "{$g1},{$g2}",
 226          ) );
 227  
 228          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 229          $this->assertEqualSets( array( $g2, $g3 ), $ids );
 230      }
 231  
 232      /**
 233       * @group hierarchical_groups
 234       */
 235  	public function test_bp_has_groups_parent_id_null() {
 236          $g1 = self::factory()->group->create();
 237          $g2 = self::factory()->group->create( array(
 238              'parent_id' => $g1,
 239          ) );
 240          $g3 = self::factory()->group->create( array(
 241              'parent_id' => $g2,
 242          ) );
 243          $g4 = self::factory()->group->create();
 244  
 245          global $groups_template;
 246          bp_has_groups( array(
 247              'parent_id' => null,
 248          ) );
 249  
 250          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 251          $this->assertEqualSets( array( $g1, $g2, $g3, $g4 ), $ids );
 252      }
 253  
 254      /**
 255       * @group hierarchical_groups
 256       */
 257  	public function test_bp_has_groups_parent_id_top_level_groups() {
 258          $g1 = self::factory()->group->create();
 259          $g2 = self::factory()->group->create( array(
 260              'parent_id' => $g1,
 261          ) );
 262          $g3 = self::factory()->group->create( array(
 263              'parent_id' => $g2,
 264          ) );
 265          $g4 = self::factory()->group->create();
 266  
 267          global $groups_template;
 268          bp_has_groups( array(
 269              'parent_id' => 0,
 270          ) );
 271  
 272          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 273          $this->assertEqualSets( array( $g1, $g4 ), $ids );
 274      }
 275  
 276      /**
 277       * @group hierarchical_groups
 278       */
 279  	public function test_bp_has_groups_parent_id_top_level_groups_using_false() {
 280          $g1 = self::factory()->group->create();
 281          $g2 = self::factory()->group->create( array(
 282              'parent_id' => $g1,
 283          ) );
 284          $g3 = self::factory()->group->create( array(
 285              'parent_id' => $g2,
 286          ) );
 287          $g4 = self::factory()->group->create();
 288  
 289          global $groups_template;
 290          bp_has_groups( array(
 291              'parent_id' => false,
 292          ) );
 293  
 294          $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
 295          $this->assertEqualSets( array( $g1, $g4 ), $ids );
 296      }
 297  
 298      /**
 299       * @group bp_group_has_members
 300       */
 301  	public function test_bp_group_has_members_vanilla() {
 302          $u1 = self::factory()->user->create();
 303          $u2 = self::factory()->user->create();
 304          $g  = self::factory()->group->create( array(
 305              'creator_id' => $u1,
 306          ) );
 307  
 308          $this->add_user_to_group( $u1, $g );
 309  
 310          global $members_template;
 311          bp_group_has_members( array(
 312              'group_id' => $g,
 313              'exclude_admins_mods' => false,
 314          ) );
 315  
 316          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 317          $this->assertEquals( array( $u1, ), $ids );
 318      }
 319  
 320      /**
 321       * Switching from BP_Groups_Member to BP_Group_Member_Query meant a
 322       * change in the format of the values returned from the query. For
 323       * backward compatibility, we translate some of the return values
 324       * of BP_Group_Member_Query to the older format. This test makes sure
 325       * that the translation happens properly.
 326       *
 327       * @group bp_group_has_members
 328       */
 329      public function test_bp_group_has_members_backpat_retval_format() {
 330          $u1 = self::factory()->user->create();
 331          $u2 = self::factory()->user->create();
 332          $g = self::factory()->group->create( array( 'creator_id' => $u2 ) );
 333  
 334          $date_modified = gmdate( 'Y-m-d H:i:s', time() - 100 );
 335  
 336          $this->add_user_to_group( $u1, $g, array( 'date_modified' => $date_modified ) );
 337  
 338          global $members_template;
 339          bp_group_has_members( array(
 340              'group_id' => $g,
 341          ) );
 342  
 343          $u1_object = new WP_User( $u1 );
 344  
 345          $expected = new stdClass;
 346          $expected->user_id = $u1;
 347          $expected->date_modified = $date_modified;
 348          $expected->is_banned = 0;
 349          $expected->user_login = $u1_object->user_login;
 350          $expected->user_nicename = $u1_object->user_nicename;
 351          $expected->user_email = $u1_object->user_email;
 352          $expected->display_name = $u1_object->display_name;
 353  
 354          // In order to use assertEquals, we need to discard the
 355          // irrelevant properties of the found object. Hack alert
 356          $found = new stdClass;
 357          foreach ( array( 'user_id', 'date_modified', 'is_banned', 'user_login', 'user_nicename', 'user_email', 'display_name' ) as $key ) {
 358              if ( isset( $members_template->members[0]->{$key} ) ) {
 359                  $found->{$key} = $members_template->members[0]->{$key};
 360              }
 361          }
 362  
 363          $this->assertEquals( $expected, $found );
 364      }
 365  
 366      /**
 367       * @group bp_group_has_members
 368       */
 369  	public function test_bp_group_has_members_with_per_page() {
 370          $g = self::factory()->group->create();
 371  
 372          $users = array();
 373          for ( $i = 1; $i <= 5; $i++ ) {
 374              $users[ $i ] = self::factory()->user->create();
 375          }
 376  
 377          $expected = array();
 378          $now = time();
 379          for ( $i = 3; $i <= 5; $i++ ) {
 380              $this->add_user_to_group( $users[ $i ], $g, array(
 381                  'date_modified' => date( 'Y-m-d H:i:s', $now - ( 60 * $i ) ),
 382              ) );
 383              $expected[] = $users[ $i ];
 384          }
 385  
 386          // hack it down to 2 (per page arg below)
 387          $expected = array_slice( $expected, 0, 2 );
 388  
 389          global $members_template;
 390          bp_group_has_members( array(
 391              'group_id' => $g,
 392              'per_page' => 2,
 393          ) );
 394  
 395          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 396          $this->assertEquals( $expected, $ids );
 397      }
 398  
 399      /**
 400       * Note: 'max' is a weird parameter. It just changes the member_count
 401       * in the global - not the sql query at all. I'm testing what it
 402       * appears to be designed to do, not what it feels like it ought to do
 403       * if it made any sense. Programming is fun, QED.
 404       *
 405       * @group bp_group_has_members
 406       */
 407  	public function test_bp_group_has_members_with_max() {
 408          $g = self::factory()->group->create();
 409  
 410          $users = array();
 411          for ( $i = 1; $i <= 5; $i++ ) {
 412              $users[ $i ] = self::factory()->user->create();
 413          }
 414  
 415          $expected = array();
 416          for ( $i = 3; $i <= 5; $i++ ) {
 417              $this->add_user_to_group( $users[ $i ], $g );
 418              $expected[] = $users[ $i ];
 419          }
 420  
 421          global $members_template;
 422          bp_group_has_members( array(
 423              'group_id' => $g,
 424              'max' => 1,
 425          ) );
 426  
 427          $this->assertEquals( 1, $members_template->member_count );
 428      }
 429  
 430      /**
 431       * @group bp_group_has_members
 432       */
 433  	public function test_bp_group_has_members_with_exclude() {
 434          $g = self::factory()->group->create();
 435          $u1 = self::factory()->user->create();
 436          $u2 = self::factory()->user->create();
 437  
 438          $this->add_user_to_group( $u1, $g );
 439          $this->add_user_to_group( $u2, $g );
 440  
 441          global $members_template;
 442          bp_group_has_members( array(
 443              'group_id' => $g,
 444              'exclude' => $u1,
 445          ) );
 446  
 447          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 448          $this->assertEquals( array( $u2 ), $ids );
 449      }
 450  
 451      /**
 452       * @group bp_group_has_members
 453       */
 454      public function test_bp_group_has_members_with_exclude_admins_mods_1() {
 455          $g = self::factory()->group->create();
 456          $u1 = self::factory()->user->create();
 457          $u2 = self::factory()->user->create();
 458          $u3 = self::factory()->user->create();
 459  
 460          $this->add_user_to_group( $u1, $g );
 461          $this->add_user_to_group( $u2, $g );
 462          $this->add_user_to_group( $u3, $g );
 463  
 464          $m1 = new BP_Groups_Member( $u1, $g );
 465          $m1->promote( 'admin' );
 466          $m2 = new BP_Groups_Member( $u2, $g );
 467          $m2->promote( 'mod' );
 468  
 469          global $members_template;
 470          bp_group_has_members( array(
 471              'group_id' => $g,
 472              'exclude_admins_mods' => 1,
 473          ) );
 474  
 475          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 476          $this->assertEquals( array( $u3 ), $ids );
 477      }
 478  
 479      /**
 480       * @group bp_group_has_members
 481       */
 482      public function test_bp_group_has_members_with_exclude_admins_mods_0() {
 483          $u1 = self::factory()->user->create();
 484          $u2 = self::factory()->user->create();
 485          $u3 = self::factory()->user->create();
 486          $g = self::factory()->group->create( array(
 487              'creator_id' => $u1,
 488          ) );
 489  
 490          $now = time();
 491          $this->add_user_to_group( $u2, $g, array(
 492              'date_modified' => date( 'Y-m-d H:i:s', $now - 60 ),
 493          ) );
 494          $this->add_user_to_group( $u3, $g, array(
 495              'date_modified' => date( 'Y-m-d H:i:s', $now - 60*60 ),
 496          ) );
 497  
 498          $m1 = new BP_Groups_Member( $u1, $g );
 499          $m1->promote( 'admin' );
 500          $m2 = new BP_Groups_Member( $u2, $g );
 501          $m2->promote( 'mod' );
 502  
 503          global $members_template;
 504          bp_group_has_members( array(
 505              'group_id' => $g,
 506              'exclude_admins_mods' => 0,
 507          ) );
 508  
 509          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 510          $this->assertEquals( array( $u1, $u2, $u3 ), $ids );
 511      }
 512  
 513      /**
 514       * @group bp_group_has_members
 515       */
 516      public function test_bp_group_has_members_with_exclude_banned_1() {
 517          $g = self::factory()->group->create();
 518          $u1 = self::factory()->user->create();
 519          $u2 = self::factory()->user->create();
 520  
 521          $this->add_user_to_group( $u1, $g );
 522          $this->add_user_to_group( $u2, $g );
 523  
 524          $m1 = new BP_Groups_Member( $u1, $g );
 525          $m1->ban();
 526  
 527          global $members_template;
 528          bp_group_has_members( array(
 529              'group_id' => $g,
 530              'exclude_banned' => 1,
 531          ) );
 532  
 533          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 534          $this->assertEquals( array( $u2, ), $ids );
 535      }
 536  
 537      /**
 538       * @group bp_group_has_members
 539       */
 540      public function test_bp_group_has_members_with_exclude_banned_0() {
 541          $u1 = self::factory()->user->create();
 542          $u2 = self::factory()->user->create();
 543          $u3 = self::factory()->user->create();
 544  
 545          $g = self::factory()->group->create( array(
 546              'creator_id' => $u1,
 547          ) );
 548  
 549          $now = time();
 550          $this->add_user_to_group( $u2, $g, array(
 551              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*24 ),
 552          ) );
 553          $this->add_user_to_group( $u3, $g, array(
 554              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*12 ),
 555          ) );
 556  
 557          $m2 = new BP_Groups_Member( $u2, $g );
 558          $m2->ban();
 559  
 560          global $members_template;
 561          bp_group_has_members( array(
 562              'group_id' => $g,
 563              'exclude_banned' => 0,
 564              'exclude_admins_mods' => false,
 565          ) );
 566  
 567          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 568          $this->assertEquals( array( $u1, $u3, $u2 ), $ids );
 569      }
 570  
 571      /**
 572       * Default sort order should be the joined date
 573       *
 574       * @ticket BP5106
 575       * @group bp_group_has_members
 576       */
 577  	public function test_bp_group_has_members_default_order() {
 578          $now = time();
 579          $u1 = self::factory()->user->create( array(
 580              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 60 ),
 581          ) );
 582          $u2 = self::factory()->user->create( array(
 583              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 600 ),
 584          ) );
 585          $u3 = self::factory()->user->create( array(
 586              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 6000 ),
 587          ) );
 588  
 589          $g = self::factory()->group->create( array(
 590              'creator_id' => $u1,
 591          ) );
 592  
 593          $this->add_user_to_group( $u2, $g, array(
 594              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*24 ),
 595          ) );
 596  
 597          $this->add_user_to_group( $u3, $g, array(
 598              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*12 ),
 599          ) );
 600  
 601          global $members_template;
 602          bp_group_has_members( array(
 603              'group_id' => $g,
 604              'exclude_banned' => 0,
 605              'exclude_admins_mods' => false,
 606          ) );
 607  
 608          $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
 609          $this->assertEquals( array( $u1, $u3, $u2, ), $ids );
 610      }
 611  
 612      /**
 613       * @group bp_group_has_invites
 614       * @group BP_Groups_Invite_Template
 615       */
 616  	public function test_bp_group_has_invites_template_structure() {
 617          $now = time();
 618          $u1 = self::factory()->user->create( array(
 619              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 60 ),
 620          ) );
 621          $u2 = self::factory()->user->create( array(
 622              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 600 ),
 623          ) );
 624          $u3 = self::factory()->user->create( array(
 625              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 6000 ),
 626          ) );
 627          $u4 = self::factory()->user->create( array(
 628              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 60000 ),
 629          ) );
 630  
 631  
 632          $g = self::factory()->group->create( array(
 633              'creator_id' => $u1,
 634          ) );
 635  
 636          groups_invite_user( array(
 637              'user_id'       => $u2,
 638              'group_id'      => $g,
 639              'inviter_id'    => $u1,
 640              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 50 ),
 641              'send_invite'   => 1,
 642          ) );
 643  
 644          groups_invite_user( array(
 645              'user_id'       => $u3,
 646              'group_id'      => $g,
 647              'inviter_id'    => $u1,
 648              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 30 ),
 649              'send_invite'   => 1,
 650          ) );
 651  
 652          $m4 = $this->add_user_to_group( $u4, $g, array(
 653              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*36 ),
 654              'is_confirmed' => 1
 655          ) );
 656  
 657          // Populate the global
 658          bp_group_has_invites( array(
 659              'group_id' => $g,
 660              'user_id' => $u1,
 661          ) );
 662  
 663          global $invites_template;
 664  
 665          $found_users = array(
 666              0 => $u2,
 667              1 => $u3,
 668          );
 669  
 670          // Invites array
 671          $this->assertEquals( $found_users, $invites_template->invites );
 672  
 673          // Make sure user is set when loop starts
 674          $counter = 0;
 675          while ( bp_group_invites() ) : bp_group_the_invite();
 676              $this->assertEquals( $g, $invites_template->invite->group_id );
 677  
 678              $this_user = new BP_Core_User( $found_users[ $counter ] );
 679              foreach ( get_object_vars( $this_user ) as $k => $v ) {
 680                  // Doesn't matter if the backpat provides *more*
 681                  // details than the old method, so we skip cases
 682                  // where the BP_Core_User value is empty
 683                  if ( empty( $v ) ) {
 684                      continue;
 685                  }
 686  
 687                  $this->assertEquals( $v, $invites_template->invite->user->{$k} );
 688              }
 689              $counter++;
 690          endwhile;
 691      }
 692  
 693      /**
 694       * @group bp_group_has_invites
 695       * @group BP_Groups_Invite_Template
 696       */
 697  	public function test_bp_group_has_invites_pagination() {
 698          $u1 = self::factory()->user->create( array(
 699              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 60 ),
 700          ) );
 701  
 702          $g = self::factory()->group->create( array(
 703              'creator_id' => $u1,
 704          ) );
 705  
 706          $users = array();
 707          $now = time();
 708          for ( $i = 1; $i < 6; $i++ ) {
 709              $users[ $i ] = self::factory()->user->create( array(
 710                  'last_activity' => gmdate( 'Y-m-d H:i:s', $now - $i*60 ),
 711              ) );
 712  
 713              $inv = groups_invite_user( array(
 714                  'user_id'       => $users[ $i ],
 715                  'group_id'      => $g,
 716                  'inviter_id'    => $u1,
 717                  'send_invite'   => 1,
 718                  'date_modified' => gmdate( 'Y-m-d H:i:s', $now - $i*60 ),
 719              ) );
 720          }
 721  
 722          // Populate the global
 723          bp_group_has_invites( array(
 724              'group_id' => $g,
 725              'user_id' => $u1,
 726              'page' => 2,
 727              'per_page' => 2,
 728          ) );
 729  
 730          global $invites_template;
 731  
 732          $this->assertEquals( array( $users[ 3 ], $users[ 2 ] ), $invites_template->invites );
 733      }
 734  
 735      /**
 736       * Checks for proper queried items
 737       *
 738       * @group bp_group_has_membership_requests
 739       * @group BP_Group_Membership_Requests_Template
 740       */
 741  	public function test_bp_group_has_membership_requests_results() {
 742          $now = time();
 743          $u1 = self::factory()->user->create( array(
 744              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 60 ),
 745          ) );
 746  
 747          $g = self::factory()->group->create( array(
 748              'creator_id' => $u1,
 749              'status'     => 'private'
 750          ) );
 751  
 752          $users = array();
 753          $memberships = array();
 754          for ( $i = 1; $i < 5; $i++ ) {
 755              $users[ $i ] = self::factory()->user->create( array(
 756                  'last_activity' => gmdate( 'Y-m-d H:i:s', $now - ( 100 - $i ) ),
 757              ) );
 758  
 759              $memberships[ $i ] = groups_send_membership_request( array(
 760                  'user_id'       => $users[ $i ],
 761                  'group_id'      => $g,
 762                  'date_modified' => gmdate( 'Y-m-d H:i:s', $now - ( 100 - $i ) ),
 763              ) );
 764          }
 765  
 766          // Fake the current group
 767          global $groups_template;
 768  
 769          if ( ! isset( $groups_template ) ) {
 770              $groups_template = new stdClass;
 771          }
 772  
 773          if ( ! isset( $groups_template->group ) ) {
 774              $groups_template->group = new stdClass;
 775          }
 776  
 777          $groups_template->group->id = $g;
 778  
 779          // Populate the global
 780          bp_group_has_membership_requests( array(
 781              'group_id' => $g,
 782              'per_page' => 3,
 783          ) );
 784  
 785          global $requests_template;
 786  
 787          $expected_user_ids = array();
 788          $expected_mem_ids = array();
 789          for ( $j = 1; $j <= 3; $j++ ) {
 790              $expected_user_ids[] = (string) $users[ $j ];
 791              $expected_mem_ids[] = (string) $memberships[ $j ];
 792          }
 793  
 794          $this->assertEquals( $expected_user_ids, wp_list_pluck( $requests_template->requests, 'user_id' ) );
 795          $this->assertEquals( $expected_mem_ids, wp_list_pluck( $requests_template->requests, 'invitation_id' ) );
 796      }
 797  
 798      /**
 799       * Checks that the requests_template object is properly formatted
 800       *
 801       * @group bp_group_has_membership_requests
 802       * @group BP_Group_Membership_Requests_Template
 803       */
 804  	public function test_bp_group_has_membership_requests_format() {
 805          $u1 = self::factory()->user->create( array(
 806              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 60 ),
 807          ) );
 808  
 809          $g = self::factory()->group->create( array(
 810              'creator_id' => $u1,
 811              'status'     => 'private'
 812          ) );
 813  
 814          $time = time();
 815  
 816          $user = self::factory()->user->create( array(
 817              'last_activity' => gmdate( 'Y-m-d H:i:s', $time ),
 818          ) );
 819  
 820          $membership = groups_send_membership_request( array(
 821              'user_id'       => $user,
 822              'group_id'      => $g,
 823              'date_modified' => gmdate( 'Y-m-d H:i:s', $time ),
 824          ) );
 825  
 826          // Fake the current group
 827          global $groups_template;
 828  
 829          if ( ! isset( $groups_template ) ) {
 830              $groups_template = new stdClass;
 831          }
 832  
 833          if ( ! isset( $groups_template->group ) ) {
 834              $groups_template->group = new stdClass;
 835          }
 836  
 837          $groups_template->group->id = $g;
 838  
 839          // Populate the global
 840          bp_group_has_membership_requests( array(
 841              'group_id' => $g,
 842              'per_page' => 1,
 843              'max' => 1,
 844          ) );
 845  
 846          global $requests_template;
 847  
 848          $expected = new stdClass;
 849          $expected->invitation_id = $membership;
 850          $expected->group_id = $g;
 851          $expected->user_id = $user;
 852          $expected->inviter_id = '0';
 853          $expected->is_admin = '0';
 854          $expected->is_mod = '0';
 855          $expected->user_title = '';
 856          $expected->date_modified = gmdate( 'Y-m-d H:i:s', $time );
 857          $expected->comments = '';
 858          $expected->is_confirmed = '0';
 859          $expected->is_banned = '0';
 860          $expected->invite_sent = '1';
 861  
 862          // Check each expected value. If there are more in the results,
 863          // that's OK
 864          foreach ( get_object_vars( $expected ) as $k => $v ) {
 865              $this->assertEquals( $v, $requests_template->requests[0]->{$k} );
 866          }
 867      }
 868  
 869      /**
 870       * @group bp_group_is_user_banned
 871       */
 872  	public function test_bp_group_is_user_banned_in_groups_loop() {
 873          $now = time();
 874          $u1 = self::factory()->user->create( array(
 875              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 876          ) );
 877          $u2 = self::factory()->user->create( array(
 878              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 879          ) );
 880  
 881          $g1 = self::factory()->group->create( array(
 882              'creator_id' => $u1,
 883              'last_activity' => $now - 100,
 884          ) );
 885          $g2 = self::factory()->group->create( array(
 886              'creator_id' => $u2,
 887              'last_activity' => $now - 200,
 888          ) );
 889  
 890          $this->add_user_to_group( $u1, $g2, array(
 891              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 892          ) );
 893          $this->add_user_to_group( $u2, $g2, array(
 894              'date_modified' => date( 'Y-m-d H:i:s', $now - 500 ),
 895          ) );
 896          $this->add_user_to_group( $u1, $g2, array(
 897              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 898          ) );
 899  
 900          // Ban user 1 from group 2
 901          // Fool the admin check
 902          $old_user = get_current_user_id();
 903          $this->set_current_user( $u2 );
 904          buddypress()->is_item_admin = true;
 905          groups_ban_member( $u1, $g2 );
 906  
 907          // Start the groups loop
 908          $this->set_current_user( $u1 );
 909          if ( bp_has_groups() ) : while ( bp_groups() ) : bp_the_group();
 910              $found[] = bp_group_is_user_banned();
 911          endwhile; endif;
 912  
 913          // Assert
 914          $expected = array( false, true );
 915          $this->assertEquals( $expected, $found );
 916  
 917          // Clean up
 918          $GLOBALS['groups_template'] = null;
 919          $this->set_current_user( $old_user );
 920      }
 921  
 922      /**
 923       * @group bp_group_is_user_banned
 924       */
 925      public function test_bp_group_is_user_banned_not_in_groups_loop() {
 926          $now = time();
 927          $u1 = self::factory()->user->create( array(
 928              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 929          ) );
 930          $u2 = self::factory()->user->create( array(
 931              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 932          ) );
 933          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 934          $g2 = self::factory()->group->create( array( 'creator_id' => $u2 ) );
 935  
 936          $this->add_user_to_group( $u1, $g2, array(
 937              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 938          ) );
 939          $this->add_user_to_group( $u2, $g2, array(
 940              'date_modified' => date( 'Y-m-d H:i:s', $now - 500 ),
 941          ) );
 942          $this->add_user_to_group( $u1, $g2, array(
 943              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 944          ) );
 945  
 946          // Ban user 1 from group 2
 947          // Fool the admin check
 948          $old_user = get_current_user_id();
 949          $this->set_current_user( $u2 );
 950          buddypress()->is_item_admin = true;
 951          groups_ban_member( $u1, $g2 );
 952  
 953          // Do group ban checks
 954          $group1 = new BP_Groups_Group( $g1 );
 955          $group2 = new BP_Groups_Group( $g2 );
 956  
 957          $found = array();
 958          $found[] = bp_group_is_user_banned( $group1, $u1 );
 959          $found[] = bp_group_is_user_banned( $group2, $u1 );
 960  
 961          // Assert
 962          $expected = array( false, true );
 963          $this->assertEquals( $expected, $found );
 964  
 965          // Clean up
 966          $this->set_current_user( $old_user );
 967      }
 968  
 969      /**
 970       * @group bp_group_is_forum_enabled
 971       */
 972  	public function test_bp_group_is_forum_enabled() {
 973          $g1 = $this->factory->group->create( array( 'enable_forum' => 0 ) );
 974          $g2 = $this->factory->group->create( array( 'enable_forum' => 1 ) );
 975  
 976          $this->assertFalse( bp_group_is_forum_enabled( $g1 ) );
 977          $this->assertTrue( bp_group_is_forum_enabled( $g2 ) );
 978      }
 979  
 980      /**
 981       * @group bp_get_group_member_is_banned
 982       */
 983  	public function test_bp_group_member_is_banned() {
 984          $this->assertFalse( bp_get_group_member_is_banned() );
 985      }
 986  
 987      /**
 988       * @group bp_get_group_member_id
 989       */
 990  	public function test_bp_get_group_member_id() {
 991          $this->assertFalse( (bool) bp_get_group_member_id() );
 992      }
 993  
 994      /**
 995       * @group bp_get_group_form_action
 996       */
 997  	public function test_bp_bp_get_group_form_action_when_empty() {
 998          $this->assertEmpty( bp_get_group_form_action( '' ) );
 999      }
1000  
1001      /**
1002       * @group bp_get_group_form_action
1003       */
1004  	public function test_bp_bp_get_group_form_action() {
1005          $g   = $this->factory->group->create();
1006          $p   = 2;
1007          $url = trailingslashit( bp_get_group_permalink( $g ) . $p );
1008  
1009          $this->assertSame( bp_get_group_form_action( $p, $g ), $url );
1010      }
1011  
1012      /**
1013       * @group bp_get_group_member_count
1014       */
1015  	public function test_bp_get_group_member_count_0_members() {
1016          $u = $this->factory->user->create();
1017          $g = $this->factory->group->create( array( 'creator_id' => $u ) );
1018  
1019          // Fake the current group.
1020          $GLOBALS['groups_template'] = new stdClass;
1021          $GLOBALS['groups_template']->group = groups_get_group( $g );
1022  
1023          // Kick group creator.
1024          wp_delete_user( $u );
1025  
1026          $this->assertNotSame( '0 member', bp_get_group_member_count() );
1027      }
1028  
1029      /**
1030       * @group bp_get_group_member_count
1031       */
1032  	public function test_bp_get_group_member_count_1_member() {
1033          $g = $this->factory->group->create();
1034  
1035          // Fake the current group.
1036          $GLOBALS['groups_template'] = new stdClass;
1037          $GLOBALS['groups_template']->group = groups_get_group( $g );
1038  
1039          $this->assertSame( '1 member', bp_get_group_member_count() );
1040      }
1041  
1042      /**
1043       * @group bp_get_group_member_count
1044       */
1045  	public function test_bp_get_group_member_count_2_members() {
1046          $u1 = $this->factory->user->create();
1047          $u2 = $this->factory->user->create();
1048          $g  = $this->factory->group->create( array( 'creator_id' => $u1 ) );
1049  
1050          $this->add_user_to_group( $u2, $g );
1051  
1052          // Fake the current group.
1053          $GLOBALS['groups_template'] = new stdClass;
1054          $GLOBALS['groups_template']->group = groups_get_group( $g );
1055  
1056          $this->assertSame( '2 members', bp_get_group_member_count() );
1057      }
1058  
1059      /**
1060       * @group pagination
1061       * @group BP_Groups_Template
1062       */
1063      public function test_bp_groups_template_should_give_precedence_to_grpage_URL_param() {
1064          $request = $_REQUEST;
1065          $_REQUEST['grpage'] = '5';
1066  
1067          $at = new BP_Groups_Template( array(
1068              'page' => 8,
1069          ) );
1070  
1071          $this->assertEquals( 5, $at->pag_page );
1072  
1073          $_REQUEST = $request;
1074      }
1075  
1076      /**
1077       * @group pagination
1078       * @group BP_Groups_Template
1079       */
1080      public function test_bp_groups_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1081          $request = $_REQUEST;
1082          $_REQUEST['grpage'] = '0';
1083  
1084          $at = new BP_Groups_Template( array(
1085              'page' => 8,
1086          ) );
1087  
1088          $this->assertEquals( 8, $at->pag_page );
1089  
1090          $_REQUEST = $request;
1091      }
1092  
1093      /**
1094       * @group pagination
1095       * @group BP_Groups_Template
1096       */
1097      public function test_bp_groups_template_should_give_precedence_to_num_URL_param() {
1098          $request = $_REQUEST;
1099          $_REQUEST['num'] = '14';
1100  
1101          $at = new BP_Groups_Template( array(
1102              'per_page' => 13,
1103          ) );
1104  
1105          $this->assertEquals( 14, $at->pag_num );
1106  
1107          $_REQUEST = $request;
1108      }
1109  
1110      /**
1111       * @group pagination
1112       * @group BP_Groups_Template
1113       */
1114      public function test_bp_groups_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1115          $request = $_REQUEST;
1116          $_REQUEST['num'] = '0';
1117  
1118          $at = new BP_Groups_Template( array(
1119              'per_page' => 13,
1120          ) );
1121  
1122          $this->assertEquals( 13, $at->pag_num );
1123  
1124          $_REQUEST = $request;
1125      }
1126  
1127      /**
1128       * @group pagination
1129       * @group BP_Groups_Group_Members_Template
1130       */
1131      public function test_bp_groups_group_members_template_should_give_precedence_to_mlpage_URL_param() {
1132          $request = $_REQUEST;
1133          $_REQUEST['mlpage'] = '5';
1134  
1135          $at = new BP_Groups_Group_Members_Template( array(
1136              'page' => 8,
1137          ) );
1138  
1139          $this->assertEquals( 5, $at->pag_page );
1140  
1141          $_REQUEST = $request;
1142      }
1143  
1144      /**
1145       * @group pagination
1146       * @group BP_Groups_Group_Members_Template
1147       */
1148      public function test_bp_groups_group_members_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1149          $request = $_REQUEST;
1150          $_REQUEST['mlpage'] = '0';
1151  
1152          $at = new BP_Groups_Group_Members_Template( array(
1153              'page' => 8,
1154          ) );
1155  
1156          $this->assertEquals( 8, $at->pag_page );
1157  
1158          $_REQUEST = $request;
1159      }
1160  
1161      /**
1162       * @group pagination
1163       * @group BP_Groups_Group_Members_Template
1164       */
1165      public function test_bp_groups_group_members_template_should_give_precedence_to_num_URL_param() {
1166          $request = $_REQUEST;
1167          $_REQUEST['num'] = '14';
1168  
1169          $at = new BP_Groups_Group_Members_Template( array(
1170              'per_page' => 13,
1171          ) );
1172  
1173          $this->assertEquals( 14, $at->pag_num );
1174  
1175          $_REQUEST = $request;
1176      }
1177  
1178      /**
1179       * @group pagination
1180       * @group BP_Groups_Group_Members_Template
1181       */
1182      public function test_bp_groups_group_members_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1183          $request = $_REQUEST;
1184          $_REQUEST['num'] = '0';
1185  
1186          $at = new BP_Groups_Group_Members_Template( array(
1187              'per_page' => 13,
1188          ) );
1189  
1190          $this->assertEquals( 13, $at->pag_num );
1191  
1192          $_REQUEST = $request;
1193      }
1194  
1195      /**
1196       * @group pagination
1197       * @group BP_Groups_Membership_Requests_Template
1198       */
1199      public function test_bp_groups_membership_requests_template_should_give_precedence_to_mrpage_URL_param() {
1200          $request = $_REQUEST;
1201          $_REQUEST['mrpage'] = '5';
1202  
1203          $at = new BP_Groups_Membership_Requests_Template( array(
1204              'page' => 8,
1205          ) );
1206  
1207          $this->assertEquals( 5, $at->pag_page );
1208  
1209          $_REQUEST = $request;
1210      }
1211  
1212      /**
1213       * @group pagination
1214       * @group BP_Groups_Membership_Requests_Template
1215       */
1216      public function test_bp_groups_membership_requests_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1217          $request = $_REQUEST;
1218          $_REQUEST['mrpage'] = '0';
1219  
1220          $at = new BP_Groups_Membership_Requests_Template( array(
1221              'page' => 8,
1222          ) );
1223  
1224          $this->assertEquals( 8, $at->pag_page );
1225  
1226          $_REQUEST = $request;
1227      }
1228  
1229      /**
1230       * @group pagination
1231       * @group BP_Groups_Membership_Requests_Template
1232       */
1233      public function test_bp_groups_membership_requests_template_should_give_precedence_to_num_URL_param() {
1234          $request = $_REQUEST;
1235          $_REQUEST['num'] = '14';
1236  
1237          $at = new BP_Groups_Membership_Requests_Template( array(
1238              'per_page' => 13,
1239          ) );
1240  
1241          $this->assertEquals( 14, $at->pag_num );
1242  
1243          $_REQUEST = $request;
1244      }
1245  
1246      /**
1247       * @group pagination
1248       * @group BP_Groups_Membership_Requests_Template
1249       */
1250      public function test_bp_groups_membership_requests_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1251          $request = $_REQUEST;
1252          $_REQUEST['num'] = '0';
1253  
1254          $at = new BP_Groups_Membership_Requests_Template( array(
1255              'per_page' => 13,
1256          ) );
1257  
1258          $this->assertEquals( 13, $at->pag_num );
1259  
1260          $_REQUEST = $request;
1261      }
1262  
1263      /**
1264       * @group pagination
1265       * @group BP_Groups_Invite_Template
1266       */
1267      public function test_bp_groups_invite_template_should_give_precedence_to_invitepage_URL_param() {
1268          $request = $_REQUEST;
1269          $_REQUEST['invitepage'] = '5';
1270  
1271          $at = new BP_Groups_Invite_Template( array(
1272              'page' => 8,
1273          ) );
1274  
1275          $this->assertEquals( 5, $at->pag_page );
1276  
1277          $_REQUEST = $request;
1278      }
1279  
1280      /**
1281       * @group pagination
1282       * @group BP_Groups_Invite_Template
1283       */
1284      public function test_bp_groups_invite_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1285          $request = $_REQUEST;
1286          $_REQUEST['invitepage'] = '0';
1287  
1288          $at = new BP_Groups_Invite_Template( array(
1289              'page' => 8,
1290          ) );
1291  
1292          $this->assertEquals( 8, $at->pag_page );
1293  
1294          $_REQUEST = $request;
1295      }
1296  
1297      /**
1298       * @group pagination
1299       * @group BP_Groups_Invite_Template
1300       */
1301      public function test_bp_groups_invite_template_should_give_precedence_to_num_URL_param() {
1302          $request = $_REQUEST;
1303          $_REQUEST['num'] = '14';
1304  
1305          $at = new BP_Groups_Invite_Template( array(
1306              'per_page' => 13,
1307          ) );
1308  
1309          $this->assertEquals( 14, $at->pag_num );
1310  
1311          $_REQUEST = $request;
1312      }
1313  
1314      /**
1315       * @group pagination
1316       * @group BP_Groups_Invite_Template
1317       */
1318      public function test_bp_groups_invite_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1319          $request = $_REQUEST;
1320          $_REQUEST['num'] = '0';
1321  
1322          $at = new BP_Groups_Invite_Template( array(
1323              'per_page' => 13,
1324          ) );
1325  
1326          $this->assertEquals( 13, $at->pag_num );
1327  
1328          $_REQUEST = $request;
1329      }
1330  }


Generated: Sun Nov 24 01:00:53 2024 Cross-referenced by PHPXref 0.7.1