[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group activity
   5   * @group cache
   6   */
   7  class BP_Tests_Activity_Cache extends BP_UnitTestCase {
   8      /**
   9       * @group bp_activity_update_meta_cache
  10       */
  11  	public function test_bp_activity_update_meta_cache() {
  12          $a1 = self::factory()->activity->create();
  13          $a2 = self::factory()->activity->create();
  14  
  15          // Set up some data
  16          bp_activity_update_meta( $a1, 'foo', 'bar' );
  17          bp_activity_update_meta( $a1, 'Boone', 'Rules' );
  18  
  19          bp_activity_update_meta( $a2, 'foo', 'baz' );
  20          bp_activity_update_meta( $a2, 'BuddyPress', 'Is Cool' );
  21  
  22          // Prime the cache for $a1
  23          bp_activity_get_meta( $a1, 'foo' );
  24  
  25          // Ensure an empty cache for $a2
  26          wp_cache_delete( $a2, 'activity_meta' );
  27  
  28          bp_activity_update_meta_cache( array( $a1, $a2 ) );
  29  
  30          $expected = array(
  31              $a1 => array(
  32                  'foo' => array(
  33                      'bar',
  34                  ),
  35                  'Boone' => array(
  36                      'Rules',
  37                  ),
  38              ),
  39              $a2 => array(
  40                  'foo' => array(
  41                      'baz',
  42                  ),
  43                  'BuddyPress' => array(
  44                      'Is Cool',
  45                  ),
  46              ),
  47          );
  48  
  49          $found = array(
  50              $a1 => wp_cache_get( $a1, 'activity_meta' ),
  51              $a2 => wp_cache_get( $a2, 'activity_meta' ),
  52          );
  53  
  54          $this->assertEquals( $expected, $found );
  55      }
  56  
  57      /**
  58       * @group bp_activity_clear_cache_for_activity
  59       */
  60  	public function test_bp_activity_clear_cache_for_activity() {
  61          $u = self::factory()->user->create();
  62  
  63          $a = self::factory()->activity->create( array(
  64              'component'     => buddypress()->activity->id,
  65              'type'          => 'activity_update',
  66              'user_id'       => $u,
  67              'content'       => 'foo bar',
  68          ) );
  69  
  70          $a_fp = bp_activity_get( array(
  71              'type' => 'activity_update',
  72              'user' => array( 'filter' => array( 'user_id' => $u ) ),
  73          ) );
  74  
  75          $activity_updated = new BP_Activity_Activity( $a );
  76          $activity_updated->content = 'bar foo';
  77          $activity_updated->save();
  78  
  79          $a_fp = bp_activity_get( array(
  80              'type' => 'activity_update',
  81              'user' => array( 'filter' => array( 'user_id' => $u ) ),
  82          ) );
  83  
  84          $this->assertSame( 'bar foo', $a_fp['activities'][0]->content );
  85      }
  86  
  87      /**
  88       * @ticket BP7237
  89       * @ticket BP6643
  90       */
  91  	public function test_query_should_be_cached() {
  92          global $wpdb;
  93  
  94          $u = self::factory()->user->create();
  95          $a = self::factory()->activity->create( array(
  96              'component'     => buddypress()->activity->id,
  97              'type'          => 'activity_update',
  98              'user_id'       => $u,
  99              'content'       => 'foo bar',
 100          ) );
 101  
 102          $activity_args = array(
 103              'per_page' => 10,
 104              'fields' => 'ids',
 105              'count_total' => true,
 106          );
 107  
 108          $a1 = bp_activity_get( $activity_args );
 109  
 110          $num_queries = $wpdb->num_queries;
 111  
 112          $a2 = bp_activity_get( $activity_args );
 113  
 114          $this->assertEqualSets( $a1, $a2 );
 115          $this->assertSame( $num_queries, $wpdb->num_queries );
 116      }
 117  
 118      /**
 119       * @ticket BP7237
 120       * @ticket BP6643
 121       */
 122      public function test_query_cache_should_be_skipped_for_different_query_params() {
 123          global $wpdb;
 124  
 125          $u = self::factory()->user->create();
 126          $a = self::factory()->activity->create( array(
 127              'component'     => buddypress()->activity->id,
 128              'type'          => 'activity_update',
 129              'user_id'       => $u,
 130              'content'       => 'foo bar',
 131          ) );
 132  
 133          $activity_args = array(
 134              'per_page' => 10,
 135              'fields' => 'ids',
 136              'count_total' => true,
 137              'filter' => array(
 138                  'component' => buddypress()->activity->id,
 139              ),
 140          );
 141  
 142          $a1 = bp_activity_get( $activity_args );
 143  
 144          $num_queries = $wpdb->num_queries;
 145  
 146          // This is enough to make the ID and COUNT clause miss the cache.
 147          $activity_args['filter']['action'] = 'activity_update';
 148          $a2 = bp_activity_get( $activity_args );
 149  
 150          $this->assertEqualSets( $a1, $a2 );
 151  
 152          // Two extra queries: one for the IDs, one for the count.
 153          $n = $num_queries + 2;
 154          $this->assertSame( $num_queries + 2, $wpdb->num_queries );
 155      }
 156  
 157      /**
 158       * @ticket BP7237
 159       * @ticket BP6643
 160       */
 161      public function test_query_cache_should_be_invalidated_by_activity_add() {
 162          global $wpdb;
 163  
 164          $u = self::factory()->user->create();
 165          $a1 = self::factory()->activity->create( array(
 166              'component'     => buddypress()->activity->id,
 167              'type'          => 'activity_update',
 168              'user_id'       => $u,
 169              'content'       => 'foo bar',
 170          ) );
 171  
 172          $activity_args = array(
 173              'per_page' => 10,
 174              'fields' => 'ids',
 175              'count_total' => true,
 176          );
 177  
 178          $q1 = bp_activity_get( $activity_args );
 179  
 180          // Bust the cache.
 181          $a2 = self::factory()->activity->create( array(
 182              'component'     => buddypress()->activity->id,
 183              'type'          => 'activity_update',
 184              'user_id'       => $u,
 185              'content'       => 'foo bar',
 186          ) );
 187  
 188          $num_queries = $wpdb->num_queries;
 189  
 190          $q2 = bp_activity_get( $activity_args );
 191  
 192          $expected = array( $a1, $a2 );
 193  
 194          $this->assertEqualSets( $expected, $q2['activities'] );
 195          $this->assertEquals( 2, $q2['total'] );
 196          $this->assertSame( $num_queries + 2, $wpdb->num_queries );
 197      }
 198  
 199      /**
 200       * @ticket BP7237
 201       * @ticket BP6643
 202       */
 203      public function test_query_cache_should_be_invalidated_by_activity_edit() {
 204          global $wpdb;
 205  
 206          $u = self::factory()->user->create();
 207          $a = self::factory()->activity->create( array(
 208              'component'     => buddypress()->activity->id,
 209              'type'          => 'activity_update',
 210              'user_id'       => $u,
 211              'content'       => 'foo bar',
 212          ) );
 213  
 214          $activity_args = array(
 215              'per_page' => 10,
 216              'fields' => 'ids',
 217              'count_total' => true,
 218          );
 219  
 220          $q1 = bp_activity_get( $activity_args );
 221  
 222          // Bust the cache.
 223          self::factory()->activity->create( array(
 224              'id'            => $a,
 225              'component'     => buddypress()->activity->id,
 226              'type'          => 'activity_update',
 227              'user_id'       => $u,
 228              'content'       => 'foo bar baz',
 229          ) );
 230  
 231          $num_queries = $wpdb->num_queries;
 232  
 233          $q2 = bp_activity_get( $activity_args );
 234  
 235          $this->assertEqualSets( $q1, $q2 );
 236          $this->assertSame( $num_queries + 2, $wpdb->num_queries );
 237      }
 238  
 239      /**
 240       * @ticket BP7237
 241       * @ticket BP6643
 242       */
 243      public function test_query_cache_should_be_invalidated_by_activity_delete() {
 244          global $wpdb;
 245  
 246          $u = self::factory()->user->create();
 247          $a = self::factory()->activity->create( array(
 248              'component'     => buddypress()->activity->id,
 249              'type'          => 'activity_update',
 250              'user_id'       => $u,
 251              'content'       => 'foo bar',
 252          ) );
 253  
 254          $activity_args = array(
 255              'per_page' => 10,
 256              'fields' => 'ids',
 257              'count_total' => true,
 258          );
 259  
 260          $q1 = bp_activity_get( $activity_args );
 261  
 262          // Bust the cache.
 263          bp_activity_delete( array(
 264              'id' => $a,
 265          ) );
 266  
 267          $num_queries = $wpdb->num_queries;
 268  
 269          $q2 = bp_activity_get( $activity_args );
 270  
 271          $this->assertEqualSets( array(), $q2['activities'] );
 272          $this->assertEquals( 0, $q2['total'] );
 273          $this->assertSame( $num_queries + 2, $wpdb->num_queries );
 274      }
 275  
 276      /**
 277       * @ticket BP7237
 278       * @ticket BP6643
 279       */
 280      public function test_query_cache_should_be_invalidated_by_activitymeta_add() {
 281          global $wpdb;
 282  
 283          $activities = self::factory()->activity->create_many( 2 );
 284          bp_activity_add_meta( $activities[0], 'foo', 'bar' );
 285  
 286          $activity_args = array(
 287              'meta_query' => array(
 288                  array(
 289                      'key' => 'foo',
 290                      'value' => 'bar',
 291                  ),
 292              ),
 293          );
 294  
 295          $q1 = bp_activity_get( $activity_args );
 296          $this->assertEqualSets( array( $activities[0] ), wp_list_pluck( $q1['activities'], 'id' ) );
 297  
 298          bp_activity_add_meta( $activities[1], 'foo', 'bar' );
 299  
 300          $q2 = bp_activity_get( $activity_args );
 301          $this->assertEqualSets( array( $activities[0], $activities[1] ), wp_list_pluck( $q2['activities'], 'id' ) );
 302      }
 303  
 304      /**
 305       * @ticket BP7237
 306       * @ticket BP6643
 307       */
 308      public function test_query_cache_should_be_invalidated_by_activitymeta_update() {
 309          global $wpdb;
 310  
 311          $activities = self::factory()->activity->create_many( 2 );
 312          bp_activity_add_meta( $activities[0], 'foo', 'bar' );
 313          bp_activity_add_meta( $activities[1], 'foo', 'baz' );
 314  
 315          $activity_args = array(
 316              'meta_query' => array(
 317                  array(
 318                      'key' => 'foo',
 319                      'value' => 'bar',
 320                  ),
 321              ),
 322          );
 323  
 324          $q1 = bp_activity_get( $activity_args );
 325          $this->assertEqualSets( array( $activities[0] ), wp_list_pluck( $q1['activities'], 'id' ) );
 326  
 327          bp_activity_update_meta( $activities[1], 'foo', 'bar' );
 328  
 329          $q2 = bp_activity_get( $activity_args );
 330          $this->assertEqualSets( array( $activities[0], $activities[1] ), wp_list_pluck( $q2['activities'], 'id' ) );
 331      }
 332  
 333      /**
 334       * @ticket BP7237
 335       * @ticket BP6643
 336       */
 337      public function test_query_cache_should_be_invalidated_by_activitymeta_delete() {
 338          global $wpdb;
 339  
 340          $activities = self::factory()->activity->create_many( 2 );
 341          bp_activity_add_meta( $activities[0], 'foo', 'bar' );
 342          bp_activity_add_meta( $activities[1], 'foo', 'bar' );
 343  
 344          $activity_args = array(
 345              'meta_query' => array(
 346                  array(
 347                      'key' => 'foo',
 348                      'value' => 'bar',
 349                  ),
 350              ),
 351          );
 352  
 353          $q1 = bp_activity_get( $activity_args );
 354          $this->assertEqualSets( array( $activities[0], $activities[1] ), wp_list_pluck( $q1['activities'], 'id' ) );
 355  
 356          bp_activity_delete_meta( $activities[1], 'foo', 'bar' );
 357  
 358          $q2 = bp_activity_get( $activity_args );
 359          $this->assertEqualSets( array( $activities[0] ), wp_list_pluck( $q2['activities'], 'id' ) );
 360      }
 361  
 362      /**
 363       * @ticket BP8296
 364       */
 365      public function test_activity_comments_cache_should_be_cleared_when_parent_activity_marked_as_spam() {
 366          $u1 = self::factory()->user->create();
 367          $u2 = self::factory()->user->create();
 368  
 369          $activity = self::factory()->activity->create(
 370              array(
 371                  'component' => buddypress()->activity->id,
 372                  'type'      => 'activity_update',
 373                  'user_id'   => $u1,
 374                  'content'   => 'bar',
 375              )
 376          );
 377  
 378          $comment = bp_activity_new_comment(
 379              array(
 380                  'activity_id'       => $activity,
 381                  'skip_notification' => true,
 382                  'user_id'           => $u2,
 383                  'content'           => 'foo',
 384              )
 385          );
 386  
 387          $activities = bp_activity_get(
 388              array(
 389                  'display_comments' => true,
 390              )
 391          );
 392  
 393          $cached_ids = wp_list_pluck( wp_cache_get( $activity, 'bp_activity_comments' ), 'content', 'id' );
 394          $this->assertTrue( 'foo' === $cached_ids[ $comment ] );
 395  
 396          $activity_obj = new BP_Activity_Activity( $activity );
 397  
 398          // An activity is always saved after being marked as spam.
 399          bp_activity_mark_as_spam( $activity_obj );
 400          $activity_obj->save();
 401  
 402          $this->assertFalse( wp_cache_get( $activity, 'bp_activity_comments' ), 'The activity comments cache should be cleared when an activity has been marked as spam' );
 403      }
 404  
 405      /**
 406       * @ticket BP8296
 407       */
 408      public function test_activity_comments_cache_should_be_cleared_when_activity_comment_marked_as_spam() {
 409          $u1 = self::factory()->user->create();
 410          $u2 = self::factory()->user->create();
 411  
 412          $activity = self::factory()->activity->create(
 413              array(
 414                  'component' => buddypress()->activity->id,
 415                  'type'      => 'activity_update',
 416                  'user_id'   => $u1,
 417                  'content'   => 'foo',
 418              )
 419          );
 420  
 421          $comment = bp_activity_new_comment(
 422              array(
 423                  'activity_id'       => $activity,
 424                  'skip_notification' => true,
 425                  'user_id'           => $u2,
 426                  'content'           => 'bar',
 427              )
 428          );
 429  
 430          $activities = bp_activity_get(
 431              array(
 432                  'display_comments' => true,
 433              )
 434          );
 435  
 436          $cached_ids = wp_list_pluck( wp_cache_get( $activity, 'bp_activity_comments' ), 'content', 'id' );
 437          $this->assertTrue( 'bar' === $cached_ids[ $comment ] );
 438  
 439          $comment_obj = new BP_Activity_Activity( $comment );
 440  
 441          // An activity is always saved after being marked as spam.
 442          bp_activity_mark_as_spam( $comment_obj );
 443          $comment_obj->save();
 444  
 445          $this->assertFalse( wp_cache_get( $activity, 'bp_activity_comments' ), 'The parent activity comments cache should be cleared when an activity comment has been marked as spam' );
 446      }
 447  
 448      /**
 449       * @ticket BP8296
 450       */
 451      public function test_activity_comments_cache_should_be_cleared_when_parent_activity_marked_as_ham() {
 452          $u1 = self::factory()->user->create();
 453          $u2 = self::factory()->user->create();
 454  
 455          $activity = self::factory()->activity->create(
 456              array(
 457                  'component' => buddypress()->activity->id,
 458                  'type'      => 'activity_update',
 459                  'user_id'   => $u1,
 460                  'content'   => 'bar',
 461              )
 462          );
 463  
 464          $comment = bp_activity_new_comment(
 465              array(
 466                  'activity_id'       => $activity,
 467                  'skip_notification' => true,
 468                  'user_id'           => $u2,
 469                  'content'           => 'foo',
 470              )
 471          );
 472  
 473          $activities = bp_activity_get(
 474              array(
 475                  'display_comments' => true,
 476              )
 477          );
 478  
 479          $activity_obj          = new BP_Activity_Activity( $activity );
 480          $activity_obj->is_spam = 1;
 481          $activity_obj->save();
 482  
 483          // An activity is always saved after being marked as spam.
 484          bp_activity_mark_as_ham( $activity_obj );
 485          $activity_obj->save();
 486  
 487          $this->assertFalse( wp_cache_get( $activity, 'bp_activity_comments' ), 'The activity comments cache should be cleared when an activity has been marked as ham' );
 488      }
 489  
 490      /**
 491       * @ticket BP8296
 492       */
 493      public function test_activity_comments_cache_should_be_cleared_when_activity_comment_marked_as_ham() {
 494          $u1 = self::factory()->user->create();
 495          $u2 = self::factory()->user->create();
 496  
 497          $activity = self::factory()->activity->create(
 498              array(
 499                  'component' => buddypress()->activity->id,
 500                  'type'      => 'activity_update',
 501                  'user_id'   => $u1,
 502                  'content'   => 'foo',
 503              )
 504          );
 505  
 506          $comment = bp_activity_new_comment(
 507              array(
 508                  'activity_id'       => $activity,
 509                  'skip_notification' => true,
 510                  'user_id'           => $u2,
 511                  'content'           => 'bar',
 512              )
 513          );
 514  
 515          $activities = bp_activity_get(
 516              array(
 517                  'display_comments' => true,
 518              )
 519          );
 520  
 521          $comment_obj          = new BP_Activity_Activity( $comment );
 522          $comment_obj->is_spam = 1;
 523          $comment_obj->save();
 524  
 525          // An activity is always saved after being marked as spam.
 526          bp_activity_mark_as_ham( $comment_obj );
 527          $comment_obj->save();
 528  
 529          $this->assertFalse( wp_cache_get( $activity, 'bp_activity_comments' ), 'The parent activity comments cache should be cleared when an activity comment has been marked as ham' );
 530      }
 531  
 532      /**
 533       * @ticket BP8296
 534       */
 535      public function test_activity_comments_cache_should_be_cleared_when_activity_comment_has_been_updated() {
 536          $u1 = self::factory()->user->create();
 537          $u2 = self::factory()->user->create();
 538  
 539          $activity = self::factory()->activity->create(
 540              array(
 541                  'component' => buddypress()->activity->id,
 542                  'type'      => 'activity_update',
 543                  'user_id'   => $u1,
 544                  'content'   => 'foo',
 545              )
 546          );
 547  
 548          $comment = bp_activity_new_comment(
 549              array(
 550                  'activity_id'       => $activity,
 551                  'skip_notification' => true,
 552                  'user_id'           => $u2,
 553                  'content'           => 'bar',
 554              )
 555          );
 556  
 557          $activities = bp_activity_get(
 558              array(
 559                  'display_comments' => true,
 560              )
 561          );
 562  
 563          $comment_obj          = new BP_Activity_Activity( $comment );
 564          $comment_obj->content = 'foo';
 565          $comment_obj->save();
 566  
 567          $this->assertFalse( wp_cache_get( $activity, 'bp_activity_comments' ), 'The parent activity comments cache should be cleared when an activity comment has been updated' );
 568      }
 569  
 570      /**
 571       * @ticket BP8296
 572       */
 573      public function test_activity_comments_cache_should_be_cleared_when_activity_comment_has_been_deleted() {
 574          $u1 = self::factory()->user->create();
 575          $u2 = self::factory()->user->create();
 576  
 577          $activity = self::factory()->activity->create(
 578              array(
 579                  'component' => buddypress()->activity->id,
 580                  'type'      => 'activity_update',
 581                  'user_id'   => $u1,
 582                  'content'   => 'foo',
 583              )
 584          );
 585  
 586          $comment = bp_activity_new_comment(
 587              array(
 588                  'activity_id'       => $activity,
 589                  'skip_notification' => true,
 590                  'user_id'           => $u2,
 591                  'content'           => 'bar',
 592              )
 593          );
 594  
 595          $activities = bp_activity_get(
 596              array(
 597                  'display_comments' => true,
 598              )
 599          );
 600  
 601          bp_activity_delete_comment( $activity, $comment );
 602  
 603          $this->assertFalse( wp_cache_get( $activity, 'bp_activity_comments' ), 'The parent activity comments cache should be cleared when an activity comment has been deleted' );
 604      }
 605  
 606      /**
 607       * @ticket BP8296
 608       */
 609      public function test_activity_comments_cache_should_be_cleared_when_activity_comment_has_been_added() {
 610          $u1 = self::factory()->user->create();
 611          $u2 = self::factory()->user->create();
 612  
 613          $activity = self::factory()->activity->create(
 614              array(
 615                  'component' => buddypress()->activity->id,
 616                  'type'      => 'activity_update',
 617                  'user_id'   => $u1,
 618                  'content'   => 'foo',
 619              )
 620          );
 621  
 622          $c1 = bp_activity_new_comment(
 623              array(
 624                  'activity_id'       => $activity,
 625                  'skip_notification' => true,
 626                  'user_id'           => $u2,
 627                  'content'           => 'bar',
 628              )
 629          );
 630  
 631          $activities = bp_activity_get(
 632              array(
 633                  'display_comments' => true,
 634              )
 635          );
 636  
 637          $c2 = bp_activity_new_comment(
 638              array(
 639                  'activity_id'       => $activity,
 640                  'skip_notification' => true,
 641                  'user_id'           => $u1,
 642                  'content'           => 'taz',
 643              )
 644          );
 645  
 646          $this->assertFalse( wp_cache_get( $activity, 'bp_activity_comments' ), 'The parent activity comments cache should be cleared when a new activity comment has been added' );
 647      }
 648  }


Generated: Tue Jun 28 01:00:52 2022 Cross-referenced by PHPXref 0.7.1