|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @covers ::invite_anyone_access_test() |
| 5 | + */ |
| 6 | +class Invite_Anyone_Access_Test_Tests extends BP_UnitTestCase { |
| 7 | + static $user_id; |
| 8 | + |
| 9 | + public static function setUpBeforeClass() { |
| 10 | + $f = new BP_UnitTest_Factory(); |
| 11 | + self::$user_id = $f->user->create( array( |
| 12 | + 'role' => 'subscriber', |
| 13 | + ) ); |
| 14 | + } |
| 15 | + |
| 16 | + public static function tearDownAfterClass() { |
| 17 | + self::delete_user( self::$user_id ); |
| 18 | + } |
| 19 | + |
| 20 | + public function test_anon_user_should_not_have_access() { |
| 21 | + $this->assertSame( 0, bp_loggedin_user_id() ); |
| 22 | + $this->assertFalse( invite_anyone_access_test() ); |
| 23 | + } |
| 24 | + |
| 25 | + public function test_admin_user_should_have_access() { |
| 26 | + $admin_id = $this->factory->user->create(); |
| 27 | + $this->grant_bp_moderate( $admin_id ); |
| 28 | + |
| 29 | + $this->set_current_user( $admin_id ); |
| 30 | + |
| 31 | + $this->assertTrue( invite_anyone_access_test() ); |
| 32 | + } |
| 33 | + |
| 34 | + public function test_user_viewing_others_profile_should_not_have_access() { |
| 35 | + $this->set_current_user( self::$user_id ); |
| 36 | + |
| 37 | + $other_user_id = $this->factory->user->create(); |
| 38 | + $this->go_to( bp_core_get_user_domain( $other_user_id ) ); |
| 39 | + |
| 40 | + $this->assertFalse( invite_anyone_access_test() ); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_user_viewing_own_profile_should_have_access_in_absence_of_other_limits() { |
| 44 | + $iaoptions = invite_anyone_options(); |
| 45 | + $iaoptions['email_visibility_toggle'] = 'no_limit'; |
| 46 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 47 | + |
| 48 | + $this->set_current_user( self::$user_id ); |
| 49 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 50 | + |
| 51 | + $this->assertTrue( invite_anyone_access_test() ); |
| 52 | + } |
| 53 | + |
| 54 | + public function test_email_since_toggle_failure() { |
| 55 | + $iaoptions = invite_anyone_options(); |
| 56 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 57 | + $iaoptions['email_since_toggle'] = 'yes'; |
| 58 | + $iaoptions['days_since'] = 3; |
| 59 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 60 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 61 | + |
| 62 | + $two_days_ago = date( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) ); |
| 63 | + $updated = wp_update_user( array( |
| 64 | + 'ID' => self::$user_id, |
| 65 | + 'user_registered' => $two_days_ago, |
| 66 | + ) ); |
| 67 | + |
| 68 | + $this->set_current_user( self::$user_id ); |
| 69 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 70 | + |
| 71 | + $this->assertFalse( invite_anyone_access_test() ); |
| 72 | + } |
| 73 | + |
| 74 | + public function test_email_since_toggle_success() { |
| 75 | + $iaoptions = invite_anyone_options(); |
| 76 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 77 | + $iaoptions['email_since_toggle'] = 'yes'; |
| 78 | + $iaoptions['days_since'] = 1; |
| 79 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 80 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 81 | + |
| 82 | + $two_days_ago = date( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) ); |
| 83 | + $updated = wp_update_user( array( |
| 84 | + 'ID' => self::$user_id, |
| 85 | + 'user_registered' => $two_days_ago, |
| 86 | + ) ); |
| 87 | + |
| 88 | + $this->set_current_user( self::$user_id ); |
| 89 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 90 | + |
| 91 | + $this->assertTrue( invite_anyone_access_test() ); |
| 92 | + } |
| 93 | + |
| 94 | + public function test_minimum_role_subscriber() { |
| 95 | + $iaoptions = invite_anyone_options(); |
| 96 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 97 | + $iaoptions['email_since_toggle'] = 'no'; |
| 98 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 99 | + $iaoptions['minimum_role'] = 'Subscriber'; |
| 100 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 101 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 102 | + |
| 103 | + $user = new WP_User( self::$user_id ); |
| 104 | + $user->remove_role( 'subscriber' ); |
| 105 | + |
| 106 | + $this->set_current_user( self::$user_id ); |
| 107 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 108 | + |
| 109 | + $this->assertFalse( invite_anyone_access_test() ); |
| 110 | + } |
| 111 | + |
| 112 | + public function test_minimum_role_subscriber_success() { |
| 113 | + $iaoptions = invite_anyone_options(); |
| 114 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 115 | + $iaoptions['email_since_toggle'] = 'no'; |
| 116 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 117 | + $iaoptions['minimum_role'] = 'Subscriber'; |
| 118 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 119 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 120 | + |
| 121 | + $user = new WP_User( self::$user_id ); |
| 122 | + |
| 123 | + $this->set_current_user( self::$user_id ); |
| 124 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 125 | + |
| 126 | + $this->assertTrue( invite_anyone_access_test() ); |
| 127 | + } |
| 128 | + |
| 129 | + public function test_minimum_role_contributor() { |
| 130 | + $iaoptions = invite_anyone_options(); |
| 131 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 132 | + $iaoptions['email_since_toggle'] = 'no'; |
| 133 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 134 | + $iaoptions['minimum_role'] = 'Contributor'; |
| 135 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 136 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 137 | + |
| 138 | + $user = new WP_User( self::$user_id ); |
| 139 | + $user->remove_role( 'contributor' ); |
| 140 | + |
| 141 | + $this->set_current_user( self::$user_id ); |
| 142 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 143 | + |
| 144 | + $this->assertFalse( invite_anyone_access_test() ); |
| 145 | + } |
| 146 | + |
| 147 | + public function test_minimum_role_contributor_success() { |
| 148 | + $iaoptions = invite_anyone_options(); |
| 149 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 150 | + $iaoptions['email_since_toggle'] = 'no'; |
| 151 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 152 | + $iaoptions['minimum_role'] = 'Contributor'; |
| 153 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 154 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 155 | + |
| 156 | + $user = new WP_User( self::$user_id ); |
| 157 | + $user->add_role( 'contributor' ); |
| 158 | + |
| 159 | + $this->set_current_user( self::$user_id ); |
| 160 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 161 | + |
| 162 | + $this->assertTrue( invite_anyone_access_test() ); |
| 163 | + } |
| 164 | + |
| 165 | + public function test_minimum_role_author() { |
| 166 | + $iaoptions = invite_anyone_options(); |
| 167 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 168 | + $iaoptions['email_since_toggle'] = 'no'; |
| 169 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 170 | + $iaoptions['minimum_role'] = 'Author'; |
| 171 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 172 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 173 | + |
| 174 | + $user = new WP_User( self::$user_id ); |
| 175 | + $user->remove_role( 'author' ); |
| 176 | + |
| 177 | + $this->set_current_user( self::$user_id ); |
| 178 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 179 | + |
| 180 | + $this->assertFalse( invite_anyone_access_test() ); |
| 181 | + } |
| 182 | + |
| 183 | + public function test_minimum_role_author_success() { |
| 184 | + $iaoptions = invite_anyone_options(); |
| 185 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 186 | + $iaoptions['email_since_toggle'] = 'no'; |
| 187 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 188 | + $iaoptions['minimum_role'] = 'Author'; |
| 189 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 190 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 191 | + |
| 192 | + $user = new WP_User( self::$user_id ); |
| 193 | + $user->add_role( 'author' ); |
| 194 | + |
| 195 | + $this->set_current_user( self::$user_id ); |
| 196 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 197 | + |
| 198 | + $this->assertTrue( invite_anyone_access_test() ); |
| 199 | + } |
| 200 | + |
| 201 | + public function test_minimum_role_editor() { |
| 202 | + $iaoptions = invite_anyone_options(); |
| 203 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 204 | + $iaoptions['email_since_toggle'] = 'no'; |
| 205 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 206 | + $iaoptions['minimum_role'] = 'Editor'; |
| 207 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 208 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 209 | + |
| 210 | + $user = new WP_User( self::$user_id ); |
| 211 | + $user->remove_role( 'editor' ); |
| 212 | + |
| 213 | + $this->set_current_user( self::$user_id ); |
| 214 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 215 | + |
| 216 | + $this->assertFalse( invite_anyone_access_test() ); |
| 217 | + } |
| 218 | + |
| 219 | + public function test_minimum_role_editor_success() { |
| 220 | + $iaoptions = invite_anyone_options(); |
| 221 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 222 | + $iaoptions['email_since_toggle'] = 'no'; |
| 223 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 224 | + $iaoptions['minimum_role'] = 'Editor'; |
| 225 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 226 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 227 | + |
| 228 | + $user = new WP_User( self::$user_id ); |
| 229 | + $user->add_role( 'editor' ); |
| 230 | + |
| 231 | + $this->set_current_user( self::$user_id ); |
| 232 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 233 | + |
| 234 | + $this->assertTrue( invite_anyone_access_test() ); |
| 235 | + } |
| 236 | + |
| 237 | + public function test_minimum_role_administrator() { |
| 238 | + $iaoptions = invite_anyone_options(); |
| 239 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 240 | + $iaoptions['email_since_toggle'] = 'no'; |
| 241 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 242 | + $iaoptions['minimum_role'] = 'Editor'; |
| 243 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 244 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 245 | + |
| 246 | + $user = new WP_User( self::$user_id ); |
| 247 | + $user->remove_role( 'administrator' ); |
| 248 | + |
| 249 | + $this->set_current_user( self::$user_id ); |
| 250 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 251 | + |
| 252 | + $this->assertFalse( invite_anyone_access_test() ); |
| 253 | + } |
| 254 | + |
| 255 | + public function test_minimum_role_administrator_success() { |
| 256 | + $iaoptions = invite_anyone_options(); |
| 257 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 258 | + $iaoptions['email_since_toggle'] = 'no'; |
| 259 | + $iaoptions['email_role_toggle'] = 'yes'; |
| 260 | + $iaoptions['minimum_role'] = 'Administrator'; |
| 261 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 262 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 263 | + |
| 264 | + $user = new WP_User( self::$user_id ); |
| 265 | + $user->add_role( 'administrator' ); |
| 266 | + |
| 267 | + $this->set_current_user( self::$user_id ); |
| 268 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 269 | + |
| 270 | + $this->assertTrue( invite_anyone_access_test() ); |
| 271 | + } |
| 272 | + |
| 273 | + public function test_blacklist_failure() { |
| 274 | + $iaoptions = invite_anyone_options(); |
| 275 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 276 | + $iaoptions['email_since_toggle'] = 'no'; |
| 277 | + $iaoptions['email_role_toggle'] = 'no'; |
| 278 | + $iaoptions['email_blacklist_toggle'] = 'yes'; |
| 279 | + $iaoptions['email_blacklist'] = '300,' . self::$user_id . ',400'; |
| 280 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 281 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 282 | + |
| 283 | + $this->set_current_user( self::$user_id ); |
| 284 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 285 | + |
| 286 | + $this->assertFalse( invite_anyone_access_test() ); |
| 287 | + } |
| 288 | + |
| 289 | + public function test_blacklist_success() { |
| 290 | + $iaoptions = invite_anyone_options(); |
| 291 | + $iaoptions['email_visibility_toggle'] = 'limit'; |
| 292 | + $iaoptions['email_since_toggle'] = 'no'; |
| 293 | + $iaoptions['email_role_toggle'] = 'no'; |
| 294 | + $iaoptions['email_blacklist_toggle'] = 'yes'; |
| 295 | + $iaoptions['email_blacklist'] = ''; |
| 296 | + bp_update_option( 'invite_anyone', $iaoptions ); |
| 297 | + $GLOBALS['iaoptions'] = $iaoptions; |
| 298 | + |
| 299 | + $this->set_current_user( self::$user_id ); |
| 300 | + $this->go_to( bp_core_get_user_domain( self::$user_id ) ); |
| 301 | + |
| 302 | + $this->assertTrue( invite_anyone_access_test() ); |
| 303 | + } |
| 304 | +} |
0 commit comments