Ticket #32935: 32935.2.diff
| File 32935.2.diff, 3.2 KB (added by , 10 years ago) |
|---|
-
src/wp-admin/includes/ms.php
190 190 191 191 if ( !$user->exists() ) 192 192 return false; 193 194 // Global super-administrators are protected, and cannot be deleted. 195 $_super_admins = get_super_admins(); 196 if ( in_array( $user->user_login, $_super_admins, true ) ) { 197 return false; 198 } 199 193 200 /** 194 201 * Fires before a user is deleted from the network. 195 202 * -
tests/phpunit/tests/user/multisite.php
12 12 class Tests_Multisite_User extends WP_UnitTestCase { 13 13 protected $suppress = false; 14 14 15 protected $super_admins = array(); 16 15 17 function setUp() { 16 18 global $wpdb; 17 19 parent::setUp(); … … 229 231 $this->assertQueryTrue( 'is_author', 'is_archive' ); 230 232 } 231 233 234 function test_super_admin_cannot_be_deleted() { 235 $user_id = $this->factory->user->create(); 236 $this->unset_super_admin_global(); 237 grant_super_admin( $user_id ); 238 $this->assertFalse( wpmu_delete_user( $user_id ) ); 239 $this->reset_super_admin_global(); 240 } 241 242 function test_revoked_super_admin_can_be_deleted() { 243 $user_id = $this->factory->user->create(); 244 $this->unset_super_admin_global(); 245 grant_super_admin( $user_id ); 246 revoke_super_admin( $user_id ); 247 $this->assertTrue( wpmu_delete_user( $user_id ) ); 248 $this->reset_super_admin_global(); 249 } 250 251 function test_revoked_super_admin_is_deleted() { 252 $user_id = $this->factory->user->create(); 253 $this->unset_super_admin_global(); 254 grant_super_admin( $user_id ); 255 revoke_super_admin( $user_id ); 256 wpmu_delete_user( $user_id ); 257 $user = new WP_User( $user_id ); 258 259 $this->assertFalse( $user->exists(), 'WP_User->exists' ); 260 $this->reset_super_admin_global(); 261 } 262 232 263 /** 233 264 * @ticket 27205 234 265 */ 235 266 function test_granting_super_admins() { 236 if ( isset( $GLOBALS['super_admins'] ) ) {237 $old_global = $GLOBALS['super_admins'];238 unset( $GLOBALS['super_admins'] );239 }240 241 267 $user_id = $this->factory->user->create(); 268 $this->unset_super_admin_global(); 242 269 243 270 $this->assertFalse( is_super_admin( $user_id ) ); 244 271 $this->assertFalse( revoke_super_admin( $user_id ) ); … … 258 285 $this->assertTrue( is_super_admin( $user_id ) ); 259 286 $this->assertTrue( revoke_super_admin( $user_id ) ); 260 287 $this->assertTrue( revoke_super_admin( $second_user ) ); 288 $this->reset_super_admin_global(); 289 } 261 290 262 if ( isset( $old_global ) ) { 263 $GLOBALS['super_admins'] = $old_global; 291 /** 292 * Temporarily unset the super_admins global to ensure tests that rely on 293 * revoke_super_admin and grand_super_admin will run properly. 294 */ 295 function unset_super_admin_global() { 296 if ( isset( $GLOBALS['super_admins'] ) ) { 297 $this->super_admins = $GLOBALS['super_admins']; 298 unset( $GLOBALS['super_admins'] ); 264 299 } 265 300 } 266 301 302 /** 303 * Reset the super_admins global to its previous value. 304 */ 305 function reset_super_admin_global() { 306 if ( isset( $this->super_admins ) ) { 307 $GLOBALS['super_admins'] = $this->super_admins; 308 $this->super_admins = null; 309 } 310 } 267 311 } 268 312 269 313 endif ;