| 1 | <?php |
| 2 | /** |
| 3 | * @group blogs |
| 4 | * @ticket BP6306 |
| 5 | */ |
| 6 | class BP_Tests_Blogs_Filters extends BP_UnitTestCase { |
| 7 | protected $activity_actions; |
| 8 | protected $custom_post_types; |
| 9 | |
| 10 | public function setUp() { |
| 11 | parent::setUp(); |
| 12 | |
| 13 | $bp = buddypress(); |
| 14 | |
| 15 | $this->activity_actions = $bp->activity->actions; |
| 16 | $bp->activity->actions = new stdClass(); |
| 17 | |
| 18 | $this->custom_post_types = array( 'using_old_filter' ); |
| 19 | |
| 20 | register_post_type( 'using_old_filter', array( |
| 21 | 'label' => 'using_old_filter', |
| 22 | 'public' => true, |
| 23 | 'supports' => array( 'comments' ), |
| 24 | ) ); |
| 25 | |
| 26 | add_filter( 'bp_blogs_record_post_post_types', array( $this, 'filter_post_types'), 10, 1 ); |
| 27 | add_filter( 'bp_blogs_record_comment_post_types', array( $this, 'filter_post_types'), 10, 1 ); |
| 28 | } |
| 29 | |
| 30 | function tearDown() { |
| 31 | parent::tearDown(); |
| 32 | |
| 33 | $bp = buddypress(); |
| 34 | |
| 35 | _unregister_post_type( 'using_old_filter' ); |
| 36 | remove_filter( 'bp_blogs_record_post_post_types', array( $this, 'filter_post_types'), 10, 1 ); |
| 37 | 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(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @ticket BP6306 |
| 46 | */ |
| 47 | public function test_bp_activity_get_actions() { |
| 48 | $activity_actions = bp_activity_get_actions(); |
| 49 | |
| 50 | $this->assertTrue( ! isset( $activity_actions->activity->new_using_old_filter ), 'Post types registering using the bp_blogs_record_post_post_types filter should not have a specific action' ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @ticket BP6306 |
| 55 | */ |
| 56 | public function test_bp_activity_catch_transition_post_type_status() { |
| 57 | $post_id = $this->factory->post->create( array( |
| 58 | 'post_status' => 'publish', |
| 59 | 'post_type' => 'using_old_filter', |
| 60 | ) ); |
| 61 | |
| 62 | $this->assertTrue( $this->activity_exists_for_post_type( get_current_blog_id(), $post_id, 'new_blog_post' ), 'Generated activity for a post type registering using the bp_blogs_record_post_post_types filter should have a new_blog_post action' ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @ticket BP6306 |
| 67 | */ |
| 68 | public function test_bp_blogs_record_comment() { |
| 69 | $u = $this->factory->user->create(); |
| 70 | $user = $this->factory->user->get_object_by_id( $u ); |
| 71 | |
| 72 | $post_id = $this->factory->post->create( array( |
| 73 | 'post_status' => 'publish', |
| 74 | 'post_type' => 'using_old_filter', |
| 75 | 'post_author' => $u, |
| 76 | ) ); |
| 77 | |
| 78 | $comment_id = $this->factory->comment->create( array( |
| 79 | 'user_id' => $u, |
| 80 | 'comment_author_email' => $user->user_email, |
| 81 | 'comment_post_ID' => $post_id, |
| 82 | ) ); |
| 83 | |
| 84 | // Approve the comment |
| 85 | $this->factory->comment->update_object( $comment_id, array( 'comment_approved' => 1 ) ); |
| 86 | |
| 87 | $this->assertTrue( $this->activity_exists_for_post_type( get_current_blog_id(), $comment_id, 'new_blog_comment' ), 'Generated activity for comments about a post type registering using the bp_blogs_record_post_post_types filter should have a new_blog_comment action' ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @ticket BP6306 |
| 92 | */ |
| 93 | public function test_bp_blogs_record_comment_sync_activity_comment() { |
| 94 | $u = $this->factory->user->create(); |
| 95 | $user = $this->factory->user->get_object_by_id( $u ); |
| 96 | |
| 97 | add_filter( 'bp_disable_blogforum_comments', '__return_false' ); |
| 98 | |
| 99 | $post_id = $this->factory->post->create( array( |
| 100 | 'post_status' => 'publish', |
| 101 | 'post_type' => 'using_old_filter', |
| 102 | 'post_author' => $u, |
| 103 | ) ); |
| 104 | |
| 105 | $parent_activity_id = bp_activity_get_activity_id( array( |
| 106 | 'component' => 'blogs', |
| 107 | 'type' => 'new_blog_post', |
| 108 | 'item_id' => get_current_blog_id(), |
| 109 | 'secondary_item_id' => $post_id |
| 110 | ) ); |
| 111 | |
| 112 | $comment_id = $this->factory->comment->create( array( |
| 113 | 'user_id' => $u, |
| 114 | 'comment_author_email' => $user->user_email, |
| 115 | 'comment_post_ID' => $post_id, |
| 116 | ) ); |
| 117 | |
| 118 | // Approve the comment |
| 119 | $this->factory->comment->update_object( $comment_id, array( 'comment_approved' => 1 ) ); |
| 120 | |
| 121 | $this->assertTrue( $this->activity_exists_for_post_type( $parent_activity_id, '', 'activity_comment', 'stream' ), 'Generated activity for comments about a post type registering using the bp_blogs_record_post_post_types filter having sync on should have a activity_comment action' ); |
| 122 | |
| 123 | remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @ticket BP6306 |
| 128 | */ |
| 129 | public function test_bp_activity_post_types_filtering_blogs_comments() { |
| 130 | $bp = buddypress(); |
| 131 | $custom_post_types = $this->custom_post_types; |
| 132 | remove_filter( 'bp_blogs_record_post_post_types', array( $this, 'filter_post_types' ), 10, 1 ); |
| 133 | |
| 134 | register_post_type( 'foo', array( |
| 135 | 'label' => 'foo', |
| 136 | 'public' => true, |
| 137 | 'supports' => array( 'buddypress-activity', 'comments' ), |
| 138 | ) ); |
| 139 | |
| 140 | $this->custom_post_types[] = 'foo'; |
| 141 | |
| 142 | $u = $this->factory->user->create(); |
| 143 | $user = $this->factory->user->get_object_by_id( $u ); |
| 144 | |
| 145 | $post_id = $this->factory->post->create( array( |
| 146 | 'post_status' => 'publish', |
| 147 | 'post_type' => 'foo', |
| 148 | 'post_author' => $u, |
| 149 | ) ); |
| 150 | |
| 151 | $this->assertTrue( $this->activity_exists_for_post_type( get_current_blog_id(), $post_id, 'new_blog_post' ) ); |
| 152 | |
| 153 | $comment_id = $this->factory->comment->create( array( |
| 154 | 'user_id' => $u, |
| 155 | 'comment_author_email' => $user->user_email, |
| 156 | 'comment_post_ID' => $post_id, |
| 157 | ) ); |
| 158 | |
| 159 | // Approve the comment |
| 160 | $this->factory->comment->update_object( $comment_id, array( 'comment_approved' => 1 ) ); |
| 161 | |
| 162 | $this->assertTrue( $this->activity_exists_for_post_type( get_current_blog_id(), $comment_id, 'new_blog_comment' ) ); |
| 163 | |
| 164 | // Activity / Comments sync |
| 165 | add_filter( 'bp_disable_blogforum_comments', '__return_false' ); |
| 166 | |
| 167 | $comment_sync_id = $this->factory->comment->create( array( |
| 168 | 'user_id' => $u, |
| 169 | 'comment_author_email' => $user->user_email, |
| 170 | 'comment_post_ID' => $post_id, |
| 171 | ) ); |
| 172 | |
| 173 | // Approve the comment |
| 174 | $this->factory->comment->update_object( $comment_sync_id, array( 'comment_approved' => 1 ) ); |
| 175 | |
| 176 | $parent_activity_id = bp_activity_get_activity_id( array( |
| 177 | 'component' => 'blogs', |
| 178 | 'type' => 'new_blog_post', |
| 179 | 'item_id' => get_current_blog_id(), |
| 180 | 'secondary_item_id' => $post_id |
| 181 | ) ); |
| 182 | |
| 183 | $this->assertNotNull( $parent_activity_id ); |
| 184 | |
| 185 | $this->assertTrue( $this->activity_exists_for_post_type( $parent_activity_id, '', 'activity_comment', 'stream' ) ); |
| 186 | |
| 187 | remove_filter( 'bp_disable_blogforum_comments', '__return_false' ); |
| 188 | _unregister_post_type( 'foo' ); |
| 189 | $this->custom_post_types = $custom_post_types; |
| 190 | |
| 191 | // Reset globals |
| 192 | unset( $bp->activity->actions->activity->new_foo ); |
| 193 | $bp->activity->track = array(); |
| 194 | } |
| 195 | |
| 196 | public function filter_post_types( $post_types ) { |
| 197 | $post_types = array_merge( $post_types, $this->custom_post_types ); |
| 198 | return $post_types; |
| 199 | } |
| 200 | |
| 201 | protected function activity_exists_for_post_type( $item_id, $secondary_item_id, $action, $display_comments = false ) { |
| 202 | $a = bp_activity_get( array( |
| 203 | 'display_comments' => $display_comments, |
| 204 | 'filter' => array( |
| 205 | 'action' => $action, |
| 206 | 'primary_id' => $item_id, |
| 207 | 'secondary_id' => $secondary_item_id, |
| 208 | ) ) ); |
| 209 | |
| 210 | return ! empty( $a['activities'] ); |
| 211 | } |
| 212 | } |