[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/xprofile/ -> class-bp-xprofile-profiledata.php (source)

   1  <?php
   2  
   3  /**
   4   * @group xprofile
   5   * @group BP_XProfile_ProfileData
   6   */
   7  class BP_Tests_BP_XProfile_ProfileData_TestCases extends BP_UnitTestCase {
   8      /**
   9       * @group exists
  10       */
  11  	public function test_exists_when_doesnt_exist() {
  12          $u = self::factory()->user->create();
  13          $g = self::factory()->xprofile_group->create();
  14          $f = self::factory()->xprofile_field->create( array(
  15              'field_group_id' => $g,
  16          ) );
  17  
  18          $d = new BP_XProfile_ProfileData( $f, $u );
  19  
  20          $this->assertFalse( $d->exists() );
  21      }
  22  
  23      /**
  24       * @group exists
  25       */
  26  	public function test_exists_when_exists_uncached() {
  27          $u = self::factory()->user->create();
  28          $g = self::factory()->xprofile_group->create();
  29          $f = self::factory()->xprofile_field->create( array(
  30              'field_group_id' => $g,
  31          ) );
  32  
  33          xprofile_set_field_data( $f, $u, 'foo' );
  34  
  35          $d = new BP_XProfile_ProfileData( $f, $u );
  36  
  37          wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
  38  
  39          $this->assertTrue( $d->exists() );
  40      }
  41  
  42      /**
  43       * @group exists
  44       */
  45  	public function test_exists_when_exists_in_cache() {
  46          $u = self::factory()->user->create();
  47          $g = self::factory()->xprofile_group->create();
  48          $f = self::factory()->xprofile_field->create( array(
  49              'field_group_id' => $g,
  50          ) );
  51          $d = new BP_XProfile_ProfileData( $f, $u );
  52  
  53          // Fake the cache
  54          $c = new stdClass;
  55          $c->id = 3;
  56          wp_cache_set( "{$u}:{$f}", $c, 'bp_xprofile_data' );
  57  
  58          $this->assertTrue( $d->exists() );
  59      }
  60  
  61      /**
  62       * @group get_fielddataid_byid
  63       */
  64  	public function test_get_fielddataid_byid_when_doesnt_exist() {
  65          $u = self::factory()->user->create();
  66          $g = self::factory()->xprofile_group->create();
  67          $f = self::factory()->xprofile_field->create( array(
  68              'field_group_id' => $g,
  69          ) );
  70  
  71          // Just to be sure
  72          wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
  73  
  74          $this->assertEquals( 0, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) );
  75      }
  76  
  77      /**
  78       * @group get_fielddataid_byid
  79       */
  80      public function test_get_fielddataid_byid_when_exists_uncached() {
  81          $u = self::factory()->user->create();
  82          $g = self::factory()->xprofile_group->create();
  83          $f = self::factory()->xprofile_field->create( array(
  84              'field_group_id' => $g,
  85          ) );
  86  
  87          $d = new BP_XProfile_ProfileData();
  88          $d->user_id = $u;
  89          $d->field_id = $f;
  90          $d->value = 'foo';
  91          $d->save();
  92  
  93          // Ensure it's deleted from cache
  94          wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
  95  
  96          $this->assertEquals( $d->id, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) );
  97      }
  98  
  99      /**
 100       * @group get_fielddataid_byid
 101       */
 102      public function test_get_fielddataid_byid_when_exists_in_cache() {
 103          $u = self::factory()->user->create();
 104          $g = self::factory()->xprofile_group->create();
 105          $f = self::factory()->xprofile_field->create( array(
 106              'field_group_id' => $g,
 107          ) );
 108  
 109          // Fake the cache
 110          $d = new stdClass;
 111          $d->id = 5;
 112          wp_cache_set( "{$u}:{$f}", $d, 'bp_xprofile_data' );
 113  
 114          $this->assertSame( 5, BP_XProfile_ProfileData::get_fielddataid_byid( $f, $u ) );
 115      }
 116  
 117      /**
 118       * @group get_value_byid
 119       */
 120  	public function test_get_value_byid_singleuser_uncached() {
 121          $u = self::factory()->user->create();
 122          $g = self::factory()->xprofile_group->create();
 123          $f = self::factory()->xprofile_field->create( array(
 124              'field_group_id' => $g,
 125          ) );
 126  
 127          $d = new BP_XProfile_ProfileData();
 128          $d->user_id = $u;
 129          $d->field_id = $f;
 130          $d->value = 'foo';
 131          $d->save();
 132  
 133          // Ensure it's deleted from cache
 134          wp_cache_delete( "{$u}:{$f}", 'bp_xprofile_data' );
 135  
 136          $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f, $u ) );
 137      }
 138  
 139      /**
 140       * @group get_value_byid
 141       */
 142  	public function test_get_value_byid_multipleusers_uncached() {
 143          $time = date( 'Y-m-d H:i:s', time() - 60*60*24 );
 144  
 145          $u1 = self::factory()->user->create();
 146          $u2 = self::factory()->user->create();
 147          $g = self::factory()->xprofile_group->create();
 148          $f = self::factory()->xprofile_field->create( array(
 149              'field_group_id' => $g,
 150          ) );
 151  
 152          // To make the tests reliable, we have to force the last_updated
 153          // BP doesn't easily allow this except via filter
 154          $this->last_updated = $time;
 155          add_filter( 'xprofile_data_last_updated_before_save', array( $this, 'filter_time' ) );
 156  
 157          $d1 = new BP_XProfile_ProfileData();
 158          $d1->user_id = $u1;
 159          $d1->field_id = $f;
 160          $d1->value = 'foo';
 161          $d1->last_updated = $time;
 162          $d1->save();
 163  
 164          $d2 = new BP_XProfile_ProfileData();
 165          $d2->user_id = $u2;
 166          $d2->field_id = $f;
 167          $d2->value = 'bar';
 168          $d2->last_updated = $time;
 169          $d2->save();
 170  
 171          remove_filter( 'xprofile_data_last_updated_before_save', array( $this, 'filter_time' ) );
 172  
 173          // Ensure it's deleted from cache
 174          wp_cache_delete( "{$u1}:{$f}", 'bp_xprofile_data' );
 175          wp_cache_delete( "{$u2}:{$f}", 'bp_xprofile_data' );
 176  
 177          $eu1 = new stdClass;
 178          $eu1->user_id = $u1;
 179          $eu1->value = 'foo';
 180          $eu1->id = $d1->id;
 181          $eu1->field_id = $f;
 182          $eu1->last_updated = $time;
 183  
 184          $eu2 = new stdClass;
 185          $eu2->user_id = $u2;
 186          $eu2->value = 'bar';
 187          $eu2->id = $d2->id;
 188          $eu2->field_id = $f;
 189          $eu2->last_updated = $time;
 190  
 191          $expected = array( $eu1, $eu2 );
 192  
 193          $this->assertEquals( $expected, BP_XProfile_ProfileData::get_value_byid( $f, array( $u1, $u2 ) ) );
 194      }
 195  
 196      /**
 197       * @group get_value_byid
 198       */
 199  	public function test_get_value_byid_singleuser_cached() {
 200          $u = self::factory()->user->create();
 201          $g = self::factory()->xprofile_group->create();
 202          $f = self::factory()->xprofile_field->create( array(
 203              'field_group_id' => $g,
 204          ) );
 205  
 206          $time = bp_core_current_time();
 207  
 208          // Fake the cache
 209          $d = new stdClass;
 210          $d->value = 'foo';
 211          $d->field_id = $f;
 212          wp_cache_set( "{$u}:{$f}", $d, 'bp_xprofile_data' );
 213  
 214          $this->assertSame( 'foo', BP_XProfile_ProfileData::get_value_byid( $f, $u ) );
 215      }
 216  
 217      /**
 218       * @group get_value_byid
 219       */
 220  	public function test_get_value_byid_multipleusers_cached() {
 221          $time = date( 'Y-m-d H:i:s', time() - 60*60*24 );
 222  
 223          $u1 = self::factory()->user->create();
 224          $u2 = self::factory()->user->create();
 225          $g = self::factory()->xprofile_group->create();
 226          $f = self::factory()->xprofile_field->create( array(
 227              'field_group_id' => $g,
 228          ) );
 229  
 230          // Fake the cache
 231          $d1 = new stdClass;
 232          $d1->id = 10;
 233          $d1->user_id = $u1;
 234          $d1->field_id = $f;
 235          $d1->value = 'foo';
 236          $d1->last_updated = $time;
 237  
 238          $d2 = new stdClass;
 239          $d1->id = 21;
 240          $d2->user_id = $u2;
 241          $d2->field_id = $f;
 242          $d2->value = 'bar';
 243          $d2->last_updated = $time;
 244  
 245          wp_cache_set( "{$u1}:{$f}", $d1, 'bp_xprofile_data' );
 246          wp_cache_set( "{$u2}:{$f}", $d2, 'bp_xprofile_data' );
 247  
 248          $eu1 = new stdClass;
 249          $eu1->id = 10;
 250          $eu1->user_id = $u1;
 251          $eu1->field_id = $f;
 252          $eu1->value = 'foo';
 253          $eu1->last_updated = $time;
 254  
 255          $eu2 = new stdClass;
 256          $eu1->id = 21;
 257          $eu2->user_id = $u2;
 258          $eu2->field_id = $f;
 259          $eu2->value = 'bar';
 260          $eu2->last_updated = $time;
 261  
 262          $expected = array( $eu1, $eu2 );
 263  
 264          $this->assertEquals( $expected, BP_XProfile_ProfileData::get_value_byid( $f, array( $u1, $u2 ) ) );
 265      }
 266  
 267      /**
 268       * @group get_all_for_user
 269       */
 270  	public function test_get_all_for_user_uncached() {
 271          $u = self::factory()->user->create();
 272          $g1 = self::factory()->xprofile_group->create();
 273          $g2 = self::factory()->xprofile_group->create();
 274          $f1 = self::factory()->xprofile_field->create( array(
 275              'type' => 'textbox',
 276              'field_group_id' => $g1,
 277          ) );
 278          $f2 = self::factory()->xprofile_field->create( array(
 279              'type' => 'radio',
 280              'field_group_id' => $g2,
 281          ) );
 282  
 283          $time = bp_core_current_time();
 284  
 285          // Get the fullname field - hackish
 286          $f0_id = xprofile_get_field_id_from_name( bp_xprofile_fullname_field_name() );
 287          $f0 = new BP_XProfile_Field( $f0_id );
 288          $g0 = new BP_XProfile_Group( $f0->group_id );
 289          $d0 = new BP_XProfile_ProfileData( $f0->id, $u );
 290  
 291          $d1 = new BP_XProfile_ProfileData();
 292          $d1->user_id = $u;
 293          $d1->field_id = $f1;
 294          $d1->value = 'foo';
 295          $d1->last_updated = $time;
 296          $d1->save();
 297  
 298          $d2 = new BP_XProfile_ProfileData();
 299          $d2->user_id = $u;
 300          $d2->field_id = $f2;
 301          $d2->value = 'bar';
 302          $d2->last_updated = $time;
 303          $d2->save();
 304  
 305          // Ensure it's deleted from cache
 306          wp_cache_delete( "{$u}:{$f1}", 'bp_xprofile_data' );
 307          wp_cache_delete( "{$u}:{$f2}", 'bp_xprofile_data' );
 308  
 309          $u_obj = new WP_User( $u );
 310  
 311          $g1_obj = new BP_XProfile_Group( $g1 );
 312          $g2_obj = new BP_XProfile_Group( $g2 );
 313          $f1_obj = new BP_XProfile_Field( $f1 );
 314          $f2_obj = new BP_XProfile_Field( $f2 );
 315  
 316          $expected = array(
 317              'user_login' => $u_obj->user_login,
 318              'user_nicename' => $u_obj->user_nicename,
 319              'user_email' => $u_obj->user_email,
 320              $f0->name => array(
 321                  'field_group_id' => $g0->id,
 322                  'field_group_name' => $g0->name,
 323                  'field_id' => $f0->id,
 324                  'field_type' => $f0->type,
 325                  'field_data' => $d0->value,
 326              ),
 327              $f1_obj->name => array(
 328                  'field_group_id' => $g1,
 329                  'field_group_name' => $g1_obj->name,
 330                  'field_id' => $f1,
 331                  'field_type' => $f1_obj->type,
 332                  'field_data' => $d1->value,
 333              ),
 334              $f2_obj->name => array(
 335                  'field_group_id' => $g2,
 336                  'field_group_name' => $g2_obj->name,
 337                  'field_id' => $f2,
 338                  'field_type' => $f2_obj->type,
 339                  'field_data' => $d2->value,
 340              ),
 341          );
 342  
 343          $this->assertEquals( $expected, BP_XProfile_ProfileData::get_all_for_user( $u ) );
 344      }
 345  
 346      /**
 347       * @group get_all_for_user
 348       */
 349  	public function test_get_all_for_user_cached() {
 350          $u = self::factory()->user->create();
 351          $g1 = self::factory()->xprofile_group->create();
 352          $g2 = self::factory()->xprofile_group->create();
 353          $f1 = self::factory()->xprofile_field->create( array(
 354              'type' => 'textbox',
 355              'field_group_id' => $g1,
 356          ) );
 357          $f2 = self::factory()->xprofile_field->create( array(
 358              'type' => 'radio',
 359              'field_group_id' => $g2,
 360          ) );
 361  
 362          $time = bp_core_current_time();
 363  
 364          $g0 = new BP_XProfile_Group( 1 );
 365          $f0 = new BP_XProfile_Field( 1 );
 366          $d0 = new BP_XProfile_ProfileData( 1, $u );
 367  
 368          $d1 = new stdClass;
 369          $d1->user_id = $u;
 370          $d1->field_id = $f1;
 371          $d1->value = 'foo';
 372          $d1->last_updated = $time;
 373          $d1->id = 1;
 374  
 375          $d2 = new stdClass;
 376          $d2->user_id = $u;
 377          $d2->field_id = $f2;
 378          $d2->value = 'bar';
 379          $d2->last_updated = $time;
 380          $d2->id = 2;
 381  
 382          wp_cache_set( "{$u}:{$f1}", $d1, 'bp_xprofile_data' );
 383          wp_cache_set( "{$u}:{$f2}", $d2, 'bp_xprofile_data' );
 384  
 385          $u_obj = new WP_User( $u );
 386  
 387          $g1_obj = new BP_XProfile_Group( $g1 );
 388          $g2_obj = new BP_XProfile_Group( $g2 );
 389          $f1_obj = new BP_XProfile_Field( $f1 );
 390          $f2_obj = new BP_XProfile_Field( $f2 );
 391  
 392          $expected = array(
 393              'user_login' => $u_obj->user_login,
 394              'user_nicename' => $u_obj->user_nicename,
 395              'user_email' => $u_obj->user_email,
 396              $f0->name => array(
 397                  'field_group_id' => $g0->id,
 398                  'field_group_name' => $g0->name,
 399                  'field_id' => $f0->id,
 400                  'field_type' => $f0->type,
 401                  'field_data' => $d0->value,
 402              ),
 403              $f1_obj->name => array(
 404                  'field_group_id' => $g1,
 405                  'field_group_name' => $g1_obj->name,
 406                  'field_id' => $f1,
 407                  'field_type' => $f1_obj->type,
 408                  'field_data' => $d1->value,
 409              ),
 410              $f2_obj->name => array(
 411                  'field_group_id' => $g2,
 412                  'field_group_name' => $g2_obj->name,
 413                  'field_id' => $f2,
 414                  'field_type' => $f2_obj->type,
 415                  'field_data' => $d2->value,
 416              ),
 417          );
 418  
 419          $this->assertEquals( $expected, BP_XProfile_ProfileData::get_all_for_user( $u ) );
 420      }
 421  
 422      /**
 423       * @ticket BP8388
 424       * @group xprofile_remove_data
 425       */
 426      public function test_cache_invalidated_when_xprofile_remove_data_called() {
 427          $u = self::factory()->user->create();
 428          $g = self::factory()->xprofile_group->create();
 429          $f = self::factory()->xprofile_field->create( array(
 430              'field_group_id' => $g,
 431          ) );
 432  
 433          xprofile_set_field_data( $f, $u, 'Foo' );
 434          // Prime the cache.
 435          xprofile_get_field_data( $f, $u );
 436          xprofile_remove_data( $u );
 437  
 438          $d = new BP_XProfile_ProfileData( $f, $u );
 439          $this->assertFalse( $d->exists() );
 440      }
 441  
 442  	public function filter_time() {
 443          return $this->last_updated;
 444      }
 445  }


Generated: Sat Apr 27 01:00:55 2024 Cross-referenced by PHPXref 0.7.1