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


Generated: Thu Sep 19 01:01:39 2019 Cross-referenced by PHPXref 0.7.1