[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @group activity
   4   */
   5  class BP_Tests_Activity_Functions extends BP_UnitTestCase {
   6  
   7      /**
   8       * @ticket BP4488
   9       */
  10  	public function test_thumbnail_content_images() {
  11          // No images
  12          $post_content = 'foo bar';
  13          $this->assertEquals( bp_activity_thumbnail_content_images( $post_content ), 'foo bar' );
  14  
  15          // Image first, no caption. See #BP4488
  16          $post_content = '<img src="http://example.com/foo.jpg" alt="foo" width="40" height="40" class="alignnone size-full wp-image-236" /> foo bar';
  17          $this->assertEquals( bp_activity_thumbnail_content_images( $post_content ), '<img src="http://example.com/foo.jpg" width="40" height="40" alt="Thumbnail" class="align-left thumbnail" /> foo bar' );
  18  
  19          // Image first, caption. See #BP4488
  20          $post_content = '[caption id="attachment_236" align="alignnone" width="40"]<img src="http://example.com/foo.jpg" alt="FOO!" width="40" height="40" class="size-full wp-image-236" /> FOO![/caption] Awesome.';
  21          $this->assertEquals( bp_activity_thumbnail_content_images( $post_content ), '<img src="http://example.com/foo.jpg" width="40" height="40" alt="Thumbnail" class="align-left thumbnail" /> Awesome.' );
  22      }
  23  
  24      /**
  25       * @group delete
  26       */
  27  	public function test_delete_activity_by_id() {
  28  
  29          // create an activity update
  30          $activity = self::factory()->activity->create( array(
  31              'type' => 'activity_update'
  32          ) );
  33  
  34          // now delete the activity item
  35          bp_activity_delete( array(
  36              'id' => $activity
  37          ) );
  38  
  39          // now fetch the deleted activity entries
  40          $get = bp_activity_get( array(
  41              'id' => $activity
  42          ) );
  43  
  44          // activities should equal zero
  45          $this->assertEquals( 0, $get['total'] );
  46      }
  47  
  48      /**
  49       * @group delete
  50       */
  51  	public function test_delete_activity_by_type() {
  52  
  53          // Setup criteria
  54          $criteria = array(
  55              'type' => 'activity_update'
  56          );
  57  
  58          // create an activity update
  59          self::factory()->activity->create( $criteria );
  60          self::factory()->activity->create( $criteria );
  61          self::factory()->activity->create( $criteria );
  62          self::factory()->activity->create( $criteria );
  63  
  64          // now delete the activity items
  65          bp_activity_delete( $criteria );
  66  
  67          // now fetch the deleted activity entries
  68          $get = bp_activity_get( $criteria );
  69  
  70          // activities should equal zero
  71          $this->assertEquals( 0, $get['total'] );
  72      }
  73  
  74      /**
  75       * @group delete
  76       */
  77  	public function test_delete_activity_by_component() {
  78  
  79          // Setup criteria
  80          $criteria = array(
  81              'component' => 'xprofile'
  82          );
  83  
  84          // create an activity update
  85          self::factory()->activity->create( $criteria );
  86          self::factory()->activity->create( $criteria );
  87          self::factory()->activity->create( $criteria );
  88          self::factory()->activity->create( $criteria );
  89  
  90          // now delete the activity items
  91          bp_activity_delete( $criteria );
  92  
  93          // now fetch the deleted activity entries
  94          $get = bp_activity_get( $criteria );
  95  
  96          // activities should equal zero
  97          $this->assertEquals( 0, $get['total'] );
  98      }
  99  
 100      /**
 101       * @group delete
 102       */
 103  	public function test_delete_activity_by_user_id() {
 104  
 105          // Setup criteria
 106          $criteria = array(
 107              'user_id' => '1'
 108          );
 109  
 110          // create an activity update
 111          self::factory()->activity->create( $criteria );
 112          self::factory()->activity->create( $criteria );
 113          self::factory()->activity->create( $criteria );
 114          self::factory()->activity->create( $criteria );
 115  
 116          // now delete the activity items
 117          bp_activity_delete( $criteria );
 118  
 119          // now fetch the deleted activity entries
 120          $get = bp_activity_get( $criteria );
 121  
 122          // activities should equal zero
 123          $this->assertEquals( 0, $get['total'] );
 124      }
 125  
 126      /**
 127       * @group delete
 128       */
 129  	public function test_delete_activity_meta() {
 130  
 131          // create an activity update
 132          $activity = self::factory()->activity->create( array(
 133              'type' => 'activity_update'
 134          ) );
 135  
 136          // add some meta to the activity items
 137          bp_activity_update_meta( $activity, 'foo', 'bar' );
 138  
 139          // now delete the parent activity item meta entry
 140          bp_activity_delete_meta(  $activity, 'foo', 'bar' );
 141  
 142          // now fetch activity meta for the deleted activity entries
 143          $m1 = bp_activity_get_meta( $activity );
 144  
 145          // test if activity meta entries still exist
 146          $this->assertEmpty( $m1 );
 147      }
 148  
 149      /**
 150       * @group delete
 151       */
 152  	public function test_delete_activity_all_meta() {
 153  
 154          // create an activity update
 155          $activity = self::factory()->activity->create( array(
 156              'type' => 'activity_update'
 157          ) );
 158  
 159          // add some meta to the activity items
 160          bp_activity_update_meta( $activity, 'foo1', 'bar' );
 161          bp_activity_update_meta( $activity, 'foo2', 'bar' );
 162          bp_activity_update_meta( $activity, 'foo3', 'bar' );
 163          bp_activity_update_meta( $activity, 'foo4', 'bar' );
 164          bp_activity_update_meta( $activity, 'foo5', 'bar' );
 165  
 166          // now delete the parent activity item meta entry
 167          bp_activity_delete_meta( $activity );
 168  
 169          // now fetch activity meta for the deleted activity entries
 170          $m1 = bp_activity_get_meta( $activity );
 171          $m2 = bp_activity_get_meta( $activity );
 172          $m3 = bp_activity_get_meta( $activity );
 173          $m4 = bp_activity_get_meta( $activity );
 174          $m5 = bp_activity_get_meta( $activity );
 175  
 176          // test if activity meta entries still exist
 177          $this->assertEmpty( $m1 );
 178          $this->assertEmpty( $m2 );
 179          $this->assertEmpty( $m3 );
 180          $this->assertEmpty( $m4 );
 181          $this->assertEmpty( $m5 );
 182      }
 183  
 184      /**
 185       * @group delete
 186       */
 187  	public function test_delete_activity_and_comments() {
 188  
 189          // create an activity update
 190          $parent_activity = self::factory()->activity->create( array(
 191              'type' => 'activity_update',
 192          ) );
 193  
 194          // create some activity comments
 195          $comment_one = self::factory()->activity->create( array(
 196              'type'              => 'activity_comment',
 197              'item_id'           => $parent_activity,
 198              'secondary_item_id' => $parent_activity,
 199          ) );
 200  
 201          $comment_two = self::factory()->activity->create( array(
 202              'type'              => 'activity_comment',
 203              'item_id'           => $parent_activity,
 204              'secondary_item_id' => $parent_activity,
 205          ) );
 206  
 207          // now delete the parent activity item
 208          // this should hopefully delete the associated comments and meta entries
 209          bp_activity_delete( array(
 210              'id' => $parent_activity
 211          ) );
 212  
 213          // now fetch the deleted activity entries
 214          $get = bp_activity_get( array(
 215              'in'               => array( $parent_activity, $comment_one, $comment_two ),
 216              'display_comments' => 'stream'
 217          ) );
 218  
 219          // activities should equal zero
 220          $this->assertEquals( 0, $get['total'] );
 221      }
 222  
 223      /**
 224       * @group delete
 225       */
 226  	public function test_delete_activity_meta_for_comments() {
 227  
 228          // create an activity update
 229          $parent_activity = self::factory()->activity->create( array(
 230              'type' => 'activity_update',
 231          ) );
 232  
 233          // create some activity comments
 234          $comment_one = self::factory()->activity->create( array(
 235              'type'              => 'activity_comment',
 236              'item_id'           => $parent_activity,
 237              'secondary_item_id' => $parent_activity,
 238          ) );
 239  
 240          $comment_two = self::factory()->activity->create( array(
 241              'type'              => 'activity_comment',
 242              'item_id'           => $parent_activity,
 243              'secondary_item_id' => $parent_activity,
 244          ) );
 245  
 246          // add some meta to the activity items
 247          bp_activity_update_meta( $parent_activity, 'foo', 'bar' );
 248          bp_activity_update_meta( $comment_one,     'foo', 'bar' );
 249          bp_activity_update_meta( $comment_two,     'foo', 'bar' );
 250  
 251          // now delete the parent activity item
 252          // this should hopefully delete the associated comments and meta entries
 253          bp_activity_delete( array(
 254              'id' => $parent_activity
 255          ) );
 256  
 257          // now fetch activity meta for the deleted activity entries
 258          $m1 = bp_activity_get_meta( $parent_activity );
 259          $m2 = bp_activity_get_meta( $comment_one );
 260          $m3 = bp_activity_get_meta( $comment_two );
 261  
 262          // test if activity meta entries still exist
 263          $this->assertEmpty( $m1 );
 264          $this->assertEmpty( $m2 );
 265          $this->assertEmpty( $m3 );
 266      }
 267  
 268      /**
 269       * @group bp_activity_update_meta
 270       * @ticket BP5180
 271       */
 272  	public function test_bp_activity_update_meta_with_line_breaks() {
 273          $a = self::factory()->activity->create();
 274          $meta_value = 'Foo!
 275  
 276  
 277  Bar!';
 278          bp_activity_update_meta( $a, 'linebreak_test', $meta_value );
 279          $this->assertEquals( $meta_value, bp_activity_get_meta( $a, 'linebreak_test' ) );
 280      }
 281  
 282      /**
 283       * @group bp_activity_update_meta
 284       * @ticket BP5083
 285       */
 286  	public function test_bp_activity_update_meta_with_0() {
 287          $a = self::factory()->activity->create();
 288          $meta_value = 0;
 289  
 290          bp_activity_update_meta( $a, '0_test', $meta_value );
 291  
 292          $this->assertNotSame( false, bp_activity_get_meta( $a, '0_test' ) );
 293      }
 294  
 295      /**
 296       * @group activitymeta
 297       * @group bp_activity_update_meta
 298       */
 299  	public function test_bp_activity_update_meta_non_numeric_id() {
 300          $this->assertFalse( bp_activity_update_meta( 'foo', 'bar', 'baz' ) );
 301      }
 302  
 303      /**
 304       * @group activitymeta
 305       * @group bp_activity_update_meta
 306       * @ticket BP5399
 307       */
 308      public function test_bp_activity_update_meta_with_illegal_key_characters() {
 309          $a = self::factory()->activity->create();
 310          $krazy_key = ' f!@#$%^o *(){}o?+';
 311          bp_activity_update_meta( $a, $krazy_key, 'bar' );
 312  
 313          $this->assertSame( '', bp_activity_get_meta( $a, 'foo' ) );
 314      }
 315  
 316      /**
 317       * @group activitymeta
 318       * @group bp_activity_update_meta
 319       */
 320  	public function test_bp_activity_update_meta_stripslashes() {
 321          $a = self::factory()->activity->create();
 322          $value = "This string is totally slashin\'!";
 323          bp_activity_update_meta( $a, 'foo', $value );
 324  
 325          $this->assertSame( stripslashes( $value ), bp_activity_get_meta( $a, 'foo' ) );
 326      }
 327  
 328      /**
 329       * @group activitymeta
 330       * @group bp_activity_update_meta
 331       */
 332      public function test_bp_activity_update_meta_false_value_deletes() {
 333          $a = self::factory()->activity->create();
 334          bp_activity_update_meta( $a, 'foo', false );
 335          $this->assertSame( '', bp_activity_get_meta( $a, 'foo' ) );
 336      }
 337  
 338      /**
 339       * @group activitymeta
 340       * @group bp_activity_update_meta
 341       */
 342  	public function test_bp_activity_update_meta_new() {
 343          $a = self::factory()->activity->create();
 344          $this->assertSame( '', bp_activity_get_meta( $a, 'foo' ), '"foo" meta should be empty for this activity item.' );
 345          $this->assertNotEmpty( bp_activity_update_meta( $a, 'foo', 'bar' ) );
 346          $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo' ) );
 347      }
 348  
 349      /**
 350       * @group activitymeta
 351       * @group bp_activity_update_meta
 352       */
 353  	public function test_bp_activity_update_meta_existing() {
 354          $a = self::factory()->activity->create();
 355          bp_activity_update_meta( $a, 'foo', 'bar' );
 356          $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo' ) );
 357          $this->assertTrue( bp_activity_update_meta( $a, 'foo', 'baz' ) );
 358          $this->assertSame( 'baz', bp_activity_get_meta( $a, 'foo' ) );
 359      }
 360  
 361      /**
 362       * @group activitymeta
 363       * @group bp_activity_update_meta
 364       */
 365  	public function test_bp_activity_update_meta_same_value() {
 366          $a = self::factory()->activity->create();
 367          bp_activity_update_meta( $a, 'foo', 'bar' );
 368          $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo' ) );
 369          $this->assertFalse( bp_activity_update_meta( $a, 'foo', 'bar' ) );
 370          $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo' ) );
 371      }
 372  
 373      /**
 374       * @group activitymeta
 375       * @group bp_activity_update_meta
 376       */
 377  	public function test_bp_activity_update_meta_prev_value() {
 378          $a = self::factory()->activity->create();
 379          bp_activity_add_meta( $a, 'foo', 'bar' );
 380  
 381          // In earlier versions of WordPress, bp_activity_update_meta()
 382          // returns true even on failure. However, we know that in these
 383          // cases the update is failing as expected, so we skip this
 384          // assertion just to keep our tests passing
 385          // See https://core.trac.wordpress.org/ticket/24933
 386          if ( version_compare( $GLOBALS['wp_version'], '3.7', '>=' ) ) {
 387              $this->assertFalse( bp_activity_update_meta( $a, 'foo', 'bar2', 'baz' ) );
 388          }
 389  
 390          $this->assertTrue( bp_activity_update_meta( $a, 'foo', 'bar2', 'bar' ) );
 391      }
 392  
 393      /**
 394       * @group activitymeta
 395       * @group bp_activity_get_meta
 396       */
 397  	public function test_bp_activity_get_meta_empty_activity_id() {
 398          $this->assertFalse( bp_activity_get_meta( 0 ) );
 399      }
 400  
 401      /**
 402       * @group activitymeta
 403       * @group bp_activity_get_meta
 404       */
 405      public function test_bp_activity_get_meta_non_numeric_activity_id() {
 406          $this->assertFalse( bp_activity_get_meta( 'foo' ) );
 407      }
 408  
 409      /**
 410       * @group activitymeta
 411       * @group bp_activity_get_meta
 412       * @ticket BP5399
 413       */
 414      public function test_bp_activity_get_meta_with_illegal_characters() {
 415          $a = self::factory()->activity->create();
 416          bp_activity_update_meta( $a, 'foo', 'bar' );
 417  
 418          $krazy_key = ' f!@#$%^o *(){}o?+';
 419          $this->assertSame( '', bp_activity_get_meta( $a, $krazy_key ) );
 420      }
 421  
 422      /**
 423       * @group activitymeta
 424       * @group bp_activity_get_meta
 425       */
 426  	public function test_bp_activity_get_meta_multiple() {
 427          $a = self::factory()->activity->create();
 428          bp_activity_update_meta( $a, 'foo', 'bar' );
 429          bp_activity_update_meta( $a, 'foo1', 'bar1' );
 430  
 431          $expected = array(
 432              'foo' => array(
 433                  'bar',
 434              ),
 435              'foo1' => array(
 436                  'bar1',
 437              ),
 438          );
 439  
 440          $this->assertEquals( $expected, bp_activity_get_meta( $a ) );
 441      }
 442  
 443      /**
 444       * @group bp_activity_get_meta
 445       * @group activitymeta
 446       * @ticket BP5399
 447       */
 448      public function test_bp_activity_get_meta_no_results_returns_false() {
 449          $a = self::factory()->activity->create();
 450  
 451          $this->assertSame( '', bp_activity_get_meta( $a, 'foo' ) );
 452      }
 453  
 454      /**
 455       * @group activitymeta
 456       * @group bp_activity_get_meta
 457       */
 458  	public function test_bp_activity_get_meta_single_true() {
 459          $a = self::factory()->activity->create();
 460          bp_activity_add_meta( $a, 'foo', 'bar' );
 461          bp_activity_add_meta( $a, 'foo', 'baz' );
 462          $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo' ) ); // default is true
 463          $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo', true ) );
 464      }
 465  
 466      /**
 467       * @group activitymeta
 468       * @group bp_activity_get_meta
 469       */
 470  	public function test_bp_activity_get_meta_single_false() {
 471          $a = self::factory()->activity->create();
 472          bp_activity_add_meta( $a, 'foo', 'bar' );
 473          bp_activity_add_meta( $a, 'foo', 'baz' );
 474          $this->assertSame( array( 'bar', 'baz' ), bp_activity_get_meta( $a, 'foo', false ) );
 475      }
 476  
 477      /**
 478       * @group activitymeta
 479       * @group bp_activity_get_meta
 480       * @group cache
 481       */
 482  	public function test_bp_activity_get_meta_cache_all_on_get() {
 483          $a = self::factory()->activity->create();
 484          bp_activity_add_meta( $a, 'foo', 'bar' );
 485          bp_activity_add_meta( $a, 'foo1', 'baz' );
 486          $this->assertFalse( wp_cache_get( $a, 'activity_meta' ) );
 487  
 488          // A single query should prime the whole meta cache
 489          bp_activity_get_meta( $a, 'foo' );
 490  
 491          $c = wp_cache_get( $a, 'activity_meta' );
 492          $this->assertNotEmpty( $c['foo1'] );
 493      }
 494  
 495      /**
 496       * @group activitymeta
 497       * @group bp_activity_delete_meta
 498       */
 499      public function test_bp_activity_delete_meta_non_numeric_activity_id() {
 500          $this->assertFalse( bp_activity_delete_meta( 'foo', 'bar' ) );
 501      }
 502  
 503      /**
 504       * @group activitymeta
 505       * @group bp_activity_delete_meta
 506       * @ticket BP5399
 507       */
 508  	public function test_bp_activity_delete_meta_trim_meta_value() {
 509          $a = self::factory()->activity->create();
 510          bp_activity_update_meta( $a, 'foo', 'bar' );
 511          bp_activity_delete_meta( $a, 'foo', ' bar ' );
 512          $this->assertSame( 'bar', bp_activity_get_meta( $a, 'foo' ) );
 513      }
 514  
 515      /**
 516       * @group activitymeta
 517       * @group bp_activity_delete_meta
 518       */
 519  	public function test_bp_activity_delete_meta_single() {
 520          $a = self::factory()->activity->create();
 521          bp_activity_update_meta( $a, 'foo', 'bar' );
 522          $this->assertTrue( bp_activity_delete_meta( $a, 'foo', 'bar' ) );
 523          $this->assertSame( '', bp_activity_get_meta( $a, 'foo' ) );
 524      }
 525  
 526      /**
 527       * @group activitymeta
 528       * @group bp_activity_delete_meta
 529       */
 530  	public function test_bp_activity_delete_meta_all_for_activity() {
 531          $a = self::factory()->activity->create();
 532          bp_activity_update_meta( $a, 'foo', 'bar' );
 533          bp_activity_update_meta( $a, 'foo1', 'bar1' );
 534          $this->assertTrue( bp_activity_delete_meta( $a ) );
 535          $this->assertSame( '', bp_activity_get_meta( $a, 'foo' ) );
 536          $this->assertSame( '', bp_activity_get_meta( $a, 'foo1' ) );
 537      }
 538  
 539      /**
 540       * @group activitymeta
 541       * @group bp_activity_delete_meta
 542       */
 543  	public function test_bp_activity_delete_meta_with_meta_value() {
 544          $a = self::factory()->activity->create();
 545          bp_activity_update_meta( $a, 'foo', 'bar' );
 546          $this->assertTrue( bp_activity_delete_meta( $a, 'foo', 'bar' ) );
 547          $this->assertSame( '', bp_activity_get_meta( $a, 'foo' ) );
 548      }
 549  
 550      /**
 551       * @group activitymeta
 552       * @group bp_activity_delete_meta
 553       */
 554      public function test_bp_activity_delete_meta_with_delete_all_but_no_meta_key() {
 555          // With no meta key, don't delete for all items - just delete
 556          // all for a single item
 557          $a1 = self::factory()->activity->create();
 558          $a2 = self::factory()->activity->create();
 559          bp_activity_update_meta( $a1, 'foo', 'bar' );
 560          bp_activity_update_meta( $a1, 'foo1', 'bar1' );
 561          bp_activity_update_meta( $a2, 'foo', 'bar' );
 562          bp_activity_update_meta( $a2, 'foo1', 'bar1' );
 563  
 564          $this->assertTrue( bp_activity_delete_meta( $a1, '', '', true ) );
 565          $this->assertEmpty( bp_activity_get_meta( $a1 ) );
 566          $this->assertSame( 'bar', bp_activity_get_meta( $a2, 'foo' ) );
 567          $this->assertSame( 'bar1', bp_activity_get_meta( $a2, 'foo1' ) );
 568      }
 569  
 570      /**
 571       * @group activitymeta
 572       * @group bp_activity_delete_meta
 573       */
 574  	public function test_bp_activity_delete_meta_with_delete_all() {
 575          // With no meta key, don't delete for all items - just delete
 576          // all for a single item
 577          $a1 = self::factory()->activity->create();
 578          $a2 = self::factory()->activity->create();
 579          bp_activity_update_meta( $a1, 'foo', 'bar' );
 580          bp_activity_update_meta( $a1, 'foo1', 'bar1' );
 581          bp_activity_update_meta( $a2, 'foo', 'bar' );
 582          bp_activity_update_meta( $a2, 'foo1', 'bar1' );
 583  
 584          $this->assertTrue( bp_activity_delete_meta( $a1, 'foo', '', true ) );
 585          $this->assertSame( '', bp_activity_get_meta( $a1, 'foo' ) );
 586          $this->assertSame( '', bp_activity_get_meta( $a2, 'foo' ) );
 587          $this->assertSame( 'bar1', bp_activity_get_meta( $a1, 'foo1' ) );
 588          $this->assertSame( 'bar1', bp_activity_get_meta( $a2, 'foo1' ) );
 589      }
 590  
 591      /**
 592       * @group activitymeta
 593       * @group bp_activity_add_meta
 594       */
 595  	public function test_bp_activity_add_meta_no_meta_key() {
 596          $this->assertFalse( bp_activity_add_meta( 1, '', 'bar' ) );
 597      }
 598  
 599      /**
 600       * @group activitymeta
 601       * @group bp_activity_add_meta
 602       */
 603  	public function test_bp_activity_add_meta_empty_object_id() {
 604          $this->assertFalse( bp_activity_add_meta( 0, 'foo', 'bar' ) );
 605      }
 606  
 607      /**
 608       * @group activitymeta
 609       * @group bp_activity_add_meta
 610       */
 611  	public function test_bp_activity_add_meta_existing_unique() {
 612          $a = self::factory()->activity->create();
 613          bp_activity_add_meta( $a, 'foo', 'bar' );
 614          $this->assertFalse( bp_activity_add_meta( $a, 'foo', 'baz', true ) );
 615      }
 616  
 617      /**
 618       * @group activitymeta
 619       * @group bp_activity_add_meta
 620       */
 621  	public function test_bp_activity_add_meta_existing_not_unique() {
 622          $a = self::factory()->activity->create();
 623          bp_activity_add_meta( $a, 'foo', 'bar' );
 624          $this->assertNotEmpty( bp_activity_add_meta( $a, 'foo', 'baz' ) );
 625      }
 626  
 627      /**
 628       * @group bp_activity_get_user_mentionname
 629       */
 630      public function test_bp_activity_get_user_mentionname_compatibilitymode_off() {
 631          add_filter( 'bp_is_username_compatibility_mode', '__return_false' );
 632  
 633          $u = self::factory()->user->create( array(
 634              'user_login' => 'foo bar baz',
 635              'user_nicename' => 'foo-bar-baz',
 636          ) );
 637  
 638          $this->assertEquals( 'foo-bar-baz', bp_activity_get_user_mentionname( $u ) );
 639  
 640          remove_filter( 'bp_is_username_compatibility_mode', '__return_false' );
 641      }
 642  
 643      /**
 644       * @group bp_activity_get_user_mentionname
 645       */
 646      public function test_bp_activity_get_user_mentionname_compatibilitymode_on() {
 647          add_filter( 'bp_is_username_compatibility_mode', '__return_true' );
 648  
 649          $u1 = self::factory()->user->create( array(
 650              'user_login' => 'foo bar baz',
 651              'user_nicename' => 'foo-bar-baz',
 652          ) );
 653  
 654          $u2 = self::factory()->user->create( array(
 655              'user_login' => 'foo.bar.baz',
 656              'user_nicename' => 'foo-bar-baz',
 657          ) );
 658  
 659          $this->assertEquals( 'foo-bar-baz', bp_activity_get_user_mentionname( $u1 ) );
 660          $this->assertEquals( 'foo.bar.baz', bp_activity_get_user_mentionname( $u2 ) );
 661  
 662          remove_filter( 'bp_is_username_compatibility_mode', '__return_true' );
 663      }
 664  
 665      /**
 666       * @group bp_activity_get_userid_from_mentionname
 667       */
 668      public function test_bp_activity_get_userid_from_mentionname_compatibilitymode_off() {
 669          add_filter( 'bp_is_username_compatibility_mode', '__return_false' );
 670  
 671          $u = self::factory()->user->create( array(
 672              'user_login' => 'foo bar baz',
 673              'user_nicename' => 'foo-bar-baz',
 674          ) );
 675  
 676          $this->assertEquals( $u, bp_activity_get_userid_from_mentionname( 'foo-bar-baz' ) );
 677  
 678          remove_filter( 'bp_is_username_compatibility_mode', '__return_false' );
 679      }
 680  
 681      /**
 682       * @group bp_activity_get_userid_from_mentionname
 683       */
 684      public function test_bp_activity_get_userid_from_mentionname_compatibilitymode_on() {
 685          add_filter( 'bp_is_username_compatibility_mode', '__return_true' );
 686  
 687          // all spaces are hyphens
 688          $u1 = self::factory()->user->create( array(
 689              'user_login' => 'foo bar baz',
 690              'user_nicename' => 'foobarbaz',
 691          ) );
 692  
 693          // no spaces are hyphens
 694          $u2 = self::factory()->user->create( array(
 695              'user_login' => 'foo-bar-baz-1',
 696              'user_nicename' => 'foobarbaz-1',
 697          ) );
 698  
 699          // some spaces are hyphens
 700          $u3 = self::factory()->user->create( array(
 701              'user_login' => 'foo bar-baz 2',
 702              'user_nicename' => 'foobarbaz-2',
 703          ) );
 704  
 705          $u4 = self::factory()->user->create( array(
 706              'user_login' => 'foo.bar.baz',
 707              'user_nicename' => 'foo-bar-baz',
 708          ) );
 709  
 710          $this->assertEquals( $u1, bp_activity_get_userid_from_mentionname( 'foo-bar-baz' ) );
 711          $this->assertEquals( $u2, bp_activity_get_userid_from_mentionname( 'foo-bar-baz-1' ) );
 712          $this->assertEquals( $u3, bp_activity_get_userid_from_mentionname( 'foo-bar-baz-2' ) );
 713          $this->assertEquals( $u4, bp_activity_get_userid_from_mentionname( 'foo.bar.baz' ) );
 714  
 715          remove_filter( 'bp_is_username_compatibility_mode', '__return_true' );
 716      }
 717  
 718      /**
 719       * @group activity_action
 720       * @group bp_activity_format_activity_action_activity_update
 721       */
 722      public function test_bp_activity_format_activity_action_activity_update() {
 723          $u = self::factory()->user->create();
 724          $a = self::factory()->activity->create( array(
 725              'component' => buddypress()->activity->id,
 726              'type' => 'activity_update',
 727              'user_id' => $u,
 728          ) );
 729  
 730          $a_obj = new BP_Activity_Activity( $a );
 731  
 732          $expected = sprintf( '%s posted an update', bp_core_get_userlink( $u ) );
 733  
 734          $this->assertSame( $expected, $a_obj->action );
 735      }
 736  
 737      /**
 738       * @group activity_action
 739       * @group bp_activity_format_activity_action_activity_comment
 740       */
 741      public function test_bp_activity_format_activity_action_activity_comment() {
 742          $u = self::factory()->user->create();
 743          $a = self::factory()->activity->create( array(
 744              'component' => buddypress()->activity->id,
 745              'type' => 'activity_comment',
 746              'user_id' => $u,
 747          ) );
 748  
 749          $a_obj = new BP_Activity_Activity( $a );
 750  
 751          $expected = sprintf( '%s posted a new activity comment', bp_core_get_userlink( $u ) );
 752  
 753          $this->assertSame( $expected, $a_obj->action );
 754      }
 755  
 756      /**
 757       * @group activity_action
 758       * @group bp_activity_format_activity_action_activity_comment
 759       * @ticket BP8120
 760       */
 761      public function test_bp_activity_format_activity_action_children_activity_comment() {
 762          $u = self::factory()->user->create();
 763          $a1 = self::factory()->activity->create( array(
 764              'component' => buddypress()->activity->id,
 765              'type'      => 'activity_update',
 766              'user_id'   => $u,
 767          ) );
 768  
 769          $ac1 = self::factory()->activity->create( array(
 770              'type'              => 'activity_comment',
 771              'item_id'           => $a1,
 772              'secondary_item_id' => $a1,
 773              'user_id'           => $u,
 774          ) );
 775  
 776          $ac2 = self::factory()->activity->create( array(
 777              'type'              => 'activity_comment',
 778              'item_id'           => $a1,
 779              'secondary_item_id' => $ac1,
 780              'user_id'           => $u,
 781          ) );
 782  
 783          // Remove all action strings
 784          $table = buddypress()->activity->table_name;
 785          $GLOBALS['wpdb']->query( "UPDATE {$table} set action = '' WHERE id IN ( {$a1}, {$ac1}, {$ac2} )" );
 786  
 787          $get = bp_activity_get( array(
 788              'in'               => array( $a1 ),
 789              'display_comments' => 'thread'
 790          ) );
 791  
 792          $activity    = reset( $get['activities'] );
 793          $comment_one = reset( $activity->children );
 794          $comment_two = reset( $comment_one->children );
 795          $user_link   = bp_core_get_userlink( $u );
 796  
 797          $this->assertSame( sprintf( '%s posted a new activity comment', $user_link ), $comment_one->action );
 798          $this->assertSame( sprintf( '%s posted a new activity comment', $user_link ), $comment_two->action );
 799      }
 800  
 801      /**
 802       * @group activity_action
 803       * @group bp_activity_format_activity_action_custom_post_type_post
 804       * @group activity_tracking
 805       */
 806      public function test_bp_activity_format_activity_action_custom_post_type_post_nonms() {
 807          if ( is_multisite() ) {
 808              $this->markTestSkipped();
 809          }
 810  
 811          register_post_type( 'foo', array(
 812              'label'   => 'foo',
 813              'public'   => true,
 814              'supports' => array( 'buddypress-activity' ),
 815          ) );
 816  
 817          // Build the actions to fetch the tracking args
 818          bp_activity_get_actions();
 819  
 820          $u = self::factory()->user->create();
 821          $p = self::factory()->post->create( array(
 822              'post_author' => $u,
 823              'post_type'   => 'foo',
 824          ) );
 825  
 826          $a = self::factory()->activity->create( array(
 827              'component'         => 'activity',
 828              'type'              => 'new_foo',
 829              'user_id'           => $u,
 830              'item_id'           => 1,
 831              'secondary_item_id' => $p,
 832          ) );
 833  
 834          $a_obj = new BP_Activity_Activity( $a );
 835  
 836          $user_link = bp_core_get_userlink( $u );
 837          $blog_url = get_home_url();
 838          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 839          $post_link = '<a href="' . $post_url . '">item</a>';
 840  
 841          $expected = sprintf( '%s wrote a new %s', $user_link, $post_link );
 842  
 843          $this->assertSame( $expected, $a_obj->action );
 844  
 845          _unregister_post_type( 'foo' );
 846      }
 847  
 848      /**
 849       * @group activity_action
 850       * @group bp_activity_format_activity_action_custom_post_type_post_ms
 851       * @group activity_tracking
 852       */
 853      public function test_bp_activity_format_activity_action_custom_post_type_post_ms() {
 854          if ( ! is_multisite() ) {
 855              $this->markTestSkipped();
 856          }
 857  
 858          $b = self::factory()->blog->create();
 859          $u = self::factory()->user->create();
 860  
 861          switch_to_blog( $b );
 862  
 863          register_post_type( 'foo', array(
 864              'label'   => 'foo',
 865              'public'   => true,
 866              'supports' => array( 'buddypress-activity' ),
 867          ) );
 868  
 869          // Build the actions to fetch the tracking args
 870          bp_activity_get_actions();
 871  
 872          $p = self::factory()->post->create( array(
 873              'post_author' => $u,
 874              'post_type'   => 'foo',
 875          ) );
 876  
 877          $activity_args = array(
 878              'component'         => 'activity',
 879              'type'              => 'new_foo',
 880              'user_id'           => $u,
 881              'item_id'           => $b,
 882              'secondary_item_id' => $p,
 883          );
 884  
 885          _unregister_post_type( 'foo' );
 886          bp_activity_get_actions();
 887  
 888          restore_current_blog();
 889  
 890          $a = self::factory()->activity->create( $activity_args );
 891  
 892          $a_obj = new BP_Activity_Activity( $a );
 893  
 894          $user_link = bp_core_get_userlink( $u );
 895          $blog_url = get_blog_option( $a_obj->item_id, 'home' );
 896          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 897  
 898          $post_link = '<a href="' . $post_url . '">item</a>';
 899  
 900          $expected = sprintf( '%s wrote a new %s, on the site %s', $user_link, $post_link, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
 901  
 902          $this->assertSame( $expected, $a_obj->action );
 903      }
 904  
 905  
 906      /**
 907       * @group activity_action
 908       * @group bp_activity_format_activity_action_custom_post_type_post
 909       */
 910      public function test_bp_activity_format_activity_action_custom_string_post_type_post_nonms() {
 911          if ( is_multisite() ) {
 912              $this->markTestSkipped();
 913          }
 914  
 915          $labels = array(
 916              'name'                 => 'bars',
 917              'singular_name'        => 'bar',
 918              'bp_activity_new_post' => '%1$s shared a new <a href="%2$s">bar</a>',
 919          );
 920  
 921          register_post_type( 'foo', array(
 922              'labels'      => $labels,
 923              'public'      => true,
 924              'supports'    => array( 'buddypress-activity' ),
 925              'bp_activity' => array(
 926                  'action_id' => 'foo_bar',
 927              ),
 928          ) );
 929  
 930          // Build the actions to fetch the tracking args
 931          bp_activity_get_actions();
 932  
 933          $u = self::factory()->user->create();
 934          $p = self::factory()->post->create( array(
 935              'post_author' => $u,
 936              'post_type'   => 'foo',
 937          ) );
 938  
 939          $a = self::factory()->activity->create( array(
 940              'component'         => 'activity',
 941              'type'              => 'foo_bar',
 942              'user_id'           => $u,
 943              'item_id'           => 1,
 944              'secondary_item_id' => $p,
 945          ) );
 946  
 947          $a_obj = new BP_Activity_Activity( $a );
 948  
 949          $user_link = bp_core_get_userlink( $u );
 950          $blog_url = get_home_url();
 951          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 952  
 953          $expected = sprintf( '%1$s shared a new <a href="%2$s">bar</a>', $user_link, $post_url );
 954  
 955          $this->assertSame( $expected, $a_obj->action );
 956  
 957          _unregister_post_type( 'foo' );
 958      }
 959  
 960      /**
 961       * @group activity_action
 962       * @group bp_activity_format_activity_action_custom_post_type_post_ms
 963       * @group activity_tracking
 964       */
 965      public function test_bp_activity_format_activity_action_custom_string_post_type_post_ms() {
 966          if ( ! is_multisite() ) {
 967              $this->markTestSkipped();
 968          }
 969  
 970          $b = self::factory()->blog->create();
 971          $u = self::factory()->user->create();
 972  
 973          switch_to_blog( $b );
 974  
 975          $labels = array(
 976              'name'                    => 'bars',
 977              'singular_name'           => 'bar',
 978              'bp_activity_new_post_ms' => '%1$s shared a new <a href="%2$s">bar</a>, on the site %3$s',
 979          );
 980  
 981          register_post_type( 'foo', array(
 982              'labels'   => $labels,
 983              'public'   => true,
 984              'supports' => array( 'buddypress-activity' ),
 985          ) );
 986  
 987          // Build the actions to fetch the tracking args
 988          bp_activity_get_actions();
 989  
 990          $p = self::factory()->post->create( array(
 991              'post_author' => $u,
 992              'post_type'   => 'foo',
 993          ) );
 994  
 995          $activity_args = array(
 996              'component'         => 'activity',
 997              'type'              => 'new_foo',
 998              'user_id'           => $u,
 999              'item_id'           => $b,
1000              'secondary_item_id' => $p,
1001          );
1002  
1003          _unregister_post_type( 'foo' );
1004  
1005          restore_current_blog();
1006  
1007          $a = self::factory()->activity->create( $activity_args );
1008  
1009          $a_obj = new BP_Activity_Activity( $a );
1010  
1011          $user_link = bp_core_get_userlink( $u );
1012          $blog_url = get_blog_option( $a_obj->item_id, 'home' );
1013          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
1014  
1015          $expected = sprintf( '%1$s shared a new <a href="%2$s">bar</a>, on the site %3$s', $user_link, $post_url, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
1016  
1017          $this->assertSame( $expected, $a_obj->action );
1018      }
1019  
1020      /**
1021       * @group bp_activity_set_post_type_tracking_args
1022       * @group activity_tracking
1023       */
1024  	public function test_bp_activity_set_post_type_tracking_args() {
1025          $bp = buddypress();
1026  
1027          add_post_type_support( 'page', 'buddypress-activity' );
1028  
1029          bp_activity_set_post_type_tracking_args( 'page', array(
1030              'component_id' => $bp->blogs->id,
1031              'dummy'        => 'dummy value',
1032          ) );
1033  
1034          // Build the actions to fetch the tracking args
1035          bp_activity_get_actions();
1036  
1037          $u = self::factory()->user->create();
1038  
1039          $post_id = self::factory()->post->create( array(
1040              'post_author' => $u,
1041              'post_status' => 'publish',
1042              'post_type'   => 'page',
1043          ) );
1044  
1045          $a = bp_activity_get( array(
1046              'action'            => 'new_page',
1047              'item_id'           => get_current_blog_id(),
1048              'secondary_item_id' => $post_id,
1049          ) );
1050  
1051          $this->assertSame( $bp->blogs->id, $a['activities'][0]->component );
1052  
1053          remove_post_type_support( 'page', 'buddypress-activity' );
1054      }
1055  
1056      /**
1057       * @group bp_activity_set_post_type_tracking_args
1058       * @group activity_tracking
1059       */
1060      public function test_bp_activity_set_post_type_tracking_args_check_post_type_global() {
1061          $labels = array(
1062              'bp_activity_admin_filter' => 'New Foo',
1063              'bp_activity_front_filter' => 'Foos',
1064          );
1065  
1066          $bp_activity_args = array(
1067              'action_id'    => 'new_foo',
1068              'contexts'     => array( 'activity' ),
1069              'position'     => 40,
1070          );
1071  
1072          register_post_type( 'foo', array(
1073              'labels'      => $labels,
1074              'supports'    => array( 'buddypress-activity' ),
1075              'bp_activity' => $bp_activity_args
1076          ) );
1077  
1078          $register_bp_activity = get_post_type_object( 'foo' )->bp_activity;
1079          _unregister_post_type( 'foo' );
1080  
1081          register_post_type( 'foo', array(
1082              'label'       => 'foo',
1083              'supports'    => array( 'buddypress-activity' ),
1084          ) );
1085  
1086          bp_activity_set_post_type_tracking_args( 'foo', $labels + $bp_activity_args );
1087  
1088          $set_bp_activity = get_post_type_object( 'foo' )->bp_activity;
1089          _unregister_post_type( 'foo' );
1090  
1091          $this->assertSame( $set_bp_activity, $register_bp_activity );
1092      }
1093  
1094      /**
1095       * @group activity_action
1096       * @group bp_activity_format_activity_action_custom_post_type_post_ms
1097       * @group post_type_comment_activities
1098       */
1099      public function test_bp_activity_format_activity_action_custom_post_type_comment() {
1100          if ( is_multisite() ) {
1101              $b = self::factory()->blog->create();
1102              switch_to_blog( $b );
1103              add_filter( 'comment_flood_filter', '__return_false' );
1104          } else {
1105              $b = get_current_blog_id();
1106          }
1107  
1108          $u = self::factory()->user->create();
1109          $userdata = get_userdata( $u );
1110  
1111          $labels = array(
1112              'name'                       => 'bars',
1113              'singular_name'              => 'bar',
1114              'bp_activity_new_comment'    => __( '%1$s commented on the <a href="%2$s">bar</a>', 'buddypress' ),
1115              'bp_activity_new_comment_ms' => __( '%1$s commented on the <a href="%2$s">bar</a>, on the site %3$s', 'buddypress' ),
1116          );
1117  
1118          register_post_type( 'foo', array(
1119              'labels'   => $labels,
1120              'public'   => true,
1121              'supports' => array( 'buddypress-activity', 'comments' ),
1122              'bp_activity' => array(
1123                  'action_id'         => 'new_bar',
1124                  'comment_action_id' => 'new_bar_comment',
1125              ),
1126          ) );
1127  
1128          // Build the actions to fetch the tracking args
1129          bp_activity_get_actions();
1130  
1131          $p = self::factory()->post->create( array(
1132              'post_author' => $u,
1133              'post_type'   => 'foo',
1134          ) );
1135  
1136          $c = wp_new_comment( array(
1137              'comment_post_ID'      => $p,
1138              'comment_author'       => $userdata->user_nicename,
1139              'comment_author_url'   => 'http://buddypress.org',
1140              'comment_author_email' => $userdata->user_email,
1141              'comment_content'      => 'this is a blog comment',
1142              'comment_type'         => '',
1143              'comment_parent'       => 0,
1144              'user_id'              => $u,
1145          ) );
1146  
1147          $a = bp_activity_get_activity_id( array( 'type' => 'new_bar_comment' ) );
1148  
1149          $a_obj = new BP_Activity_Activity( $a );
1150  
1151          $user_link    = bp_core_get_userlink( $u );
1152          $comment_url  = get_comment_link( $c );
1153  
1154          _unregister_post_type( 'foo' );
1155  
1156          if ( is_multisite() ) {
1157              $blog_url  = get_blog_option( $a_obj->item_id, 'home' );
1158              restore_current_blog();
1159              remove_filter( 'comment_flood_filter', '__return_false' );
1160  
1161              $expected = sprintf( $labels['bp_activity_new_comment_ms'], $user_link, $comment_url, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
1162          } else {
1163              $expected = sprintf( $labels['bp_activity_new_comment'], $user_link, $comment_url );
1164          }
1165  
1166          $this->assertSame( $expected, $a_obj->action );
1167      }
1168  
1169      /**
1170       * @group bp_activity_new_comment
1171       * @group cache
1172       */
1173      public function test_bp_activity_new_comment_clear_comment_caches() {
1174          $u = self::factory()->user->create();
1175          $a1 = self::factory()->activity->create( array(
1176              'user_id' => $u,
1177          ) );
1178          $a2 = bp_activity_new_comment( array(
1179              'activity_id' => $a1,
1180              'parent_id' => $a1,
1181              'content' => 'foo',
1182              'user_id' => $u,
1183          ) );
1184          $a3 = bp_activity_new_comment( array(
1185              'activity_id' => $a1,
1186              'parent_id' => $a2,
1187              'content' => 'foo',
1188              'user_id' => $u,
1189          ) );
1190          $a4 = bp_activity_new_comment( array(
1191              'activity_id' => $a1,
1192              'parent_id' => $a3,
1193              'content' => 'foo',
1194              'user_id' => $u,
1195          ) );
1196          $a5 = bp_activity_new_comment( array(
1197              'activity_id' => $a1,
1198              'parent_id' => $a3,
1199              'content' => 'foo',
1200              'user_id' => $u,
1201          ) );
1202  
1203          // prime caches
1204          bp_activity_get( array(
1205              'in' => array( $a1 ),
1206              'display_comments' => 'threaded',
1207          ) );
1208  
1209          // should be populated
1210          $this->assertNotEmpty( wp_cache_get( $a1, 'bp_activity_comments' ) );
1211  
1212          bp_activity_new_comment( array(
1213              'activity_id' => $a1,
1214              'parent_id' => $a4,
1215              'content' => 'foo',
1216              'user_id' => $u,
1217          ) );
1218  
1219          // should be empty
1220          $this->assertFalse( wp_cache_get( $a1, 'bp_activity_comments' ) );
1221      }
1222  
1223      /**
1224       * @group bp_activity_new_comment
1225       * @group cache
1226       */
1227      public function test_bp_activity_new_comment_clear_activity_caches() {
1228          $u = self::factory()->user->create();
1229          $a1 = self::factory()->activity->create( array(
1230              'user_id' => $u,
1231          ) );
1232          $a2 = bp_activity_new_comment( array(
1233              'activity_id' => $a1,
1234              'parent_id' => $a1,
1235              'content' => 'foo',
1236              'user_id' => $u,
1237          ) );
1238          $a3 = bp_activity_new_comment( array(
1239              'activity_id' => $a1,
1240              'parent_id' => $a2,
1241              'content' => 'foo',
1242              'user_id' => $u,
1243          ) );
1244          $a4 = bp_activity_new_comment( array(
1245              'activity_id' => $a1,
1246              'parent_id' => $a3,
1247              'content' => 'foo',
1248              'user_id' => $u,
1249          ) );
1250          $a5 = bp_activity_new_comment( array(
1251              'activity_id' => $a1,
1252              'parent_id' => $a3,
1253              'content' => 'foo',
1254              'user_id' => $u,
1255          ) );
1256  
1257          // prime caches
1258          bp_activity_get( array(
1259              'in' => array( $a1 ),
1260              'display_comments' => 'threaded',
1261          ) );
1262  
1263          // should be populated
1264          $this->assertNotEmpty( wp_cache_get( $a1, 'bp_activity' ) );
1265          $this->assertNotEmpty( wp_cache_get( $a2, 'bp_activity' ) );
1266          $this->assertNotEmpty( wp_cache_get( $a3, 'bp_activity' ) );
1267          $this->assertNotEmpty( wp_cache_get( $a4, 'bp_activity' ) );
1268          $this->assertNotEmpty( wp_cache_get( $a5, 'bp_activity' ) );
1269  
1270          // Stuff may run on bp_activity_comment_posted that loads the
1271          // cache, so we use this dumb technique to check cache values
1272          // before any of that stuff gets a chance to run. WordPress
1273          // sure is neat sometimes
1274          $this->acaches = array(
1275              $a1 => '',
1276              $a2 => '',
1277              $a3 => '',
1278              $a4 => '',
1279          );
1280          add_action( 'bp_activity_comment_posted', array( $this, 'check_activity_caches' ), 0 );
1281  
1282          bp_activity_new_comment( array(
1283              'activity_id' => $a1,
1284              'parent_id' => $a4,
1285              'content' => 'foo',
1286              'user_id' => $u,
1287          ) );
1288  
1289          // should be empty
1290          foreach ( $this->acaches as $k => $v ) {
1291              $this->assertFalse( $v, "Cache should be false for $k" );
1292          }
1293      }
1294  
1295      /**
1296       * @group bp_activity_delete_comment
1297       * @group cache
1298       */
1299  	public function test_bp_activity_delete_comment_clear_cache() {
1300          $u = self::factory()->user->create();
1301          // add new activity update and comment to this update
1302          $a1 = self::factory()->activity->create( array(
1303              'user_id' => $u,
1304          ) );
1305          $a2 = bp_activity_new_comment( array(
1306              'activity_id' => $a1,
1307              'parent_id' => $a1,
1308              'content' => 'foo',
1309              'user_id' => $u,
1310          ) );
1311  
1312          // prime cache
1313          bp_activity_get( array(
1314              'in' => array( $a1 ),
1315              'display_comments' => 'threaded',
1316          ) );
1317  
1318          // delete activity comment
1319          bp_activity_delete_comment( $a1, $a2 );
1320  
1321          // assert comment cache as empty for $a1
1322          $this->assertEmpty( wp_cache_get( $a1, 'bp_activity_comments' ) );
1323      }
1324  
1325      /**
1326       * @group  bp_activity_delete_comment
1327       * @ticket BP7450
1328       */
1329      public function test_bp_activity_delete_comment_shouldnt_delete_all_comments_when_parameters_are_empty() {
1330          $u = self::factory()->user->create();
1331  
1332          // create an activity update
1333          $parent_activity = self::factory()->activity->create( array(
1334              'type'    => 'activity_update',
1335              'user_id' => $u
1336          ) );
1337  
1338          // create some activity comments
1339          $comment_one = bp_activity_new_comment( array(
1340              'user_id'     => $u,
1341              'activity_id' => $parent_activity,
1342              'content'     => 'depth 1'
1343          ) );
1344  
1345          $comment_one_one = bp_activity_new_comment( array(
1346              'user_id'     => $u,
1347              'activity_id' => $parent_activity,
1348              'parent_id'   => $comment_one,
1349              'content'     => 'depth 2'
1350          ) );
1351  
1352          $comment_two = bp_activity_new_comment( array(
1353              'user_id'     => $u,
1354              'activity_id' => $parent_activity,
1355              'content'     => 'depth 1'
1356          ) );
1357  
1358          // Pass empty values to bp_activity_delete_comment()
1359          $retval = bp_activity_delete_comment( 0, 0 );
1360          $this->assertFalse( $retval );
1361  
1362          // Instantiate activity loop, which also includes activity comments.
1363          bp_has_activities( 'display_comments=stream' );
1364  
1365          // Activity comments should not be deleted.
1366          $this->assertSame( 4, $GLOBALS['activities_template']->activity_count );
1367  
1368          // Clean up after ourselves!
1369          $GLOBALS['activities_template'] = null;
1370      }
1371  
1372      /**
1373       * @group bp_activity_new_comment
1374       * @group BP5907
1375       */
1376  	public function test_bp_activity_comment_on_deleted_activity() {
1377          $u = self::factory()->user->create();
1378          $a = self::factory()->activity->create();
1379  
1380          bp_activity_delete_by_activity_id( $a );
1381  
1382          $c = bp_activity_new_comment( array(
1383              'activity_id' => $a,
1384              'parent_id' => $a,
1385              'content' => 'foo',
1386              'user_id' => $u,
1387          ) );
1388  
1389          $this->assertEmpty( $c );
1390      }
1391  
1392      /**
1393       * @group favorites
1394       * @group bp_activity_add_user_favorite
1395       */
1396  	public function test_add_user_favorite_already_favorited() {
1397          $u = self::factory()->user->create();
1398          $a = self::factory()->activity->create();
1399  
1400          // bp_activity_add_user_favorite() requires a logged-in user.
1401          $current_user = bp_loggedin_user_id();
1402          $this->set_current_user( $u );
1403  
1404          $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
1405  
1406          $this->assertFalse( bp_activity_add_user_favorite( $a, $u ) );
1407          $this->assertSame( array( $a ), bp_activity_get_user_favorites( $u ) );
1408          $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
1409  
1410          $this->set_current_user( $current_user );
1411      }
1412  
1413      /**
1414       * @group favorites
1415       * @group bp_activity_add_user_favorite
1416       */
1417  	public function test_add_user_favorite_not_yet_favorited() {
1418          $u = self::factory()->user->create();
1419          $a = self::factory()->activity->create();
1420  
1421          // bp_activity_add_user_favorite() requires a logged-in user.
1422          $current_user = bp_loggedin_user_id();
1423          $this->set_current_user( $u );
1424          $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
1425  
1426          $this->set_current_user( $current_user );
1427      }
1428  
1429      /**
1430       * @group favorites
1431       * @group bp_activity_remove_user_favorite
1432       */
1433  	public function test_remove_user_favorite_bad_activity_id() {
1434          $u1 = self::factory()->user->create();
1435          $u2 = self::factory()->user->create();
1436          $a = self::factory()->activity->create();
1437  
1438          // bp_activity_add_user_favorite() requires a logged-in user.
1439          $current_user = bp_loggedin_user_id();
1440          $this->set_current_user( $u1 );
1441  
1442          // Only favorite for user 1
1443          bp_activity_add_user_favorite( $a, $u1 );
1444  
1445          // Removing for user 2 should fail
1446          $this->assertFalse( bp_activity_remove_user_favorite( $a, $u2 ) );
1447          $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
1448  
1449          $this->set_current_user( $current_user );
1450      }
1451  
1452      /**
1453       * @group bp_activity_post_update
1454       */
1455  	public function test_bp_activity_post_update_empty_content() {
1456          $this->assertFalse( bp_activity_post_update( array( 'user_id' => 3, ) ) );
1457      }
1458  
1459      /**
1460       * @group bp_activity_post_update
1461       */
1462      public function test_bp_activity_post_update_empty_content_wp_error() {
1463          $activity = bp_activity_post_update( array(
1464              'user_id'    => 3,
1465              'error_type' => 'wp_error',
1466          ) );
1467  
1468          $this->assertInstanceOf( 'WP_Error', $activity );
1469          $this->assertEquals( 'bp_activity_missing_content', $activity->get_error_code() );
1470      }
1471  
1472      /**
1473       * @group bp_activity_post_update
1474       */
1475  	public function test_bp_activity_post_update_inactive_user() {
1476          $this->assertFalse( bp_activity_post_update( array(
1477              'user_id' => 3456,
1478              'content' => 'foo',
1479          ) ) );
1480      }
1481  
1482      /**
1483       * @group bp_activity_post_update
1484       */
1485      public function test_bp_activity_post_update_inactive_user_wp_error() {
1486          $activity = bp_activity_post_update( array(
1487              'user_id'    => 3456,
1488              'content'    => 'foo',
1489              'error_type' => 'wp_error',
1490          ) );
1491  
1492          $this->assertInstanceOf( 'WP_Error', $activity );
1493          $this->assertEquals( 'bp_activity_inactive_user', $activity->get_error_code() );
1494      }
1495  
1496      /**
1497       * @group bp_activity_post_update
1498       */
1499  	public function test_bp_activity_post_update_success() {
1500          $u = self::factory()->user->create();
1501  
1502          $a = bp_activity_post_update( array(
1503              'user_id' => $u,
1504              'content' => 'foo',
1505          ) );
1506  
1507          $this->assertNotEmpty( $a );
1508      }
1509  
1510      /**
1511       * @group bp_activity_get_activity_id
1512       */
1513  	public function test_bp_activity_get_activity_id() {
1514          $args = array(
1515              'user_id' => 5,
1516              'component' => 'foo',
1517              'type' => 'bar',
1518              'item_id' => 12,
1519              'secondary_item_id' => 44,
1520          );
1521  
1522          $a = self::factory()->activity->create( $args );
1523  
1524          $this->assertEquals( $a, bp_activity_get_activity_id( $args ) );
1525      }
1526  
1527      /**
1528       * @group bp_activity_delete_by_item_id
1529       */
1530  	public function test_bp_activity_delete_by_item_id() {
1531          $args = array(
1532              'user_id' => 5,
1533              'component' => 'foo',
1534              'type' => 'bar',
1535              'item_id' => 12,
1536              'secondary_item_id' => 44,
1537          );
1538  
1539          $a = self::factory()->activity->create( $args );
1540  
1541          $this->assertTrue( bp_activity_delete_by_item_id( $args ) );
1542  
1543          $found = bp_activity_get_specific( array(
1544              'activity_ids' => array( $a ),
1545          ) );
1546  
1547          $this->assertSame( array(), $found['activities'] );
1548      }
1549  
1550      /**
1551       * @group bp_activity_user_can_read
1552       */
1553  	public function test_user_can_access_their_own_activity() {
1554          $u = self::factory()->user->create();
1555  
1556          $a = self::factory()->activity->create( array(
1557              'user_id' => $u,
1558          ) );
1559  
1560          $o = self::factory()->activity->get_object_by_id( $a );
1561  
1562          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1563      }
1564  
1565      /**
1566       * @group bp_activity_user_can_read
1567       */
1568  	public function test_admin_can_access_someone_elses_activity() {
1569          $u = self::factory()->user->create();
1570          $u2 = self::factory()->user->create( array( 'role' => 'administrator' ) );
1571  
1572          $a = self::factory()->activity->create( array(
1573              'user_id' => $u,
1574          ) );
1575  
1576          $o = self::factory()->activity->get_object_by_id( $a );
1577  
1578          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1579  
1580          $this->set_current_user( $u2 );
1581          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1582      }
1583  
1584      /**
1585       * @group bp_activity_user_can_read
1586       */
1587  	public function test_user_cannot_access_spam_activity() {
1588          $u = self::factory()->user->create();
1589          $u2 = self::factory()->user->create();
1590  
1591          $a = self::factory()->activity->create( array(
1592              'user_id' => $u,
1593          ) );
1594  
1595          $o = self::factory()->activity->get_object_by_id( $a );
1596  
1597          bp_activity_mark_as_spam( $o );
1598  
1599          $this->assertFalse( bp_activity_user_can_read( $o, $u ) );
1600          $this->assertFalse( bp_activity_user_can_read( $o, $u2 ) );
1601      }
1602  
1603      /**
1604       * @group bp_activity_user_can_read
1605       */
1606  	public function test_admin_can_access_spam_activity() {
1607          $u = self::factory()->user->create();
1608          $u2 = self::factory()->user->create( array( 'role' => 'administrator' ) );
1609  
1610          $a = self::factory()->activity->create( array(
1611              'user_id' => $u,
1612          ) );
1613  
1614          $o = self::factory()->activity->get_object_by_id( $a );
1615  
1616          bp_activity_mark_as_spam( $o );
1617  
1618          $this->set_current_user( $u2 );
1619          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1620      }
1621  
1622      /**
1623       * @group bp_activity_user_can_read
1624       */
1625      public function test_group_admin_access_someone_elses_activity_in_a_group() {
1626          $u  = self::factory()->user->create();
1627          $u2 = self::factory()->user->create();
1628  
1629          $g  = self::factory()->group->create();
1630  
1631          $a = self::factory()->activity->create( array(
1632              'component' => buddypress()->groups->id,
1633              'user_id'   => $u,
1634              'item_id'   => $g,
1635          ) );
1636  
1637          $o = self::factory()->activity->get_object_by_id( $a );
1638  
1639          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1640  
1641          self::add_user_to_group( $u2, $g );
1642  
1643          $m1 = new BP_Groups_Member( $u2, $g );
1644          $m1->promote( 'admin' );
1645  
1646          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1647      }
1648  
1649      /**
1650       * @group bp_activity_user_can_read
1651       */
1652      public function test_non_member_can_access_to_someone_elses_activity_in_a_group() {
1653          $u  = self::factory()->user->create();
1654          $u2 = self::factory()->user->create();
1655  
1656          $g  = self::factory()->group->create();
1657  
1658          self::add_user_to_group( $u, $g );
1659  
1660          $a = self::factory()->activity->create( array(
1661              'component' => buddypress()->groups->id,
1662              'user_id'   => $u,
1663              'item_id'   => $g,
1664          ) );
1665  
1666          $o = self::factory()->activity->get_object_by_id( $a );
1667  
1668          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1669          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1670      }
1671  
1672      /**
1673       * @group bp_activity_user_can_read
1674       */
1675      public function test_user_access_to_his_activity_in_hidden_group() {
1676          $u  = self::factory()->user->create();
1677          $g  = self::factory()->group->create( array(
1678              'status' => 'hidden',
1679          ) );
1680  
1681          self::add_user_to_group( $u, $g );
1682  
1683          $a = self::factory()->activity->create( array(
1684              'component' => buddypress()->groups->id,
1685              'user_id'   => $u,
1686              'item_id'   => $g,
1687          ) );
1688  
1689          $o = self::factory()->activity->get_object_by_id( $a );
1690  
1691          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1692      }
1693  
1694      /**
1695       * @group bp_activity_user_can_read
1696       */
1697      public function test_user_access_to_his_activity_in_private_group() {
1698          $u  = self::factory()->user->create();
1699          $g  = self::factory()->group->create( array(
1700              'status' => 'private',
1701          ) );
1702  
1703          self::add_user_to_group( $u, $g );
1704  
1705          $a = self::factory()->activity->create( array(
1706              'component' => buddypress()->groups->id,
1707              'user_id'   => $u,
1708              'item_id'   => $g,
1709          ) );
1710  
1711          $o = self::factory()->activity->get_object_by_id( $a );
1712  
1713          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1714      }
1715  
1716      /**
1717       * @group bp_activity_user_can_read
1718       */
1719      public function test_banned_user_cannot_access_to_his_activity_in_a_private_group() {
1720          $u  = self::factory()->user->create();
1721          $g  = self::factory()->group->create( array(
1722              'status' => 'private',
1723          ) );
1724  
1725          self::add_user_to_group( $u, $g );
1726  
1727          $a = self::factory()->activity->create( array(
1728              'component' => buddypress()->groups->id,
1729              'user_id'   => $u,
1730              'item_id'   => $g,
1731          ) );
1732  
1733          buddypress()->is_item_admin = true;
1734          groups_ban_member( $u, $g );
1735  
1736          $o = self::factory()->activity->get_object_by_id( $a );
1737  
1738          $this->assertFalse( bp_activity_user_can_read( $o, $u ) );
1739      }
1740  
1741      /**
1742       * @group bp_activity_user_can_read
1743       */
1744      public function test_removed_member_cannot_access_to_his_activity_in_a_private_group() {
1745          $u  = self::factory()->user->create();
1746          $g  = self::factory()->group->create( array(
1747              'status' => 'private',
1748          ) );
1749  
1750          self::add_user_to_group( $u, $g );
1751  
1752          $a = self::factory()->activity->create( array(
1753              'component' => buddypress()->groups->id,
1754              'user_id'   => $u,
1755              'item_id'   => $g,
1756          ) );
1757  
1758          buddypress()->is_item_admin = true;
1759          groups_remove_member( $u, $g );
1760  
1761          $o = self::factory()->activity->get_object_by_id( $a );
1762  
1763          $this->assertFalse( bp_activity_user_can_read( $o, $u ) );
1764      }
1765  
1766  	public function check_activity_caches() {
1767          foreach ( $this->acaches as $k => $v ) {
1768              $this->acaches[ $k ] = wp_cache_get( $k, 'bp_activity' );
1769          }
1770      }
1771  
1772      /**
1773       * @ticket BP7818
1774       * @ticket BP7698
1775       */
1776  	public function test_bp_activity_personal_data_exporter() {
1777          $u = self::factory()->user->create();
1778  
1779          $a1 = self::factory()->activity->create(
1780              array(
1781                  'user_id'   => $u,
1782                  'component' => 'activity',
1783                  'type'      => 'activity_update',
1784              )
1785          );
1786  
1787          $a2 = self::factory()->activity->create(
1788              array(
1789                  'user_id'           => $u,
1790                  'component'         => 'activity',
1791                  'type'              => 'activity_comment',
1792                  'item_id'           => $a1,
1793                  'secondary_item_id' => $a1,
1794              )
1795          );
1796  
1797          $g = self::factory()->group->create(
1798              array(
1799                  'name' => 'My Cool Group',
1800              )
1801          );
1802  
1803          $a3 = self::factory()->activity->create(
1804              array(
1805                  'user_id'   => $u,
1806                  'component' => 'groups',
1807                  'type'      => 'activity_update',
1808                  'item_id'   => $g,
1809              )
1810          );
1811  
1812          $test_user = new WP_User( $u );
1813  
1814          $actual = bp_activity_personal_data_exporter( $test_user->user_email, 1 );
1815  
1816          $this->assertTrue( $actual['done'] );
1817  
1818          // Number of exported activity items.
1819          $this->assertSame( 3, count( $actual['data'] ) );
1820      }
1821  
1822      /**
1823       * @ticket BP8175
1824       */
1825      public function test_activity_data_should_be_deleted_on_user_delete_non_multisite() {
1826          if ( is_multisite() ) {
1827              $this->markTestSkipped( __METHOD__ . ' requires non-multisite.' );
1828          }
1829  
1830          $u1 = self::factory()->user->create();
1831          $a1 = self::factory()->activity->create(
1832              array(
1833                  'user_id'   => $u1,
1834                  'component' => 'activity',
1835                  'type'      => 'activity_update',
1836              )
1837          );
1838  
1839          $latest_update = array(
1840              'id'      => $a1,
1841              'content' => 'Foo',
1842          );
1843          bp_update_user_meta( $u1, 'bp_latest_update', $latest_update );
1844  
1845          bp_activity_add_user_favorite( $a1, $u1 );
1846  
1847          $found = bp_activity_get(
1848              array(
1849                  'filter' => array(
1850                      'user_id' => $u1,
1851                  )
1852              )
1853          );
1854  
1855          $this->assertEqualSets( [ $a1 ], wp_list_pluck( $found['activities'], 'id' ) );
1856          $this->assertEqualSets( [ $a1 ], bp_activity_get_user_favorites( $u1 ) );
1857          $this->assertSame( $latest_update, bp_get_user_meta( $u1, 'bp_latest_update', true ) );
1858  
1859          wp_delete_user( $u1 );
1860  
1861          $found = bp_activity_get(
1862              array(
1863                  'filter' => array(
1864                      'user_id' => $u1,
1865                  )
1866              )
1867          );
1868  
1869          $this->assertEmpty( $found['activities'] );
1870          $this->assertEmpty( bp_activity_get_user_favorites( $u1 ) );
1871          $this->assertSame( '', bp_get_user_meta( $u1, 'bp_latest_update', true ) );
1872      }
1873  
1874      /**
1875       * @ticket BP8175
1876       */
1877      public function test_activity_data_should_be_deleted_on_user_delete_multisite() {
1878          if ( ! is_multisite() ) {
1879              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
1880          }
1881  
1882          $u1 = self::factory()->user->create();
1883          $a1 = self::factory()->activity->create(
1884              array(
1885                  'user_id'   => $u1,
1886                  'component' => 'activity',
1887                  'type'      => 'activity_update',
1888              )
1889          );
1890  
1891          $latest_update = array(
1892              'id'      => $a1,
1893              'content' => 'Foo',
1894          );
1895          bp_update_user_meta( $u1, 'bp_latest_update', $latest_update );
1896  
1897          bp_activity_add_user_favorite( $a1, $u1 );
1898  
1899          $found = bp_activity_get(
1900              array(
1901                  'filter' => array(
1902                      'user_id' => $u1,
1903                  )
1904              )
1905          );
1906  
1907          $this->assertEqualSets( [ $a1 ], wp_list_pluck( $found['activities'], 'id' ) );
1908          $this->assertEqualSets( [ $a1 ], bp_activity_get_user_favorites( $u1 ) );
1909          $this->assertSame( $latest_update, bp_get_user_meta( $u1, 'bp_latest_update', true ) );
1910  
1911          wpmu_delete_user( $u1 );
1912  
1913          $found = bp_activity_get(
1914              array(
1915                  'filter' => array(
1916                      'user_id' => $u1,
1917                  )
1918              )
1919          );
1920  
1921          $this->assertEmpty( $found['activities'] );
1922          $this->assertEmpty( bp_activity_get_user_favorites( $u1 ) );
1923          $this->assertSame( '', bp_get_user_meta( $u1, 'bp_latest_update', true ) );
1924      }
1925  
1926      /**
1927       * @ticket BP8175
1928       */
1929      public function test_activity_data_should_not_be_deleted_on_wp_delete_user_multisite() {
1930          if ( ! is_multisite() ) {
1931              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
1932          }
1933  
1934          $u1 = self::factory()->user->create();
1935          $a1 = self::factory()->activity->create(
1936              array(
1937                  'user_id'   => $u1,
1938                  'component' => 'activity',
1939                  'type'      => 'activity_update',
1940              )
1941          );
1942  
1943          $latest_update = array(
1944              'id'      => $a1,
1945              'content' => 'Foo',
1946          );
1947          bp_update_user_meta( $u1, 'bp_latest_update', $latest_update );
1948  
1949          bp_activity_add_user_favorite( $a1, $u1 );
1950  
1951          $found = bp_activity_get(
1952              array(
1953                  'filter' => array(
1954                      'user_id' => $u1,
1955                  )
1956              )
1957          );
1958  
1959          $this->assertEqualSets( [ $a1 ], wp_list_pluck( $found['activities'], 'id' ) );
1960          $this->assertEqualSets( [ $a1 ], bp_activity_get_user_favorites( $u1 ) );
1961          $this->assertSame( $latest_update, bp_get_user_meta( $u1, 'bp_latest_update', true ) );
1962  
1963          wp_delete_user( $u1 );
1964  
1965          $found = bp_activity_get(
1966              array(
1967                  'filter' => array(
1968                      'user_id' => $u1,
1969                  )
1970              )
1971          );
1972  
1973          $this->assertEqualSets( [ $a1 ], wp_list_pluck( $found['activities'], 'id' ) );
1974          $this->assertEqualSets( [ $a1 ], bp_activity_get_user_favorites( $u1 ) );
1975          $this->assertSame( $latest_update, bp_get_user_meta( $u1, 'bp_latest_update', true ) );
1976      }
1977  
1978      /**
1979       * @ticket BP8591
1980       */
1981  	public function test_activity_admin_screen_count_activities() {
1982          $u1 = self::factory()->user->create();
1983          $a1 = self::factory()->activity->create_many(
1984              5,
1985              array(
1986                  'user_id'   => $u1,
1987                  'component' => 'activity',
1988                  'type'      => 'activity_update',
1989              )
1990          );
1991          bp_update_user_last_activity( $u1, date( 'Y-m-d H:i:s', bp_core_current_time( true, 'timestamp' ) ) );
1992  
1993          $count_activities = bp_activity_get(
1994              array(
1995                  'show_hidden'      => true,
1996                  'count_total_only' => true,
1997              )
1998          );
1999  
2000          $this->assertTrue( 5 === (int) $count_activities['total'] );
2001      }
2002  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1