| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group extend |
| | 5 | * @group buddypress |
| | 6 | * @group groups |
| | 7 | * @group subscriptions |
| | 8 | */ |
| | 9 | class BBP_Tests_Extend_BuddyPress_Groups_Subscriptions extends BBP_UnitTestCase { |
| | 10 | |
| | 11 | protected $group_extension = null; |
| | 12 | protected $old_current_filter = null; |
| | 13 | protected $current_filter = null; |
| | 14 | protected $group; |
| | 15 | protected $group_id; |
| | 16 | |
| | 17 | public function setUp() { |
| | 18 | parent::setUp(); |
| | 19 | |
| | 20 | if ( ! function_exists( 'buddypress' ) ) { |
| | 21 | return; |
| | 22 | } |
| | 23 | |
| | 24 | $this->group_extension = new BBP_Forums_Group_Extension; |
| | 25 | |
| | 26 | $this->group_id = $this->bp_factory->group->create( array( 'status' => 'public' ) ); |
| | 27 | $this->group = groups_get_group( array( 'group_id' => $this->group_id ) ); |
| | 28 | } |
| | 29 | |
| | 30 | public function tearDown() { |
| | 31 | parent::tearDown(); |
| | 32 | |
| | 33 | $this->group_extension = null; |
| | 34 | } |
| | 35 | |
| | 36 | protected function set_current_filter( $filter = array() ) { |
| | 37 | global $wp_current_filter; |
| | 38 | $this->old_current_filter = $wp_current_filter; |
| | 39 | $wp_current_filter[] = $filter; |
| | 40 | } |
| | 41 | |
| | 42 | protected function restore_current_filter() { |
| | 43 | global $wp_current_filter; |
| | 44 | $wp_current_filter = $this->old_current_filter; |
| | 45 | } |
| | 46 | |
| | 47 | /** |
| | 48 | * Copied from `BBP_Forums_Group_Extension::new_forum()`. |
| | 49 | */ |
| | 50 | private function attach_forum_to_group( $forum_id, $group_id ) { |
| | 51 | bbp_add_forum_id_to_group( $group_id, $forum_id ); |
| | 52 | bbp_add_group_id_to_forum( $forum_id, $group_id ); |
| | 53 | } |
| | 54 | |
| | 55 | /** |
| | 56 | * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe |
| | 57 | */ |
| | 58 | public function test_leave_group_unsubscribe_when_user_banned_from_group() { |
| | 59 | $this->group->status = 'public'; |
| | 60 | $this->group->save(); |
| | 61 | |
| | 62 | $old_current_user = get_current_user_id(); |
| | 63 | $u1 = $this->group->creator_id; |
| | 64 | $u2 = $this->factory->user->create( array( |
| | 65 | 'user_login' => 'bbPress Groups User', |
| | 66 | 'user_pass' => 'password', |
| | 67 | 'user_email' => 'bbp_user@example.org', |
| | 68 | ) ); |
| | 69 | wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) ); |
| | 70 | BP_UnitTestCase::set_current_user( $u1 ); |
| | 71 | BP_UnitTestCase::add_user_to_group( $u2, $this->group_id ); |
| | 72 | |
| | 73 | $f = $this->factory->forum->create(); |
| | 74 | $t = $this->factory->topic->create( array( |
| | 75 | 'post_parent' => $f, |
| | 76 | 'post_author' => $u1, |
| | 77 | ) ); |
| | 78 | $this->attach_forum_to_group( $f, $this->group_id ); |
| | 79 | |
| | 80 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 81 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 82 | |
| | 83 | $this->set_current_filter( 'groups_ban_member' ); |
| | 84 | |
| | 85 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| | 86 | |
| | 87 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 88 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 89 | |
| | 90 | $this->group->status = 'private'; |
| | 91 | $this->group->save(); |
| | 92 | |
| | 93 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 94 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 95 | |
| | 96 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| | 97 | |
| | 98 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 99 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 100 | |
| | 101 | $this->group->status = 'hidden'; |
| | 102 | $this->group->save(); |
| | 103 | |
| | 104 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 105 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 106 | |
| | 107 | add_filter( 'bbp_before_group_unsubscribe_parse_args', create_function( '', " |
| | 108 | return array( |
| | 109 | 'leave' => array( |
| | 110 | 'hidden' => true, |
| | 111 | 'private' => true, |
| | 112 | 'public' => true, |
| | 113 | ), |
| | 114 | 'remove' => array( |
| | 115 | 'hidden' => true, |
| | 116 | 'private' => true, |
| | 117 | 'public' => true, |
| | 118 | ), |
| | 119 | 'ban' => array( |
| | 120 | 'hidden' => false, |
| | 121 | 'private' => true, |
| | 122 | 'public' => true, |
| | 123 | ), |
| | 124 | );" |
| | 125 | ) ); |
| | 126 | |
| | 127 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| | 128 | |
| | 129 | $this->restore_current_filter(); |
| | 130 | |
| | 131 | $this->assertTrue( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 132 | $this->assertTrue( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 133 | |
| | 134 | // Restore old user |
| | 135 | BP_UnitTestCase::set_current_user( $old_current_user ); |
| | 136 | } |
| | 137 | |
| | 138 | /** |
| | 139 | * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe |
| | 140 | */ |
| | 141 | public function test_leave_group_unsubscribe_when_user_removed_from_group() { |
| | 142 | $this->group->status = 'public'; |
| | 143 | $this->group->save(); |
| | 144 | |
| | 145 | $old_current_user = get_current_user_id(); |
| | 146 | $u1 = $this->group->creator_id; |
| | 147 | $u2 = $this->factory->user->create( array( |
| | 148 | 'user_login' => 'bbPress Groups User', |
| | 149 | 'user_pass' => 'password', |
| | 150 | 'user_email' => 'bbp_user@example.org', |
| | 151 | ) ); |
| | 152 | wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) ); |
| | 153 | BP_UnitTestCase::set_current_user( $u1 ); |
| | 154 | BP_UnitTestCase::add_user_to_group( $u2, $this->group_id ); |
| | 155 | |
| | 156 | $f = $this->factory->forum->create(); |
| | 157 | $t = $this->factory->topic->create( array( |
| | 158 | 'post_parent' => $f, |
| | 159 | 'post_author' => $u1, |
| | 160 | ) ); |
| | 161 | $this->attach_forum_to_group( $f, $this->group_id ); |
| | 162 | |
| | 163 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 164 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 165 | |
| | 166 | $this->set_current_filter( 'groups_remove_member' ); |
| | 167 | |
| | 168 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| | 169 | |
| | 170 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 171 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 172 | |
| | 173 | $this->group->status = 'private'; |
| | 174 | $this->group->save(); |
| | 175 | |
| | 176 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 177 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 178 | |
| | 179 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| | 180 | |
| | 181 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 182 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 183 | |
| | 184 | $this->group->status = 'hidden'; |
| | 185 | $this->group->save(); |
| | 186 | |
| | 187 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 188 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 189 | |
| | 190 | add_filter( 'bbp_before_group_unsubscribe_parse_args', create_function( '', " |
| | 191 | return array( |
| | 192 | 'leave' => array( |
| | 193 | 'hidden' => true, |
| | 194 | 'private' => true, |
| | 195 | 'public' => true, |
| | 196 | ), |
| | 197 | 'remove' => array( |
| | 198 | 'hidden' => false, |
| | 199 | 'private' => true, |
| | 200 | 'public' => true, |
| | 201 | ), |
| | 202 | 'ban' => array( |
| | 203 | 'hidden' => true, |
| | 204 | 'private' => true, |
| | 205 | 'public' => true, |
| | 206 | ), |
| | 207 | );" |
| | 208 | ) ); |
| | 209 | |
| | 210 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| | 211 | |
| | 212 | $this->restore_current_filter(); |
| | 213 | |
| | 214 | $this->assertTrue( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 215 | $this->assertTrue( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 216 | |
| | 217 | // Restore old user |
| | 218 | BP_UnitTestCase::set_current_user( $old_current_user ); |
| | 219 | } |
| | 220 | |
| | 221 | /** |
| | 222 | * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe |
| | 223 | */ |
| | 224 | public function test_leave_group_unsubscribe_when_user_leaves_group() { |
| | 225 | |
| | 226 | // See #BP6597. |
| | 227 | buddypress()->current_action = 'leave-group'; |
| | 228 | |
| | 229 | $g = $this->bp_factory->group->create( array( 'status' => 'public' ) ); |
| | 230 | $group = groups_get_group( array( 'group_id' => $g ) ); |
| | 231 | |
| | 232 | $old_current_user = get_current_user_id(); |
| | 233 | $u1 = $group->creator_id; |
| | 234 | $u2 = $this->factory->user->create( array( |
| | 235 | 'user_login' => 'bbPress Groups User', |
| | 236 | 'user_pass' => 'password', |
| | 237 | 'user_email' => 'bbp_user@example.org', |
| | 238 | ) ); |
| | 239 | BP_UnitTestCase::set_current_user( $u2 ); |
| | 240 | BP_UnitTestCase::add_user_to_group( $u2, $g ); |
| | 241 | |
| | 242 | $f = $this->factory->forum->create(); |
| | 243 | $t = $this->factory->topic->create( array( |
| | 244 | 'post_parent' => $f, |
| | 245 | 'post_author' => $u1, |
| | 246 | ) ); |
| | 247 | $this->attach_forum_to_group( $f, $g ); |
| | 248 | |
| | 249 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 250 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 251 | |
| | 252 | $this->group_extension->leave_group_unsubscribe( $g, $u2 ); |
| | 253 | |
| | 254 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 255 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 256 | |
| | 257 | $group->status = 'private'; |
| | 258 | $group->save(); |
| | 259 | |
| | 260 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 261 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 262 | |
| | 263 | $this->group_extension->leave_group_unsubscribe( $g, $u2 ); |
| | 264 | |
| | 265 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 266 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 267 | |
| | 268 | $group->status = 'hidden'; |
| | 269 | $group->save(); |
| | 270 | |
| | 271 | bbp_add_user_forum_subscription( $u2, $f ); |
| | 272 | bbp_add_user_topic_subscription( $u2, $t ); |
| | 273 | |
| | 274 | add_filter( 'bbp_before_group_unsubscribe_parse_args', create_function( '', " |
| | 275 | return array( |
| | 276 | 'leave' => array( |
| | 277 | 'hidden' => false, |
| | 278 | 'private' => true, |
| | 279 | 'public' => true, |
| | 280 | ), |
| | 281 | 'remove' => array( |
| | 282 | 'hidden' => true, |
| | 283 | 'private' => true, |
| | 284 | 'public' => true, |
| | 285 | ), |
| | 286 | 'ban' => array( |
| | 287 | 'hidden' => true, |
| | 288 | 'private' => true, |
| | 289 | 'public' => true, |
| | 290 | ), |
| | 291 | );" |
| | 292 | ) ); |
| | 293 | |
| | 294 | $this->group_extension->leave_group_unsubscribe( $g, $u2 ); |
| | 295 | |
| | 296 | $this->assertTrue( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| | 297 | $this->assertTrue( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| | 298 | |
| | 299 | // Reset BP current action |
| | 300 | buddypress()->current_action = ''; |
| | 301 | |
| | 302 | // Restore old user |
| | 303 | BP_UnitTestCase::set_current_user( $old_current_user ); |
| | 304 | } |
| | 305 | } |