From 3e28e2fae85531dd5d5a59171cf71959f61bf5c6 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Tue, 25 Mar 2014 12:13:40 +0000 Subject: [PATCH] Multisite: Don't set the $super_admins global in grant_super_admin(), revoke_super_admin(). Adds tests and docs. props jdgrimes. see #27205. git-svn-id: https://develop.svn.wordpress.org/trunk@27706 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ms.php | 14 ++++++++------ tests/phpunit/tests/ms.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/wp-admin/includes/ms.php b/src/wp-admin/includes/ms.php index f048417186..59c28b382b 100644 --- a/src/wp-admin/includes/ms.php +++ b/src/wp-admin/includes/ms.php @@ -686,13 +686,14 @@ function choose_primary_blog() { * * @since 3.0.0 * @param int $user_id ID of the user to be granted Super Admin privileges. + * @return bool True on success, false on failure. This can fail when the user is + * already a super admin or when the $super_admins global is defined. */ function grant_super_admin( $user_id ) { - global $super_admins; - // If global super_admins override is defined, there is nothing to do here. - if ( isset( $super_admins ) ) + if ( isset( $GLOBALS['super_admins'] ) ) { return false; + } /** * Fires before the user is granted Super Admin privileges. @@ -729,13 +730,14 @@ function grant_super_admin( $user_id ) { * * @since 3.0.0 * @param int $user_id ID of the user Super Admin privileges to be revoked from. + * @return bool True on success, false on failure. This can fail when the user's email + * is the network admin email or when the $super_admins global is defined. */ function revoke_super_admin( $user_id ) { - global $super_admins; - // If global super_admins override is defined, there is nothing to do here. - if ( isset( $super_admins ) ) + if ( isset( $GLOBALS['super_admins'] ) ) { return false; + } /** * Fires before the user's Super Admin privileges are revoked. diff --git a/tests/phpunit/tests/ms.php b/tests/phpunit/tests/ms.php index 83be7d7ef6..7687e1e52f 100644 --- a/tests/phpunit/tests/ms.php +++ b/tests/phpunit/tests/ms.php @@ -1300,6 +1300,41 @@ class Tests_MS extends WP_UnitTestCase { $this->go_to( get_author_posts_url( $user_id ) ); $this->assertQueryTrue( 'is_author', 'is_archive' ); } + + /** + * @ticket 27205 + */ + function test_granting_super_admins() { + if ( isset( $GLOBALS['super_admins'] ) ) { + $old_global = $GLOBALS['super_admins']; + unset( $GLOBALS['super_admins'] ); + } + + $user_id = $this->factory->user->create(); + + $this->assertFalse( is_super_admin( $user_id ) ); + $this->assertFalse( revoke_super_admin( $user_id ) ); + $this->assertTrue( grant_super_admin( $user_id ) ); + $this->assertTrue( is_super_admin( $user_id ) ); + $this->assertFalse( grant_super_admin( $user_id ) ); + $this->assertTrue( revoke_super_admin( $user_id ) ); + + // None of these operations should set the $super_admins global. + $this->assertFalse( isset( $GLOBALS['super_admins'] ) ); + + // Try with two users. + $second_user = $this->factory->user->create(); + $this->assertTrue( grant_super_admin( $user_id ) ); + $this->assertTrue( grant_super_admin( $second_user ) ); + $this->assertTrue( is_super_admin( $second_user ) ); + $this->assertTrue( is_super_admin( $user_id ) ); + $this->assertTrue( revoke_super_admin( $user_id ) ); + $this->assertTrue( revoke_super_admin( $second_user ) ); + + if ( isset( $old_global ) ) { + $GLOBALS['super_admins'] = $old_global; + } + } } endif;