[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group groups
   5   */
   6  class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase {
   7      static $user;
   8      static $admin_user;
   9      static $groups;
  10  
  11  	public function setUp() {
  12          parent::setUp();
  13          groups_remove_member( self::$user, self::$groups[2] );
  14      }
  15  
  16  	public static function wpSetUpBeforeClass( $f ) {
  17          $f = new BP_UnitTest_Factory();
  18  
  19          self::$user = $f->user->create( array(
  20              'user_login' => 'bp_get_user_groups_user',
  21              'user_email' => 'bp_get_user_groups_user@example.com',
  22          ) );
  23          self::$admin_user = $f->user->create( array(
  24              'user_login' => 'bp_get_user_groups_admin_user',
  25              'user_email' => 'bp_get_user_groups_admin_user@example.com',
  26          ) );
  27          self::$groups = $f->group->create_many( 4, array(
  28              'creator_id' => self::$admin_user,
  29              'status'     => 'private'
  30          ) );
  31  
  32          $now = time();
  33          self::add_user_to_group( self::$user, self::$groups[1], array(
  34              'date_modified' => date( 'Y-m-d H:i:s', $now - 10 ),
  35          ) );
  36          self::add_user_to_group( self::$user, self::$groups[0], array(
  37              'date_modified' => date( 'Y-m-d H:i:s', $now ),
  38          ) );
  39  
  40          self::commit_transaction();
  41      }
  42  
  43  	public static function tearDownAfterClass() {
  44          foreach ( self::$groups as $group ) {
  45              groups_delete_group( $group );
  46          }
  47  
  48          if ( is_multisite() ) {
  49              wpmu_delete_user( self::$user );
  50              wpmu_delete_user( self::$admin_user );
  51          } else {
  52              wp_delete_user( self::$user );
  53              wp_delete_user( self::$admin_user );
  54          }
  55  
  56          self::commit_transaction();
  57      }
  58  
  59  	public function test_default_params() {
  60          $found = bp_get_user_groups( self::$user );
  61  
  62          $this->assertEqualSets( array( self::$groups[0], self::$groups[1] ), wp_list_pluck( $found, 'group_id' ) );
  63  
  64          foreach ( $found as $index => $f ) {
  65              $this->assertInternalType( 'int', $index );
  66              $this->assertInternalType( 'object', $f );
  67              $this->assertInternalType( 'int', $f->group_id );
  68              $this->assertSame( $index, $f->group_id );
  69          }
  70      }
  71  
  72  	public function test_is_confirmed_true() {
  73          groups_send_membership_request( array(
  74              'user_id' => self::$user,
  75              'group_id' => self::$groups[2]
  76          ) );
  77  
  78          $expected = array( self::$groups[0], self::$groups[1] );
  79          $found = bp_get_user_groups( self::$user, array(
  80              'is_confirmed' => true,
  81          ) );
  82  
  83          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
  84      }
  85  
  86  	public function test_is_confirmed_false() {
  87          groups_send_membership_request( array(
  88              'user_id' => self::$user,
  89              'group_id' => self::$groups[2]
  90          ) );
  91  
  92          $expected = array( self::$groups[2] );
  93          $found = bp_get_user_groups( self::$user, array(
  94              'is_confirmed' => false,
  95          ) );
  96  
  97          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
  98      }
  99  
 100  	public function test_is_confirmed_null() {
 101          groups_send_membership_request( array(
 102              'user_id' => self::$user,
 103              'group_id' => self::$groups[2]
 104          ) );
 105  
 106          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 107          $found = bp_get_user_groups( self::$user, array(
 108              'is_confirmed' => null,
 109          ) );
 110  
 111          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 112      }
 113  
 114  	public function test_is_banned_true() {
 115          $this->add_user_to_group( self::$user, self::$groups[2] );
 116          $member = new BP_Groups_Member( self::$user, self::$groups[2] );
 117          $member->ban();
 118  
 119          $expected = array( self::$groups[2] );
 120          $found = bp_get_user_groups( self::$user, array(
 121              'is_banned' => true,
 122          ) );
 123  
 124          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 125      }
 126  
 127  	public function test_is_banned_false() {
 128          $this->add_user_to_group( self::$user, self::$groups[2] );
 129          $member = new BP_Groups_Member( self::$user, self::$groups[2] );
 130          $member->ban();
 131  
 132          $expected = array( self::$groups[0], self::$groups[1] );
 133          $found = bp_get_user_groups( self::$user, array(
 134              'is_banned' => false,
 135          ) );
 136  
 137          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 138      }
 139  
 140  	public function test_is_banned_null() {
 141          $this->add_user_to_group( self::$user, self::$groups[2] );
 142          $member = new BP_Groups_Member( self::$user, self::$groups[2] );
 143          $member->ban();
 144  
 145          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 146          $found = bp_get_user_groups( self::$user, array(
 147              'is_banned' => null,
 148          ) );
 149  
 150          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 151      }
 152  
 153  	public function test_is_admin_true() {
 154          $this->add_user_to_group( self::$user, self::$groups[2], array(
 155              'is_admin' => true,
 156          ) );
 157  
 158          $expected = array( self::$groups[2] );
 159          $found = bp_get_user_groups( self::$user, array(
 160              'is_admin' => true,
 161          ) );
 162  
 163          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 164      }
 165  
 166  	public function test_is_admin_false() {
 167          $this->add_user_to_group( self::$user, self::$groups[2], array(
 168              'is_admin' => true,
 169          ) );
 170  
 171          $expected = array( self::$groups[0], self::$groups[1] );
 172          $found = bp_get_user_groups( self::$user, array(
 173              'is_admin' => false,
 174          ) );
 175  
 176          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 177      }
 178  
 179  	public function test_is_admin_null() {
 180          $this->add_user_to_group( self::$user, self::$groups[2], array(
 181              'is_admin' => false,
 182          ) );
 183  
 184          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 185          $found = bp_get_user_groups( self::$user, array(
 186              'is_admin' => null,
 187          ) );
 188  
 189          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 190      }
 191  
 192  	public function test_is_mod_true() {
 193          $this->add_user_to_group( self::$user, self::$groups[2], array(
 194              'is_mod' => true,
 195          ) );
 196  
 197          $expected = array( self::$groups[2] );
 198          $found = bp_get_user_groups( self::$user, array(
 199              'is_mod' => true,
 200          ) );
 201  
 202          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 203      }
 204  
 205  	public function test_is_mod_false() {
 206          $this->add_user_to_group( self::$user, self::$groups[2], array(
 207              'is_mod' => true,
 208          ) );
 209  
 210          $expected = array( self::$groups[0], self::$groups[1] );
 211          $found = bp_get_user_groups( self::$user, array(
 212              'is_mod' => false,
 213          ) );
 214  
 215          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 216      }
 217  
 218  	public function test_is_mod_null() {
 219          $this->add_user_to_group( self::$user, self::$groups[2], array(
 220              'is_mod' => false,
 221          ) );
 222  
 223          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 224          $found = bp_get_user_groups( self::$user, array(
 225              'is_mod' => null,
 226          ) );
 227  
 228          $this->assertEqualSets( $expected, wp_list_pluck( $found, 'group_id' ) );
 229      }
 230  
 231  	public function test_orderby_should_default_to_group_id() {
 232          $expected = bp_get_user_groups( self::$user );
 233          $found = bp_get_user_groups( self::$user, array(
 234              'orderby' => 'group_id',
 235          ) );
 236  
 237          $this->assertEquals( $expected, $found );
 238      }
 239  
 240  	public function test_orderby_group_id() {
 241          $expected = array( self::$groups[0], self::$groups[1] );
 242          $found = bp_get_user_groups( self::$user, array(
 243              'orderby' => 'group_id',
 244          ) );
 245  
 246          $this->assertSame( $expected, array_keys( $found ) );
 247      }
 248  
 249  	public function test_orderby_id() {
 250          $expected = array( self::$groups[1], self::$groups[0] );
 251          $found = bp_get_user_groups( self::$user, array(
 252              'orderby' => 'id',
 253          ) );
 254  
 255          $this->assertSame( $expected, array_values( wp_list_pluck( $found, 'group_id' ) ) );
 256      }
 257  
 258  	public function test_orderby_date_modified() {
 259          $this->add_user_to_group( self::$user, self::$groups[2], array(
 260              'date_modified' => '2015-03-14 09:26:53',
 261          ) );
 262  
 263          $expected = array( self::$groups[2], self::$groups[1], self::$groups[0] );
 264          $found = bp_get_user_groups( self::$user, array(
 265              'orderby' => 'date_modified',
 266          ) );
 267  
 268          $this->assertSame( $expected, array_values( wp_list_pluck( $found, 'group_id' ) ) );
 269      }
 270  
 271  	public function test_orderby_group_id_with_order_desc() {
 272          $expected = array( self::$groups[1], self::$groups[0] );
 273          $found = bp_get_user_groups( self::$user, array(
 274              'orderby' => 'group_id',
 275              'order' => 'DESC',
 276          ) );
 277  
 278          $this->assertSame( $expected, array_keys( $found ) );
 279      }
 280  
 281  	public function test_orderby_date_modified_with_order_desc() {
 282          $this->add_user_to_group( self::$user, self::$groups[2], array(
 283              'date_modified' => '2015-03-14 09:26:53',
 284          ) );
 285  
 286          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 287          $found = bp_get_user_groups( self::$user, array(
 288              'orderby' => 'date_modified',
 289              'order' => 'DESC',
 290          ) );
 291  
 292          $this->assertSame( $expected, array_values( wp_list_pluck( $found, 'group_id' ) ) );
 293      }
 294  
 295      /**
 296       * @group cache
 297       */
 298  	public function test_results_should_be_cached() {
 299          global $wpdb;
 300  
 301          $g1 = bp_get_user_groups( self::$user );
 302  
 303          $num_queries = $wpdb->num_queries;
 304          $g2 = bp_get_user_groups( self::$user );
 305  
 306          $this->assertSame( $num_queries, $wpdb->num_queries );
 307          $this->assertEquals( $g1, $g2 );
 308      }
 309  
 310      /**
 311       * @group cache
 312       */
 313      public function test_cache_should_be_invalidated_on_group_join() {
 314          // Populate cache.
 315          $g1 = bp_get_user_groups( self::$user );
 316  
 317          groups_join_group( self::$groups[2], self::$user );
 318  
 319          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 320          $found = bp_get_user_groups( self::$user );
 321  
 322          $this->assertEqualSets( $expected, array_keys( $found ) );
 323      }
 324  
 325      /**
 326       * @group cache
 327       */
 328      public function test_cache_should_be_invalidated_on_group_leave() {
 329          // Populate cache.
 330          $g1 = bp_get_user_groups( self::$user );
 331  
 332          groups_leave_group( self::$groups[1], self::$user );
 333  
 334          $expected = array( self::$groups[0] );
 335          $found = bp_get_user_groups( self::$user );
 336  
 337          $this->assertEqualSets( $expected, array_keys( $found ) );
 338      }
 339  
 340      /**
 341       * @group cache
 342       */
 343      public function test_cache_should_be_invalidated_on_group_remove() {
 344          // Populate cache.
 345          $g1 = bp_get_user_groups( self::$user );
 346  
 347          $m = new BP_Groups_Member( self::$user, self::$groups[1] );
 348          $m->remove();
 349  
 350          $expected = array( self::$groups[0] );
 351          $found = bp_get_user_groups( self::$user );
 352  
 353          $this->assertEqualSets( $expected, array_keys( $found ) );
 354      }
 355  
 356      /**
 357       * @group cache
 358       */
 359  	public function test_cache_should_be_invalidated_on_group_ban() {
 360          // Populate cache.
 361          $g1 = bp_get_user_groups( self::$user );
 362  
 363          $m = new BP_Groups_Member( self::$user, self::$groups[1] );
 364          $m->ban();
 365  
 366          $expected = array( self::$groups[0] );
 367          $found = bp_get_user_groups( self::$user );
 368  
 369          $this->assertEqualSets( $expected, array_keys( $found ) );
 370      }
 371  
 372      /**
 373       * @group cache
 374       */
 375      public function test_cache_should_be_invalidated_on_group_unban() {
 376          $m1 = new BP_Groups_Member( self::$user, self::$groups[1] );
 377          $m1->ban();
 378  
 379          // Populate cache.
 380          $g1 = bp_get_user_groups( self::$user );
 381  
 382          $m2 = new BP_Groups_Member( self::$user, self::$groups[1] );
 383          $m2->unban();
 384  
 385          $expected = array( self::$groups[0], self::$groups[1] );
 386          $found = bp_get_user_groups( self::$user );
 387  
 388          $this->assertEqualSets( $expected, array_keys( $found ) );
 389      }
 390  
 391      /**
 392       * @group cache
 393       */
 394      public function test_cache_should_be_invalidated_on_group_invite() {
 395          // Populate cache.
 396          $g1 = bp_get_user_groups( self::$user, array(
 397              'is_confirmed' => null,
 398          ) );
 399  
 400          groups_invite_user( array(
 401              'user_id'     => self::$user,
 402              'group_id'    => self::$groups[2],
 403              'inviter_id'  => self::$admin_user,
 404              'send_invite' => 1
 405          ) );
 406  
 407          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 408          $found = bp_get_user_groups( self::$user, array(
 409              'is_confirmed' => null,
 410          ) );
 411  
 412          $this->assertEqualSets( $expected, array_keys( $found ) );
 413      }
 414  
 415      /**
 416       * @group cache
 417       */
 418      public function test_cache_should_be_invalidated_on_group_uninvite() {
 419          groups_invite_user( array(
 420              'user_id'     => self::$user,
 421              'group_id'    => self::$groups[2],
 422              'inviter_id'  => self::$admin_user,
 423              'send_invite' => 1,
 424          ) );
 425  
 426          // Populate cache.
 427          $g1 = bp_get_user_groups( self::$user, array(
 428              'is_confirmed' => null,
 429          ) );
 430  
 431          groups_uninvite_user( self::$user, self::$groups[2] );
 432  
 433          $expected = array( self::$groups[0], self::$groups[1] );
 434          $found = bp_get_user_groups( self::$user, array(
 435              'is_confirmed' => null,
 436          ) );
 437  
 438          $this->assertEqualSets( $expected, array_keys( $found ) );
 439      }
 440  
 441      /**
 442       * @group cache
 443       */
 444      public function test_cache_should_be_invalidated_on_group_invite_acceptance() {
 445          groups_invite_user( array(
 446              'user_id'     => self::$user,
 447              'group_id'    => self::$groups[2],
 448              'inviter_id'  => self::$admin_user,
 449              'send_invite' => 1
 450          ) );
 451  
 452          // Populate cache.
 453          $g1 = bp_get_user_groups( self::$user );
 454  
 455          groups_accept_invite( self::$user, self::$groups[2] );
 456  
 457          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 458          $found = bp_get_user_groups( self::$user );
 459  
 460          $this->assertEqualSets( $expected, array_keys( $found ) );
 461      }
 462  
 463      /**
 464       * @group cache
 465       */
 466      public function test_cache_should_be_invalidated_on_group_invite_reject() {
 467          groups_invite_user( array(
 468              'user_id'     => self::$user,
 469              'group_id'    => self::$groups[2],
 470              'inviter_id'  => self::$admin_user,
 471              'send_invite' => 1
 472          ) );
 473  
 474          // Populate cache.
 475          $g1 = bp_get_user_groups( self::$user, array(
 476              'is_confirmed' => null,
 477          ) );
 478  
 479          groups_reject_invite( self::$user, self::$groups[2] );
 480  
 481          $expected = array( self::$groups[0], self::$groups[1] );
 482          $found = bp_get_user_groups( self::$user, array(
 483              'is_confirmed' => null,
 484          ) );
 485  
 486          $this->assertEqualSets( $expected, array_keys( $found ) );
 487      }
 488  
 489      /**
 490       * @group cache
 491       */
 492      public function test_cache_should_be_invalidated_on_group_invite_delete() {
 493          groups_invite_user( array(
 494              'user_id'     => self::$user,
 495              'group_id'    => self::$groups[2],
 496              'inviter_id'  => self::$admin_user,
 497              'send_invite' => 1
 498          ) );
 499  
 500          // Populate cache.
 501          $g1 = bp_get_user_groups( self::$user, array(
 502              'is_confirmed' => null,
 503          ) );
 504  
 505          groups_delete_invite( self::$user, self::$groups[2] );
 506  
 507          $expected = array( self::$groups[0], self::$groups[1] );
 508          $found = bp_get_user_groups( self::$user, array(
 509              'is_confirmed' => null,
 510          ) );
 511  
 512          $this->assertEqualSets( $expected, array_keys( $found ) );
 513      }
 514  
 515      /**
 516       * @group cache
 517       */
 518      public function test_cache_should_be_invalidated_on_group_request() {
 519          // Populate cache.
 520          $g1 = bp_get_user_groups( self::$user, array(
 521              'is_confirmed' => null,
 522          ) );
 523  
 524          // For `wp_mail()`.
 525          $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null;
 526          $_SERVER['SERVER_NAME'] = '';
 527  
 528          groups_send_membership_request( array(
 529              'user_id' => self::$user,
 530              'group_id' => self::$groups[2]
 531          ) );
 532  
 533          // For `wp_mail()`.
 534          if ( is_null( $server_name ) ) {
 535              unset( $_SERVER['SERVER_NAME'] );
 536          } else {
 537              $_SERVER['SERVER_NAME'] = $server_name;
 538          }
 539  
 540          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 541          $found = bp_get_user_groups( self::$user, array(
 542              'is_confirmed' => null,
 543          ) );
 544  
 545          $this->assertEqualSets( $expected, array_keys( $found ) );
 546      }
 547  
 548      /**
 549       * @group cache
 550       */
 551      public function test_cache_should_be_invalidated_on_group_request_acceptance() {
 552          // For `wp_mail()`.
 553          $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null;
 554          $_SERVER['SERVER_NAME'] = '';
 555  
 556          groups_send_membership_request( array(
 557              'user_id'       => self::$user,
 558              'group_id'      => self::$groups[2],
 559          ) );
 560  
 561          // Populate cache.
 562          $g1 = bp_get_user_groups( self::$user );
 563  
 564          $m = new BP_Groups_Member( self::$user, self::$groups[2] );
 565  
 566          groups_accept_membership_request( false, self::$user, self::$groups[2] );
 567  
 568          // For `wp_mail()`.
 569          if ( is_null( $server_name ) ) {
 570              unset( $_SERVER['SERVER_NAME'] );
 571          } else {
 572              $_SERVER['SERVER_NAME'] = $server_name;
 573          }
 574  
 575          $expected = array( self::$groups[0], self::$groups[1], self::$groups[2] );
 576          $found = bp_get_user_groups( self::$user );
 577  
 578          $this->assertEqualSets( $expected, array_keys( $found ) );
 579      }
 580  }


Generated: Mon Oct 21 01:01:35 2019 Cross-referenced by PHPXref 0.7.1