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


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