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


Generated: Mon Jul 6 01:01:29 2020 Cross-referenced by PHPXref 0.7.1