[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/core/ -> class-bp-user-query.php (source)

   1  <?php
   2  
   3  /**
   4   * @group core
   5   * @group BP_User_Query
   6   */
   7  class BP_Tests_BP_User_Query_TestCases extends BP_UnitTestCase {
   8      /**
   9       * Checks that user_id returns friends
  10       */
  11  	public function test_bp_user_query_friends() {
  12          $u1 = self::factory()->user->create();
  13          $u2 = self::factory()->user->create();
  14          $u3 = self::factory()->user->create();
  15          friends_add_friend( $u1, $u2, true );
  16  
  17          $q = new BP_User_Query( array(
  18              'user_id' => $u2,
  19          ) );
  20  
  21          $friends = is_array( $q->results ) ? array_values( $q->results ) : array();
  22          $friend_ids = wp_list_pluck( $friends, 'ID' );
  23          $this->assertEquals( $friend_ids, array( $u1 ) );
  24      }
  25  
  26      /**
  27       * @ticket BP4938
  28       */
  29  	public function test_bp_user_query_friends_with_include() {
  30          $u1 = self::factory()->user->create();
  31          $u2 = self::factory()->user->create();
  32          $u3 = self::factory()->user->create();
  33          $u4 = self::factory()->user->create();
  34          friends_add_friend( $u1, $u2, true );
  35          friends_add_friend( $u1, $u3, true );
  36  
  37          $q = new BP_User_Query( array(
  38              'user_id' => $u1,
  39  
  40              // Represents an independent filter passed by a plugin
  41              // u4 is not a friend of u1 and should not be returned
  42              'include' => array( $u2, $u4 ),
  43          ) );
  44  
  45          $friends = is_array( $q->results ) ? array_values( $q->results ) : array();
  46          $friend_ids = wp_list_pluck( $friends, 'ID' );
  47          $this->assertEquals( $friend_ids, array( $u2 ) );
  48      }
  49  
  50      public function test_bp_user_query_friends_with_include_but_zero_friends() {
  51          $u1 = self::factory()->user->create();
  52          $u2 = self::factory()->user->create();
  53          $u3 = self::factory()->user->create();
  54          $u4 = self::factory()->user->create();
  55  
  56          $q = new BP_User_Query( array(
  57              'user_id' => $u1,
  58  
  59              // Represents an independent filter passed by a plugin
  60              // u4 is not a friend of u1 and should not be returned
  61              'include' => array( $u2, $u4 ),
  62          ) );
  63  
  64          $friends = is_array( $q->results ) ? array_values( $q->results ) : array();
  65          $friend_ids = wp_list_pluck( $friends, 'ID' );
  66          $this->assertEquals( $friend_ids, array() );
  67      }
  68  
  69      /**
  70       * @ticket BP7248
  71       */
  72      public function test_include_array_contaning_only_0_should_result_in_no_results_query() {
  73          $q = new BP_User_Query( array(
  74              'include' => array( 0 ),
  75          ) );
  76  
  77          $this->assertContains( '0 = 1', $q->uid_clauses['where'] );
  78      }
  79  
  80      /**
  81       * @ticket BP7248
  82       */
  83      public function test_include_array_contaning_0_but_also_real_IDs_should_not_result_in_no_results_query() {
  84          $q = new BP_User_Query( array(
  85              'include' => array( 0, 1 ),
  86          ) );
  87  
  88          $this->assertNotContains( '0 = 1', $q->uid_clauses['where'] );
  89      }
  90  
  91      /**
  92       * @group user_ids
  93       */
  94      public function test_bp_user_query_user_ids_with_invalid_user_id() {
  95          $now = time();
  96          $u1 = self::factory()->user->create();
  97          $u2 = self::factory()->user->create();
  98  
  99          // invalid user ID
 100          $u3 = $u2 + 1;
 101  
 102          $old_user = get_current_user_id();
 103          $this->set_current_user( $u1 );
 104  
 105          // pass 'user_ids' to user query to trigger this bug
 106          $q = new BP_User_Query( array(
 107              'user_ids' => array( $u2, $u3 )
 108          ) );
 109  
 110          // $q->user_ids property should now not contain invalid user IDs
 111          $this->assertNotContains( $u3, $q->user_ids );
 112  
 113          // clean up
 114          $this->set_current_user( $old_user );
 115      }
 116  
 117  	public function test_bp_user_query_sort_by_popular() {
 118          $u1 = self::factory()->user->create();
 119          $u2 = self::factory()->user->create();
 120          $u3 = self::factory()->user->create();
 121          $u4 = self::factory()->user->create();
 122  
 123          bp_update_user_meta( $u1, bp_get_user_meta_key( 'total_friend_count' ), '5' );
 124          bp_update_user_meta( $u2, bp_get_user_meta_key( 'total_friend_count' ), '90' );
 125          bp_update_user_meta( $u3, bp_get_user_meta_key( 'total_friend_count' ), '101' );
 126          bp_update_user_meta( $u4, bp_get_user_meta_key( 'total_friend_count' ), '3002' );
 127  
 128          $q = new BP_User_Query( array(
 129              'type' => 'popular',
 130          ) );
 131  
 132          $users = is_array( $q->results ) ? array_values( $q->results ) : array();
 133          $user_ids = wp_parse_id_list( wp_list_pluck( $users, 'ID' ) );
 134  
 135          $expected = array( $u4, $u3, $u2, $u1 );
 136          $this->assertEquals( $expected, $user_ids );
 137      }
 138  
 139      /**
 140       * @group online
 141       */
 142  	public function test_bp_user_query_type_online() {
 143          $now = time();
 144          $u1 = self::factory()->user->create( array(
 145              'last_activity' => date( 'Y-m-d H:i:s', $now ),
 146          ) );
 147          $u2 = self::factory()->user->create( array(
 148              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*13 ),
 149          ) );
 150          $u3 = self::factory()->user->create( array(
 151              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*16 ),
 152          ) );
 153  
 154          $q = new BP_User_Query( array(
 155              'type' => 'online',
 156          ) );
 157  
 158          $users = is_array( $q->results ) ? array_values( $q->results ) : array();
 159          $user_ids = wp_parse_id_list( wp_list_pluck( $users, 'ID' ) );
 160          $this->assertEquals( array( $u1, $u2 ), $user_ids );
 161      }
 162  
 163      /**
 164       * @group online
 165       */
 166      public function test_bp_user_query_type_online_five_minute_interval() {
 167          $now = time();
 168          $u1 = self::factory()->user->create( array(
 169              'last_activity' => date( 'Y-m-d H:i:s', $now ),
 170          ) );
 171          $u2 = self::factory()->user->create( array(
 172              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*4 ),
 173          ) );
 174          $u3 = self::factory()->user->create( array(
 175              'last_activity' => date( 'Y-m-d H:i:s', $now - 60*6 ),
 176          ) );
 177  
 178          add_filter( 'bp_user_query_online_interval', function() { return 5; } );
 179  
 180          $q = new BP_User_Query( array(
 181              'type' => 'online',
 182          ) );
 183  
 184          $users = is_array( $q->results ) ? array_values( $q->results ) : array();
 185          $user_ids = wp_parse_id_list( wp_list_pluck( $users, 'ID' ) );
 186          $this->assertEquals( array( $u1, $u2 ), $user_ids );
 187      }
 188  
 189  
 190  	public function test_bp_user_query_search_with_apostrophe() {
 191          // Apostrophe. Search_terms must escaped to mimic POST payload
 192          $user_id = self::factory()->user->create();
 193          xprofile_set_field_data( 1, $user_id, "Foo'Bar" );
 194          $q = new BP_User_Query( array( 'search_terms' => "oo\'Ba", ) );
 195  
 196          $found_user_id = null;
 197          if ( ! empty( $q->results ) ) {
 198              $found_user = array_pop( $q->results );
 199              $found_user_id = $found_user->ID;
 200          }
 201  
 202          $this->assertEquals( $user_id, $found_user_id );
 203      }
 204  
 205  	public function test_bp_user_query_search_with_percent_sign() {
 206  
 207          // LIKE special character: %
 208          $user_id = self::factory()->user->create();
 209          xprofile_set_field_data( 1, $user_id, "Foo%Bar" );
 210          $q = new BP_User_Query( array( 'search_terms' => "oo%Bar", ) );
 211  
 212          $found_user_id = null;
 213          if ( ! empty( $q->results ) ) {
 214              $found_user = array_pop( $q->results );
 215              $found_user_id = $found_user->ID;
 216          }
 217  
 218          $this->assertEquals( $user_id, $found_user_id );
 219  
 220      }
 221  
 222  	public function test_bp_user_query_search_with_underscore() {
 223  
 224          // LIKE special character: _
 225          $user_id = self::factory()->user->create();
 226          xprofile_set_field_data( 1, $user_id, "Foo_Bar" );
 227          $q = new BP_User_Query( array( 'search_terms' => "oo_Bar", ) );
 228  
 229          $found_user_id = null;
 230          if ( ! empty( $q->results ) ) {
 231              $found_user = array_pop( $q->results );
 232              $found_user_id = $found_user->ID;
 233          }
 234  
 235          $this->assertEquals( $user_id, $found_user_id );
 236      }
 237  
 238  	public function test_bp_user_query_search_with_ampersand_sign() {
 239  
 240          // LIKE special character: &
 241          $user_id = self::factory()->user->create();
 242          xprofile_set_field_data( 1, $user_id, "a&mpersand" );
 243          $q = new BP_User_Query( array( 'search_terms' => "a&m", ) );
 244  
 245          $found_user_id = null;
 246          if ( ! empty( $q->results ) ) {
 247              $found_user = array_pop( $q->results );
 248              $found_user_id = $found_user->ID;
 249          }
 250  
 251          $this->assertEquals( $user_id, $found_user_id );
 252  
 253      }
 254  
 255      /**
 256       * @group search_terms
 257       */
 258  	public function test_bp_user_query_search_core_fields() {
 259          $user_id = self::factory()->user->create( array(
 260              'user_login' => 'foo',
 261          ) );
 262          xprofile_set_field_data( 1, $user_id, "Bar" );
 263          $q = new BP_User_Query( array( 'search_terms' => 'foo', ) );
 264  
 265          $found_user_id = null;
 266          if ( ! empty( $q->results ) ) {
 267              $found_user = array_pop( $q->results );
 268              $found_user_id = $found_user->ID;
 269          }
 270  
 271          $this->assertEquals( $user_id, $found_user_id );
 272      }
 273  
 274  	public function test_bp_user_query_search_wildcards() {
 275          $u1 = self::factory()->user->create( array(
 276              'user_login' => 'xfoo',
 277          ) );
 278          xprofile_set_field_data( 1, $u1, "Bar" );
 279          $q1 = new BP_User_Query( array( 'search_terms' => 'foo', 'search_wildcard' => 'left' ) );
 280  
 281          $u2 = self::factory()->user->create( array(
 282              'user_login' => 'foox',
 283          ) );
 284          xprofile_set_field_data( 1, $u2, "Bar" );
 285          $q2 = new BP_User_Query( array( 'search_terms' => 'foo', 'search_wildcard' => 'right' ) );
 286  
 287          $u3 = self::factory()->user->create( array(
 288              'user_login' => 'xfoox',
 289          ) );
 290          xprofile_set_field_data( 1, $u3, "Bar" );
 291          $q3 = new BP_User_Query( array( 'search_terms' => 'foo', 'search_wildcard' => 'both' ) );
 292  
 293          $this->assertNotEmpty( $q1->results );
 294          $q1 = array_pop( $q1->results );
 295          $this->assertEquals( $u1, $q1->ID );
 296  
 297          $this->assertNotEmpty( $q2->results );
 298          $q2 = array_pop( $q2->results );
 299          $this->assertEquals( $u2, $q2->ID );
 300  
 301          $this->assertNotEmpty( $q3->results );
 302          foreach ( $q3->results as $user ) {
 303              $this->assertTrue( in_array( $user->ID, array( $u1, $u2, $u3 ) ) );
 304          }
 305      }
 306  
 307      /**
 308       * @group exclude
 309       */
 310  	public function test_bp_user_query_with_exclude() {
 311          // Grab list of existing users who should also be excluded
 312          global $wpdb;
 313          $existing_users = $wpdb->get_col( "SELECT ID FROM {$wpdb->users}" );
 314  
 315          $u1 = self::factory()->user->create();
 316          $u2 = self::factory()->user->create();
 317  
 318          $exclude = array_merge( array( $u1 ), $existing_users );
 319          $q = new BP_User_Query( array( 'exclude' => $exclude, ) );
 320  
 321          $found_user_ids = null;
 322          if ( ! empty( $q->results ) ) {
 323              $found_user_ids = array_values( wp_parse_id_list( wp_list_pluck( $q->results, 'ID' ) ) );
 324          }
 325  
 326          $this->assertEquals( array( $u2 ), $found_user_ids );
 327      }
 328  
 329      /**
 330       * @group exclude
 331       * @ticket BP8040
 332       */
 333      public function test_bp_user_query_should_ignore_empty_exclude() {
 334          $u1 = self::factory()->user->create();
 335          $u2 = self::factory()->user->create();
 336  
 337          $q = new BP_User_Query( array( 'exclude' => array() ) );
 338  
 339          $found_user_ids = null;
 340          if ( ! empty( $q->results ) ) {
 341              $found_user_ids = array_values( wp_parse_id_list( wp_list_pluck( $q->results, 'ID' ) ) );
 342          }
 343  
 344          $this->assertContains( $u1, $found_user_ids );
 345          $this->assertContains( $u2, $found_user_ids );
 346      }
 347      /**
 348       * @group type
 349       * @group spam
 350       */
 351      public function test_bp_user_query_type_alphabetical_spam_xprofileon() {
 352          if ( is_multisite() ) {
 353              $this->markTestSkipped();
 354          }
 355  
 356          // Make sure xprofile is on
 357          $xprofile_toggle = isset( buddypress()->active_components['xprofile'] );
 358          buddypress()->active_components['xprofile'] = 1;
 359          add_filter( 'bp_disable_profile_sync', '__return_false' );
 360  
 361          $u1 = self::factory()->user->create();
 362          $u2 = self::factory()->user->create();
 363  
 364          global $wpdb;
 365          bp_core_process_spammer_status( $u1, 'spam' );
 366  
 367          $q = new BP_User_Query( array( 'type' => 'alphabetical', ) );
 368  
 369          // Restore xprofile setting
 370          if ( $xprofile_toggle ) {
 371              buddypress()->active_components['xprofile'] = 1;
 372          } else {
 373              unset( buddypress()->active_components['xprofile'] );
 374          }
 375          remove_filter( 'bp_disable_profile_sync', '__return_false' );
 376  
 377          $found_user_ids = null;
 378  
 379          if ( ! empty( $q->results ) ) {
 380              $found_user_ids = array_values( wp_parse_id_list( wp_list_pluck( $q->results, 'ID' ) ) );
 381          }
 382  
 383          // Do a assertNotContains because there are weird issues with user #1 as created by WP
 384          $this->assertNotContains( $u1, $found_user_ids );
 385      }
 386  
 387      /**
 388       * @group type
 389       * @group spam
 390       */
 391      public function test_bp_user_query_type_alphabetical_spam_xprofileoff() {
 392          $u1 = self::factory()->user->create();
 393          $u2 = self::factory()->user->create();
 394  
 395          // Make sure xprofile and profile sync are off
 396          $xprofile_toggle = isset( buddypress()->active_components['xprofile'] );
 397          buddypress()->active_components['xprofile'] = 0;
 398          add_filter( 'bp_disable_profile_sync', '__return_false' );
 399  
 400          bp_core_process_spammer_status( $u1, 'spam' );
 401  
 402          $q = new BP_User_Query( array( 'type' => 'alphabetical', ) );
 403  
 404          // Restore xprofile setting
 405          if ( $xprofile_toggle ) {
 406              buddypress()->active_components['xprofile'] = 1;
 407          } else {
 408              unset( buddypress()->active_components['xprofile'] );
 409          }
 410          remove_filter( 'bp_disable_profile_sync', '__return_false' );
 411  
 412          $found_user_ids = null;
 413  
 414          if ( ! empty( $q->results ) ) {
 415              $found_user_ids = array_values( wp_parse_id_list( wp_list_pluck( $q->results, 'ID' ) ) );
 416          }
 417  
 418          // Do a assertNotContains because there are weird issues with user #1 as created by WP
 419          $this->assertNotContains( $u1, $found_user_ids );
 420      }
 421  
 422      /**
 423       * @group meta
 424       * @group BP5904
 425       */
 426  	public function test_bp_user_query_with_user_meta_argument() {
 427          $u1 = self::factory()->user->create();
 428          $u2 = self::factory()->user->create();
 429  
 430          bp_update_user_meta( $u2, 'foo', 'bar' );
 431  
 432          $q = new BP_User_Query( array(
 433              'meta_key'        => 'foo',
 434              'meta_value'      => 'bar',
 435          ) );
 436  
 437          $found_user_ids = array_values( wp_parse_id_list( wp_list_pluck( $q->results, 'ID' ) ) );
 438  
 439          // Do a assertNotContains because there are weird issues with user #1 as created by WP
 440          $this->assertNotContains( $u1, $found_user_ids );
 441          $this->assertEquals( array( $u2 ), $found_user_ids );
 442      }
 443  
 444      /**
 445       * @group meta
 446       * @group BP5904
 447       */
 448      public function test_bp_user_query_with_user_meta_argument_no_user() {
 449          $u1 = self::factory()->user->create();
 450          $u2 = self::factory()->user->create();
 451  
 452          $q = new BP_User_Query( array(
 453              'meta_key'        => 'foo',
 454              'meta_value'      => 'bar',
 455          ) );
 456  
 457          $found_user_ids = array_values( wp_parse_id_list( wp_list_pluck( $q->results, 'ID' ) ) );
 458  
 459          $this->assertEmpty( $found_user_ids );
 460      }
 461  
 462      /**
 463       * @group member_types
 464       */
 465  	public function test_member_type_single_value() {
 466          bp_register_member_type( 'foo' );
 467          bp_register_member_type( 'bar' );
 468          $users = self::factory()->user->create_many( 3 );
 469          bp_set_member_type( $users[0], 'foo' );
 470          bp_set_member_type( $users[1], 'bar' );
 471  
 472          $q = new BP_User_Query( array(
 473              'member_type' => 'bar',
 474          ) );
 475  
 476          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 477          $this->assertEquals( array( $users[1] ), $found );
 478      }
 479  
 480      /**
 481       * @group member_types
 482       */
 483  	public function test_member_type_array_with_single_value() {
 484          bp_register_member_type( 'foo' );
 485          bp_register_member_type( 'bar' );
 486          $users = self::factory()->user->create_many( 3 );
 487          bp_set_member_type( $users[0], 'foo' );
 488          bp_set_member_type( $users[1], 'bar' );
 489  
 490          $q = new BP_User_Query( array(
 491              'member_type' => array( 'bar' ),
 492          ) );
 493  
 494          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 495          $this->assertEquals( array( $users[1] ), $found );
 496      }
 497  
 498      /**
 499       * @group member_types
 500       */
 501  	public function test_member_type_comma_separated_values() {
 502          bp_register_member_type( 'foo' );
 503          bp_register_member_type( 'bar' );
 504          $users = self::factory()->user->create_many( 3 );
 505          bp_set_member_type( $users[0], 'foo' );
 506          bp_set_member_type( $users[1], 'bar' );
 507  
 508          $q = new BP_User_Query( array(
 509              'member_type' => 'foo, bar',
 510          ) );
 511  
 512          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 513          $this->assertEqualSets( array( $users[0], $users[1] ), $found );
 514      }
 515  
 516      /**
 517       * @group member_types
 518       */
 519  	public function test_member_type_array_with_multiple_values() {
 520          bp_register_member_type( 'foo' );
 521          bp_register_member_type( 'bar' );
 522          $users = self::factory()->user->create_many( 3 );
 523          bp_set_member_type( $users[0], 'foo' );
 524          bp_set_member_type( $users[1], 'bar' );
 525  
 526          $q = new BP_User_Query( array(
 527              'member_type' => array( 'foo', 'bar' ),
 528          ) );
 529  
 530          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 531          $this->assertEqualSets( array( $users[0], $users[1] ), $found );
 532      }
 533  
 534      /**
 535       * @group member_types
 536       */
 537      public function test_member_type_comma_separated_values_should_discard_non_existent_taxonomies() {
 538          bp_register_member_type( 'foo' );
 539          bp_register_member_type( 'bar' );
 540          $users = self::factory()->user->create_many( 3 );
 541          bp_set_member_type( $users[0], 'foo' );
 542          bp_set_member_type( $users[1], 'bar' );
 543  
 544          $q = new BP_User_Query( array(
 545              'member_type' => 'foo, baz',
 546          ) );
 547  
 548          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 549          $this->assertEqualSets( array( $users[0] ), $found );
 550      }
 551  
 552      /**
 553       * @group member_types
 554       * @ticket BP6334
 555       */
 556      public function test_should_return_no_results_when_no_users_match_the_specified_member_type() {
 557          bp_register_member_type( 'foo' );
 558          $users = self::factory()->user->create_many( 3 );
 559  
 560          $q = new BP_User_Query( array(
 561              'member_type' => 'foo, baz',
 562          ) );
 563  
 564          $this->assertEmpty( $q->results );
 565      }
 566  
 567      /**
 568       * @group member_types
 569       */
 570  	public function test_member_type__in_single_value() {
 571          bp_register_member_type( 'foo' );
 572          bp_register_member_type( 'bar' );
 573          $users = self::factory()->user->create_many( 3 );
 574          bp_set_member_type( $users[0], 'foo' );
 575          bp_set_member_type( $users[1], 'bar' );
 576  
 577          $q = new BP_User_Query( array(
 578              'member_type__in' => 'bar',
 579          ) );
 580  
 581          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 582          $this->assertEquals( array( $users[1] ), $found );
 583      }
 584  
 585      /**
 586       * @group member_types
 587       */
 588  	public function test_member_type__in_array_with_single_value() {
 589          bp_register_member_type( 'foo' );
 590          bp_register_member_type( 'bar' );
 591          $users = self::factory()->user->create_many( 3 );
 592          bp_set_member_type( $users[0], 'foo' );
 593          bp_set_member_type( $users[1], 'bar' );
 594  
 595          $q = new BP_User_Query( array(
 596              'member_type__in' => array( 'bar' ),
 597          ) );
 598  
 599          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 600          $this->assertEquals( array( $users[1] ), $found );
 601      }
 602  
 603      /**
 604       * @group member_types
 605       */
 606  	public function test_member_type__in_comma_separated_values() {
 607          bp_register_member_type( 'foo' );
 608          bp_register_member_type( 'bar' );
 609          $users = self::factory()->user->create_many( 3 );
 610          bp_set_member_type( $users[0], 'foo' );
 611          bp_set_member_type( $users[1], 'bar' );
 612  
 613          $q = new BP_User_Query( array(
 614              'member_type__in' => 'foo, bar',
 615          ) );
 616  
 617          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 618          $this->assertEqualSets( array( $users[0], $users[1] ), $found );
 619      }
 620  
 621      /**
 622       * @group member_types
 623       */
 624      public function test_member_type__in_array_with_multiple_values() {
 625          bp_register_member_type( 'foo' );
 626          bp_register_member_type( 'bar' );
 627          $users = self::factory()->user->create_many( 3 );
 628          bp_set_member_type( $users[0], 'foo' );
 629          bp_set_member_type( $users[1], 'bar' );
 630  
 631          $q = new BP_User_Query( array(
 632              'member_type__in' => array( 'foo', 'bar' ),
 633          ) );
 634  
 635          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 636          $this->assertEqualSets( array( $users[0], $users[1] ), $found );
 637      }
 638  
 639      /**
 640       * @group member_types
 641       */
 642      public function test_member_type__in_comma_separated_values_should_discard_non_existent_taxonomies() {
 643          bp_register_member_type( 'foo' );
 644          bp_register_member_type( 'bar' );
 645          $users = self::factory()->user->create_many( 3 );
 646          bp_set_member_type( $users[0], 'foo' );
 647          bp_set_member_type( $users[1], 'bar' );
 648  
 649          $q = new BP_User_Query( array(
 650              'member_type__in' => 'foo, baz',
 651          ) );
 652  
 653          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 654          $this->assertEqualSets( array( $users[0] ), $found );
 655      }
 656  
 657      /**
 658       * @group member_types
 659       */
 660      public function test_should_return_no_results_when_no_users_match_the_specified_member_type__in() {
 661          bp_register_member_type( 'foo' );
 662          $users = self::factory()->user->create_many( 3 );
 663  
 664          $q = new BP_User_Query( array(
 665              'member_type__in' => 'foo, baz',
 666          ) );
 667  
 668          $this->assertEmpty( $q->results );
 669      }
 670  
 671      /**
 672       * @group member_types
 673       */
 674      public function test_member_type_should_take_precedence_over_member_type__in() {
 675          bp_register_member_type( 'foo' );
 676          bp_register_member_type( 'bar' );
 677          $users = self::factory()->user->create_many( 3 );
 678          bp_set_member_type( $users[0], 'foo' );
 679          bp_set_member_type( $users[1], 'bar' );
 680  
 681          $q = new BP_User_Query( array(
 682              'member_type__in' => 'foo',
 683              'member_type' => 'bar'
 684          ) );
 685  
 686          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 687          $this->assertEqualSets( array( $users[1] ), $found );
 688      }
 689  
 690      /**
 691       * @group member_types
 692       */
 693      public function test_member_type__not_in_returns_members_from_other_types_and_members_with_no_types() {
 694          bp_register_member_type( 'foo' );
 695          bp_register_member_type( 'bar' );
 696          $users = self::factory()->user->create_many( 3 );
 697          bp_set_member_type( $users[0], 'foo' );
 698          bp_set_member_type( $users[1], 'bar' );
 699  
 700          $q = new BP_User_Query( array(
 701              'member_type__not_in' => 'foo',
 702          ) );
 703  
 704          $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
 705          $this->assertEqualSets( array( $users[1], $users[2] ), $found );
 706      }
 707  
 708      /**
 709       * @group member_types
 710       */
 711      public function test_should_return_no_results_when_all_users_match_the_specified_member_type__not_in() {
 712          bp_register_member_type( 'foo' );
 713          $users = self::factory()->user->create_many( 3 );
 714          bp_set_member_type( $users[0], 'foo' );
 715          bp_set_member_type( $users[1], 'foo' );
 716          bp_set_member_type( $users[2], 'foo' );
 717  
 718          $q = new BP_User_Query( array(
 719              'member_type__not_in' => 'foo',
 720          ) );
 721  
 722          $this->assertEmpty( $q->results );
 723      }
 724  
 725      /**
 726       * @group member_types
 727       */
 728      public function test_member_type__not_in_takes_precedence_over_member_type() {
 729          bp_register_member_type( 'foo' );
 730          $users = self::factory()->user->create_many( 3 );
 731          bp_set_member_type( $users[0], 'foo' );
 732          bp_set_member_type( $users[1], 'foo' );
 733          bp_set_member_type( $users[2], 'foo' );
 734  
 735          $q = new BP_User_Query( array(
 736              'member_type__not_in' => 'foo',
 737              'member_type' => 'foo'
 738          ) );
 739  
 740          $this->assertEmpty( $q->results );
 741      }
 742  
 743      /**
 744       * @group member_types
 745       */
 746      public function test_member_type__not_in_takes_precedence_over_member_type__in() {
 747          bp_register_member_type( 'foo' );
 748          $users = self::factory()->user->create_many( 3 );
 749          bp_set_member_type( $users[0], 'foo' );
 750          bp_set_member_type( $users[1], 'foo' );
 751          bp_set_member_type( $users[2], 'foo' );
 752  
 753          $q = new BP_User_Query( array(
 754              'member_type__not_in' => 'foo',
 755              'member_type__in' => 'foo'
 756          ) );
 757  
 758          $this->assertEmpty( $q->results );
 759      }
 760  
 761      /**
 762       * @group cache
 763       * @group member_types
 764       */
 765      public function test_member_type_should_be_prefetched_into_cache_during_user_query() {
 766          bp_register_member_type( 'foo' );
 767          bp_register_member_type( 'bar' );
 768          $users = self::factory()->user->create_many( 4 );
 769          bp_set_member_type( $users[0], 'foo' );
 770          bp_set_member_type( $users[1], 'bar' );
 771          bp_set_member_type( $users[2], 'foo' );
 772  
 773          $q = new BP_User_Query( array(
 774              'include' => $users,
 775          ) );
 776  
 777          $this->assertSame( array( 'foo' ), wp_cache_get( $users[0], 'bp_member_member_type' ) );
 778          $this->assertSame( array( 'bar' ), wp_cache_get( $users[1], 'bp_member_member_type' ) );
 779          $this->assertSame( array( 'foo' ), wp_cache_get( $users[2], 'bp_member_member_type' ) );
 780          $this->assertSame( '', wp_cache_get( $users[3], 'bp_member_member_type' ) );
 781      }
 782  
 783      /**
 784       * @group date_query
 785       */
 786  	public function test_date_query_before() {
 787          $u1 = self::factory()->user->create( array(
 788              'last_activity' => date( 'Y-m-d H:i:s', time() ),
 789          ) );
 790          $u2 = self::factory()->user->create( array(
 791              'last_activity' => '2008-03-25 17:13:55',
 792          ) );
 793          $u3 = self::factory()->user->create( array(
 794              'last_activity' => '2010-01-01 12:00',
 795          ) );
 796  
 797          // 'date_query' before test
 798          $query = new BP_User_Query( array(
 799              'date_query' => array( array(
 800                  'before' => array(
 801                      'year'  => 2010,
 802                      'month' => 1,
 803                      'day'   => 1,
 804                  ),
 805              ) )
 806          ) );
 807  
 808          $this->assertEquals( $u2, $query->user_ids[0] );
 809      }
 810  
 811      /**
 812       * @group date_query
 813       */
 814  	public function test_date_query_range() {
 815          $u1 = self::factory()->user->create( array(
 816              'last_activity' => date( 'Y-m-d H:i:s', time() ),
 817          ) );
 818          $u2 = self::factory()->user->create( array(
 819              'last_activity' => '2008-03-25 17:13:55',
 820          ) );
 821          $u3 = self::factory()->user->create( array(
 822              'last_activity' => '2001-01-01 12:00',
 823          ) );
 824  
 825          // 'date_query' range test
 826          $query = new BP_User_Query( array(
 827              'date_query' => array( array(
 828                  'after'  => 'January 2nd, 2001',
 829                  'before' => array(
 830                      'year'  => 2010,
 831                      'month' => 1,
 832                      'day'   => 1,
 833                  ),
 834                  'inclusive' => true,
 835              ) )
 836          ) );
 837  
 838          $this->assertEquals( $u2, $query->user_ids[0] );
 839      }
 840  
 841      /**
 842       * @group date_query
 843       */
 844  	public function test_date_query_after() {
 845          $u1 = self::factory()->user->create( array(
 846              'last_activity' => date( 'Y-m-d H:i:s', time() ),
 847          ) );
 848          $u2 = self::factory()->user->create( array(
 849              'last_activity' => '2008-03-25 17:13:55',
 850          ) );
 851          $u3 = self::factory()->user->create( array(
 852              'last_activity' => '2001-01-01 12:00',
 853          ) );
 854  
 855          // 'date_query' after and relative test
 856          $query = new BP_User_Query( array(
 857              'date_query' => array( array(
 858                  'after' => '1 day ago'
 859              ) )
 860          ) );
 861  
 862          $this->assertEquals( $u1, $query->user_ids[0] );
 863      }
 864  
 865  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1