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