[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group groups
   5   * @group cache
   6   */
   7  class BP_Tests_Group_Cache extends BP_UnitTestCase {
   8      /**
   9       * @group bp_groups_update_meta_cache
  10       */
  11  	public function test_bp_groups_update_meta_cache() {
  12          $g1 = self::factory()->group->create();
  13          $g2 = self::factory()->group->create();
  14  
  15          $time = bp_core_current_time();
  16  
  17          // Set up some data
  18          groups_update_groupmeta( $g1, 'total_member_count', 4 );
  19          groups_update_groupmeta( $g1, 'last_activity', $time );
  20          groups_update_groupmeta( $g1, 'foo', 'bar' );
  21  
  22          groups_update_groupmeta( $g2, 'total_member_count', 81 );
  23          groups_update_groupmeta( $g2, 'last_activity', $time );
  24          groups_update_groupmeta( $g2, 'foo', 'baz' );
  25  
  26          // Prime the cache for $g1
  27          groups_update_groupmeta( $g1, 'foo', 'bar' );
  28          groups_get_groupmeta( $g1, 'foo' );
  29  
  30          // Ensure an empty cache for $g2
  31          wp_cache_delete( $g2, 'group_meta' );
  32  
  33          bp_groups_update_meta_cache( array( $g1, $g2 ) );
  34  
  35          $expected = array(
  36              $g1 => array(
  37                  'total_member_count' => array(
  38                      4,
  39                  ),
  40                  'last_activity' => array(
  41                      $time,
  42                  ),
  43                  'foo' => array(
  44                      'bar',
  45                  ),
  46              ),
  47              $g2 => array(
  48                  'total_member_count' => array(
  49                      81,
  50                  ),
  51                  'last_activity' => array(
  52                      $time,
  53                  ),
  54                  'foo' => array(
  55                      'baz',
  56                  ),
  57              ),
  58          );
  59  
  60          $found = array(
  61              $g1 => wp_cache_get( $g1, 'group_meta' ),
  62              $g2 => wp_cache_get( $g2, 'group_meta' ),
  63          );
  64  
  65          $this->assertEquals( $expected, $found );
  66      }
  67  
  68      /**
  69       * @group groups_update_groupmeta
  70       * @group groups_delete_group_cache_on_metadata_change
  71       */
  72      public function test_bp_groups_delete_group_cache_on_metadata_add() {
  73          $g = self::factory()->group->create();
  74  
  75          // Prime cache
  76          groups_get_group( $g );
  77  
  78          $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
  79  
  80          // Trigger flush
  81          groups_update_groupmeta( $g, 'foo', 'bar' );
  82  
  83          $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
  84      }
  85  
  86      /**
  87       * @group groups_update_groupmeta
  88       * @group groups_delete_group_cache_on_metadata_change
  89       */
  90      public function test_bp_groups_delete_group_cache_on_metadata_change() {
  91          $g = self::factory()->group->create();
  92  
  93          // Prime cache
  94          groups_update_groupmeta( $g, 'foo', 'bar' );
  95          groups_get_group( $g );
  96  
  97          $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
  98  
  99          // Trigger flush
 100          groups_update_groupmeta( $g, 'foo', 'baz' );
 101          $this->assertFalse( wp_cache_get( $g, 'bp_groups' ) );
 102      }
 103  
 104      /**
 105       * @group bp_groups_prefetch_activity_object_data
 106       */
 107      public function test_bp_groups_prefetch_activity_object_data_all_cached() {
 108          $g = self::factory()->group->create();
 109  
 110          // Prime cache
 111          groups_get_group( $g );
 112  
 113          // fake an activity
 114          $a = new stdClass;
 115          $a->component = buddypress()->groups->id;
 116          $a->item_id = $g;
 117          $activities = array(
 118              $a,
 119          );
 120  
 121          bp_groups_prefetch_activity_object_data( $activities );
 122  
 123          // This assertion is not really necessary - just checks to see
 124          // whether a fatal error has occurred above
 125          $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
 126      }
 127  
 128      /**
 129       * @group bp_groups_prefetch_activity_object_data
 130       */
 131      public function test_bp_groups_prefetch_activity_object_data_some_cached() {
 132          $g1 = self::factory()->group->create();
 133          $g2 = self::factory()->group->create();
 134  
 135          // Prime cache
 136          groups_get_group( $g1 );
 137  
 138          // fake activities
 139          $a1 = new stdClass;
 140          $a1->component = buddypress()->groups->id;
 141          $a1->item_id = $g1;
 142  
 143          $a2 = new stdClass;
 144          $a2->component = buddypress()->groups->id;
 145          $a2->item_id = $g2;
 146  
 147          $activities = array(
 148              $a1,
 149              $a2,
 150          );
 151  
 152          bp_groups_prefetch_activity_object_data( $activities );
 153  
 154          $this->assertNotEmpty( wp_cache_get( $g1, 'bp_groups' ) );
 155          $this->assertNotEmpty( wp_cache_get( $g2, 'bp_groups' ) );
 156      }
 157  
 158      /**
 159       * @group groups_get_group_admins
 160       */
 161  	public function test_groups_get_group_admins_cache() {
 162          $u1 = self::factory()->user->create();
 163          $u2 = self::factory()->user->create();
 164          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 165  
 166          // User 2 joins the group
 167          groups_join_group( $g, $u2 );
 168  
 169          // prime cache
 170          groups_get_group_admins( $g );
 171  
 172          // promote user 2 to an admin
 173          bp_update_is_item_admin( true );
 174          groups_promote_member( $u2, $g, 'admin' );
 175  
 176          // assert that cache is invalidated
 177          $this->assertEmpty( wp_cache_get( $g, 'bp_group_admins' ) );
 178  
 179          // assert new cached value
 180          $this->assertEquals( 2, count( groups_get_group_admins( $g ) ) );
 181      }
 182  
 183      /**
 184       * @group groups_get_group_mods
 185       */
 186  	public function test_groups_get_group_mods_cache() {
 187          $u1 = self::factory()->user->create();
 188          $u2 = self::factory()->user->create();
 189          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 190  
 191          // User 2 joins the group
 192          groups_join_group( $g, $u2 );
 193  
 194          // prime cache
 195          groups_get_group_mods( $g );
 196  
 197          // promote user 2 to an admin
 198          bp_update_is_item_admin( true );
 199          groups_promote_member( $u2, $g, 'mod' );
 200  
 201          // assert new cached value
 202          $this->assertEquals( 1, count( groups_get_group_mods( $g ) ) );
 203      }
 204  
 205      /**
 206       * @group groups_get_group_mods
 207       */
 208      public function test_groups_get_group_mods_cache_on_member_save() {
 209          $u1 = self::factory()->user->create();
 210          $u2 = self::factory()->user->create();
 211          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 212  
 213          // prime cache
 214          groups_get_group_mods( $g );
 215  
 216          // promote user 2 to an admin via BP_Groups_Member::save()
 217          self::add_user_to_group( $u2, $g, array( 'is_mod' => 1 ) );
 218  
 219          // assert new cached value
 220          $this->assertEquals( 1, count( groups_get_group_mods( $g ) ) );
 221      }
 222  
 223      /**
 224       * @group groups_get_group_admins
 225       */
 226      public function test_groups_get_group_admins_cache_on_member_save() {
 227          $u1 = self::factory()->user->create();
 228          $u2 = self::factory()->user->create();
 229          $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
 230  
 231          // prime cache
 232          groups_get_group_admins( $g );
 233  
 234          // promote user 2 to an admin via BP_Groups_Member::save()
 235          self::add_user_to_group( $u2, $g, array( 'is_admin' => 1 ) );
 236  
 237          // assert that cache is invalidated
 238          $this->assertEmpty( wp_cache_get( $g, 'bp_group_admins' ) );
 239  
 240          // assert new cached value
 241          $this->assertEquals( 2, count( groups_get_group_admins( $g ) ) );
 242      }
 243  
 244      /**
 245       * @group groups_get_total_group_count
 246       * @group counts
 247       */
 248      public function test_groups_get_total_group_count_should_respect_cached_value_of_0() {
 249          global $wpdb;
 250  
 251          // prime cache
 252          // no groups are created by default, so count is zero
 253          groups_get_total_group_count();
 254          $first_query_count = $wpdb->num_queries;
 255  
 256          // run function again
 257          groups_get_total_group_count();
 258  
 259          // check if function references cache or hits the DB by comparing query count
 260          $this->assertEquals( $first_query_count, $wpdb->num_queries );
 261      }
 262  
 263      /**
 264       * @group groups_get_total_group_count
 265       * @group counts
 266       */
 267  	public function test_total_groups_count() {
 268          $u1 = self::factory()->user->create();
 269          $u2 = self::factory()->user->create();
 270          $u3 = self::factory()->user->create();
 271          self::factory()->group->create( array( 'creator_id' => $u1 ) );
 272          self::factory()->group->create( array( 'creator_id' => $u2 ) );
 273  
 274          $this->assertEquals( 2, groups_get_total_group_count() );
 275          $this->assertEquals( 2, BP_Groups_Group::get_total_group_count() );
 276  
 277          self::factory()->group->create( array( 'creator_id' => $u3 ) );
 278  
 279          $this->assertEquals( 3, groups_get_total_group_count( true ) );
 280          $this->assertEquals( 3, BP_Groups_Group::get_total_group_count() );
 281      }
 282  }


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