[ 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          $m2 = $this->add_user_to_group( $u2, $g, array(
 593              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*24 ),
 594              'is_confirmed' => 0,
 595              'inviter_id' => $u1,
 596              'invite_sent' => true,
 597          ) );
 598  
 599          $m3 = $this->add_user_to_group( $u3, $g, array(
 600              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*12 ),
 601              'is_confirmed' => 0,
 602              'inviter_id' => $u1,
 603              'invite_sent' => true,
 604          ) );
 605  
 606          $m4 = $this->add_user_to_group( $u4, $g, array(
 607              'date_modified' => gmdate( 'Y-m-d H:i:s', $now - 60*60*36 ),
 608              'is_confirmed' => 1,
 609              'inviter_id' => $u1,
 610              'invite_sent' => true,
 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 ),
 667              ) );
 668  
 669              $this->add_user_to_group( $users[ $i ], $g, array(
 670                  'date_modified' => gmdate( 'Y-m-d H:i:s', $now - $i ),
 671                  'is_confirmed' => 0,
 672                  'inviter_id' => $u1,
 673                  'invite_sent' => true,
 674              ) );
 675          }
 676  
 677          // Populate the global
 678          bp_group_has_invites( array(
 679              'group_id' => $g,
 680              'user_id' => $u1,
 681              'page' => 2,
 682              'per_page' => 2,
 683          ) );
 684  
 685          global $invites_template;
 686  
 687          $this->assertEquals( array( $users[ 3 ], $users[ 2 ] ), $invites_template->invites );
 688      }
 689  
 690      /**
 691       * Checks for proper queried items
 692       *
 693       * @group bp_group_has_membership_requests
 694       * @group BP_Group_Membership_Requests_Template
 695       */
 696  	public function test_bp_group_has_membership_requests_results() {
 697          $now = time();
 698          $u1 = self::factory()->user->create( array(
 699              'last_activity' => gmdate( 'Y-m-d H:i:s', $now - 60 ),
 700          ) );
 701  
 702          $g = self::factory()->group->create( array(
 703              'creator_id' => $u1,
 704          ) );
 705  
 706          $users = array();
 707          $memberships = array();
 708          for ( $i = 1; $i < 5; $i++ ) {
 709              $users[ $i ] = self::factory()->user->create( array(
 710                  'last_activity' => gmdate( 'Y-m-d H:i:s', $now - ( 100 - $i ) ),
 711              ) );
 712  
 713              $memberships[ $i ] = $this->add_user_to_group( $users[ $i ], $g, array(
 714                  // this date_modified ensures that order will match
 715                  // id order. necessary due to a quirk in the legacy
 716                  // implementation
 717                  'date_modified' => gmdate( 'Y-m-d H:i:s', $now - ( 100 - $i ) ),
 718                  'is_confirmed' => 0,
 719                  'inviter_id' => 0,
 720                  'invite_sent' => false,
 721              ) );
 722          }
 723  
 724          // Fake the current group
 725          global $groups_template;
 726  
 727          if ( ! isset( $groups_template ) ) {
 728              $groups_template = new stdClass;
 729          }
 730  
 731          if ( ! isset( $groups_template->group ) ) {
 732              $groups_template->group = new stdClass;
 733          }
 734  
 735          $groups_template->group->id = $g;
 736  
 737          // Populate the global
 738          bp_group_has_membership_requests( array(
 739              'group_id' => $g,
 740              'per_page' => 3,
 741          ) );
 742  
 743          global $requests_template;
 744  
 745          $expected_user_ids = array();
 746          $expected_mem_ids = array();
 747          for ( $j = 1; $j <= 3; $j++ ) {
 748              $expected_user_ids[] = (string) $users[ $j ];
 749              $expected_mem_ids[] = (string) $memberships[ $j ];
 750          }
 751  
 752          $this->assertEquals( $expected_user_ids, wp_list_pluck( $requests_template->requests, 'user_id' ) );
 753          $this->assertEquals( $expected_mem_ids, wp_list_pluck( $requests_template->requests, 'id' ) );
 754      }
 755  
 756      /**
 757       * Checks that the requests_template object is properly formatted
 758       *
 759       * @group bp_group_has_membership_requests
 760       * @group BP_Group_Membership_Requests_Template
 761       */
 762  	public function test_bp_group_has_membership_requests_format() {
 763          $u1 = self::factory()->user->create( array(
 764              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 60 ),
 765          ) );
 766  
 767          $g = self::factory()->group->create( array(
 768              'creator_id' => $u1,
 769          ) );
 770  
 771          $time = time();
 772  
 773          $user = self::factory()->user->create( array(
 774              'last_activity' => gmdate( 'Y-m-d H:i:s', $time ),
 775          ) );
 776  
 777          $membership = $this->add_user_to_group( $user, $g, array(
 778              'date_modified' => gmdate( 'Y-m-d H:i:s', $time ),
 779              'is_confirmed' => 0,
 780              'inviter_id' => 0,
 781              'invite_sent' => false,
 782          ) );
 783  
 784          // Fake the current group
 785          global $groups_template;
 786  
 787          if ( ! isset( $groups_template ) ) {
 788              $groups_template = new stdClass;
 789          }
 790  
 791          if ( ! isset( $groups_template->group ) ) {
 792              $groups_template->group = new stdClass;
 793          }
 794  
 795          $groups_template->group->id = $g;
 796  
 797          // Populate the global
 798          bp_group_has_membership_requests( array(
 799              'group_id' => $g,
 800              'per_page' => 1,
 801              'max' => 1,
 802          ) );
 803  
 804          global $requests_template;
 805  
 806          $expected = new stdClass;
 807          $expected->id = $membership;
 808          $expected->group_id = $g;
 809          $expected->user_id = $user;
 810          $expected->inviter_id = '0';
 811          $expected->is_admin = '0';
 812          $expected->is_mod = '0';
 813          $expected->user_title = '';
 814          $expected->date_modified = gmdate( 'Y-m-d H:i:s', $time );
 815          $expected->comments = '';
 816          $expected->is_confirmed = '0';
 817          $expected->is_banned = '0';
 818          $expected->invite_sent = '0';
 819  
 820          // Check each expected value. If there are more in the results,
 821          // that's OK
 822          foreach ( get_object_vars( $expected ) as $k => $v ) {
 823              $this->assertEquals( $v, $requests_template->requests[0]->{$k} );
 824          }
 825      }
 826  
 827      /**
 828       * @group bp_group_is_user_banned
 829       */
 830  	public function test_bp_group_is_user_banned_in_groups_loop() {
 831          $now = time();
 832          $u1 = self::factory()->user->create( array(
 833              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 834          ) );
 835          $u2 = self::factory()->user->create( array(
 836              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 837          ) );
 838  
 839          $g1 = self::factory()->group->create( array(
 840              'creator_id' => $u1,
 841              'last_activity' => $now - 100,
 842          ) );
 843          $g2 = self::factory()->group->create( array(
 844              'creator_id' => $u2,
 845              'last_activity' => $now - 200,
 846          ) );
 847  
 848          $this->add_user_to_group( $u1, $g2, array(
 849              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 850          ) );
 851          $this->add_user_to_group( $u2, $g2, array(
 852              'date_modified' => date( 'Y-m-d H:i:s', $now - 500 ),
 853          ) );
 854          $this->add_user_to_group( $u1, $g2, array(
 855              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 856          ) );
 857  
 858          // Ban user 1 from group 2
 859          // Fool the admin check
 860          $old_user = get_current_user_id();
 861          $this->set_current_user( $u2 );
 862          buddypress()->is_item_admin = true;
 863          groups_ban_member( $u1, $g2 );
 864  
 865          // Start the groups loop
 866          $this->set_current_user( $u1 );
 867          if ( bp_has_groups() ) : while ( bp_groups() ) : bp_the_group();
 868              $found[] = bp_group_is_user_banned();
 869          endwhile; endif;
 870  
 871          // Assert
 872          $expected = array( false, true );
 873          $this->assertEquals( $expected, $found );
 874  
 875          // Clean up
 876          $GLOBALS['groups_template'] = null;
 877          $this->set_current_user( $old_user );
 878      }
 879  
 880      /**
 881       * @group bp_group_is_user_banned
 882       */
 883      public function test_bp_group_is_user_banned_not_in_groups_loop() {
 884          $now = time();
 885          $u1 = self::factory()->user->create( array(
 886              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 887          ) );
 888          $u2 = self::factory()->user->create( array(
 889              'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
 890          ) );
 891          $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 892          $g2 = self::factory()->group->create( array( 'creator_id' => $u2 ) );
 893  
 894          $this->add_user_to_group( $u1, $g2, array(
 895              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 896          ) );
 897          $this->add_user_to_group( $u2, $g2, array(
 898              'date_modified' => date( 'Y-m-d H:i:s', $now - 500 ),
 899          ) );
 900          $this->add_user_to_group( $u1, $g2, array(
 901              'date_modified' => date( 'Y-m-d H:i:s', $now - 50 ),
 902          ) );
 903  
 904          // Ban user 1 from group 2
 905          // Fool the admin check
 906          $old_user = get_current_user_id();
 907          $this->set_current_user( $u2 );
 908          buddypress()->is_item_admin = true;
 909          groups_ban_member( $u1, $g2 );
 910  
 911          // Do group ban checks
 912          $group1 = new BP_Groups_Group( $g1 );
 913          $group2 = new BP_Groups_Group( $g2 );
 914  
 915          $found = array();
 916          $found[] = bp_group_is_user_banned( $group1, $u1 );
 917          $found[] = bp_group_is_user_banned( $group2, $u1 );
 918  
 919          // Assert
 920          $expected = array( false, true );
 921          $this->assertEquals( $expected, $found );
 922  
 923          // Clean up
 924          $this->set_current_user( $old_user );
 925      }
 926  
 927      /**
 928       * @group bp_get_group_member_count
 929       */
 930  	public function test_bp_get_group_member_count_0_members() {
 931          global $groups_template;
 932          $gt = $groups_template;
 933          $groups_template = new stdClass;
 934          $groups_template->group = new stdClass;
 935          $groups_template->group->total_member_count = 0;
 936  
 937          $found = bp_get_group_member_count();
 938  
 939          $groups_template = $gt;
 940  
 941          $this->assertSame( '0 members', $found );
 942      }
 943  
 944      /**
 945       * @group bp_get_group_member_count
 946       */
 947  	public function test_bp_get_group_member_count_1_member() {
 948          global $groups_template;
 949          $gt = $groups_template;
 950          $groups_template = new stdClass;
 951          $groups_template->group = new stdClass;
 952          $groups_template->group->total_member_count = 1;
 953  
 954          $found = bp_get_group_member_count();
 955  
 956          $groups_template = $gt;
 957  
 958          $this->assertSame( '1 member', $found );
 959      }
 960  
 961      /**
 962       * @group bp_get_group_member_count
 963       */
 964  	public function test_bp_get_group_member_count_2_members() {
 965          global $groups_template;
 966          $gt = $groups_template;
 967          $groups_template = new stdClass;
 968          $groups_template->group = new stdClass;
 969          $groups_template->group->total_member_count = 2;
 970  
 971          $found = bp_get_group_member_count();
 972  
 973          $groups_template = $gt;
 974  
 975          $this->assertSame( '2 members', $found );
 976      }
 977  
 978      /**
 979       * @group pagination
 980       * @group BP_Groups_Template
 981       */
 982      public function test_bp_groups_template_should_give_precedence_to_grpage_URL_param() {
 983          $request = $_REQUEST;
 984          $_REQUEST['grpage'] = '5';
 985  
 986          $at = new BP_Groups_Template( array(
 987              'page' => 8,
 988          ) );
 989  
 990          $this->assertEquals( 5, $at->pag_page );
 991  
 992          $_REQUEST = $request;
 993      }
 994  
 995      /**
 996       * @group pagination
 997       * @group BP_Groups_Template
 998       */
 999      public function test_bp_groups_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1000          $request = $_REQUEST;
