[ 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_custom_post_type_post
 759       * @group activity_tracking
 760       */
 761      public function test_bp_activity_format_activity_action_custom_post_type_post_nonms() {
 762          if ( is_multisite() ) {
 763              $this->markTestSkipped();
 764          }
 765  
 766          register_post_type( 'foo', array(
 767              'label'   => 'foo',
 768              'public'   => true,
 769              'supports' => array( 'buddypress-activity' ),
 770          ) );
 771  
 772          // Build the actions to fetch the tracking args
 773          bp_activity_get_actions();
 774  
 775          $u = self::factory()->user->create();
 776          $p = self::factory()->post->create( array(
 777              'post_author' => $u,
 778              'post_type'   => 'foo',
 779          ) );
 780  
 781          $a = self::factory()->activity->create( array(
 782              'component'         => 'activity',
 783              'type'              => 'new_foo',
 784              'user_id'           => $u,
 785              'item_id'           => 1,
 786              'secondary_item_id' => $p,
 787          ) );
 788  
 789          $a_obj = new BP_Activity_Activity( $a );
 790  
 791          $user_link = bp_core_get_userlink( $u );
 792          $blog_url = get_home_url();
 793          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 794          $post_link = '<a href="' . $post_url . '">item</a>';
 795  
 796          $expected = sprintf( '%s wrote a new %s', $user_link, $post_link );
 797  
 798          $this->assertSame( $expected, $a_obj->action );
 799  
 800          _unregister_post_type( 'foo' );
 801      }
 802  
 803      /**
 804       * @group activity_action
 805       * @group bp_activity_format_activity_action_custom_post_type_post_ms
 806       * @group activity_tracking
 807       */
 808      public function test_bp_activity_format_activity_action_custom_post_type_post_ms() {
 809          if ( ! is_multisite() ) {
 810              $this->markTestSkipped();
 811          }
 812  
 813          if ( function_exists( 'wp_initialize_site' ) ) {
 814              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 815          }
 816  
 817          $b = self::factory()->blog->create();
 818          $u = self::factory()->user->create();
 819  
 820          switch_to_blog( $b );
 821  
 822          register_post_type( 'foo', array(
 823              'label'   => 'foo',
 824              'public'   => true,
 825              'supports' => array( 'buddypress-activity' ),
 826          ) );
 827  
 828          // Build the actions to fetch the tracking args
 829          bp_activity_get_actions();
 830  
 831          $p = self::factory()->post->create( array(
 832              'post_author' => $u,
 833              'post_type'   => 'foo',
 834          ) );
 835  
 836          $activity_args = array(
 837              'component'         => 'activity',
 838              'type'              => 'new_foo',
 839              'user_id'           => $u,
 840              'item_id'           => $b,
 841              'secondary_item_id' => $p,
 842          );
 843  
 844          _unregister_post_type( 'foo' );
 845          bp_activity_get_actions();
 846  
 847          restore_current_blog();
 848  
 849          $a = self::factory()->activity->create( $activity_args );
 850  
 851          $a_obj = new BP_Activity_Activity( $a );
 852  
 853          $user_link = bp_core_get_userlink( $u );
 854          $blog_url = get_blog_option( $a_obj->item_id, 'home' );
 855          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 856  
 857          $post_link = '<a href="' . $post_url . '">item</a>';
 858  
 859          $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>' );
 860  
 861          $this->assertSame( $expected, $a_obj->action );
 862      }
 863  
 864  
 865      /**
 866       * @group activity_action
 867       * @group bp_activity_format_activity_action_custom_post_type_post
 868       */
 869      public function test_bp_activity_format_activity_action_custom_string_post_type_post_nonms() {
 870          if ( is_multisite() ) {
 871              $this->markTestSkipped();
 872          }
 873  
 874          $labels = array(
 875              'name'                 => 'bars',
 876              'singular_name'        => 'bar',
 877              'bp_activity_new_post' => '%1$s shared a new <a href="%2$s">bar</a>',
 878          );
 879  
 880          register_post_type( 'foo', array(
 881              'labels'      => $labels,
 882              'public'      => true,
 883              'supports'    => array( 'buddypress-activity' ),
 884              'bp_activity' => array(
 885                  'action_id' => 'foo_bar',
 886              ),
 887          ) );
 888  
 889          // Build the actions to fetch the tracking args
 890          bp_activity_get_actions();
 891  
 892          $u = self::factory()->user->create();
 893          $p = self::factory()->post->create( array(
 894              'post_author' => $u,
 895              'post_type'   => 'foo',
 896          ) );
 897  
 898          $a = self::factory()->activity->create( array(
 899              'component'         => 'activity',
 900              'type'              => 'foo_bar',
 901              'user_id'           => $u,
 902              'item_id'           => 1,
 903              'secondary_item_id' => $p,
 904          ) );
 905  
 906          $a_obj = new BP_Activity_Activity( $a );
 907  
 908          $user_link = bp_core_get_userlink( $u );
 909          $blog_url = get_home_url();
 910          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 911  
 912          $expected = sprintf( '%1$s shared a new <a href="%2$s">bar</a>', $user_link, $post_url );
 913  
 914          $this->assertSame( $expected, $a_obj->action );
 915  
 916          _unregister_post_type( 'foo' );
 917      }
 918  
 919      /**
 920       * @group activity_action
 921       * @group bp_activity_format_activity_action_custom_post_type_post_ms
 922       * @group activity_tracking
 923       */
 924      public function test_bp_activity_format_activity_action_custom_string_post_type_post_ms() {
 925          if ( ! is_multisite() ) {
 926              $this->markTestSkipped();
 927          }
 928  
 929          if ( function_exists( 'wp_initialize_site' ) ) {
 930              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 931          }
 932  
 933          $b = self::factory()->blog->create();
 934          $u = self::factory()->user->create();
 935  
 936          switch_to_blog( $b );
 937  
 938          $labels = array(
 939              'name'                    => 'bars',
 940              'singular_name'           => 'bar',
 941              'bp_activity_new_post_ms' => '%1$s shared a new <a href="%2$s">bar</a>, on the site %3$s',
 942          );
 943  
 944          register_post_type( 'foo', array(
 945              'labels'   => $labels,
 946              'public'   => true,
 947              'supports' => array( 'buddypress-activity' ),
 948          ) );
 949  
 950          // Build the actions to fetch the tracking args
 951          bp_activity_get_actions();
 952  
 953          $p = self::factory()->post->create( array(
 954              'post_author' => $u,
 955              'post_type'   => 'foo',
 956          ) );
 957  
 958          $activity_args = array(
 959              'component'         => 'activity',
 960              'type'              => 'new_foo',
 961              'user_id'           => $u,
 962              'item_id'           => $b,
 963              'secondary_item_id' => $p,
 964          );
 965  
 966          _unregister_post_type( 'foo' );
 967  
 968          restore_current_blog();
 969  
 970          $a = self::factory()->activity->create( $activity_args );
 971  
 972          $a_obj = new BP_Activity_Activity( $a );
 973  
 974          $user_link = bp_core_get_userlink( $u );
 975          $blog_url = get_blog_option( $a_obj->item_id, 'home' );
 976          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 977  
 978          $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>' );
 979  
 980          $this->assertSame( $expected, $a_obj->action );
 981      }
 982  
 983      /**
 984       * @group bp_activity_set_post_type_tracking_args
 985       * @group activity_tracking
 986       */
 987  	public function test_bp_activity_set_post_type_tracking_args() {
 988          $bp = buddypress();
 989  
 990          add_post_type_support( 'page', 'buddypress-activity' );
 991  
 992          bp_activity_set_post_type_tracking_args( 'page', array(
 993              'component_id' => $bp->blogs->id,
 994              'dummy'        => 'dummy value',
 995          ) );
 996  
 997          // Build the actions to fetch the tracking args
 998          bp_activity_get_actions();
 999  
1000          $u = self::factory()->user->create();
1001  
1002          $post_id = self::factory()->post->create( array(
1003              'post_author' => $u,
1004              'post_status' => 'publish',
1005              'post_type'   => 'page',
1006          ) );
1007  
1008          $a = bp_activity_get( array(
1009              'action'            => 'new_page',
1010              'item_id'           => get_current_blog_id(),
1011              'secondary_item_id' => $post_id,
1012          ) );
1013  
1014          $this->assertSame( $bp->blogs->id, $a['activities'][0]->component );
1015  
1016          remove_post_type_support( 'page', 'buddypress-activity' );
1017      }
1018  
1019      /**
1020       * @group bp_activity_set_post_type_tracking_args
1021       * @group activity_tracking
1022       */
1023      public function test_bp_activity_set_post_type_tracking_args_check_post_type_global() {
1024          $labels = array(
1025              'bp_activity_admin_filter' => 'New Foo',
1026              'bp_activity_front_filter' => 'Foos',
1027          );
1028  
1029          $bp_activity_args = array(
1030              'action_id'    => 'new_foo',
1031              'contexts'     => array( 'activity' ),
1032              'position'     => 40,
1033          );
1034  
1035          register_post_type( 'foo', array(
1036              'labels'      => $labels,
1037              'supports'    => array( 'buddypress-activity' ),
1038              'bp_activity' => $bp_activity_args
1039          ) );
1040  
1041          $register_bp_activity = get_post_type_object( 'foo' )->bp_activity;
1042          _unregister_post_type( 'foo' );
1043  
1044          register_post_type( 'foo', array(
1045              'label'       => 'foo',
1046              'supports'    => array( 'buddypress-activity' ),
1047          ) );
1048  
1049          bp_activity_set_post_type_tracking_args( 'foo', $labels + $bp_activity_args );
1050  
1051          $set_bp_activity = get_post_type_object( 'foo' )->bp_activity;
1052          _unregister_post_type( 'foo' );
1053  
1054          $this->assertSame( $set_bp_activity, $register_bp_activity );
1055      }
1056  
1057      /**
1058       * @group activity_action
1059       * @group bp_activity_format_activity_action_custom_post_type_post_ms
1060       * @group post_type_comment_activities
1061       */
1062      public function test_bp_activity_format_activity_action_custom_post_type_comment() {
1063          if ( is_multisite() ) {
1064              if ( function_exists( 'wp_initialize_site' ) ) {
1065                  $this->setExpectedDeprecated( 'wpmu_new_blog' );
1066              }
1067  
1068              $b = self::factory()->blog->create();
1069              switch_to_blog( $b );
1070              add_filter( 'comment_flood_filter', '__return_false' );
1071          } else {
1072              $b = get_current_blog_id();
1073          }
1074  
1075          $u = self::factory()->user->create();
1076          $userdata = get_userdata( $u );
1077  
1078          $labels = array(
1079              'name'                       => 'bars',
1080              'singular_name'              => 'bar',
1081              'bp_activity_new_comment'    => __( '%1$s commented on the <a href="%2$s">bar</a>', 'buddypress' ),
1082              'bp_activity_new_comment_ms' => __( '%1$s commented on the <a href="%2$s">bar</a>, on the site %3$s', 'buddypress' ),
1083          );
1084  
1085          register_post_type( 'foo', array(
1086              'labels'   => $labels,
1087              'public'   => true,
1088              'supports' => array( 'buddypress-activity', 'comments' ),
1089              'bp_activity' => array(
1090                  'action_id'         => 'new_bar',
1091                  'comment_action_id' => 'new_bar_comment',
1092              ),
1093          ) );
1094  
1095          // Build the actions to fetch the tracking args
1096          bp_activity_get_actions();
1097  
1098          $p = self::factory()->post->create( array(
1099              'post_author' => $u,
1100              'post_type'   => 'foo',
1101          ) );
1102  
1103          $c = wp_new_comment( array(
1104              'comment_post_ID'      => $p,
1105              'comment_author'       => $userdata->user_nicename,
1106              'comment_author_url'   => 'http://buddypress.org',
1107              'comment_author_email' => $userdata->user_email,
1108              'comment_content'      => 'this is a blog comment',
1109              'comment_type'         => '',
1110              'comment_parent'       => 0,
1111              'user_id'              => $u,
1112          ) );
1113  
1114          $a = bp_activity_get_activity_id( array( 'type' => 'new_bar_comment' ) );
1115  
1116          $a_obj = new BP_Activity_Activity( $a );
1117  
1118          $user_link    = bp_core_get_userlink( $u );
1119          $comment_url  = get_comment_link( $c );
1120  
1121          _unregister_post_type( 'foo' );
1122  
1123          if ( is_multisite() ) {
1124              $blog_url  = get_blog_option( $a_obj->item_id, 'home' );
1125              restore_current_blog();
1126              remove_filter( 'comment_flood_filter', '__return_false' );
1127  
1128              $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>' );
1129          } else {
1130              $expected = sprintf( $labels['bp_activity_new_comment'], $user_link, $comment_url );
1131          }
1132  
1133          $this->assertSame( $expected, $a_obj->action );
1134      }
1135  
1136      /**
1137       * @group bp_activity_new_comment
1138       * @group cache
1139       */
1140      public function test_bp_activity_new_comment_clear_comment_caches() {
1141          $u = self::factory()->user->create();
1142          $a1 = self::factory()->activity->create( array(
1143              'user_id' => $u,
1144          ) );
1145          $a2 = bp_activity_new_comment( array(
1146              'activity_id' => $a1,
1147              'parent_id' => $a1,
1148              'content' => 'foo',
1149              'user_id' => $u,
1150          ) );
1151          $a3 = bp_activity_new_comment( array(
1152              'activity_id' => $a1,
1153              'parent_id' => $a2,
1154              'content' => 'foo',
1155              'user_id' => $u,
1156          ) );
1157          $a4 = bp_activity_new_comment( array(
1158              'activity_id' => $a1,
1159              'parent_id' => $a3,
1160              'content' => 'foo',
1161              'user_id' => $u,
1162          ) );
1163          $a5 = bp_activity_new_comment( array(
1164              'activity_id' => $a1,
1165              'parent_id' => $a3,
1166              'content' => 'foo',
1167              'user_id' => $u,
1168          ) );
1169  
1170          // prime caches
1171          bp_activity_get( array(
1172              'in' => array( $a1 ),
1173              'display_comments' => 'threaded',
1174          ) );
1175  
1176          // should be populated
1177          $this->assertNotEmpty( wp_cache_get( $a1, 'bp_activity_comments' ) );
1178  
1179          bp_activity_new_comment( array(
1180              'activity_id' => $a1,
1181              'parent_id' => $a4,
1182              'content' => 'foo',
1183              'user_id' => $u,
1184          ) );
1185  
1186          // should be empty
1187          $this->assertFalse( wp_cache_get( $a1, 'bp_activity_comments' ) );
1188      }
1189  
1190      /**
1191       * @group bp_activity_new_comment
1192       * @group cache
1193       */
1194      public function test_bp_activity_new_comment_clear_activity_caches() {
1195          $u = self::factory()->user->create();
1196          $a1 = self::factory()->activity->create( array(
1197              'user_id' => $u,
1198          ) );
1199          $a2 = bp_activity_new_comment( array(
1200              'activity_id' => $a1,
1201              'parent_id' => $a1,
1202              'content' => 'foo',
1203              'user_id' => $u,
1204          ) );
1205          $a3 = bp_activity_new_comment( array(
1206              'activity_id' => $a1,
1207              'parent_id' => $a2,
1208              'content' => 'foo',
1209              'user_id' => $u,
1210          ) );
1211          $a4 = bp_activity_new_comment( array(
1212              'activity_id' => $a1,
1213              'parent_id' => $a3,
1214              'content' => 'foo',
1215              'user_id' => $u,
1216          ) );
1217          $a5 = bp_activity_new_comment( array(
1218              'activity_id' => $a1,
1219              'parent_id' => $a3,
1220              'content' => 'foo',
1221              'user_id' => $u,
1222          ) );
1223  
1224          // prime caches
1225          bp_activity_get( array(
1226              'in' => array( $a1 ),
1227              'display_comments' => 'threaded',
1228          ) );
1229  
1230          // should be populated
1231          $this->assertNotEmpty( wp_cache_get( $a1, 'bp_activity' ) );
1232          $this->assertNotEmpty( wp_cache_get( $a2, 'bp_activity' ) );
1233          $this->assertNotEmpty( wp_cache_get( $a3, 'bp_activity' ) );
1234          $this->assertNotEmpty( wp_cache_get( $a4, 'bp_activity' ) );
1235          $this->assertNotEmpty( wp_cache_get( $a5, 'bp_activity' ) );
1236  
1237          // Stuff may run on bp_activity_comment_posted that loads the
1238          // cache, so we use this dumb technique to check cache values
1239          // before any of that stuff gets a chance to run. WordPress
1240          // sure is neat sometimes
1241          $this->acaches = array(
1242              $a1 => '',
1243              $a2 => '',
1244              $a3 => '',
1245              $a4 => '',
1246          );
1247          add_action( 'bp_activity_comment_posted', array( $this, 'check_activity_caches' ), 0 );
1248  
1249          bp_activity_new_comment( array(
1250              'activity_id' => $a1,
1251              'parent_id' => $a4,
1252              'content' => 'foo',
1253              'user_id' => $u,
1254          ) );
1255  
1256          // should be empty
1257          foreach ( $this->acaches as $k => $v ) {
1258              $this->assertFalse( $v, "Cache should be false for $k" );
1259          }
1260      }
1261  
1262      /**
1263       * @group bp_activity_delete_comment
1264       * @group cache
1265       */
1266  	public function test_bp_activity_delete_comment_clear_cache() {
1267          $u = self::factory()->user->create();
1268          // add new activity update and comment to this update
1269          $a1 = self::factory()->activity->create( array(
1270              'user_id' => $u,
1271          ) );
1272          $a2 = bp_activity_new_comment( array(
1273              'activity_id' => $a1,
1274              'parent_id' => $a1,
1275              'content' => 'foo',
1276              'user_id' => $u,
1277          ) );
1278  
1279          // prime cache
1280          bp_activity_get( array(
1281              'in' => array( $a1 ),
1282              'display_comments' => 'threaded',
1283          ) );
1284  
1285          // delete activity comment
1286          bp_activity_delete_comment( $a1, $a2 );
1287  
1288          // assert comment cache as empty for $a1
1289          $this->assertEmpty( wp_cache_get( $a1, 'bp_activity_comments' ) );
1290      }
1291  
1292      /**
1293       * @group  bp_activity_delete_comment
1294       * @ticket BP7450
1295       */
1296      public function test_bp_activity_delete_comment_shouldnt_delete_all_comments_when_parameters_are_empty() {
1297          $u = self::factory()->user->create();
1298  
1299          // create an activity update
1300          $parent_activity = self::factory()->activity->create( array(
1301              'type'    => 'activity_update',
1302              'user_id' => $u
1303          ) );
1304  
1305          // create some activity comments
1306          $comment_one = bp_activity_new_comment( array(
1307              'user_id'     => $u,
1308              'activity_id' => $parent_activity,
1309              'content'     => 'depth 1'
1310          ) );
1311  
1312          $comment_one_one = bp_activity_new_comment( array(
1313              'user_id'     => $u,
1314              'activity_id' => $parent_activity,
1315              'parent_id'   => $comment_one,
1316              'content'     => 'depth 2'
1317          ) );
1318  
1319          $comment_two = bp_activity_new_comment( array(
1320              'user_id'     => $u,
1321              'activity_id' => $parent_activity,
1322              'content'     => 'depth 1'
1323          ) );
1324  
1325          // Pass empty values to bp_activity_delete_comment()
1326          $retval = bp_activity_delete_comment( 0, 0 );
1327          $this->assertFalse( $retval );
1328  
1329          // Instantiate activity loop, which also includes activity comments.
1330          bp_has_activities( 'display_comments=stream' );
1331  
1332          // Activity comments should not be deleted.
1333          $this->assertSame( 4, $GLOBALS['activities_template']->activity_count );
1334  
1335          // Clean up after ourselves!
1336          $GLOBALS['activities_template'] = null;
1337      }
1338  
1339      /**
1340       * @group bp_activity_new_comment
1341       * @group BP5907
1342       */
1343  	public function test_bp_activity_comment_on_deleted_activity() {
1344          $u = self::factory()->user->create();
1345          $a = self::factory()->activity->create();
1346  
1347          bp_activity_delete_by_activity_id( $a );
1348  
1349          $c = bp_activity_new_comment( array(
1350              'activity_id' => $a,
1351              'parent_id' => $a,
1352              'content' => 'foo',
1353              'user_id' => $u,
1354          ) );
1355  
1356          $this->assertEmpty( $c );
1357      }
1358  
1359      /**
1360       * @group favorites
1361       * @group bp_activity_add_user_favorite
1362       */
1363  	public function test_add_user_favorite_already_favorited() {
1364          $u = self::factory()->user->create();
1365          $a = self::factory()->activity->create();
1366  
1367          // bp_activity_add_user_favorite() requires a logged-in user.
1368          $current_user = bp_loggedin_user_id();
1369          $this->set_current_user( $u );
1370  
1371          $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
1372  
1373          $this->assertFalse( bp_activity_add_user_favorite( $a, $u ) );
1374          $this->assertSame( array( $a ), bp_activity_get_user_favorites( $u ) );
1375          $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
1376  
1377          $this->set_current_user( $current_user );
1378      }
1379  
1380      /**
1381       * @group favorites
1382       * @group bp_activity_add_user_favorite
1383       */
1384  	public function test_add_user_favorite_not_yet_favorited() {
1385          $u = self::factory()->user->create();
1386          $a = self::factory()->activity->create();
1387  
1388          // bp_activity_add_user_favorite() requires a logged-in user.
1389          $current_user = bp_loggedin_user_id();
1390          $this->set_current_user( $u );
1391          $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
1392  
1393          $this->set_current_user( $current_user );
1394      }
1395  
1396      /**
1397       * @group favorites
1398       * @group bp_activity_remove_user_favorite
1399       */
1400  	public function test_remove_user_favorite_bad_activity_id() {
1401          $u1 = self::factory()->user->create();
1402          $u2 = self::factory()->user->create();
1403          $a = self::factory()->activity->create();
1404  
1405          // bp_activity_add_user_favorite() requires a logged-in user.
1406          $current_user = bp_loggedin_user_id();
1407          $this->set_current_user( $u1 );
1408  
1409          // Only favorite for user 1
1410          bp_activity_add_user_favorite( $a, $u1 );
1411  
1412          // Removing for user 2 should fail
1413          $this->assertFalse( bp_activity_remove_user_favorite( $a, $u2 ) );
1414          $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
1415  
1416          $this->set_current_user( $current_user );
1417      }
1418  
1419      /**
1420       * @group bp_activity_post_update
1421       */
1422  	public function test_bp_activity_post_update_empty_content() {
1423          $this->assertFalse( bp_activity_post_update( array( 'user_id' => 3, ) ) );
1424      }
1425  
1426      /**
1427       * @group bp_activity_post_update
1428       */
1429  	public function test_bp_activity_post_update_inactive_user() {
1430          $this->assertFalse( bp_activity_post_update( array(
1431              'user_id' => 3456,
1432              'content' => 'foo',
1433          ) ) );
1434      }
1435  
1436      /**
1437       * @group bp_activity_post_update
1438       */
1439  	public function test_bp_activity_post_update_success() {
1440          $u = self::factory()->user->create();
1441  
1442          $a = bp_activity_post_update( array(
1443              'user_id' => $u,
1444              'content' => 'foo',
1445          ) );
1446  
1447          $this->assertNotEmpty( $a );
1448      }
1449  
1450      /**
1451       * @group bp_activity_get_activity_id
1452       */
1453  	public function test_bp_activity_get_activity_id() {
1454          $args = array(
1455              'user_id' => 5,
1456              'component' => 'foo',
1457              'type' => 'bar',
1458              'item_id' => 12,
1459              'secondary_item_id' => 44,
1460          );
1461  
1462          $a = self::factory()->activity->create( $args );
1463  
1464          $this->assertEquals( $a, bp_activity_get_activity_id( $args ) );
1465      }
1466  
1467      /**
1468       * @group bp_activity_delete_by_item_id
1469       */
1470  	public function test_bp_activity_delete_by_item_id() {
1471          $args = array(
1472              'user_id' => 5,
1473              'component' => 'foo',
1474              'type' => 'bar',
1475              'item_id' => 12,
1476              'secondary_item_id' => 44,
1477          );
1478  
1479          $a = self::factory()->activity->create( $args );
1480  
1481          $this->assertTrue( bp_activity_delete_by_item_id( $args ) );
1482  
1483          $found = bp_activity_get_specific( array(
1484              'activity_ids' => array( $a ),
1485          ) );
1486  
1487          $this->assertSame( array(), $found['activities'] );
1488      }
1489  
1490      /**
1491       * @group bp_activity_user_can_read
1492       */
1493  	public function test_user_can_access_their_own_activity() {
1494          $u = self::factory()->user->create();
1495  
1496          $a = self::factory()->activity->create( array(
1497              'user_id' => $u,
1498          ) );
1499  
1500          $o = self::factory()->activity->get_object_by_id( $a );
1501  
1502          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1503      }
1504  
1505      /**
1506       * @group bp_activity_user_can_read
1507       */
1508  	public function test_admin_can_access_someone_elses_activity() {
1509          $u = self::factory()->user->create();
1510          $u2 = self::factory()->user->create( array( 'role' => 'administrator' ) );
1511  
1512          $a = self::factory()->activity->create( array(
1513              'user_id' => $u,
1514          ) );
1515  
1516          $o = self::factory()->activity->get_object_by_id( $a );
1517  
1518          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1519  
1520          $this->set_current_user( $u2 );
1521          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1522      }
1523  
1524      /**
1525       * @group bp_activity_user_can_read
1526       */
1527  	public function test_user_cannot_access_spam_activity() {
1528          $u = self::factory()->user->create();
1529          $u2 = self::factory()->user->create();
1530  
1531          $a = self::factory()->activity->create( array(
1532              'user_id' => $u,
1533          ) );
1534  
1535          $o = self::factory()->activity->get_object_by_id( $a );
1536  
1537          bp_activity_mark_as_spam( $o );
1538  
1539          $this->assertFalse( bp_activity_user_can_read( $o, $u ) );
1540          $this->assertFalse( bp_activity_user_can_read( $o, $u2 ) );
1541      }
1542  
1543      /**
1544       * @group bp_activity_user_can_read
1545       */
1546  	public function test_admin_can_access_spam_activity() {
1547          $u = self::factory()->user->create();
1548          $u2 = self::factory()->user->create( array( 'role' => 'administrator' ) );
1549  
1550          $a = self::factory()->activity->create( array(
1551              'user_id' => $u,
1552          ) );
1553  
1554          $o = self::factory()->activity->get_object_by_id( $a );
1555  
1556          bp_activity_mark_as_spam( $o );
1557  
1558          $this->set_current_user( $u2 );
1559          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1560      }
1561  
1562      /**
1563       * @group bp_activity_user_can_read
1564       */
1565      public function test_group_admin_access_someone_elses_activity_in_a_group() {
1566          $u  = self::factory()->user->create();
1567          $u2 = self::factory()->user->create();
1568  
1569          $g  = self::factory()->group->create();
1570  
1571          $a = self::factory()->activity->create( array(
1572              'component' => buddypress()->groups->id,
1573              'user_id'   => $u,
1574              'item_id'   => $g,
1575          ) );
1576  
1577          $o = self::factory()->activity->get_object_by_id( $a );
1578  
1579          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1580  
1581          self::add_user_to_group( $u2, $g );
1582  
1583          $m1 = new BP_Groups_Member( $u2, $g );
1584          $m1->promote( 'admin' );
1585  
1586          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1587      }
1588  
1589      /**
1590       * @group bp_activity_user_can_read
1591       */
1592      public function test_non_member_can_access_to_someone_elses_activity_in_a_group() {
1593          $u  = self::factory()->user->create();
1594          $u2 = self::factory()->user->create();
1595  
1596          $g  = self::factory()->group->create();
1597  
1598          self::add_user_to_group( $u, $g );
1599  
1600          $a = self::factory()->activity->create( array(
1601              'component' => buddypress()->groups->id,
1602              'user_id'   => $u,
1603              'item_id'   => $g,
1604          ) );
1605  
1606          $o = self::factory()->activity->get_object_by_id( $a );
1607  
1608          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1609          $this->assertTrue( bp_activity_user_can_read( $o, $u2 ) );
1610      }
1611  
1612      /**
1613       * @group bp_activity_user_can_read
1614       */
1615      public function test_user_access_to_his_activity_in_hidden_group() {
1616          $u  = self::factory()->user->create();
1617          $g  = self::factory()->group->create( array(
1618              'status' => 'hidden',
1619          ) );
1620  
1621          self::add_user_to_group( $u, $g );
1622  
1623          $a = self::factory()->activity->create( array(
1624              'component' => buddypress()->groups->id,
1625              'user_id'   => $u,
1626              'item_id'   => $g,
1627          ) );
1628  
1629          $o = self::factory()->activity->get_object_by_id( $a );
1630  
1631          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1632      }
1633  
1634      /**
1635       * @group bp_activity_user_can_read
1636       */
1637      public function test_user_access_to_his_activity_in_private_group() {
1638          $u  = self::factory()->user->create();
1639          $g  = self::factory()->group->create( array(
1640              'status' => 'private',
1641          ) );
1642  
1643          self::add_user_to_group( $u, $g );
1644  
1645          $a = self::factory()->activity->create( array(
1646              'component' => buddypress()->groups->id,
1647              'user_id'   => $u,
1648              'item_id'   => $g,
1649          ) );
1650  
1651          $o = self::factory()->activity->get_object_by_id( $a );
1652  
1653          $this->assertTrue( bp_activity_user_can_read( $o, $u ) );
1654      }
1655  
1656      /**
1657       * @group bp_activity_user_can_read
1658       */
1659      public function test_banned_user_cannot_access_to_his_activity_in_a_private_group() {
1660          $u  = self::factory()->user->create();
1661          $g  = self::factory()->group->create( array(
1662              'status' => 'private',
1663          ) );
1664  
1665          self::add_user_to_group( $u, $g );
1666  
1667          $a = self::factory()->activity->create( array(
1668              'component' => buddypress()->groups->id,
1669              'user_id'   => $u,
1670              'item_id'   => $g,
1671          ) );
1672  
1673          buddypress()->is_item_admin = true;
1674          groups_ban_member( $u, $g );
1675  
1676          $o = self::factory()->activity->get_object_by_id( $a );
1677  
1678          $this->assertFalse( bp_activity_user_can_read( $o, $u ) );
1679      }
1680  
1681      /**
1682       * @group bp_activity_user_can_read
1683       */
1684      public function test_removed_member_cannot_access_to_his_activity_in_a_private_group() {
1685          $u  = self::factory()->user->create();
1686          $g  = self::factory()->group->create( array(
1687              'status' => 'private',
1688          ) );
1689  
1690          self::add_user_to_group( $u, $g );
1691  
1692          $a = self::factory()->activity->create( array(
1693              'component' => buddypress()->groups->id,
1694              'user_id'   => $u,
1695              'item_id'   => $g,
1696          ) );
1697  
1698          buddypress()->is_item_admin = true;
1699          groups_remove_member( $u, $g );
1700  
1701          $o = self::factory()->activity->get_object_by_id( $a );
1702  
1703          $this->assertFalse( bp_activity_user_can_read( $o, $u ) );
1704      }
1705  
1706  	public function check_activity_caches() {
1707          foreach ( $this->acaches as $k => $v ) {
1708              $this->acaches[ $k ] = wp_cache_get( $k, 'bp_activity' );
1709          }
1710      }
1711  
1712      /**
1713       * @ticket BP7818
1714       * @ticket BP7698
1715       */
1716  	public function test_bp_activity_personal_data_exporter() {
1717          $u = self::factory()->user->create();
1718  
1719          $a1 = self::factory()->activity->create(
1720              array(
1721                  'user_id'   => $u,
1722                  'component' => 'activity',
1723                  'type'      => 'activity_update',
1724              )
1725          );
1726  
1727          $a2 = self::factory()->activity->create(
1728              array(
1729                  'user_id'           => $u,
1730                  'component'         => 'activity',
1731                  'type'              => 'activity_comment',
1732                  'item_id'           => $a1,
1733                  'secondary_item_id' => $a1,
1734              )
1735          );
1736  
1737          $g = self::factory()->group->create(
1738              array(
1739                  'name' => 'My Cool Group',
1740              )
1741          );
1742  
1743          $a3 = self::factory()->activity->create(
1744              array(
1745                  'user_id'   => $u,
1746                  'component' => 'groups',
1747                  'type'      => 'activity_update',
1748                  'item_id'   => $g,
1749              )
1750          );
1751  
1752          $test_user = new WP_User( $u );
1753  
1754          $actual = bp_activity_personal_data_exporter( $test_user->user_email, 1 );
1755  
1756          $this->assertTrue( $actual['done'] );
1757  
1758          // Number of exported activity items.
1759          $this->assertSame( 3, count( $actual['data'] ) );
1760      }
1761  }


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1