| 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(); |
| 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 | |
| 60 | $old_current_user = get_current_user_id(); |
| 61 | $u1 = $this->group->creator_id; |
| 62 | $u2 = $this->factory->user->create( array( |
| 63 | 'user_login' => 'bbPress Groups User', |
| 64 | 'user_pass' => 'password', |
| 65 | 'user_email' => 'bbp_user@example.org', |
| 66 | ) ); |
| 67 | wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) ); |
| 68 | BP_UnitTestCase::set_current_user( $u1 ); |
| 69 | BP_UnitTestCase::add_user_to_group( $u2, $this->group_id ); |
| 70 | |
| 71 | $f = $this->factory->forum->create(); |
| 72 | $t = $this->factory->topic->create( array( |
| 73 | 'post_parent' => $f, |
| 74 | 'post_author' => $u1, |
| 75 | ) ); |
| 76 | $this->attach_forum_to_group( $f, $this->group_id ); |
| 77 | |
| 78 | bbp_add_user_forum_subscription( $u2, $f ); |
| 79 | bbp_add_user_topic_subscription( $u2, $t ); |
| 80 | bbp_add_user_favorite( $u2, $t ); |
| 81 | |
| 82 | $this->set_current_filter( 'groups_ban_member' ); |
| 83 | |
| 84 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| 85 | |
| 86 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| 87 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| 88 | $this->assertFalse( bbp_is_user_favorite( $u2, $t ) ); |
| 89 | |
| 90 | $this->restore_current_filter(); |
| 91 | |
| 92 | // Restore old user |
| 93 | BP_UnitTestCase::set_current_user( $old_current_user ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe |
| 98 | */ |
| 99 | public function test_leave_group_unsubscribe_when_user_removed_from_group() { |
| 100 | |
| 101 | $old_current_user = get_current_user_id(); |
| 102 | $u1 = $this->group->creator_id; |
| 103 | $u2 = $this->factory->user->create( array( |
| 104 | 'user_login' => 'bbPress Groups User', |
| 105 | 'user_pass' => 'password', |
| 106 | 'user_email' => 'bbp_user@example.org', |
| 107 | ) ); |
| 108 | wp_update_user( array( 'ID' => $u1, 'role' => 'administrator', ) ); |
| 109 | BP_UnitTestCase::set_current_user( $u1 ); |
| 110 | BP_UnitTestCase::add_user_to_group( $u2, $this->group_id ); |
| 111 | |
| 112 | $f = $this->factory->forum->create(); |
| 113 | $t = $this->factory->topic->create( array( |
| 114 | 'post_parent' => $f, |
| 115 | 'post_author' => $u1, |
| 116 | ) ); |
| 117 | $this->attach_forum_to_group( $f, $this->group_id ); |
| 118 | |
| 119 | bbp_add_user_forum_subscription( $u2, $f ); |
| 120 | bbp_add_user_topic_subscription( $u2, $t ); |
| 121 | bbp_add_user_favorite( $u2, $t ); |
| 122 | |
| 123 | $this->set_current_filter( 'groups_remove_member' ); |
| 124 | |
| 125 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| 126 | |
| 127 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| 128 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| 129 | $this->assertFalse( bbp_is_user_favorite( $u2, $t ) ); |
| 130 | |
| 131 | $this->restore_current_filter(); |
| 132 | |
| 133 | // Restore old user |
| 134 | BP_UnitTestCase::set_current_user( $old_current_user ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @covers BBP_Forums_Group_Extension::leave_group_unsubscribe |
| 139 | */ |
| 140 | public function test_leave_group_unsubscribe_when_user_leaves_group() { |
| 141 | |
| 142 | // See https://buddypress.trac.wordpress.org/ticket/6597. |
| 143 | buddypress()->current_action = 'leave-group'; |
| 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 | BP_UnitTestCase::set_current_user( $u2 ); |
| 153 | BP_UnitTestCase::add_user_to_group( $u2, $this->group_id ); |
| 154 | |
| 155 | $f = $this->factory->forum->create(); |
| 156 | $t = $this->factory->topic->create( array( |
| 157 | 'post_parent' => $f, |
| 158 | 'post_author' => $u1, |
| 159 | ) ); |
| 160 | $this->attach_forum_to_group( $f, $this->group_id ); |
| 161 | |
| 162 | bbp_add_user_forum_subscription( $u2, $f ); |
| 163 | bbp_add_user_topic_subscription( $u2, $t ); |
| 164 | bbp_add_user_favorite( $u2, $t ); |
| 165 | |
| 166 | $this->group_extension->leave_group_unsubscribe( $this->group_id, $u2 ); |
| 167 | |
| 168 | $this->assertFalse( bbp_is_user_subscribed_to_forum( $u2, $f ) ); |
| 169 | $this->assertFalse( bbp_is_user_subscribed_to_topic( $u2, $t ) ); |
| 170 | $this->assertFalse( bbp_is_user_favorite( $u2, $t ) ); |
| 171 | |
| 172 | $this->restore_current_filter(); |
| 173 | |
| 174 | // Reset BP current action |
| 175 | buddypress()->current_action = ''; |
| 176 | |
| 177 | // Restore old user |
| 178 | BP_UnitTestCase::set_current_user( $old_current_user ); |
| 179 | } |
| 180 | } |