| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * BuddyPress Extension Activity Tests. |
| 5 | * |
| 6 | * @group extend |
| 7 | * @group buddypress |
| 8 | * @group activity |
| 9 | */ |
| 10 | class BBP_Tests_Extend_BuddyPress_Activity extends BBP_UnitTestCase { |
| 11 | |
| 12 | /** |
| 13 | * Copied from `BBP_Forums_Group_Extension::new_forum()`. |
| 14 | * |
| 15 | * @since x.x.x |
| 16 | * |
| 17 | * @param int $forum_id The forum id. |
| 18 | * @param int $group_id The group id. |
| 19 | */ |
| 20 | private function attach_forum_to_group( $forum_id, $group_id ) { |
| 21 | bbp_add_forum_id_to_group( $group_id, $forum_id ); |
| 22 | bbp_add_group_id_to_forum( $forum_id, $group_id ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Dynamic activity actions for site-wide forum topics. |
| 27 | * |
| 28 | * @since 2.6.0 bbPress (rXXXX) |
| 29 | * |
| 30 | * @ticket BBP2794 |
| 31 | */ |
| 32 | public function test_bp_activity_actions_for_site_wide_forum_topic() { |
| 33 | $u = $this->factory->user->create(); |
| 34 | $f = $this->factory->forum->create(); |
| 35 | $t = $this->factory->topic->create( array( |
| 36 | 'post_parent' => $f, |
| 37 | 'post_author' => $u, |
| 38 | ) ); |
| 39 | |
| 40 | // Set up our activity text test string. |
| 41 | $user_link = bbp_get_user_profile_link( $u ); |
| 42 | $topic_permalink = bbp_get_topic_permalink( $t ); |
| 43 | $topic_title = get_post_field( 'post_title', $t, 'raw' ); |
| 44 | $topic_content = get_post_field( 'post_content', $t, 'raw' ); |
| 45 | $topic_link = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>'; |
| 46 | $forum_permalink = bbp_get_forum_permalink( $f ); |
| 47 | $forum_title = get_post_field( 'post_title', $f, 'raw' ); |
| 48 | $forum_link = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>'; |
| 49 | $activity_text = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 50 | |
| 51 | // Create the activity. |
| 52 | bbpress()->extend->buddypress->activity->topic_create( $t, $f, array(), $u ); |
| 53 | |
| 54 | $activity_id = (int) get_post_meta( $t, '_bbp_activity_id', true ); |
| 55 | $activity = new BP_Activity_Activity( $activity_id ); |
| 56 | |
| 57 | // Test the default generated string. |
| 58 | $this->assertEquals( $activity_text, $activity->action ); |
| 59 | |
| 60 | // Update a few items for testing. |
| 61 | wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) ); |
| 62 | $user_link = bbp_get_user_profile_link( $u ); |
| 63 | |
| 64 | wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) ); |
| 65 | $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>'; |
| 66 | |
| 67 | wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) ); |
| 68 | $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>'; |
| 69 | |
| 70 | // Set up our new test string. |
| 71 | $activity_text = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 72 | |
| 73 | $activity = new BP_Activity_Activity( $activity_id ); |
| 74 | |
| 75 | // Are we dynamic? |
| 76 | $this->assertEquals( $activity_text, $activity->action ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Dynamic activity actions for replies to site-wide forum topics. |
| 81 | * |
| 82 | * @since 2.6.0 bbPress (rXXXX) |
| 83 | * |
| 84 | * @ticket BBP2794 |
| 85 | */ |
| 86 | public function test_bp_activity_actions_for_reply_to_site_wide_forum_topic() { |
| 87 | $u = $this->factory->user->create(); |
| 88 | $f = $this->factory->forum->create(); |
| 89 | $t = $this->factory->topic->create( array( |
| 90 | 'post_parent' => $f, |
| 91 | 'post_author' => $u, |
| 92 | ) ); |
| 93 | $r = $this->factory->reply->create( array( |
| 94 | 'post_parent' => $t, |
| 95 | 'post_author' => $u, |
| 96 | ) ); |
| 97 | |
| 98 | // Set up our activity text test string. |
| 99 | $user_link = bbp_get_user_profile_link( $u ); |
| 100 | $topic_permalink = bbp_get_topic_permalink( $t ); |
| 101 | $topic_title = get_post_field( 'post_title', $t, 'raw' ); |
| 102 | $topic_content = get_post_field( 'post_content', $t, 'raw' ); |
| 103 | $topic_link = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>'; |
| 104 | $forum_permalink = bbp_get_forum_permalink( $f ); |
| 105 | $forum_title = get_post_field( 'post_title', $f, 'raw' ); |
| 106 | $forum_link = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>'; |
| 107 | $activity_text = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 108 | |
| 109 | // Create the activity. |
| 110 | bbpress()->extend->buddypress->activity->reply_create( $r, $t, $f, array(), $u ); |
| 111 | |
| 112 | $activity_id = (int) get_post_meta( $r, '_bbp_activity_id', true ); |
| 113 | $activity = new BP_Activity_Activity( $activity_id ); |
| 114 | |
| 115 | // Test the default generated string. |
| 116 | $this->assertEquals( $activity_text, $activity->action ); |
| 117 | |
| 118 | // Update a few items for testing. |
| 119 | wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) ); |
| 120 | $user_link = bbp_get_user_profile_link( $u ); |
| 121 | |
| 122 | wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) ); |
| 123 | $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>'; |
| 124 | |
| 125 | wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) ); |
| 126 | $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>'; |
| 127 | |
| 128 | // Set up our new test string. |
| 129 | $activity_text = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 130 | |
| 131 | $activity = new BP_Activity_Activity( $activity_id ); |
| 132 | |
| 133 | // Are we dynamic? |
| 134 | $this->assertEquals( $activity_text, $activity->action ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Dynamic activity actions for group forum topics. |
| 139 | * |
| 140 | * @since 2.6.0 bbPress (rXXXX) |
| 141 | * |
| 142 | * @ticket BBP2794 |
| 143 | */ |
| 144 | public function test_bp_activity_actions_for_group_forum_topic() { |
| 145 | $g = $this->bp_factory->group->create(); |
| 146 | $group = groups_get_group( array( 'group_id' => $g ) ); |
| 147 | $u = $group->creator_id; |
| 148 | $f = $this->factory->forum->create(); |
| 149 | $t = $this->factory->topic->create( array( |
| 150 | 'post_parent' => $f, |
| 151 | 'post_author' => $u, |
| 152 | ) ); |
| 153 | $r = $this->factory->reply->create( array( |
| 154 | 'post_parent' => $t, |
| 155 | 'post_author' => $u, |
| 156 | ) ); |
| 157 | $this->attach_forum_to_group( $f, $g ); |
| 158 | buddypress()->groups->current_group = $group; |
| 159 | |
| 160 | // Set up our activity text test string. |
| 161 | $user_link = bbp_get_user_profile_link( $u ); |
| 162 | $topic_permalink = bbp_get_topic_permalink( $t ); |
| 163 | $topic_title = get_post_field( 'post_title', $t, 'raw' ); |
| 164 | $topic_link = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>'; |
| 165 | $forum_permalink = bbp_get_forum_permalink( $f ); |
| 166 | $forum_title = get_post_field( 'post_title', $f, 'raw' ); |
| 167 | $forum_link = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>'; |
| 168 | $activity_text = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 169 | |
| 170 | // Create the activity. |
| 171 | bbpress()->extend->buddypress->activity->topic_create( $t, $f, array(), $u ); |
| 172 | |
| 173 | $activity_id = (int) get_post_meta( $t, '_bbp_activity_id', true ); |
| 174 | $activity = new BP_Activity_Activity( $activity_id ); |
| 175 | |
| 176 | // Test the default generated string. |
| 177 | $this->assertEquals( $activity_text, $activity->action ); |
| 178 | |
| 179 | // Update a few items for testing. |
| 180 | wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) ); |
| 181 | $user_link = bbp_get_user_profile_link( $u ); |
| 182 | |
| 183 | wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) ); |
| 184 | $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>'; |
| 185 | |
| 186 | wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) ); |
| 187 | $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>'; |
| 188 | |
| 189 | // Set up our new test string. |
| 190 | $activity_text = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 191 | |
| 192 | $activity = new BP_Activity_Activity( $activity_id ); |
| 193 | |
| 194 | // Are we dynamic? |
| 195 | $this->assertEquals( $activity_text, $activity->action ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Dynamic activity actions for replies to group forum topics. |
| 200 | * |
| 201 | * @since 2.6.0 bbPress (rXXXX) |
| 202 | * |
| 203 | * @ticket BBP2794 |
| 204 | */ |
| 205 | public function test_bp_activity_actions_for_reply_to_group_forum_topic() { |
| 206 | $g = $this->bp_factory->group->create(); |
| 207 | $group = groups_get_group( array( 'group_id' => $g ) ); |
| 208 | $u = $group->creator_id; |
| 209 | $f = $this->factory->forum->create(); |
| 210 | $t = $this->factory->topic->create( array( |
| 211 | 'post_parent' => $f, |
| 212 | 'post_author' => $u, |
| 213 | ) ); |
| 214 | $r = $this->factory->reply->create( array( |
| 215 | 'post_parent' => $t, |
| 216 | 'post_author' => $u, |
| 217 | ) ); |
| 218 | $this->attach_forum_to_group( $f, $g ); |
| 219 | buddypress()->groups->current_group = $group; |
| 220 | |
| 221 | // Set up our activity text test string. |
| 222 | $user_link = bbp_get_user_profile_link( $u ); |
| 223 | $topic_permalink = bbp_get_topic_permalink( $t ); |
| 224 | $topic_title = get_post_field( 'post_title', $t, 'raw' ); |
| 225 | $topic_content = get_post_field( 'post_content', $t, 'raw' ); |
| 226 | $topic_link = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>'; |
| 227 | $forum_permalink = bbp_get_forum_permalink( $f ); |
| 228 | $forum_title = get_post_field( 'post_title', $f, 'raw' ); |
| 229 | $forum_link = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>'; |
| 230 | $activity_text = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 231 | |
| 232 | // Create the activity. |
| 233 | bbpress()->extend->buddypress->activity->reply_create( $r, $t, $f, array(), $u ); |
| 234 | |
| 235 | $activity_id = (int) get_post_meta( $r, '_bbp_activity_id', true ); |
| 236 | $activity = new BP_Activity_Activity( $activity_id ); |
| 237 | |
| 238 | // Test the default generated string. |
| 239 | $this->assertEquals( $activity_text, $activity->action ); |
| 240 | |
| 241 | // Update a few items for testing. |
| 242 | wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) ); |
| 243 | $user_link = bbp_get_user_profile_link( $u ); |
| 244 | |
| 245 | wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) ); |
| 246 | $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>'; |
| 247 | |
| 248 | wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) ); |
| 249 | $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>'; |
| 250 | |
| 251 | // Set up our new test string. |
| 252 | $activity_text = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link ); |
| 253 | |
| 254 | $activity = new BP_Activity_Activity( $activity_id ); |
| 255 | |
| 256 | // Are we dynamic? |
| 257 | $this->assertEquals( $activity_text, $activity->action ); |
| 258 | } |
| 259 | } |