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


Generated: Thu Sep 19 01:01:39 2019 Cross-referenced by PHPXref 0.7.1