[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group blogs
   5   * @group cache
   6   */
   7  class BP_Tests_Blogs_Cache extends BP_UnitTestCase {
   8      /**
   9       * @group bp_blogs_update_meta_cache
  10       */
  11  	public function test_bp_blogs_update_meta_cache() {
  12          if ( ! is_multisite() ) {
  13              $this->markTestSkipped();
  14          }
  15  
  16          if ( function_exists( 'wp_initialize_site' ) ) {
  17              $this->setExpectedDeprecated( 'wpmu_new_blog' );
  18          }
  19  
  20          $b1 = self::factory()->blog->create();
  21          $b2 = self::factory()->blog->create();
  22  
  23          bp_blogs_add_blogmeta( $b1, 'foo', 'bar' );
  24          bp_blogs_add_blogmeta( $b1, 'foo2', 'bar2' );
  25          bp_blogs_add_blogmeta( $b2, 'foo', 'bar' );
  26  
  27          // Need this info
  28          $b1_name = bp_blogs_get_blogmeta( $b1, 'name' );
  29          $b1_description = bp_blogs_get_blogmeta( $b1, 'description' );
  30          $b1_last_activity = bp_blogs_get_blogmeta( $b1, 'last_activity' );
  31  
  32          $b2_name = bp_blogs_get_blogmeta( $b2, 'name' );
  33          $b2_description = bp_blogs_get_blogmeta( $b2, 'description' );
  34          $b2_last_activity = bp_blogs_get_blogmeta( $b2, 'last_activity' );
  35  
  36          // Clear caches (due to _get_)
  37          wp_cache_delete( $b1, 'bp_blog_meta' );
  38          wp_cache_delete( $b2, 'bp_blog_meta' );
  39  
  40          // Caches should be empty
  41          $this->assertFalse( wp_cache_get( $b1, 'bp_blog_meta' ) );
  42          $this->assertFalse( wp_cache_get( $b2, 'bp_blog_meta' ) );
  43  
  44          bp_blogs_update_meta_cache( array( $b1, $b2 ) );
  45  
  46          $b1_expected = array(
  47              'name' => array(
  48                  $b1_name,
  49              ),
  50              'description' => array(
  51                  $b1_description,
  52              ),
  53              'last_activity' => array(
  54                  $b1_last_activity,
  55              ),
  56              'foo' => array(
  57                  'bar',
  58              ),
  59              'foo2' => array(
  60                  'bar2',
  61              ),
  62          );
  63  
  64          $b2_expected = array(
  65              'name' => array(
  66                  $b2_name,
  67              ),
  68              'description' => array(
  69                  $b2_description,
  70              ),
  71              'last_activity' => array(
  72                  $b2_last_activity,
  73              ),
  74              'foo' => array(
  75                  'bar',
  76              ),
  77          );
  78  
  79          // The cache may contain more than just this, so loop through
  80          // and check only relevant keys
  81          $b1_found = wp_cache_get( $b1, 'bp_blog_meta' );
  82          foreach ( $b1_expected as $k => $v ) {
  83              $this->assertSame( $v, $b1_found[ $k ] );
  84          }
  85  
  86          $b2_found = wp_cache_get( $b2, 'bp_blog_meta' );
  87          foreach ( $b2_expected as $k => $v ) {
  88              $this->assertSame( $v, $b2_found[ $k ] );
  89          }
  90      }
  91  
  92      /**
  93       * @group bp_blogs_update_meta_cache
  94       * @group bp_has_blogs
  95       */
  96  	public function test_bp_blogs_update_meta_cache_bp_has_blogs() {
  97          if ( ! is_multisite() ) {
  98              $this->markTestSkipped();
  99          }
 100  
 101          if ( function_exists( 'wp_initialize_site' ) ) {
 102              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 103          }
 104  
 105          $u = self::factory()->user->create();
 106  
 107          // Switch user so we have access to non-public blogs
 108          $old_user = get_current_user_id();
 109          $this->set_current_user( $u );
 110  
 111          $b1 = self::factory()->blog->create();
 112          $b2 = self::factory()->blog->create();
 113  
 114          bp_blogs_record_blog( $b1, $u, true );
 115          bp_blogs_record_blog( $b2, $u, true );
 116  
 117          bp_blogs_add_blogmeta( $b1, 'foo', 'bar' );
 118          bp_blogs_add_blogmeta( $b1, 'foo2', 'bar2' );
 119          bp_blogs_add_blogmeta( $b2, 'foo', 'bar' );
 120  
 121          // Need this info
 122          $b1_name = bp_blogs_get_blogmeta( $b1, 'name' );
 123          $b1_description = bp_blogs_get_blogmeta( $b1, 'description' );
 124          $b1_last_activity = bp_blogs_get_blogmeta( $b1, 'last_activity' );
 125  
 126          $b2_name = bp_blogs_get_blogmeta( $b2, 'name' );
 127          $b2_description = bp_blogs_get_blogmeta( $b2, 'description' );
 128          $b2_last_activity = bp_blogs_get_blogmeta( $b2, 'last_activity' );
 129  
 130          // Clear caches (due to _get_)
 131          wp_cache_delete( $b1, 'bp_blog_meta' );
 132          wp_cache_delete( $b2, 'bp_blog_meta' );
 133  
 134          // Caches should be empty
 135          $this->assertFalse( wp_cache_get( $b1, 'bp_blog_meta' ) );
 136          $this->assertFalse( wp_cache_get( $b2, 'bp_blog_meta' ) );
 137  
 138          bp_has_blogs( array(
 139              'user_id' => $u,
 140          ) );
 141  
 142          $b1_expected = array(
 143              'name' => array(
 144                  $b1_name,
 145              ),
 146              'description' => array(
 147                  $b1_description,
 148              ),
 149              'last_activity' => array(
 150                  $b1_last_activity,
 151              ),
 152              'foo' => array(
 153                  'bar',
 154              ),
 155              'foo2' => array(
 156                  'bar2',
 157              ),
 158          );
 159  
 160          $b2_expected = array(
 161              'name' => array(
 162                  $b2_name,
 163              ),
 164              'description' => array(
 165                  $b2_description,
 166              ),
 167              'last_activity' => array(
 168                  $b2_last_activity,
 169              ),
 170              'foo' => array(
 171                  'bar',
 172              ),
 173          );
 174  
 175          // The cache may contain more than just this, so loop through
 176          // and check only relevant keys
 177          $b1_found = wp_cache_get( $b1, 'bp_blog_meta' );
 178          foreach ( $b1_expected as $k => $v ) {
 179              $this->assertSame( $v, $b1_found[ $k ] );
 180          }
 181  
 182          $b2_found = wp_cache_get( $b2, 'bp_blog_meta' );
 183          foreach ( $b2_expected as $k => $v ) {
 184              $this->assertSame( $v, $b2_found[ $k ] );
 185          }
 186  
 187          $this->set_current_user( $old_user );
 188      }
 189  
 190      /**
 191       * @group bp_blogs_update_meta_cache
 192       * @group bp_has_blogs
 193       */
 194      public function test_bp_blogs_update_meta_cache_bp_has_blogs_false() {
 195          if ( ! is_multisite() ) {
 196              $this->markTestSkipped();
 197          }
 198  
 199          if ( function_exists( 'wp_initialize_site' ) ) {
 200              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 201          }
 202  
 203          $u = self::factory()->user->create();
 204  
 205          // Switch user so we have access to non-public blogs
 206          $old_user = get_current_user_id();
 207          $this->set_current_user( $u );
 208  
 209          $b1 = self::factory()->blog->create();
 210          $b2 = self::factory()->blog->create();
 211  
 212          bp_blogs_record_blog( $b1, $u, true );
 213          bp_blogs_record_blog( $b2, $u, true );
 214  
 215          bp_blogs_add_blogmeta( $b1, 'foo', 'bar' );
 216          bp_blogs_add_blogmeta( $b1, 'foo2', 'bar2' );
 217          bp_blogs_add_blogmeta( $b2, 'foo', 'bar' );
 218  
 219          // Need this info
 220          $b1_name = bp_blogs_get_blogmeta( $b1, 'name' );
 221          $b1_description = bp_blogs_get_blogmeta( $b1, 'description' );
 222          $b1_last_activity = bp_blogs_get_blogmeta( $b1, 'last_activity' );
 223  
 224          $b2_name = bp_blogs_get_blogmeta( $b2, 'name' );
 225          $b2_description = bp_blogs_get_blogmeta( $b2, 'description' );
 226          $b2_last_activity = bp_blogs_get_blogmeta( $b2, 'last_activity' );
 227  
 228          // Clear caches (due to _get_)
 229          wp_cache_delete( $b1, 'bp_blog_meta' );
 230          wp_cache_delete( $b2, 'bp_blog_meta' );
 231  
 232          // Caches should be empty
 233          $this->assertFalse( wp_cache_get( $b1, 'bp_blog_meta' ) );
 234          $this->assertFalse( wp_cache_get( $b2, 'bp_blog_meta' ) );
 235  
 236          bp_has_blogs( array(
 237              'update_meta_cache' => false,
 238          ) );
 239  
 240          // Caches should be empty
 241          $this->assertFalse( wp_cache_get( $b1, 'bp_blog_meta' ) );
 242          $this->assertFalse( wp_cache_get( $b2, 'bp_blog_meta' ) );
 243  
 244          $this->set_current_user( $old_user );
 245      }
 246  
 247      /**
 248       * @group bp_blogs_total_blogs
 249       * @group counts
 250       */
 251      public function test_bp_blogs_total_count_should_respect_cached_value_of_0() {
 252          if ( ! is_multisite() ) {
 253              $this->markTestSkipped();
 254          }
 255  
 256          global $wpdb;
 257  
 258          // prime cache
 259          // no blogs are created by default, so count is zero
 260          bp_blogs_total_blogs();
 261          $first_query_count = $wpdb->num_queries;
 262  
 263          // run function again
 264          bp_blogs_total_blogs();
 265  
 266          // check if function references cache or hits the DB by comparing query count
 267          $this->assertEquals( $first_query_count, $wpdb->num_queries );
 268      }
 269  
 270      /**
 271       * @group bp_blogs_total_blogs
 272       */
 273      public function test_bp_blogs_total_blogs_count_after_delete_blog() {
 274          if ( ! is_multisite() ) {
 275              $this->markTestSkipped();
 276          }
 277  
 278          if ( function_exists( 'wp_initialize_site' ) ) {
 279              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 280              $this->setExpectedDeprecated( 'delete_blog' );
 281          }
 282  
 283          $u = self::factory()->user->create();
 284  
 285          // need to make sure we set the 'public' flag due to how BP_Blogs_Blogs:get_all() works
 286          $b1 = self::factory()->blog->create( array(
 287              'meta' => array(
 288                  'public' => 1
 289              )
 290          ) );
 291          $b2 = self::factory()->blog->create( array(
 292              'meta' => array(
 293                  'public' => 1
 294              )
 295          ) );
 296  
 297          bp_blogs_record_blog( $b1, $u );
 298          bp_blogs_record_blog( $b2, $u );
 299  
 300          // prime total blog count
 301          bp_blogs_total_blogs();
 302  
 303          // delete a blog
 304          wpmu_delete_blog( $b2 );
 305  
 306          $this->assertEquals( 1, bp_blogs_total_blogs() );
 307      }
 308  
 309      /**
 310       * @group update_blog_details
 311       */
 312      public function test_update_blog_details_should_purge_blogmeta_cache() {
 313          if ( ! is_multisite() ) {
 314              $this->markTestSkipped();
 315          }
 316  
 317          if ( function_exists( 'wp_initialize_site' ) ) {
 318              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 319          }
 320  
 321          $u = self::factory()->user->create();
 322  
 323          $b1 = self::factory()->blog->create();
 324          bp_blogs_record_blog( $b1, $u, true );
 325  
 326          // prime cache
 327          bp_blogs_get_blogmeta( $b1, 'url' );
 328          $this->assertNotEmpty( wp_cache_get( $b1, 'bp_blog_meta' ) );
 329  
 330          // updating blog details should purge cache
 331          update_blog_details( $b1, array(
 332              'domain' => 'awesome.com'
 333          ) );
 334  
 335          // assert cache is purged
 336          $this->assertEmpty( wp_cache_get( $b1, 'bp_blog_meta' ) );
 337      }
 338  }


Generated: Thu Dec 12 01:01:36 2019 Cross-referenced by PHPXref 0.7.1