1001          $_REQUEST['grpage'] = '0';
1002  
1003          $at = new BP_Groups_Template( array(
1004              'page' => 8,
1005          ) );
1006  
1007          $this->assertEquals( 8, $at->pag_page );
1008  
1009          $_REQUEST = $request;
1010      }
1011  
1012      /**
1013       * @group pagination
1014       * @group BP_Groups_Template
1015       */
1016      public function test_bp_groups_template_should_give_precedence_to_num_URL_param() {
1017          $request = $_REQUEST;
1018          $_REQUEST['num'] = '14';
1019  
1020          $at = new BP_Groups_Template( array(
1021              'per_page' => 13,
1022          ) );
1023  
1024          $this->assertEquals( 14, $at->pag_num );
1025  
1026          $_REQUEST = $request;
1027      }
1028  
1029      /**
1030       * @group pagination
1031       * @group BP_Groups_Template
1032       */
1033      public function test_bp_groups_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1034          $request = $_REQUEST;
1035          $_REQUEST['num'] = '0';
1036  
1037          $at = new BP_Groups_Template( array(
1038              'per_page' => 13,
1039          ) );
1040  
1041          $this->assertEquals( 13, $at->pag_num );
1042  
1043          $_REQUEST = $request;
1044      }
1045  
1046      /**
1047       * @group pagination
1048       * @group BP_Groups_Group_Members_Template
1049       */
1050      public function test_bp_groups_group_members_template_should_give_precedence_to_mlpage_URL_param() {
1051          $request = $_REQUEST;
1052          $_REQUEST['mlpage'] = '5';
1053  
1054          $at = new BP_Groups_Group_Members_Template( array(
1055              'page' => 8,
1056          ) );
1057  
1058          $this->assertEquals( 5, $at->pag_page );
1059  
1060          $_REQUEST = $request;
1061      }
1062  
1063      /**
1064       * @group pagination
1065       * @group BP_Groups_Group_Members_Template
1066       */
1067      public function test_bp_groups_group_members_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1068          $request = $_REQUEST;
1069          $_REQUEST['mlpage'] = '0';
1070  
1071          $at = new BP_Groups_Group_Members_Template( array(
1072              'page' => 8,
1073          ) );
1074  
1075          $this->assertEquals( 8, $at->pag_page );
1076  
1077          $_REQUEST = $request;
1078      }
1079  
1080      /**
1081       * @group pagination
1082       * @group BP_Groups_Group_Members_Template
1083       */
1084      public function test_bp_groups_group_members_template_should_give_precedence_to_num_URL_param() {
1085          $request = $_REQUEST;
1086          $_REQUEST['num'] = '14';
1087  
1088          $at = new BP_Groups_Group_Members_Template( array(
1089              'per_page' => 13,
1090          ) );
1091  
1092          $this->assertEquals( 14, $at->pag_num );
1093  
1094          $_REQUEST = $request;
1095      }
1096  
1097      /**
1098       * @group pagination
1099       * @group BP_Groups_Group_Members_Template
1100       */
1101      public function test_bp_groups_group_members_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1102          $request = $_REQUEST;
1103          $_REQUEST['num'] = '0';
1104  
1105          $at = new BP_Groups_Group_Members_Template( array(
1106              'per_page' => 13,
1107          ) );
1108  
1109          $this->assertEquals( 13, $at->pag_num );
1110  
1111          $_REQUEST = $request;
1112      }
1113  
1114      /**
1115       * @group pagination
1116       * @group BP_Groups_Membership_Requests_Template
1117       */
1118      public function test_bp_groups_membership_requests_template_should_give_precedence_to_mrpage_URL_param() {
1119          $request = $_REQUEST;
1120          $_REQUEST['mrpage'] = '5';
1121  
1122          $at = new BP_Groups_Membership_Requests_Template( array(
1123              'page' => 8,
1124          ) );
1125  
1126          $this->assertEquals( 5, $at->pag_page );
1127  
1128          $_REQUEST = $request;
1129      }
1130  
1131      /**
1132       * @group pagination
1133       * @group BP_Groups_Membership_Requests_Template
1134       */
1135      public function test_bp_groups_membership_requests_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1136          $request = $_REQUEST;
1137          $_REQUEST['mrpage'] = '0';
1138  
1139          $at = new BP_Groups_Membership_Requests_Template( array(
1140              'page' => 8,
1141          ) );
1142  
1143          $this->assertEquals( 8, $at->pag_page );
1144  
1145          $_REQUEST = $request;
1146      }
1147  
1148      /**
1149       * @group pagination
1150       * @group BP_Groups_Membership_Requests_Template
1151       */
1152      public function test_bp_groups_membership_requests_template_should_give_precedence_to_num_URL_param() {
1153          $request = $_REQUEST;
1154          $_REQUEST['num'] = '14';
1155  
1156          $at = new BP_Groups_Membership_Requests_Template( array(
1157              'per_page' => 13,
1158          ) );
1159  
1160          $this->assertEquals( 14, $at->pag_num );
1161  
1162          $_REQUEST = $request;
1163      }
1164  
1165      /**
1166       * @group pagination
1167       * @group BP_Groups_Membership_Requests_Template
1168       */
1169      public function test_bp_groups_membership_requests_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1170          $request = $_REQUEST;
1171          $_REQUEST['num'] = '0';
1172  
1173          $at = new BP_Groups_Membership_Requests_Template( array(
1174              'per_page' => 13,
1175          ) );
1176  
1177          $this->assertEquals( 13, $at->pag_num );
1178  
1179          $_REQUEST = $request;
1180      }
1181  
1182      /**
1183       * @group pagination
1184       * @group BP_Groups_Invite_Template
1185       */
1186      public function test_bp_groups_invite_template_should_give_precedence_to_invitepage_URL_param() {
1187          $request = $_REQUEST;
1188          $_REQUEST['invitepage'] = '5';
1189  
1190          $at = new BP_Groups_Invite_Template( array(
1191              'page' => 8,
1192          ) );
1193  
1194          $this->assertEquals( 5, $at->pag_page );
1195  
1196          $_REQUEST = $request;
1197      }
1198  
1199      /**
1200       * @group pagination
1201       * @group BP_Groups_Invite_Template
1202       */
1203      public function test_bp_groups_invite_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
1204          $request = $_REQUEST;
1205          $_REQUEST['invitepage'] = '0';
1206  
1207          $at = new BP_Groups_Invite_Template( array(
1208              'page' => 8,
1209          ) );
1210  
1211          $this->assertEquals( 8, $at->pag_page );
1212  
1213          $_REQUEST = $request;
1214      }
1215  
1216      /**
1217       * @group pagination
1218       * @group BP_Groups_Invite_Template
1219       */
1220      public function test_bp_groups_invite_template_should_give_precedence_to_num_URL_param() {
1221          $request = $_REQUEST;
1222          $_REQUEST['num'] = '14';
1223  
1224          $at = new BP_Groups_Invite_Template( array(
1225              'per_page' => 13,
1226          ) );
1227  
1228          $this->assertEquals( 14, $at->pag_num );
1229  
1230          $_REQUEST = $request;
1231      }
1232  
1233      /**
1234       * @group pagination
1235       * @group BP_Groups_Invite_Template
1236       */
1237      public function test_bp_groups_invite_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
1238          $request = $_REQUEST;
1239          $_REQUEST['num'] = '0';
1240  
1241          $at = new BP_Groups_Invite_Template( array(
1242              'per_page' => 13,
1243          ) );
1244  
1245          $this->assertEquals( 13, $at->pag_num );
1246  
1247          $_REQUEST = $request;
1248      }
1249  }


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