[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/xprofile/ -> cache.php (source)

   1  <?php
   2  
   3  /**
   4   * @group xprofile
   5   * @group cache
   6   */
   7  class BP_Tests_XProfile_Cache extends BP_UnitTestCase {
   8      /**
   9       * @group bp_xprofile_update_meta_cache
  10       */
  11  	public function test_bp_xprofile_update_meta_cache() {
  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          $d->user_id = $u;
  20          $d->field_id = $f;
  21          $d->value = 'foo';
  22          $d->last_updated = bp_core_current_time();
  23          $d->save();
  24  
  25          bp_xprofile_add_meta( $g, 'group', 'group_foo', 'group_bar' );
  26          bp_xprofile_add_meta( $f, 'field', 'field_foo', 'field_bar' );
  27          bp_xprofile_add_meta( $d->id, 'data', 'data_foo', 'data_bar' );
  28  
  29          // prime cache
  30          bp_xprofile_update_meta_cache( array(
  31              'group' => array( $g ),
  32              'field' => array( $f ),
  33              'data' => array( $d->id ),
  34          ) );
  35  
  36          $g_expected = array(
  37              'group_foo' => array(
  38                  'group_bar',
  39              ),
  40          );
  41  
  42          $this->assertSame( $g_expected, wp_cache_get( $g, 'xprofile_group_meta' ) );
  43  
  44          $f_expected = array(
  45              'field_foo' => array(
  46                  'field_bar',
  47              ),
  48          );
  49  
  50          $this->assertSame( $f_expected, wp_cache_get( $f, 'xprofile_field_meta' ) );
  51  
  52          $d_expected = array(
  53              'data_foo' => array(
  54                  'data_bar',
  55              ),
  56          );
  57  
  58          $this->assertSame( $d_expected, wp_cache_get( $d->id, 'xprofile_data_meta' ) );
  59      }
  60  
  61      /**
  62       * @group bp_xprofile_update_meta_cache
  63       * @group bp_has_profile
  64       */
  65  	public function test_bp_has_profile_meta_cache() {
  66          $u = self::factory()->user->create();
  67          $g = self::factory()->xprofile_group->create();
  68          $f = self::factory()->xprofile_field->create( array(
  69              'field_group_id' => $g,
  70          ) );
  71  
  72          $d = new BP_XProfile_ProfileData( $f, $u );
  73          $d->user_id = $u;
  74          $d->field_id = $f;
  75          $d->value = 'foo';
  76          $d->last_updated = bp_core_current_time();
  77          $d->save();
  78  
  79          bp_xprofile_add_meta( $g, 'group', 'group_foo', 'group_bar' );
  80          bp_xprofile_add_meta( $f, 'field', 'field_foo', 'field_bar' );
  81          bp_xprofile_add_meta( $d->id, 'data', 'data_foo', 'data_bar' );
  82  
  83          // prime cache
  84          bp_has_profile( array(
  85              'user_id' => $u,
  86              'profile_group_id' => $g,
  87          ) );
  88  
  89          $g_expected = array(
  90              'group_foo' => array(
  91                  'group_bar',
  92              ),
  93          );
  94  
  95          $this->assertSame( $g_expected, wp_cache_get( $g, 'xprofile_group_meta' ) );
  96  
  97          $f_expected = array(
  98              'field_foo' => array(
  99                  'field_bar',
 100              ),
 101          );
 102  
 103          $this->assertSame( $f_expected, wp_cache_get( $f, 'xprofile_field_meta' ) );
 104  
 105          $d_expected = array(
 106              'data_foo' => array(
 107                  'data_bar',
 108              ),
 109          );
 110  
 111          $this->assertSame( $d_expected, wp_cache_get( $d->id, 'xprofile_data_meta' ) );
 112      }
 113  
 114      /**
 115       * @group bp_xprofile_update_meta_cache
 116       * @group bp_has_profile
 117       */
 118      public function test_bp_has_profile_meta_cache_update_meta_cache_false() {
 119          $u = self::factory()->user->create();
 120          $g = self::factory()->xprofile_group->create();
 121          $f = self::factory()->xprofile_field->create( array(
 122              'field_group_id' => $g,
 123          ) );
 124  
 125          $d = new BP_XProfile_ProfileData( $f, $u );
 126          $d->user_id = $u;
 127          $d->field_id = $f;
 128          $d->value = 'foo';
 129          $d->last_updated = bp_core_current_time();
 130          $d->save();
 131  
 132          bp_xprofile_add_meta( $g, 'group', 'group_foo', 'group_bar' );
 133          bp_xprofile_add_meta( $f, 'field', 'field_foo', 'field_bar' );
 134          bp_xprofile_add_meta( $d->id, 'data', 'data_foo', 'data_bar' );
 135  
 136          // Prime cache.
 137          bp_has_profile( array(
 138              'user_id' => $u,
 139              'profile_group_id' => $g,
 140              'update_meta_cache' => false,
 141          ) );
 142  
 143          $this->assertFalse( wp_cache_get( $g, 'xprofile_group_meta' ) );
 144          $this->assertFalse( wp_cache_get( $f, 'xprofile_field_meta' ) );
 145          $this->assertFalse( wp_cache_get( $d->id, 'xprofile_data_meta' ) );
 146      }
 147  
 148      /**
 149       * @ticket BP6638
 150       */
 151      public function test_field_cache_should_be_invalidated_on_save() {
 152          $g = self::factory()->xprofile_group->create();
 153          $f = self::factory()->xprofile_field->create( array(
 154              'field_group_id' => $g,
 155              'name' => 'Foo',
 156          ) );
 157  
 158          $field = xprofile_get_field( $f );
 159          $this->assertSame( 'Foo', $field->name );
 160  
 161          $field->name = 'Bar';
 162          $this->assertNotEmpty( $field->save() );
 163  
 164          $field_2 = xprofile_get_field( $f );
 165          $this->assertSame( 'Bar', $field_2->name );
 166      }
 167  
 168      /**
 169       * @ticket BP7407
 170       */
 171  	public function test_get_field_id_from_name_should_be_cached() {
 172          global $wpdb;
 173  
 174          $g = self::factory()->xprofile_group->create();
 175          $f = self::factory()->xprofile_field->create( array(
 176              'field_group_id' => $g,
 177              'name' => 'Foo',
 178          ) );
 179  
 180          // Prime cache.
 181          BP_XProfile_Field::get_id_from_name( 'Foo' );
 182  
 183          $num_queries = $wpdb->num_queries;
 184  
 185          $this->assertSame( $f, BP_XProfile_Field::get_id_from_name( 'Foo' ) );
 186          $this->assertSame( $num_queries, $wpdb->num_queries );
 187      }
 188  
 189      /**
 190       * @ticket BP7407
 191       */
 192      public function test_get_field_id_from_name_cache_should_be_invalidated_on_field_update() {
 193          $g = self::factory()->xprofile_group->create();
 194          $f1 = self::factory()->xprofile_field->create( array(
 195              'field_group_id' => $g,
 196              'name' => 'Foo',
 197          ) );
 198          $f2 = self::factory()->xprofile_field->create( array(
 199              'field_group_id' => $g,
 200              'name' => 'Bar',
 201          ) );
 202  
 203          // Prime cache.
 204          xprofile_get_field_id_from_name( 'Foo' );
 205          xprofile_get_field_id_from_name( 'Bar' );
 206  
 207          // Free up the name 'Bar'.
 208          $field2 = xprofile_get_field( $f2 );
 209          $field2->name = 'Quz';
 210          $field2->save();
 211  
 212          // Take the name 'Bar'.
 213          $field1 = xprofile_get_field( $f1 );
 214          $field1->name = 'Bar';
 215          $field1->save();
 216  
 217          $this->assertSame( $f1, BP_XProfile_Field::get_id_from_name( 'Bar' ) );
 218      }
 219  
 220      /**
 221       * @ticket BP7407
 222       */
 223      public function test_get_field_id_from_name_cache_should_be_invalidated_on_field_deletion() {
 224          $g = self::factory()->xprofile_group->create();
 225          $f = self::factory()->xprofile_field->create( array(
 226              'field_group_id' => $g,
 227              'name' => 'Foo',
 228          ) );
 229  
 230          // Prime cache.
 231          xprofile_get_field_id_from_name( 'Foo' );
 232  
 233          xprofile_delete_field( $f );
 234  
 235          $this->assertNull( BP_XProfile_Field::get_id_from_name( 'Bar' ) );
 236      }
 237  }


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