Skip to:
Content

BuddyPress.org

Changeset 10545


Ignore:
Timestamp:
02/07/2016 04:55:04 PM (4 years ago)
Author:
imath
Message:

Post Type Activities: add unit tests and improve existing ones.

See #6482
Fixes #6128

Location:
trunk/tests/phpunit
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r10297 r10545  
    5050        // Clean up after autocommits.
    5151        add_action( 'bp_blogs_recorded_existing_blogs', array( $this, 'set_autocommit_flag' ) );
     52
     53        // Make sure Activity actions are reset before each test
     54        $this->reset_bp_activity_actions();
     55
     56        // Make sure all Post types activities globals are reset before each test
     57        $this->reset_bp_activity_post_types_globals();
    5258    }
    5359
     
    96102
    97103        parent::clean_up_global_scope();
     104    }
     105
     106    protected function reset_bp_activity_actions() {
     107        buddypress()->activity->actions = new stdClass();
     108
     109        /**
     110         * Populate the global with default activity actions only
     111         * before each test.
     112         */
     113        do_action( 'bp_register_activity_actions' );
     114    }
     115
     116    protected function reset_bp_activity_post_types_globals() {
     117        global $wp_post_types;
     118
     119        // Remove all remaining tracking arguments to each post type
     120        foreach ( $wp_post_types as $post_type => $post_type_arg ) {
     121            if ( post_type_supports( $post_type, 'buddypress-activity' ) ) {
     122                remove_post_type_support( $post_type, 'buddypress-activity' );
     123            }
     124
     125            if ( isset( $post_type_arg->bp_activity ) ) {
     126                unset( $post_type_arg->bp_activity );
     127            }
     128        }
     129
     130        buddypress()->activity->track = array();
    98131    }
    99132
  • trunk/tests/phpunit/testcases/activity/actions.php

    r9819 r10545  
    1010     */
    1111    public function test_bp_activity_catch_transition_post_type_status_publish() {
    12         $bp = buddypress();
    13 
    1412        register_post_type( 'foo', array(
    1513            'label'   => 'foo',
     
    2927
    3028        _unregister_post_type( 'foo' );
    31 
    32         // Reset globals
    33         unset( $bp->activity->actions->activity->new_foo );
    34         $bp->activity->track = array();
    3529    }
    3630
     
    4034     */
    4135    public function test_bp_activity_catch_transition_post_type_status_publish_to_publish() {
    42         $bp = buddypress();
    43 
    4436        register_post_type( 'foo', array(
    4537            'label'   => 'foo',
     
    6961
    7062        _unregister_post_type( 'foo' );
    71 
    72         // Reset globals
    73         unset( $bp->activity->actions->activity->new_foo );
    74         $bp->activity->track = array();
    7563    }
    7664
     
    8068     */
    8169    public function test_bp_activity_catch_transition_post_type_status_publish_password() {
    82         $bp = buddypress();
    83 
    8470        register_post_type( 'foo', array(
    8571            'label'   => 'foo',
     
    10793
    10894        _unregister_post_type( 'foo' );
    109 
    110         // Reset globals
    111         unset( $bp->activity->actions->activity->new_foo );
    112         $bp->activity->track = array();
    11395    }
    11496
     
    118100     */
    119101    public function test_bp_activity_catch_transition_post_type_status_publish_trash() {
    120         $bp = buddypress();
    121 
    122102        register_post_type( 'foo', array(
    123103            'label'   => 'foo',
     
    142122
    143123        _unregister_post_type( 'foo' );
    144 
    145         // Reset globals
    146         unset( $bp->activity->actions->activity->new_foo );
    147         $bp->activity->track = array();
    148124    }
    149125
  • trunk/tests/phpunit/testcases/activity/functions.php

    r10515 r10545  
    764764        }
    765765
    766         $bp = buddypress();
    767 
    768766        register_post_type( 'foo', array(
    769767            'label'   => 'foo',
     
    801799
    802800        _unregister_post_type( 'foo' );
    803 
    804         // Reset globals
    805         unset( $bp->activity->actions->activity->new_foo );
    806         $bp->activity->track = array();
    807801    }
    808802
     
    816810            return;
    817811        }
    818 
    819         $bp = buddypress();
    820812
    821813        $b = $this->factory->blog->create();
     
    864856
    865857        $this->assertSame( $expected, $a_obj->action );
    866 
    867         // Reset globals
    868         unset( $bp->activity->actions->activity->new_foo );
    869         $bp->activity->track = array();
    870858    }
    871859
     
    879867            return;
    880868        }
    881 
    882         $bp = buddypress();
    883869
    884870        $labels = array(
     
    925911
    926912        _unregister_post_type( 'foo' );
    927 
    928         // Reset globals
    929         unset( $bp->activity->actions->activity->foo_bar );
    930         $bp->activity->track = array();
    931913    }
    932914
     
    940922            return;
    941923        }
    942 
    943         $bp = buddypress();
    944         $reset = $bp->activity->actions;
    945924
    946925        $b = $this->factory->blog->create();
     
    992971
    993972        $this->assertSame( $expected, $a_obj->action );
    994 
    995         // Reset globals
    996         unset( $bp->activity->actions->activity->new_foo );
    997         $bp->activity->track = array();
    998973    }
    999974
     
    10321007
    10331008        remove_post_type_support( 'page', 'buddypress-activity' );
    1034 
    1035         // Reset globals
    1036         unset( $bp->activity->actions->blogs->new_page );
    1037         $bp->activity->track = array();
    10381009    }
    10391010
     
    10741045
    10751046        $this->assertSame( $set_bp_activity, $register_bp_activity );
     1047    }
     1048
     1049    /**
     1050     * @group activity_action
     1051     * @group bp_activity_format_activity_action_custom_post_type_post_ms
     1052     * @group post_type_comment_activities
     1053     */
     1054    public function test_bp_activity_format_activity_action_custom_post_type_comment() {
     1055        if ( is_multisite() ) {
     1056            $b = $this->factory->blog->create();
     1057            switch_to_blog( $b );
     1058            add_filter( 'comment_flood_filter', '__return_false' );
     1059        } else {
     1060            $b = get_current_blog_id();
     1061        }
     1062
     1063        $u = $this->factory->user->create();
     1064        $userdata = get_userdata( $u );
     1065
     1066        $labels = array(
     1067            'name'                       => 'bars',
     1068            'singular_name'              => 'bar',
     1069            'bp_activity_new_comment'    => __( '%1$s commented on the <a href="%2$s">bar</a>', 'buddypress' ),
     1070            'bp_activity_new_comment_ms' => __( '%1$s commented on the <a href="%2$s">bar</a>, on the site %3$s', 'buddypress' ),
     1071        );
     1072
     1073        register_post_type( 'foo', array(
     1074            'labels'   => $labels,
     1075            'public'   => true,
     1076            'supports' => array( 'buddypress-activity', 'comments' ),
     1077            'bp_activity' => array(
     1078                'action_id'         => 'new_bar',
     1079                'comment_action_id' => 'new_bar_comment',
     1080            ),
     1081        ) );
     1082
     1083        // Build the actions to fetch the tracking args
     1084        bp_activity_get_actions();
     1085
     1086        $p = $this->factory->post->create( array(
     1087            'post_author' => $u,
     1088            'post_type'   => 'foo',
     1089        ) );
     1090
     1091        $c = wp_new_comment( array(
     1092            'comment_post_ID'      => $p,
     1093            'comment_author'       => $userdata->user_nicename,
     1094            'comment_author_url'   => 'http://buddypress.org',
     1095            'comment_author_email' => $userdata->user_email,
     1096            'comment_content'      => 'this is a blog comment',
     1097            'comment_type'         => '',
     1098            'comment_parent'       => 0,
     1099            'user_id'              => $u,
     1100        ) );
     1101
     1102        $a = bp_activity_get_activity_id( array( 'type' => 'new_bar_comment' ) );
     1103
     1104        $a_obj = new BP_Activity_Activity( $a );
     1105
     1106        $user_link    = bp_core_get_userlink( $u );
     1107        $comment_url  = get_comment_link( $c );
     1108
     1109        _unregister_post_type( 'foo' );
     1110
     1111        if ( is_multisite() ) {
     1112            $blog_url  = get_blog_option( $a_obj->item_id, 'home' );
     1113            restore_current_blog();
     1114            remove_filter( 'comment_flood_filter', '__return_false' );
     1115
     1116            $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>' );
     1117        } else {
     1118            $expected = sprintf( $labels['bp_activity_new_comment'], $user_link, $comment_url );
     1119        }
     1120
     1121        $this->assertSame( $expected, $a_obj->action );
    10761122    }
    10771123
  • trunk/tests/phpunit/testcases/activity/functions/bpActivityGetActions.php

    r10515 r10545  
    134134
    135135        $expected = array(
    136             'new_foo'       => 'new_foo',
    137             'new_blog_post' => 'new_blog_post',
     136            'new_foo'          => 'new_foo',
     137            'new_blog_post'    => 'new_blog_post',
     138            'new_blog_comment' => 'new_blog_comment',
    138139        );
    139140
     
    147148        $actions = bp_activity_get_actions();
    148149        $expected = array(
    149             'new_blog_post' => 'new_blog_post',
     150            'new_blog_post'    => 'new_blog_post',
     151            'new_blog_comment' => 'new_blog_comment',
    150152        );
    151153        $this->assertSame( $expected, wp_list_pluck( (array) $actions->blogs, 'key' ) );
     
    167169
    168170        $expected = array(
    169             'new_foo'       => 'new_foo',
    170             'new_blog_post' => 'new_blog_post',
     171            'new_foo'          => 'new_foo',
     172            'new_blog_post'    => 'new_blog_post',
     173            'new_blog_comment' => 'new_blog_comment',
    171174        );
    172175
  • trunk/tests/phpunit/testcases/activity/template.php

    r10435 r10545  
    910910     * @group filter_query
    911911     * @group BP_Activity_Query
     912     * @group post_type_comment_activities
    912913     */
    913914    function test_bp_has_activities_with_filter_query_compare_regex() {
     
    13001301    /**
    13011302     * @group bp_has_activities
     1303     * @group post_type_comment_activities
    13021304     */
    13031305    public function test_bp_has_activities_with_type_new_blog_comments() {
     
    14311433
    14321434    /**
     1435     * @group bp_activity_can_comment
     1436     */
     1437    public function test_bp_activity_can_comment() {
     1438        global $activities_template;
     1439        $reset_activities_template = $activities_template;
     1440
     1441        $activities_template = new stdClass;
     1442        $activities_template->disable_blogforum_replies = true;
     1443        $activities_template->activity = (object) array( 'type' => 'activity_comment' );
     1444
     1445        $this->assertFalse( bp_activity_can_comment(), 'bp_activity_can_comment() should return false if the activity type is activity_comment' );
     1446
     1447        $types = array(
     1448            'new_blog_post',
     1449            'new_blog_comment',
     1450            'new_forum_topic',
     1451            'new_forum_post'
     1452        );
     1453
     1454        foreach ( $types as $type_false ) {
     1455            $activities_template->activity->type = $type_false;
     1456            $this->assertFalse( bp_activity_can_comment(), 'Comments about blog or forum posts/replies are disabled' );
     1457        }
     1458
     1459        $activities_template->disable_blogforum_replies = false;
     1460        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     1461
     1462        foreach ( $types as $type_true ) {
     1463            $activities_template->activity->type = $type_true;
     1464            $this->assertTrue( bp_activity_can_comment(), 'Comments about blog or forum posts/replies are enabled' );
     1465        }
     1466
     1467        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     1468
     1469        // clean up!
     1470        $activities_template = $reset_activities_template;
     1471    }
     1472
     1473    /**
     1474     * @group bp_activity_can_comment
     1475     */
     1476    public function test_bp_activity_can_comment_post_type_activity() {
     1477        global $activities_template;
     1478        $bp = buddypress();
     1479
     1480        $reset_activities_template = $activities_template;
     1481        $reset_activity_track = $bp->activity->track;
     1482
     1483        $activities_template = new stdClass;
     1484        $activities_template->disable_blogforum_replies = true;
     1485
     1486        register_post_type( 'foo', array(
     1487            'label'   => 'foo',
     1488            'public'   => true,
     1489            'supports' => array( 'buddypress-activity' ),
     1490        ) );
     1491
     1492        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1493
     1494        $activities_template->activity = (object) array( 'type' => 'new_foo' );
     1495
     1496        $this->assertTrue( bp_activity_can_comment(), 'If post type does not support comments, a post type activity can be commented' );
     1497
     1498        add_post_type_support( 'foo', 'comments' );
     1499
     1500        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1501
     1502        $this->assertFalse( bp_activity_can_comment(), 'If post type support comments, a post type activity cannot be commented' );
     1503
     1504        $bp_activity_support = (array) $bp->activity->track['new_foo'];
     1505        $bp_activity_support['activity_comment'] = true;
     1506
     1507        bp_activity_set_post_type_tracking_args( 'foo', $bp_activity_support );
     1508        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1509
     1510        $this->assertTrue( bp_activity_can_comment(), 'If post type supports activity comments, a post type activity can be commented' );
     1511
     1512        // clean up!
     1513        $activities_template = $reset_activities_template;
     1514        $bp->activity->track = $reset_activity_track;
     1515    }
     1516
     1517    /**
    14331518     * @group bp_activity_has_more_items
    14341519     */
  • trunk/tests/phpunit/testcases/blogs/activity.php

    r9819 r10545  
    157157     * @group activity_action
    158158     * @group bp_blogs_format_activity_action_new_blog_comment
     159     * @group post_type_comment_activities
    159160     */
    160161    public function test_bp_blogs_format_activity_action_new_blog_comment_ms_nonrootblog() {
     
    319320        }
    320321
    321         $bp = buddypress();
    322         $activity_actions = $bp->activity->actions;
    323         $bp->activity->actions = new stdClass();
     322        buddypress()->activity->actions = new stdClass();
    324323
    325324        $u = $this->factory->user->create();
     
    347346
    348347        $this->assertSame( $expected, $a_obj['activities'][0]->action );
    349 
    350         // Reset activity actions
    351         $bp->activity->actions = $activity_actions;
    352         $bp->activity->track = array();
    353348    }
    354349
     
    362357        }
    363358
    364         $bp = buddypress();
    365         $activity_actions = $bp->activity->actions;
    366         $bp->activity->actions = new stdClass();
     359        buddypress()->activity->actions = new stdClass();
    367360
    368361        $u = $this->factory->user->create();
     
    408401
    409402        $this->assertSame( $expected, $a_obj['activities'][0]->action );
    410 
    411         // Reset activity actions
    412         $bp->activity->actions = $activity_actions;
    413         $bp->activity->track = array();
     403    }
     404
     405    /**
     406     * @group bp_blogs_sync_add_from_activity_comment
     407     * @group post_type_comment_activities
     408     */
     409    public function test_bp_blogs_sync_add_from_activity_comment() {
     410        $old_user = get_current_user_id();
     411        $u = $this->factory->user->create();
     412        $this->set_current_user( $u );
     413        $userdata = get_userdata( $u );
     414
     415        // let's use activity comments instead of single "new_blog_comment" activity items
     416        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     417
     418        // create the blog post
     419        $post_id = $this->factory->post->create( array(
     420            'post_status' => 'publish',
     421            'post_type'   => 'post',
     422            'post_title'  => 'Test activity comment to post comment',
     423        ) );
     424
     425        // grab the activity ID for the activity comment
     426        $a1 = bp_activity_get_activity_id( array(
     427            'type'      => 'new_blog_post',
     428            'component' => buddypress()->blogs->id,
     429            'filter'    => array(
     430                'item_id' => get_current_blog_id(),
     431                'secondary_item_id' => $post_id
     432            ),
     433        ) );
     434
     435        $a2 = bp_activity_new_comment( array(
     436            'content'     => 'this content shoud be in a new post comment',
     437            'user_id'     => $u,
     438            'activity_id' => $a1,
     439        ) );
     440
     441        $approved_comments = get_approved_comments( $post_id );
     442        $comment = reset( $approved_comments );
     443
     444        $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' );
     445        $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' );
     446
     447        // reset
     448        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     449
     450        $this->set_current_user( $old_user );
     451    }
     452
     453    /**
     454     * @group bp_blogs_sync_delete_from_activity_comment
     455     * @group post_type_comment_activities
     456     */
     457    public function test_bp_blogs_sync_delete_from_activity_comment() {
     458        $old_user = get_current_user_id();
     459        $u = $this->factory->user->create();
     460        $this->set_current_user( $u );
     461        $userdata = get_userdata( $u );
     462
     463        // let's use activity comments instead of single "new_blog_comment" activity items
     464        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     465
     466        // create the blog post
     467        $post_id = $this->factory->post->create( array(
     468            'post_status' => 'publish',
     469            'post_type'   => 'post',
     470            'post_title'  => 'Test activity comment to post comment',
     471        ) );
     472
     473        // grab the activity ID for the activity comment
     474        $a1 = bp_activity_get_activity_id( array(
     475            'type'      => 'new_blog_post',
     476            'component' => buddypress()->blogs->id,
     477            'filter'    => array(
     478                'item_id' => get_current_blog_id(),
     479                'secondary_item_id' => $post_id
     480            ),
     481        ) );
     482
     483        $a2 = bp_activity_new_comment( array(
     484            'content'     => 'the generated comment should be deleted once the activity comment is removed',
     485            'user_id'     => $u,
     486            'activity_id' => $a1,
     487        ) );
     488
     489        bp_activity_delete_comment( $a1, $a2 );
     490
     491        $post_comments = get_comments( array( 'post_id' => $post_id ) );
     492
     493        $this->assertEmpty( $post_comments, 'A post comment should be deleted when the corresponding activity is' );
     494
     495        // reset
     496        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     497
     498        $this->set_current_user( $old_user );
     499    }
     500
     501    /**
     502     * @group bp_blogs_sync_activity_edit_to_post_comment
     503     * @group post_type_comment_activities
     504     */
     505    public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_unspam_activity_comment() {
     506        $old_user = get_current_user_id();
     507        $u = $this->factory->user->create();
     508        $this->set_current_user( $u );
     509        $userdata = get_userdata( $u );
     510
     511        // let's use activity comments instead of single "new_blog_comment" activity items
     512        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     513
     514        // create the blog post
     515        $post_id = $this->factory->post->create( array(
     516            'post_status' => 'publish',
     517            'post_type'   => 'post',
     518            'post_title'  => 'Test activity comment to post comment',
     519        ) );
     520
     521        // grab the activity ID for the activity comment
     522        $a1 = bp_activity_get_activity_id( array(
     523            'type'      => 'new_blog_post',
     524            'component' => buddypress()->blogs->id,
     525            'filter'    => array(
     526                'item_id' => get_current_blog_id(),
     527                'secondary_item_id' => $post_id
     528            ),
     529        ) );
     530
     531        $a2 = bp_activity_new_comment( array(
     532            'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
     533            'user_id'     => $u,
     534            'activity_id' => $a1,
     535        ) );
     536
     537        $activity = new BP_Activity_Activity( $a2 );
     538
     539        bp_activity_mark_as_spam( $activity );
     540        $activity->save();
     541
     542        $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     543
     544        $this->assertEmpty( $post_comments, 'A post comment should be spammed when the corresponding activity is spammed' );
     545
     546        bp_activity_mark_as_ham( $activity );
     547        $activity->save();
     548
     549        $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     550        $comment = reset( $post_comments );
     551
     552        $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' );
     553        $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' );
     554
     555        // reset
     556        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     557
     558        $this->set_current_user( $old_user );
     559    }
     560
     561    /**
     562     * @group bp_blogs_sync_activity_edit_to_post_comment
     563     * @group post_type_comment_activities
     564     */
     565    public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_activity_comment_unspam_post_comment() {
     566        $old_user = get_current_user_id();
     567        $u = $this->factory->user->create();
     568        $this->set_current_user( $u );
     569        $userdata = get_userdata( $u );
     570
     571        // let's use activity comments instead of single "new_blog_comment" activity items
     572        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     573
     574        // create the blog post
     575        $post_id = $this->factory->post->create( array(
     576            'post_status' => 'publish',
     577            'post_type'   => 'post',
     578            'post_title'  => 'Test activity comment to post comment',
     579        ) );
     580
     581        // grab the activity ID for the activity comment
     582        $a1 = bp_activity_get_activity_id( array(
     583            'type'      => 'new_blog_post',
     584            'component' => buddypress()->blogs->id,
     585            'filter'    => array(
     586                'item_id' => get_current_blog_id(),
     587                'secondary_item_id' => $post_id
     588            ),
     589        ) );
     590
     591        $a2 = bp_activity_new_comment( array(
     592            'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
     593            'user_id'     => $u,
     594            'activity_id' => $a1,
     595        ) );
     596
     597        $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' );
     598
     599        $activity = new BP_Activity_Activity( $a2 );
     600
     601        bp_activity_mark_as_spam( $activity );
     602        $activity->save();
     603
     604        wp_unspam_comment( $c );
     605
     606        $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     607        $comment = reset( $post_comments );
     608
     609        $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' );
     610        $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' );
     611
     612        // reset
     613        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     614
     615        $this->set_current_user( $old_user );
     616    }
     617
     618    /**
     619     * @group bp_blogs_sync_activity_edit_to_post_comment
     620     * @group post_type_comment_activities
     621     * @group imath
     622     */
     623    public function test_bp_blogs_sync_activity_edit_to_post_comment_trash_comment_ham_activity() {
     624        $old_user = get_current_user_id();
     625        $u = $this->factory->user->create();
     626        $this->set_current_user( $u );
     627        $userdata = get_userdata( $u );
     628
     629        // let's use activity comments instead of single "new_blog_comment" activity items
     630        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     631
     632        // create the blog post
     633        $post_id = $this->factory->post->create( array(
     634            'post_status' => 'publish',
     635            'post_type'   => 'post',
     636            'post_title'  => 'Test activity comment to post comment',
     637        ) );
     638
     639        // grab the activity ID for the activity comment
     640        $a1 = bp_activity_get_activity_id( array(
     641            'type'      => 'new_blog_post',
     642            'component' => buddypress()->blogs->id,
     643            'filter'    => array(
     644                'item_id' => get_current_blog_id(),
     645                'secondary_item_id' => $post_id
     646            ),
     647        ) );
     648
     649        $a2 = bp_activity_new_comment( array(
     650            'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
     651            'user_id'     => $u,
     652            'activity_id' => $a1,
     653        ) );
     654
     655        $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' );
     656
     657        wp_trash_comment( $c );
     658
     659        $activity = new BP_Activity_Activity( $a2 );
     660
     661        bp_activity_mark_as_ham( $activity );
     662        $activity->save();
     663
     664        $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     665        $comment = reset( $post_comments );
     666
     667        $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' );
     668        $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' );
     669
     670        // reset
     671        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     672
     673        $this->set_current_user( $old_user );
    414674    }
    415675
  • trunk/tests/phpunit/testcases/blogs/filters.php

    r9837 r10545  
    1010    public function setUp() {
    1111        parent::setUp();
    12 
    13         $bp = buddypress();
    14 
    15         $this->activity_actions = $bp->activity->actions;
    16         $bp->activity->actions = new stdClass();
    1712
    1813        $this->custom_post_types = array( 'using_old_filter' );
     
    3631        remove_filter( 'bp_blogs_record_post_post_types',    array( $this, 'filter_post_types'), 10, 1 );
    3732        remove_filter( 'bp_blogs_record_comment_post_types', array( $this, 'filter_post_types'), 10, 1 );
    38 
    39         // Reset activity actions
    40         $bp->activity->actions = $this->activity_actions;
    41         $bp->activity->track = array();
    4233    }
    4334
     
    6556    /**
    6657     * @goup bp_blogs_record_comment
     58     * @group post_type_comment_activities
    6759     */
    6860    public function test_bp_blogs_record_comment() {
     
    9082    /**
    9183     * @goup bp_blogs_record_comment_sync_activity_comment
     84     * @group post_type_comment_activities
    9285     */
    9386    public function test_bp_blogs_record_comment_sync_activity_comment() {
  • trunk/tests/phpunit/testcases/blogs/functions.php

    r10277 r10545  
    514514    /**
    515515     * @group bp_blogs_catch_transition_post_status
     516     * @group post_type_comment_activities
    516517     */
    517518    public function test_update_blog_post_and_new_blog_comment_and_activity_comment_meta() {
     
    597598    /**
    598599     * @group bp_blogs_transition_activity_status
    599      * @group bp_blogs_remove_comment
     600     * @group bp_blogs_post_type_remove_comment
     601     * @group post_type_comment_activities
    600602     */
    601603    public function test_bp_blogs_remove_comment_should_remove_spammed_activity_comment() {
     
    659661
    660662    /**
     663     * @group bp_blogs_post_type_remove_comment
     664     * @group post_type_comment_activities
     665     */
     666    public function test_bp_blogs_post_type_remove_comment() {
     667        $old_user = get_current_user_id();
     668        $u = $this->factory->user->create();
     669        $this->set_current_user( $u );
     670        $userdata = get_userdata( $u );
     671
     672        // create the blog post
     673        $p = $this->factory->post->create( array(
     674            'post_status' => 'publish',
     675            'post_type' => 'post',
     676            'post_title' => 'First title',
     677        ) );
     678
     679        $c = wp_new_comment( array(
     680            'comment_post_ID'      => $p,
     681            'comment_author'       => $userdata->user_nicename,
     682            'comment_author_url'   => 'http://buddypress.org',
     683            'comment_author_email' => $userdata->user_email,
     684            'comment_content'      => 'this comment will be removed',
     685            'comment_type'         => '',
     686            'comment_parent'       => 0,
     687            'user_id'              => $u,
     688        ) );
     689
     690        // An activity should exist
     691        $a = bp_activity_get_activity_id( array(
     692            'user_id' => $u,
     693            'type'    => 'new_blog_comment'
     694        ) );
     695
     696        // now permanently delete the comment
     697        wp_delete_comment( $c, true );
     698
     699        // The activity comment should no longer exist
     700        $ac = bp_activity_get( array( 'in' => $a ) );
     701        $this->assertTrue( empty( $ac['activities'] ) );
     702    }
     703
     704    /**
    661705     * @group bp_blogs_catch_transition_post_status
    662706     */
     
    715759     * @group bp_blogs_record_comment
    716760     * @group unique
     761     * @group post_type_comment_activities
    717762     */
    718763    public function test_bp_blogs_record_comment_no_duplicate_activity_comments() {
     
    778823    /**
    779824     * @group bp_blogs_record_comment
     825     * @group post_type_comment_activities
    780826     */
    781827    public function test_bp_blogs_record_comment_should_record_parent_blog_post_activity_if_not_found() {
     
    827873        ) );
    828874
     875        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     876
    829877        // Assert that activity item for blog post was created after adding a comment
    830878        $this->assertNotNull( $a1, 'Activity item was not created for existing blog post when recording post comment.' );
    831879
    832880        $this->set_current_user( $old_user );
     881    }
     882
     883    /**
     884     * @group bp_blogs_comment_sync_activity_comment
     885     * @group post_type_comment_activities
     886     */
     887    public function test_bp_blogs_comment_sync_activity_comment_for_custom_post_type() {
     888        if ( is_multisite() ) {
     889            $b = $this->factory->blog->create();
     890            switch_to_blog( $b );
     891            add_filter( 'comment_flood_filter', '__return_false' );
     892        } else {
     893            $b = get_current_blog_id();
     894        }
     895
     896        $u = $this->factory->user->create();
     897        $userdata = get_userdata( $u );
     898
     899        $labels = array(
     900            'name'                       => 'bars',
     901            'singular_name'              => 'bar',
     902        );
     903
     904        register_post_type( 'foo', array(
     905            'labels'   => $labels,
     906            'public'   => true,
     907            'supports' => array( 'comments' ),
     908        ) );
     909
     910        add_post_type_support( 'foo', 'buddypress-activity' );
     911
     912        bp_activity_set_post_type_tracking_args( 'foo', array(
     913            'comment_action_id' => 'new_foo_comment',
     914        ) );
     915
     916        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     917
     918        $p = $this->factory->post->create( array(
     919            'post_author' => $u,
     920            'post_type'   => 'foo',
     921        ) );
     922
     923        $a1 = bp_activity_get_activity_id( array(
     924            'type'      => 'new_foo',
     925            'filter'    => array(
     926                'item_id' => $b,
     927                'secondary_item_id' => $p
     928            ),
     929        ) );
     930
     931        $c = wp_new_comment( array(
     932            'comment_post_ID'      => $p,
     933            'comment_author'       => $userdata->user_nicename,
     934            'comment_author_url'   => 'http://buddypress.org',
     935            'comment_author_email' => $userdata->user_email,
     936            'comment_content'      => 'this is a foo comment',
     937            'comment_type'         => '',
     938            'comment_parent'       => 0,
     939            'user_id'              => $u,
     940        ) );
     941
     942        $a2 = bp_activity_new_comment( array(
     943            'content'     => 'this should generate a new foo comment',
     944            'user_id'     => $u,
     945            'activity_id' => $a1,
     946        ) );
     947
     948        $activity_args = array(
     949            'type'              => 'activity_comment',
     950            'display_comments'  => 'stream',
     951            'meta_query'        => array( array(
     952                'key'       => 'bp_blogs_foo_comment_id',
     953                'compare'   => 'exists',
     954            ) )
     955        );
     956
     957        $a = bp_activity_get( $activity_args );
     958        $aids = wp_list_pluck( $a['activities'], 'id' );
     959        $cids = wp_list_pluck( get_approved_comments( $p ), 'comment_ID' );
     960
     961        foreach ( $aids as $aid ) {
     962            $this->assertTrue( in_array( bp_activity_get_meta( $aid, 'bp_blogs_foo_comment_id' ), $cids ), 'The comment ID should be in the activity meta' );
     963        }
     964
     965        foreach ( $cids as $cid ) {
     966            $this->assertTrue( in_array( get_comment_meta( $cid, 'bp_activity_comment_id', true ), $aids ), 'The activity ID should be in the comment meta' );
     967        }
     968
     969        _unregister_post_type( 'foo' );
     970
     971        if ( is_multisite() ) {
     972            restore_current_blog();
     973            remove_filter( 'comment_flood_filter', '__return_false' );
     974        }
     975
     976        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
    833977    }
    834978
Note: See TracChangeset for help on using the changeset viewer.