[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group core
   5   * @group BP_Core_User
   6   */
   7  class BP_Tests_BP_Core_User_TestCases extends BP_UnitTestCase {
   8      /**
   9       * @expectedDeprecated BP_Core_User::get_users
  10       */
  11  	public function test_get_users_with_exclude_querystring() {
  12          add_filter( 'bp_use_legacy_user_query', '__return_true' );
  13  
  14          $u1 = self::factory()->user->create();
  15          $u2 = self::factory()->user->create();
  16          $u3 = self::factory()->user->create();
  17  
  18          $exclude_qs = $u1 . ',junkstring,' . $u3;
  19  
  20          $users = BP_Core_User::get_users( 'active', 0, 1, 0, false, false, true, $exclude_qs );
  21          $user_ids = wp_parse_id_list( wp_list_pluck( $users['users'], 'id' ) );
  22  
  23          remove_filter( 'bp_use_legacy_user_query', '__return_true' );
  24  
  25          $this->assertEquals( array( $u2 ), $user_ids );
  26      }
  27  
  28      /**
  29       * @expectedDeprecated BP_Core_User::get_users
  30       */
  31  	public function test_get_users_with_exclude_array() {
  32          add_filter( 'bp_use_legacy_user_query', '__return_true' );
  33  
  34          $u1 = self::factory()->user->create();
  35          $u2 = self::factory()->user->create();
  36          $u3 = self::factory()->user->create();
  37  
  38          $exclude_array = array(
  39              $u1,
  40              'junkstring',
  41              $u3,
  42          );
  43  
  44          $users = BP_Core_User::get_users( 'active', 0, 1, 0, false, false, true, $exclude_array );
  45          $user_ids = wp_parse_id_list( wp_list_pluck( $users['users'], 'id' ) );
  46  
  47          remove_filter( 'bp_use_legacy_user_query', '__return_true' );
  48  
  49          $this->assertEquals( array( $u2 ), $user_ids );
  50      }
  51  
  52      /**
  53       * @expectedDeprecated BP_Core_User::get_users
  54       */
  55  	public function test_get_users_with_include_querystring() {
  56          add_filter( 'bp_use_legacy_user_query', '__return_true' );
  57  
  58          $u1 = self::factory()->user->create( array(
  59              'last_activity' => gmdate( 'Y-m-d H:i:s' ),
  60          ) );
  61          $u2 = self::factory()->user->create( array(
  62              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
  63          ) );
  64          $u3 = self::factory()->user->create( array(
  65              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 50 ),
  66          ) );
  67  
  68          $include_qs = $u1 . ',junkstring,' . $u3;
  69  
  70          $users = BP_Core_User::get_users( 'active', 0, 1, 0, $include_qs );
  71          $user_ids = wp_parse_id_list( wp_list_pluck( $users['users'], 'id' ) );
  72  
  73          remove_filter( 'bp_use_legacy_user_query', '__return_true' );
  74  
  75          $this->assertEquals( array( $u1, $u3 ), $user_ids );
  76      }
  77  
  78      /**
  79       * @expectedDeprecated BP_Core_User::get_users
  80       */
  81  	public function test_get_users_with_include_array() {
  82          add_filter( 'bp_use_legacy_user_query', '__return_true' );
  83  
  84          $u1 = self::factory()->user->create( array(
  85              'last_activity' => gmdate( 'Y-m-d H:i:s' ),
  86          ) );
  87          $u2 = self::factory()->user->create( array(
  88              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
  89          ) );
  90          $u3 = self::factory()->user->create( array(
  91              'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 50 ),
  92          ) );
  93  
  94  
  95          $include_array = array(
  96              $u1,
  97              'junkstring',
  98              $u3,
  99          );
 100  
 101          $users = BP_Core_User::get_users( 'active', 0, 1, 0, $include_array );
 102          $user_ids = wp_list_pluck( $users['users'], 'id' );
 103  
 104          // typecast...ugh
 105          $user_ids = array_map( 'intval', $user_ids );
 106  
 107          remove_filter( 'bp_use_legacy_user_query', '__return_true' );
 108  
 109          $this->assertEquals( array( $u1, $u3 ), $user_ids );
 110      }
 111  
 112      /**
 113       * @expectedDeprecated BP_Core_User::get_users
 114       * @group get_users
 115       * @group type
 116       */
 117  	public function test_type_alphabetical() {
 118          $u1 = self::factory()->user->create( array(
 119              'display_name' => 'foo',
 120          ) );
 121          $u2 = self::factory()->user->create( array(
 122              'display_name' => 'bar',
 123          ) );
 124  
 125          global $wpdb;
 126  
 127          $q = BP_Core_User::get_users( 'alphabetical' );
 128          $found = array_map( 'intval', wp_list_pluck( $q['users'], 'id' ) );
 129  
 130          $this->assertEquals( array( $u2, $u1 ), $found );
 131      }
 132  
 133      /**
 134       * @group get_users_by_letter
 135       */
 136  	public function test_get_users_by_letter() {
 137          $u1 = self::factory()->user->create( array(
 138              'display_name' => 'foo',
 139          ) );
 140          $u2 = self::factory()->user->create( array(
 141              'display_name' => 'bar',
 142          ) );
 143  
 144          $q = BP_Core_User::get_users_by_letter( 'b' );
 145          $found = array_map( 'intval', wp_list_pluck( $q['users'], 'id' ) );
 146  
 147          $this->assertEquals( array( $u2 ), $found );
 148      }
 149  
 150      /**
 151       * @group search_users
 152       */
 153  	public function test_search_users() {
 154          $u1 = self::factory()->user->create( array(
 155              'display_name' => 'foo',
 156          ) );
 157          $u2 = self::factory()->user->create( array(
 158              'display_name' => 'bar',
 159          ) );
 160  
 161          $q = BP_Core_User::search_users( 'ar' );
 162          $found = array_map( 'intval', wp_list_pluck( $q['users'], 'id' ) );
 163  
 164          $this->assertEquals( array( $u2 ), $found );
 165      }
 166  
 167  	public function test_get_specific_users() {
 168          $u1 = self::factory()->user->create();
 169          $u2 = self::factory()->user->create();
 170          $u3 = self::factory()->user->create();
 171  
 172          $include_array = array(
 173              $u1,
 174              'junkstring',
 175              $u3,
 176          );
 177  
 178          $users = BP_Core_User::get_specific_users( $include_array );
 179          $user_ids = wp_parse_id_list( wp_list_pluck( $users['users'], 'id' ) );
 180  
 181          $this->assertEquals( array( $u1, $u3 ), $user_ids );
 182      }
 183  
 184      /**
 185       * @group last_activity
 186       */
 187  	public function test_get_last_activity() {
 188          $u = self::factory()->user->create();
 189          $time = bp_core_current_time();
 190  
 191          BP_Core_User::update_last_activity( $u, $time );
 192  
 193          $a = BP_Core_User::get_last_activity( $u );
 194          $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : '';
 195  
 196          $this->assertEquals( $time, $found );
 197      }
 198  
 199      /**
 200       * @group last_activity
 201       * @group cache
 202       */
 203  	public function test_get_last_activity_store_in_cache() {
 204          $u = self::factory()->user->create();
 205          $time = bp_core_current_time();
 206  
 207          // Cache is set during user creation. Clear to reflect actual
 208          // pageload
 209          wp_cache_delete( $u, 'bp_last_activity' );
 210  
 211          // prime cache
 212          $a = BP_Core_User::get_last_activity( $u );
 213  
 214          $this->assertSame( $a[ $u ], wp_cache_get( $u, 'bp_last_activity' ) );
 215      }
 216  
 217      /**
 218       * @group last_activity
 219       * @group cache
 220       */
 221      public function test_get_last_activity_store_in_cache_multiple_users() {
 222          $u1 = self::factory()->user->create();
 223          $u2 = self::factory()->user->create();
 224          $time = bp_core_current_time();
 225  
 226          // Cache is set during user creation. Clear to reflect actual
 227          // pageload
 228          wp_cache_delete( $u1, 'bp_last_activity' );
 229          wp_cache_delete( $u2, 'bp_last_activity' );
 230  
 231          // prime cache
 232          $a = BP_Core_User::get_last_activity( array( $u1, $u2 ) );
 233  
 234          $this->assertSame( $a[ $u1 ], wp_cache_get( $u1, 'bp_last_activity' ) );
 235          $this->assertSame( $a[ $u2 ], wp_cache_get( $u2, 'bp_last_activity' ) );
 236      }
 237  
 238      /**
 239       * @group last_activity
 240       * @group cache
 241       */
 242  	public function test_get_last_activity_from_cache_single_user() {
 243          $u    = self::factory()->user->create();
 244          $time = bp_core_current_time();
 245  
 246          BP_Core_User::update_last_activity( $u, $time );
 247  
 248          // Cache is set during user creation. Clear to reflect actual
 249          // pageload
 250          wp_cache_delete( $u, 'bp_last_activity' );
 251  
 252          // Prime cache
 253          $uncached = BP_Core_User::get_last_activity( $u );
 254  
 255          // Fetch again to get from the cache
 256          $cached = BP_Core_User::get_last_activity( $u );
 257  
 258          $this->assertSame( $uncached, $cached );
 259      }
 260  
 261      /**
 262       * @group last_activity
 263       * @group cache
 264       */
 265      public function test_get_last_activity_in_cache_multiple_users() {
 266          $u1 = self::factory()->user->create();
 267          $u2 = self::factory()->user->create();
 268          $time = bp_core_current_time();
 269  
 270          BP_Core_User::update_last_activity( $u1, $time );
 271          BP_Core_User::update_last_activity( $u2, $time );
 272  
 273          // Cache is set during user creation. Clear to reflect actual pageload
 274          wp_cache_delete( $u1, 'bp_last_activity' );
 275          wp_cache_delete( $u2, 'bp_last_activity' );
 276  
 277          // Prime cache
 278          $uncached = BP_Core_User::get_last_activity( array( $u1, $u2 ) );
 279  
 280          // Second grab will be from the cache
 281          $cached = BP_Core_User::get_last_activity( array( $u1, $u2 ) );
 282          $cached_u1 = wp_cache_get( $u1, 'bp_last_activity' );
 283  
 284          $this->assertSame( $cached, $uncached );
 285      }
 286  
 287      /**
 288       * @group last_activity
 289       */
 290  	public function test_update_last_activity() {
 291          $u = self::factory()->user->create();
 292          $time = bp_core_current_time();
 293          $time2 = '1968-12-25 01:23:45';
 294  
 295          BP_Core_User::update_last_activity( $u, $time );
 296          $a = BP_Core_User::get_last_activity( $u );
 297          $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : '';
 298          $this->assertEquals( $time, $found );
 299  
 300          BP_Core_User::update_last_activity( $u, $time2 );
 301          $a = BP_Core_User::get_last_activity( $u );
 302          $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : '';
 303          $this->assertEquals( $time2, $found );
 304      }
 305  
 306      /**
 307       * @group last_activity
 308       */
 309  	public function test_delete_last_activity() {
 310          $u = self::factory()->user->create();
 311          $time = bp_core_current_time();
 312  
 313          BP_Core_User::update_last_activity( $u, $time );
 314          $a = BP_Core_User::get_last_activity( $u );
 315          $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : '';
 316          $this->assertEquals( $time, $found );
 317  
 318          BP_Core_User::delete_last_activity( $u );
 319          $a = BP_Core_User::get_last_activity( $u );
 320          $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : '';
 321          $this->assertEquals( '', $found );
 322      }
 323  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1