[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group blogs
   5   * @group activity
   6   */
   7  class BP_Tests_Blogs_Activity extends BP_UnitTestCase {
   8      /**
   9       * @group bp_blogs_register_activity_actions
  10       * @group activity_tracking
  11       */
  12      public function test_bp_blogs_loader_post_tracking_args_filter() {
  13          $bp = buddypress();
  14  
  15          $expected = array( 'new_blog_post', 'new_blog_comment' );
  16  
  17          if ( is_multisite() ) {
  18              $expected = array_merge( array( 'new_blog' ), $expected );
  19          }
  20  
  21          $actions = bp_activity_get_actions();
  22          $actions = array_keys( (array) $actions->blogs );
  23  
  24          $this->assertEquals( $expected, $actions );
  25      }
  26  
  27      /**
  28       * @group activity_action
  29       * @group bp_blogs_format_activity_action_new_blog
  30       */
  31  	public function test_bp_blogs_format_activity_action_new_blog() {
  32          if ( ! is_multisite() ) {
  33              $this->markTestSkipped();
  34          }
  35  
  36          if ( function_exists( 'wp_initialize_site' ) ) {
  37              $this->setExpectedDeprecated( 'wpmu_new_blog' );
  38          }
  39  
  40          $b = self::factory()->blog->create();
  41          $u = self::factory()->user->create();
  42          $a = self::factory()->activity->create( array(
  43              'component' => buddypress()->blogs->id,
  44              'type' => 'new_blog',
  45              'user_id' => $u,
  46              'item_id' => $b,
  47          ) );
  48  
  49          $a_obj = new BP_Activity_Activity( $a );
  50  
  51          $expected = sprintf( '%s created the site %s', bp_core_get_userlink( $u ), '<a href="' . get_blog_option( $b, 'home' ) . '">' . get_blog_option( $b, 'blogname' ) . '</a>' );
  52  
  53          $this->assertSame( $expected, $a_obj->action );
  54      }
  55  
  56      /**
  57       * @group activity_action
  58       * @group bp_blogs_format_activity_action_new_blog_post
  59       */
  60  	public function test_bp_blogs_format_activity_action_new_blog_post_nonms() {
  61          if ( is_multisite() ) {
  62              $this->markTestSkipped();
  63          }
  64  
  65          $u = self::factory()->user->create();
  66          $p = self::factory()->post->create( array(
  67              'post_author' => $u,
  68          ) );
  69          $a = self::factory()->activity->create( array(
  70              'component' => buddypress()->blogs->id,
  71              'type' => 'new_blog_post',
  72              'user_id' => $u,
  73              'item_id' => 1,
  74              'secondary_item_id' => $p,
  75          ) );
  76  
  77          $a_obj = new BP_Activity_Activity( $a );
  78  
  79          $user_link = bp_core_get_userlink( $u );
  80          $blog_url = get_home_url();
  81          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
  82          $post_title = bp_activity_get_meta( $a, 'post_title' );
  83          $post_link = '<a href="' . $post_url . '">' . $post_title . '</a>';
  84  
  85          $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
  86  
  87          $this->assertSame( $expected, $a_obj->action );
  88      }
  89  
  90      /**
  91       * @group activity_action
  92       * @group bp_blogs_format_activity_action_new_blog_post
  93       */
  94  	public function test_bp_blogs_format_activity_action_new_blog_post_ms_rootblog() {
  95          if ( ! is_multisite() ) {
  96              $this->markTestSkipped();
  97          }
  98  
  99          $u = self::factory()->user->create();
 100          $p = self::factory()->post->create( array(
 101              'post_author' => $u,
 102          ) );
 103          $a = self::factory()->activity->create( array(
 104              'component' => buddypress()->blogs->id,
 105              'type' => 'new_blog_post',
 106              'user_id' => $u,
 107              'item_id' => 1,
 108              'secondary_item_id' => $p,
 109          ) );
 110  
 111          $a_obj = new BP_Activity_Activity( $a );
 112  
 113          $user_link = bp_core_get_userlink( $u );
 114          $blog_url = get_home_url();
 115          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 116          $post_title = bp_activity_get_meta( $a, 'post_title' );
 117          $post_link = '<a href="' . $post_url . '">' . $post_title . '</a>';
 118  
 119          $expected = sprintf( '%s wrote a new post, %s, on the site %s', $user_link, $post_link, '<a href="' . $blog_url . '">' . bp_blogs_get_blogmeta( $a_obj->item_id, 'name' ) . '</a>' );
 120  
 121          $this->assertSame( $expected, $a_obj->action );
 122      }
 123  
 124      /**
 125       * @group activity_action
 126       * @group bp_blogs_format_activity_action_new_blog_post
 127       */
 128  	public function test_bp_blogs_format_activity_action_new_blog_post_ms_nonrootblog() {
 129          if ( ! is_multisite() ) {
 130              $this->markTestSkipped();
 131          }
 132  
 133          if ( function_exists( 'wp_initialize_site' ) ) {
 134              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 135          }
 136  
 137          $b = self::factory()->blog->create();
 138          $u = self::factory()->user->create();
 139  
 140          switch_to_blog( $b );
 141          $p = self::factory()->post->create( array(
 142              'post_author' => $u,
 143          ) );
 144          $p_obj = get_post( $p );
 145          restore_current_blog();
 146  
 147          $a = self::factory()->activity->create( array(
 148              'component' => buddypress()->blogs->id,
 149              'type' => 'new_blog_post',
 150              'user_id' => $u,
 151              'item_id' => $b,
 152              'secondary_item_id' => $p,
 153          ) );
 154  
 155          $a_obj = new BP_Activity_Activity( $a );
 156  
 157          $user_link = bp_core_get_userlink( $u );
 158          $blog_url = get_blog_option( $a_obj->item_id, 'home' );
 159          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 160          $post_title = $p_obj->post_title;
 161          $post_link = '<a href="' . $post_url . '">' . $post_title . '</a>';
 162  
 163          $expected = sprintf( '%s wrote a new post, %s, on the site %s', $user_link, $post_link, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
 164  
 165          $this->assertSame( $expected, $a_obj->action );
 166      }
 167  
 168      /**
 169       * @group activity_action
 170       * @group bp_blogs_format_activity_action_new_blog_comment
 171       * @group post_type_comment_activities
 172       */
 173  	public function test_bp_blogs_format_activity_action_new_blog_comment_ms_nonrootblog() {
 174          if ( ! is_multisite() ) {
 175              $this->markTestSkipped();
 176          }
 177  
 178          if ( function_exists( 'wp_initialize_site' ) ) {
 179              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 180          }
 181  
 182          $b = self::factory()->blog->create();
 183          $u = self::factory()->user->create();
 184  
 185          switch_to_blog( $b );
 186          $p = self::factory()->post->create( array(
 187              'post_author' => $u,
 188          ) );
 189          $p_obj = get_post( $p );
 190          $c = self::factory()->comment->create( array(
 191              'comment_post_ID' => $p,
 192          ) );
 193          $c_obj = get_comment( $c );
 194          restore_current_blog();
 195  
 196          $a = self::factory()->activity->create( array(
 197              'component' => buddypress()->blogs->id,
 198              'type' => 'new_blog_comment',
 199              'user_id' => $u,
 200              'item_id' => $b,
 201              'secondary_item_id' => $c,
 202          ) );
 203  
 204          $a_obj = new BP_Activity_Activity( $a );
 205  
 206          $user_link = bp_core_get_userlink( $u );
 207          $blog_url = get_blog_option( $a_obj->item_id, 'home' );
 208          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 209          $post_title = $p_obj->post_title;
 210          $post_link = '<a href="' . $post_url . '">' . $post_title . '</a>';
 211  
 212          $expected = sprintf( '%s commented on the post, %s, on the site %s', $user_link, $post_link, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
 213  
 214          $this->assertSame( $expected, $a_obj->action );
 215      }
 216  
 217      /**
 218       * @group bp_blogs_format_activity_action_new_blog
 219       */
 220      public function test_bp_activity_format_activity_action_new_blog_backpat() {
 221          if ( ! is_multisite() ) {
 222              $this->markTestSkipped();
 223          }
 224  
 225          if ( function_exists( 'wp_initialize_site' ) ) {
 226              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 227          }
 228  
 229          add_filter( 'bp_blogs_activity_created_blog_action', array( $this, 'created_blog_passthrough' ), 10, 2 );
 230  
 231          $b = self::factory()->blog->create();
 232          $u = self::factory()->user->create();
 233  
 234          $recorded_blog          = new BP_Blogs_Blog;
 235          $recorded_blog->user_id = $u;
 236          $recorded_blog->blog_id = $b;
 237          $recorded_blog_id       = $recorded_blog->save();
 238  
 239          $a = self::factory()->activity->create( array(
 240              'component' => buddypress()->blogs->id,
 241              'type' => 'new_blog',
 242              'user_id' => $u,
 243              'item_id' => $b,
 244          ) );
 245  
 246          $this->assertEquals( $this->userblog_id, $recorded_blog_id );
 247      }
 248  
 249      /**
 250       * @group bp_blogs_format_activity_action_new_blog_post
 251       */
 252      public function test_bp_activity_format_activity_action_new_blog_post_backpat() {
 253          if ( ! is_multisite() ) {
 254              $this->markTestSkipped();
 255          }
 256  
 257          if ( function_exists( 'wp_initialize_site' ) ) {
 258              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 259          }
 260  
 261          add_filter( 'bp_blogs_activity_new_post_action', array( $this, 'new_post_passthrough' ), 10, 2 );
 262  
 263          $b = self::factory()->blog->create();
 264  
 265          switch_to_blog( $b );
 266          $p = self::factory()->post->create();
 267          restore_current_blog();
 268  
 269          $u = self::factory()->user->create();
 270          $a = self::factory()->activity->create( array(
 271              'component' => buddypress()->blogs->id,
 272              'type' => 'new_blog_post',
 273              'user_id' => $u,
 274              'item_id' => $b,
 275              'secondary_item_id' => $p,
 276          ) );
 277  
 278          $this->assertEquals( $this->post_id, $p );
 279      }
 280  
 281      /**
 282       * @group bp_blogs_format_activity_action_new_blog_comment
 283       */
 284      public function test_bp_activity_format_activity_action_new_blog_comment_backpat() {
 285          if ( ! is_multisite() ) {
 286              $this->markTestSkipped();
 287          }
 288  
 289          if ( function_exists( 'wp_initialize_site' ) ) {
 290              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 291          }
 292  
 293          add_filter( 'bp_blogs_activity_new_comment_action', array( $this, 'new_comment_passthrough' ), 10, 2 );
 294  
 295          $b = self::factory()->blog->create();
 296  
 297          switch_to_blog( $b );
 298          $p = self::factory()->post->create();
 299          $c = self::factory()->comment->create( array(
 300              'comment_post_ID' => $p,
 301          ) );
 302          restore_current_blog();
 303  
 304          $u = self::factory()->user->create();
 305          $a = self::factory()->activity->create( array(
 306              'component' => buddypress()->blogs->id,
 307              'type' => 'new_blog_comment',
 308              'user_id' => $u,
 309              'item_id' => $b,
 310              'secondary_item_id' => $c,
 311          ) );
 312  
 313          $this->assertEquals( $this->comment_post_id, $p );
 314      }
 315  
 316      /**
 317       * @group post_type_comment_activities
 318       */
 319      public function test_bp_blogs_update_post_title_activity_meta_should_not_be_the_same_for_same_comment_id() {
 320          global $wpdb;
 321  
 322          if ( ! is_multisite() ) {
 323              $this->markTestSkipped();
 324          }
 325  
 326          if ( function_exists( 'wp_initialize_site' ) ) {
 327              $this->setExpectedDeprecated( 'wpmu_new_blog' );
 328          }
 329  
 330          $b1 = self::factory()->blog->create();
 331          $b2 = self::factory()->blog->create();
 332          $b3 = self::factory()->blog->create();
 333          $u = self::factory()->user->create();
 334          $u2 = self::factory()->user->create();
 335          $commenter = self::factory()->user->get_object_by_id( $u2 );
 336  
 337          $bids = array( $b1, $b2, $b3 );
 338          $pids = array();
 339  
 340          foreach ( $bids as $bid ) {
 341              switch_to_blog( $bid );
 342  
 343              // Ensure blog privacy is public so post activities are recorded.
 344              update_option( 'blog_public', 1 );
 345  
 346              // Create the post.
 347              $pids[ $bid ] = self::factory()->post->create( array(
 348                  'post_author' => $u,
 349              ) );
 350  
 351              /*
 352               * Create the post comment.
 353               *
 354               * Both user_id and comment_author_email are required for unit tests.
 355               */
 356              $c = self::factory()->comment->create( array(
 357                  'user_id'         => $u2,
 358                  'comment_author_email' => $commenter->user_email,
 359                  'comment_post_ID' => $pids[ $bid ],
 360              ) );
 361  
 362              // Approve the comment so the activity item is generated.
 363              self::factory()->comment->update_object( $c, array( 'comment_approved' => 1 ) );
 364  
 365              restore_current_blog();
 366          }
 367  
 368          // Now update the post title on one blog only.
 369          switch_to_blog( $b1 );
 370          wp_update_post( array(
 371              'ID' => $pids[ $b1 ],
 372              'post_title' => 'Updated'
 373          ) );
 374          restore_current_blog();
 375  
 376          // Check our activity meta to see if the post title is different.
 377          $aids = bp_activity_get( array(
 378              'fields' => 'ids',
 379              'filter' => array(
 380                  'action' => 'new_blog_comment'
 381              )
 382          ) );
 383          $aids= $aids['activities'];
 384          foreach ( $aids as $aid ) {
 385              // Skip the check for the correct blog.
 386              $a = new BP_Activity_Activity( $aid );
 387              if ( $a->item_id == $b1 ) {
 388                  continue;
 389              }
 390  
 391              // Assert that post title is different.
 392              $post_title = bp_activity_get_meta( $aid, 'post_title' );
 393              $this->assertFalse( 'Updated' === $post_title, 'Post title should not be the same across all sites with the same post comment ID' );
 394          }
 395      }
 396  
 397      /**
 398       * @ticket BP6126
 399       */
 400      public function test_check_activity_actions_are_set_when_creating_activity_object() {
 401          $bp = buddypress();
 402  
 403          if ( isset( $bp->activity->actions ) ) {
 404              unset( $bp->activity->actions );
 405          }
 406  
 407          $u = self::factory()->user->create();
 408          $p = self::factory()->post->create( array( 'post_author' => $u, ) );
 409          $a = self::factory()->activity->create( array(
 410              'component'         => buddypress()->blogs->id,
 411              'item_id'           => 1,
 412              'secondary_item_id' => $p,
 413              'type'              => 'new_blog_post',
 414              'user_id'           => $u,
 415          ) );
 416  
 417          $a_obj = new BP_Activity_Activity( $a );
 418          $this->assertTrue( ! empty( $a_obj->action ) );
 419  
 420      }
 421  
 422      /**
 423       * @group activity_action
 424       * @group bp_blogs_format_activity_action_new_blog_post
 425       */
 426  	public function test_bp_blogs_format_activity_action_new_blog_post_no_title() {
 427          if ( is_multisite() ) {
 428              $this->markTestSkipped();
 429          }
 430  
 431          buddypress()->activity->actions = new stdClass();
 432  
 433          $u = self::factory()->user->create();
 434          $p = wp_insert_post( array(
 435              'post_author' => $u,
 436              'post_title'  => '', // no title: the object of the test
 437              'post_status' => 'publish',
 438              'post_content' => 'foo bar',
 439          ) );
 440  
 441          $user_link = bp_core_get_userlink( $u );
 442          $blog_url = get_home_url();
 443          $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 444          $post_link = '<a href="' . $post_url . '">(no title)</a>';
 445  
 446          // Set activity actions
 447          bp_activity_get_actions();
 448  
 449          $a_obj = bp_activity_get( array(
 450              'item_id'           => 1,
 451              'secondary_item_id' => $p,
 452          ) );
 453  
 454          $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
 455  
 456          $this->assertSame( $expected, $a_obj['activities'][0]->action );
 457      }
 458  
 459      /**
 460       * @group activity_action
 461       * @group bp_blogs_format_activity_action_new_blog_post
 462       */
 463  	public function test_bp_blogs_format_activity_action_new_blog_post_updated_without_title() {
 464          if ( is_multisite() ) {
 465              $this->markTestSkipped();
 466          }
 467  
 468          buddypress()->activity->actions = new stdClass();
 469  
 470          $u = self::factory()->user->create();
 471          $p = wp_insert_post( array(
 472              'post_author' => $u,
 473              'post_title'  => 'foo',
 474              'post_status' => 'publish',
 475              'post_content' => 'foo bar',
 476          ) );
 477  
 478          $user_link  = bp_core_get_userlink( $u );
 479          $blog_url   = get_home_url();
 480          $post_url   = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
 481          $post_title = get_the_title( $p );
 482          $post_link  = '<a href="' . $post_url . '">' . $post_title . '</a>';
 483  
 484          // Set actions
 485          bp_activity_get_actions();
 486  
 487          $a_obj = bp_activity_get( array(
 488              'item_id'           => 1,
 489              'secondary_item_id' => $p,
 490          ) );
 491  
 492          $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
 493  
 494          $this->assertSame( $expected, $a_obj['activities'][0]->action );
 495  
 496          // Update the post by removing its title
 497          wp_update_post( array(
 498              'ID'         => $p,
 499              'post_title' => '',
 500          ) );
 501  
 502          // we now expect the (no title) post link
 503          $post_link = '<a href="' . $post_url . '">(no title)</a>';
 504          $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
 505  
 506          $a_obj = bp_activity_get( array(
 507              'item_id'           => 1,
 508              'secondary_item_id' => $p,
 509          ) );
 510  
 511          $this->assertSame( $expected, $a_obj['activities'][0]->action );
 512      }
 513  
 514      /**
 515       * @group bp_blogs_sync_add_from_activity_comment
 516       * @group post_type_comment_activities
 517       */
 518  	public function test_bp_blogs_sync_add_from_activity_comment() {
 519          $old_user = get_current_user_id();
 520          $u = self::factory()->user->create();
 521          $this->set_current_user( $u );
 522          $userdata = get_userdata( $u );
 523  
 524          // let's use activity comments instead of single "new_blog_comment" activity items
 525          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 526  
 527          // create the blog post
 528          $post_id = self::factory()->post->create( array(
 529              'post_status' => 'publish',
 530              'post_type'   => 'post',
 531              'post_title'  => 'Test activity comment to post comment',
 532          ) );
 533  
 534          // grab the activity ID for the activity comment
 535          $a1 = bp_activity_get_activity_id( array(
 536              'type'      => 'new_blog_post',
 537              'component' => buddypress()->blogs->id,
 538              'filter'    => array(
 539                  'item_id' => get_current_blog_id(),
 540                  'secondary_item_id' => $post_id
 541              ),
 542          ) );
 543  
 544          $a2 = bp_activity_new_comment( array(
 545              'content'     => 'this content shoud be in a new post comment',
 546              'user_id'     => $u,
 547              'activity_id' => $a1,
 548          ) );
 549  
 550          $approved_comments = get_approved_comments( $post_id );
 551          $comment = reset( $approved_comments );
 552  
 553          $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
 554          $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
 555  
 556          // reset
 557          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 558  
 559          $this->set_current_user( $old_user );
 560      }
 561  
 562      /**
 563       * @group bp_blogs_sync_delete_from_activity_comment
 564       * @group post_type_comment_activities
 565       */
 566      public function test_bp_blogs_sync_delete_from_activity_comment() {
 567          $old_user = get_current_user_id();
 568          $u = self::factory()->user->create();
 569          $this->set_current_user( $u );
 570          $userdata = get_userdata( $u );
 571  
 572          // let's use activity comments instead of single "new_blog_comment" activity items
 573          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 574  
 575          // create the blog post
 576          $post_id = self::factory()->post->create( array(
 577              'post_status' => 'publish',
 578              'post_type'   => 'post',
 579              'post_title'  => 'Test activity comment to post comment',
 580          ) );
 581  
 582          // grab the activity ID for the activity comment
 583          $a1 = bp_activity_get_activity_id( array(
 584              'type'      => 'new_blog_post',
 585              'component' => buddypress()->blogs->id,
 586              'filter'    => array(
 587                  'item_id' => get_current_blog_id(),
 588                  'secondary_item_id' => $post_id
 589              ),
 590          ) );
 591  
 592          $a2 = bp_activity_new_comment( array(
 593              'content'     => 'the generated comment should be deleted once the activity comment is removed',
 594              'user_id'     => $u,
 595              'activity_id' => $a1,
 596          ) );
 597  
 598          bp_activity_delete_comment( $a1, $a2 );
 599  
 600          $post_comments = get_comments( array( 'post_id' => $post_id ) );
 601  
 602          $this->assertEmpty( $post_comments, 'A post comment should be deleted when the corresponding activity is' );
 603  
 604          // reset
 605          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 606  
 607          $this->set_current_user( $old_user );
 608      }
 609  
 610      /**
 611       * @group bp_blogs_sync_activity_edit_to_post_comment
 612       * @group post_type_comment_activities
 613       */
 614      public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_unspam_activity_comment() {
 615          $old_user = get_current_user_id();
 616          $u = self::factory()->user->create();
 617          $this->set_current_user( $u );
 618          $userdata = get_userdata( $u );
 619  
 620          // let's use activity comments instead of single "new_blog_comment" activity items
 621          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 622  
 623          // create the blog post
 624          $post_id = self::factory()->post->create( array(
 625              'post_status' => 'publish',
 626              'post_type'   => 'post',
 627              'post_title'  => 'Test activity comment to post comment',
 628          ) );
 629  
 630          // grab the activity ID for the activity comment
 631          $a1 = bp_activity_get_activity_id( array(
 632              'type'      => 'new_blog_post',
 633              'component' => buddypress()->blogs->id,
 634              'filter'    => array(
 635                  'item_id' => get_current_blog_id(),
 636                  'secondary_item_id' => $post_id
 637              ),
 638          ) );
 639  
 640          $a2 = bp_activity_new_comment( array(
 641              'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
 642              'user_id'     => $u,
 643              'activity_id' => $a1,
 644          ) );
 645  
 646          $activity = new BP_Activity_Activity( $a2 );
 647  
 648          bp_activity_mark_as_spam( $activity );
 649          $activity->save();
 650  
 651          $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
 652  
 653          $this->assertEmpty( $post_comments, 'A post comment should be spammed when the corresponding activity is spammed' );
 654  
 655          bp_activity_mark_as_ham( $activity );
 656          $activity->save();
 657  
 658          $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
 659          $comment = reset( $post_comments );
 660  
 661          $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
 662          $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
 663  
 664          // reset
 665          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 666  
 667          $this->set_current_user( $old_user );
 668      }
 669  
 670      /**
 671       * @group bp_blogs_sync_activity_edit_to_post_comment
 672       * @group post_type_comment_activities
 673       */
 674      public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_activity_comment_unspam_post_comment() {
 675          $old_user = get_current_user_id();
 676          $u = self::factory()->user->create();
 677          $this->set_current_user( $u );
 678          $userdata = get_userdata( $u );
 679  
 680          // let's use activity comments instead of single "new_blog_comment" activity items
 681          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 682  
 683          // create the blog post
 684          $post_id = self::factory()->post->create( array(
 685              'post_status' => 'publish',
 686              'post_type'   => 'post',
 687              'post_title'  => 'Test activity comment to post comment',
 688          ) );
 689  
 690          // grab the activity ID for the activity comment
 691          $a1 = bp_activity_get_activity_id( array(
 692              'type'      => 'new_blog_post',
 693              'component' => buddypress()->blogs->id,
 694              'filter'    => array(
 695                  'item_id' => get_current_blog_id(),
 696                  'secondary_item_id' => $post_id
 697              ),
 698          ) );
 699  
 700          $a2 = bp_activity_new_comment( array(
 701              'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
 702              'user_id'     => $u,
 703              'activity_id' => $a1,
 704          ) );
 705  
 706          $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' );
 707  
 708          $activity = new BP_Activity_Activity( $a2 );
 709  
 710          bp_activity_mark_as_spam( $activity );
 711          $activity->save();
 712  
 713          wp_unspam_comment( $c );
 714  
 715          $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
 716          $comment = reset( $post_comments );
 717  
 718          $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
 719          $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
 720  
 721          // reset
 722          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 723  
 724          $this->set_current_user( $old_user );
 725      }
 726  
 727      /**
 728       * @group bp_blogs_sync_activity_edit_to_post_comment
 729       * @group post_type_comment_activities
 730       */
 731      public function test_bp_blogs_sync_activity_edit_to_post_comment_trash_comment_ham_activity() {
 732          $old_user = get_current_user_id();
 733          $u = self::factory()->user->create();
 734          $this->set_current_user( $u );
 735          $userdata = get_userdata( $u );
 736  
 737          // let's use activity comments instead of single "new_blog_comment" activity items
 738          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 739  
 740          // create the blog post
 741          $post_id = self::factory()->post->create( array(
 742              'post_status' => 'publish',
 743              'post_type'   => 'post',
 744              'post_title'  => 'Test activity comment to post comment',
 745          ) );
 746  
 747          // grab the activity ID for the activity comment
 748          $a1 = bp_activity_get_activity_id( array(
 749              'type'      => 'new_blog_post',
 750              'component' => buddypress()->blogs->id,
 751              'filter'    => array(
 752                  'item_id' => get_current_blog_id(),
 753                  'secondary_item_id' => $post_id
 754              ),
 755          ) );
 756  
 757          $a2 = bp_activity_new_comment( array(
 758              'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
 759              'user_id'     => $u,
 760              'activity_id' => $a1,
 761          ) );
 762  
 763          $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' );
 764  
 765          wp_trash_comment( $c );
 766  
 767          $activity = new BP_Activity_Activity( $a2 );
 768  
 769          bp_activity_mark_as_ham( $activity );
 770          $activity->save();
 771  
 772          $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
 773          $comment = reset( $post_comments );
 774  
 775          $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
 776          $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
 777  
 778          // reset
 779          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 780  
 781          $this->set_current_user( $old_user );
 782      }
 783  
 784      /**
 785       * @group bp_blogs_sync_activity_edit_to_post_comment
 786       * @group post_type_comment_activities
 787       */
 788      public function test_spammed_activity_comment_should_not_create_post_comment() {
 789          $old_user = get_current_user_id();
 790          $u = self::factory()->user->create();
 791          $this->set_current_user( $u );
 792          $userdata = get_userdata( $u );
 793  
 794          // let's use activity comments instead of single "new_blog_comment" activity items.
 795          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 796  
 797          // create the blog post.
 798          $post_id = self::factory()->post->create( array(
 799              'post_status' => 'publish',
 800              'post_type'   => 'post',
 801              'post_title'  => 'Test activity comment to post comment',
 802          ) );
 803  
 804          // Grab the activity ID for the activity comment.
 805          $a1 = bp_activity_get_activity_id( array(
 806              'type'      => 'new_blog_post',
 807              'component' => buddypress()->blogs->id,
 808              'filter'    => array(
 809                  'item_id' => get_current_blog_id(),
 810                  'secondary_item_id' => $post_id
 811              ),
 812          ) );
 813  
 814          // Set activity item to spam.
 815          add_action( 'bp_activity_before_save', array( $this, 'set_activity_to_spam' ) );
 816  
 817          // Create spammed activity comment.
 818          $a2 = bp_activity_new_comment( array(
 819              'content'     => 'this activity comment shoud not be created as a new post comment. yolo.',
 820              'user_id'     => $u,
 821              'activity_id' => $a1,
 822          ) );
 823  
 824          // Grab post comments.
 825          $approved_comments = get_approved_comments( $post_id );
 826          $comment = reset( $approved_comments );
 827  
 828          // Assert that post comment wasn't created.
 829          $this->assertEmpty( $comment );
 830  
 831          // Reset.
 832          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 833          remove_action( 'bp_activity_before_save', array( $this, 'set_activity_to_spam' ) );
 834  
 835          $this->set_current_user( $old_user );
 836      }
 837  
 838      /**
 839       * Dopey passthrough method so we can check that the correct values
 840       * are being passed to the filter
 841       */
 842  	public function created_blog_passthrough( $a, $b ) {
 843          $this->userblog_id = isset( $b->id ) ? $b->id : '';
 844          return $a;
 845      }
 846  
 847      /**
 848       * Dopey passthrough method so we can check that the correct values
 849       * are being passed to the filter
 850       */
 851  	public function new_post_passthrough( $a, $b ) {
 852          $this->post_id = isset( $b->ID ) ? $b->ID : '';
 853          return $a;
 854      }
 855  
 856      /**
 857       * Dopey passthrough method so we can check that the correct values
 858       * are being passed to the filter
 859       */
 860  	public function new_comment_passthrough( $a, $b ) {
 861          $this->comment_post_id = isset( $b->comment_post_ID ) ? $b->comment_post_ID : '';
 862          return $a;
 863      }
 864  
 865      /**
 866       * Explicitly set activity to spam.
 867       */
 868  	public function set_activity_to_spam( $activity ) {
 869          $activity->is_spam = 1;
 870      }
 871  }


Generated: Mon Nov 18 01:01:37 2019 Cross-referenced by PHPXref 0.7.1