Make WordPress Core

Ticket #32935: 32935.2.diff

File 32935.2.diff, 3.2 KB (added by jeremyfelt, 10 years ago)
  • src/wp-admin/includes/ms.php

     
    190190
    191191        if ( !$user->exists() )
    192192                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
    193200        /**
    194201         * Fires before a user is deleted from the network.
    195202         *
  • tests/phpunit/tests/user/multisite.php

     
    1212class Tests_Multisite_User extends WP_UnitTestCase {
    1313        protected $suppress = false;
    1414
     15        protected $super_admins = array();
     16
    1517        function setUp() {
    1618                global $wpdb;
    1719                parent::setUp();
     
    229231                $this->assertQueryTrue( 'is_author', 'is_archive' );
    230232        }
    231233
     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
    232263        /**
    233264         * @ticket 27205
    234265         */
    235266        function test_granting_super_admins() {
    236                 if ( isset( $GLOBALS['super_admins'] ) ) {
    237                         $old_global = $GLOBALS['super_admins'];
    238                         unset( $GLOBALS['super_admins'] );
    239                 }
    240 
    241267                $user_id = $this->factory->user->create();
     268                $this->unset_super_admin_global();
    242269
    243270                $this->assertFalse( is_super_admin( $user_id ) );
    244271                $this->assertFalse( revoke_super_admin( $user_id ) );
     
    258285                $this->assertTrue( is_super_admin( $user_id ) );
    259286                $this->assertTrue( revoke_super_admin( $user_id ) );
    260287                $this->assertTrue( revoke_super_admin( $second_user ) );
     288                $this->reset_super_admin_global();
     289        }
    261290
    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'] );
    264299                }
    265300        }
    266301
     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        }
    267311}
    268312
    269313endif ;