[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group cache
   5   * @group members
   6   */
   7  class BP_Tests_Members_Cache extends BP_UnitTestCase {
   8      /**
   9       * @group bp_core_get_total_member_count
  10       * @group cache
  11       */
  12      public function test_bp_core_get_total_member_count_should_respect_cached_value_of_0() {
  13          global $wpdb;
  14  
  15          // set cached value to zero
  16          wp_cache_set( 'bp_total_member_count', 0, 'bp' );
  17          $num_queries = $wpdb->num_queries;
  18  
  19          // run function
  20          bp_core_get_total_member_count();
  21  
  22          // check if function references cache or hits the DB by comparing query count
  23          $this->assertEquals( $num_queries, $wpdb->num_queries );
  24      }
  25  
  26      /**
  27       * @ticket BP7237
  28       * @ticket BP6643
  29       * @ticket BP7245
  30       */
  31      public function test_last_activity_should_bust_activity_with_last_activity_cache() {
  32          global $wpdb;
  33  
  34          $u1 = self::factory()->user->create();
  35          $u2 = self::factory()->user->create();
  36  
  37          $time_1 = date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
  38          $time_2 = date( 'Y-m-d H:i:s', time() - ( HOUR_IN_SECONDS * 2 ) );
  39          bp_update_user_last_activity( $u1, $time_1 );
  40          bp_update_user_last_activity( $u2, $time_2 );
  41  
  42          $activity_args_a = array(
  43              'filter' => array(
  44                  'object' => buddypress()->members->id,
  45                  'action' => 'last_activity',
  46              ),
  47              'max' => 1,
  48          );
  49  
  50          $activity_args_b = array(
  51              'filter' => array(
  52                  'action' => 'new_member',
  53              ),
  54              'fields' => 'ids',
  55          );
  56  
  57          // Prime bp_activity and bp_activity_with_last_activity caches.
  58          $a1 = bp_activity_get( $activity_args_a );
  59          $expected = array( $u1, $u2 );
  60          $found = array_map( 'intval', wp_list_pluck( $a1['activities'], 'user_id' ) );
  61          $this->assertSame( $expected, $found );
  62  
  63          $b1 = bp_activity_get( $activity_args_b );
  64  
  65          // Bump u2 activity so it should appear first.
  66          $new_time = date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
  67          bp_update_user_last_activity( $u2, $new_time );
  68  
  69          $a2 = bp_activity_get( $activity_args_a );
  70          $expected = array( $u2, $u1 );
  71          $found = array_map( 'intval', wp_list_pluck( $a2['activities'], 'user_id' ) );
  72          $this->assertSame( $expected, $found );
  73  
  74          $num_queries = $wpdb->num_queries;
  75  
  76          // bp_activity cache should not have been touched.
  77          $b2 = bp_activity_get( $activity_args_b );
  78          $this->assertEqualSets( $b1, $b2 );
  79          $this->assertSame( $num_queries, $wpdb->num_queries );
  80      }
  81  }
  82  


Generated: Mon Mar 30 01:01:27 2020 Cross-referenced by PHPXref 0.7.1