[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @group groups 5 * @group activity 6 */ 7 class BP_Tests_Groups_Activity extends BP_UnitTestCase { 8 protected $groups_post_update_args; 9 10 /** 11 * @group activity_action 12 * @group bp_groups_format_activity_action_created_group 13 */ 14 public function test_bp_groups_format_activity_action_created_group() { 15 $u = self::factory()->user->create(); 16 $g = self::factory()->group->create(); 17 $a = self::factory()->activity->create( array( 18 'component' => buddypress()->groups->id, 19 'type' => 'created_group', 20 'user_id' => $u, 21 'item_id' => $g, 22 ) ); 23 24 $a_obj = new BP_Activity_Activity( $a ); 25 $g_obj = groups_get_group( $g ); 26 27 $expected = sprintf( __( '%s created the group %s', 'buddypress' ), bp_core_get_userlink( $u ), '<a href="' . bp_get_group_permalink( $g_obj ) . '">' . $g_obj->name . '</a>' ); 28 29 $this->assertSame( $expected, $a_obj->action ); 30 } 31 32 /** 33 * @group activity_action 34 * @group bp_groups_format_activity_action_joined_group 35 */ 36 public function test_bp_groups_format_activity_action_joined_group() { 37 $u = self::factory()->user->create(); 38 $g = self::factory()->group->create(); 39 $a = self::factory()->activity->create( array( 40 'component' => buddypress()->groups->id, 41 'type' => 'joined_group', 42 'user_id' => $u, 43 'item_id' => $g, 44 ) ); 45 46 $a_obj = new BP_Activity_Activity( $a ); 47 $g_obj = groups_get_group( $g ); 48 49 $expected = sprintf( __( '%s joined the group %s', 'buddypress' ), bp_core_get_userlink( $u ), '<a href="' . bp_get_group_permalink( $g_obj ) . '">' . $g_obj->name . '</a>' ); 50 51 $this->assertSame( $expected, $a_obj->action ); 52 } 53 54 /** 55 * @group activity_action 56 * @group bp_groups_format_activity_action_group_details_updated 57 */ 58 public function test_bp_groups_format_activity_action_group_details_updated_with_no_change() { 59 $group = self::factory()->group->create_and_get(); 60 groups_edit_base_group_details( array( 61 'group_id' => $group->id, 62 'name' => $group->name, 63 'slug' => $group->slug, 64 'description' => $group->description, 65 'notify_members' => true, 66 ) ); 67 68 $a = bp_activity_get( array( 69 'component' => buddypress()->groups->id, 70 'action' => 'group_details_updated', 71 'item_id' => $group->id, 72 ) ); 73 74 $this->assertTrue( empty( $a['activities'] ) ); 75 } 76 77 /** 78 * @group activity_action 79 * @group bp_groups_format_activity_action_group_details_updated 80 */ 81 public function test_bp_groups_format_activity_action_group_details_updated_with_notify_members_false() { 82 $group = self::factory()->group->create_and_get(); 83 groups_edit_base_group_details( array( 84 'group_id' => $group->id, 85 'name' => 'Foo', 86 'slug' => $group->slug, 87 'description' => $group->description, 88 'notify_members' => false, 89 ) ); 90 91 $a = bp_activity_get( array( 92 'component' => buddypress()->groups->id, 93 'action' => 'group_details_updated', 94 'item_id' => $group->id, 95 ) ); 96 97 $this->assertTrue( empty( $a['activities'] ) ); 98 } 99 100 /** 101 * @group activity_action 102 * @group bp_groups_format_activity_action_group_details_updated 103 */ 104 public function test_bp_groups_format_activity_action_group_details_updated_with_updated_name() { 105 $old_user = get_current_user_id(); 106 $u = self::factory()->user->create(); 107 $this->set_current_user( $u ); 108 109 $group = self::factory()->group->create_and_get(); 110 groups_edit_base_group_details( array( 111 'group_id' => $group->id, 112 'name' => 'Foo', 113 'slug' => $group->slug, 114 'description' => $group->description, 115 'notify_members' => true, 116 ) ); 117 118 $a = bp_activity_get( array( 119 'component' => buddypress()->groups->id, 120 'action' => 'group_details_updated', 121 'item_id' => $group->id, 122 ) ); 123 124 $this->assertNotEmpty( $a['activities'] ); 125 126 $expected = sprintf( esc_html__( '%s changed the name of the group %s from "%s" to "%s"', 'buddypress' ), bp_core_get_userlink( $u ), '<a href="' . bp_get_group_permalink( $group ) . '">Foo</a>', $group->name, 'Foo' ); 127 $this->assertSame( $expected, $a['activities'][0]->action ); 128 129 $this->set_current_user( $old_user ); 130 } 131 132 /** 133 * @group activity_action 134 * @group bp_groups_format_activity_action_group_details_updated 135 */ 136 public function test_bp_groups_format_activity_action_group_details_updated_with_updated_description() { 137 $old_user = get_current_user_id(); 138 $u = self::factory()->user->create(); 139 $this->set_current_user( $u ); 140 141 $group = self::factory()->group->create_and_get(); 142 groups_edit_base_group_details( array( 143 'group_id' => $group->id, 144 'name' => $group->name, 145 'slug' => $group->slug, 146 'description' => 'Bar', 147 'notify_members' => true, 148 ) ); 149 150 $a = bp_activity_get( array( 151 'component' => buddypress()->groups->id, 152 'action' => 'group_details_updated', 153 'item_id' => $group->id, 154 ) ); 155 156 $this->assertNotEmpty( $a['activities'] ); 157 158 $expected = sprintf( esc_html__( '%s changed the description of the group %s from "%s" to "%s"', 'buddypress' ), bp_core_get_userlink( $u ), '<a href="' . bp_get_group_permalink( $group ) . '">' . $group->name . '</a>', $group->description, 'Bar' ); 159 $this->assertSame( $expected, $a['activities'][0]->action ); 160 161 $this->set_current_user( $old_user ); 162 } 163 164 /** 165 * @group activity_action 166 * @group bp_groups_format_activity_action_group_details_updated 167 */ 168 public function test_bp_groups_format_activity_action_group_details_updated_with_updated_slug() { 169 $old_user = get_current_user_id(); 170 $u = self::factory()->user->create(); 171 $this->set_current_user( $u ); 172 173 $group = self::factory()->group->create_and_get(); 174 groups_edit_base_group_details( array( 175 'group_id' => $group->id, 176 'name' => $group->name, 177 'slug' => 'flaxen', 178 'description' => $group->description, 179 'notify_members' => true, 180 ) ); 181 $new_group_details = groups_get_group( $group->id ); 182 183 $a = bp_activity_get( array( 184 'component' => buddypress()->groups->id, 185 'action' => 'group_details_updated', 186 'item_id' => $group->id, 187 ) ); 188 189 $this->assertNotEmpty( $a['activities'] ); 190 191 $expected = sprintf( __( '%s changed the permalink of the group %s.', 'buddypress' ), bp_core_get_userlink( $u ), '<a href="' . bp_get_group_permalink( $new_group_details ) . '">' . $group->name . '</a>' ); 192 $this->assertSame( $expected, $a['activities'][0]->action ); 193 194 $this->set_current_user( $old_user ); 195 } 196 197 /** 198 * @group activity_action 199 * @group bp_groups_format_activity_action_group_details_updated 200 */ 201 public function test_bp_groups_format_activity_action_group_details_updated_with_updated_name_and_description() { 202 $old_user = get_current_user_id(); 203 $u = self::factory()->user->create(); 204 $this->set_current_user( $u ); 205 206 $group = self::factory()->group->create_and_get(); 207 groups_edit_base_group_details( array( 208 'group_id' => $group->id, 209 'name' => 'Foo', 210 'slug' => $group->slug, 211 'description' => 'Bar', 212 'notify_members' => true, 213 ) ); 214 215 $a = bp_activity_get( array( 216 'component' => buddypress()->groups->id, 217 'action' => 'group_details_updated', 218 'item_id' => $group->id, 219 ) ); 220 221 $this->assertNotEmpty( $a['activities'] ); 222 223 $expected = sprintf( __( '%s changed the name and description of the group %s', 'buddypress' ), bp_core_get_userlink( $u ), '<a href="' . bp_get_group_permalink( $group ) . '">Foo</a>' ); 224 $this->assertSame( $expected, $a['activities'][0]->action ); 225 226 $this->set_current_user( $old_user ); 227 } 228 229 /** 230 * @group activity_action 231 * @group bp_groups_format_activity_action_group_activity_update 232 */ 233 public function test_bp_groups_format_activity_action_group_activity_update() { 234 $u = self::factory()->user->create(); 235 $g = self::factory()->group->create(); 236 $a = self::factory()->activity->create( array( 237 'component' => buddypress()->groups->id, 238 'type' => 'activity_update', 239 'user_id' => $u, 240 'item_id' => $g, 241 ) ); 242 243 $a_obj = new BP_Activity_Activity( $a ); 244 $g_obj = groups_get_group( $g ); 245 246 $expected = sprintf( esc_html__( '%1$s posted an update in the group %2$s', 'buddypress' ), bp_core_get_userlink( $u ), '<a href="' . esc_url( bp_get_group_permalink( $g_obj ) ) . '">' . esc_html( $g_obj->name ) . '</a>' ); 247 248 $this->assertSame( $expected, $a_obj->action ); 249 } 250 251 /** 252 * @group groups_post_update 253 */ 254 public function test_groups_post_update() { 255 $u = self::factory()->user->create(); 256 $g = self::factory()->group->create(); 257 258 // The user is a group member. 259 groups_join_group( $g, $u ); 260 261 $activity_args = array( 262 'content' => 'Test group_post_update', 263 'user_id' => $u, 264 'group_id' => $g, 265 'error_type' => 'wp_error', 266 ); 267 268 add_filter( 'bp_before_groups_record_activity_parse_args', array( $this, 'groups_post_update_args' ), 10, 1 ); 269 270 groups_post_update( $activity_args ); 271 272 remove_filter( 'bp_before_groups_record_activity_parse_args', array( $this, 'groups_post_update_args' ), 10, 1 ); 273 274 $expected = array_merge( $activity_args, array( 'item_id' => $g ) ); 275 unset( $expected['group_id'] ); 276 277 $this->assertEquals( $expected, $this->groups_post_update_args ); 278 } 279 280 /** 281 * @group groups_post_update 282 */ 283 public function test_groups_post_update_in_group() { 284 $bp = buddypress(); 285 $u = self::factory()->user->create(); 286 $g = self::factory()->group->create(); 287 288 // The user is a group member. 289 groups_join_group( $g, $u ); 290 291 $bp->groups->current_group = groups_get_group( $g ); 292 293 $activity_args = array( 294 'content' => 'Test group_post_update in a group', 295 'user_id' => $u, 296 ); 297 298 $a = groups_post_update( $activity_args ); 299 $a_obj = new BP_Activity_Activity( $a ); 300 301 $this->assertSame( $a_obj->item_id, $g ); 302 $this->assertSame( $a_obj->component, 'groups' ); 303 304 unset( $bp->groups->current_group ); 305 } 306 307 /** 308 * @group bp_activity_can_comment 309 */ 310 public function test_groups_activity_can_comment() { 311 $old_user = get_current_user_id(); 312 $u1 = self::factory()->user->create(); 313 $u2 = self::factory()->user->create(); 314 315 $g = self::factory()->group->create(); 316 317 // User 1 is a group member, while user 2 isn't. 318 groups_join_group( $g, $u1 ); 319 320 $a = self::factory()->activity->create( array( 321 'component' => buddypress()->groups->id, 322 'type' => 'created_group', 323 'user_id' => $u1, 324 'item_id' => $g, 325 ) ); 326 327 $this->set_current_user( $u1 ); 328 if ( bp_has_activities( array( 'in' => $a ) ) ) { 329 while ( bp_activities() ) : bp_the_activity(); 330 // User 1 should be able to comment. 331 $this->assertTrue( bp_activity_can_comment() ); 332 endwhile; 333 } 334 335 $this->set_current_user( $u2 ); 336 if ( bp_has_activities( array( 'in' => $a ) ) ) { 337 while ( bp_activities() ) : bp_the_activity(); 338 // User 2 should not be able to comment. 339 $this->assertFalse( bp_activity_can_comment() ); 340 endwhile; 341 } 342 343 $this->set_current_user( $old_user ); 344 } 345 346 public function groups_post_update_args( $args = array() ) { 347 $this->groups_post_update_args = array_intersect_key( $args, array( 348 'content' => true, 349 'user_id' => true, 350 'item_id' => true, 351 'error_type' => true, 352 ) ); 353 354 return $args; 355 } 356 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |