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


Generated: Thu Nov 26 01:01:39 2020 Cross-referenced by PHPXref 0.7.1