diff --git a/features/super-admin.feature b/features/super-admin.feature index 05ff00f983..4c8b543d83 100644 --- a/features/super-admin.feature +++ b/features/super-admin.feature @@ -10,7 +10,13 @@ Feature: Manage super admins associated with a multisite instance """ When I run `wp super-admin add superadmin` - And I run `wp super-admin list` + Then STDOUT should be: + """ + Success: Granted super-admin capabilities to 1 user. + """ + And the return code should be 0 + + When I run `wp super-admin list` Then STDOUT should be: """ admin @@ -18,10 +24,15 @@ Feature: Manage super admins associated with a multisite instance """ When I run `wp super-admin add superadmin` - Then STDERR should contain: + Then STDERR should be: """ Warning: User 'superadmin' already has super-admin capabilities. """ + And STDOUT should be: + """ + Success: Super admins remain unchanged. + """ + And the return code should be 0 When I run `wp super-admin list` Then STDOUT should be: @@ -48,3 +59,19 @@ Feature: Manage super admins associated with a multisite instance """ [{"user_login":"superadmin"}] """ + + When I try `wp super-admin add noadmin` + Then STDERR should be: + """ + Warning: Invalid user ID, email or login: 'noadmin' + Error: Couldn't grant super-admin capabilities to 1 of 1 users. + """ + And the return code should be 1 + + When I try `wp super-admin add admin noadmin` + Then STDERR should be: + """ + Warning: Invalid user ID, email or login: 'noadmin' + Error: Only granted super-admin capabilities to 1 of 2 users. + """ + And the return code should be 1 diff --git a/php/commands/super-admin.php b/php/commands/super-admin.php index ce46b64961..4c79be9f42 100644 --- a/php/commands/super-admin.php +++ b/php/commands/super-admin.php @@ -94,32 +94,41 @@ public function _list( $_, $assoc_args ) { */ public function add( $args, $_ ) { + $successes = $errors = 0; $users = $this->fetcher->get_many( $args ); + if ( count( $users ) != count( $args ) ) { + $errors = count( $args ) - count( $users ); + } $user_logins = wp_list_pluck( $users, 'user_login' ); $super_admins = self::get_admins(); $num_super_admins = count( $super_admins ); foreach ( $user_logins as $user_login ) { - $user = get_user_by( 'login', $user_login ); - - if ( !$user ) { - WP_CLI::warning( "Couldn't find '{$user_login}' user." ); - continue; - } - - if ( in_array( $user->user_login, $super_admins ) ) { + if ( in_array( $user_login, $super_admins ) ) { WP_CLI::warning( "User '{$user_login}' already has super-admin capabilities." ); continue; } - $super_admins[] = $user->user_login; + $super_admins[] = $user_login; + $successes++; } if ( $num_super_admins === count( $super_admins ) ) { - WP_CLI::log( 'No changes.' ); + if ( $errors ) { + $user_count = count( $args ); + WP_CLI::error( "Couldn't grant super-admin capabilities to {$errors} of {$user_count} users." ); + } else { + WP_CLI::success( 'Super admins remain unchanged.' ); + } } else { if ( update_site_option( 'site_admins' , $super_admins ) ) { - WP_CLI::success( 'Granted super-admin capabilities.' ); + if ( $errors ) { + $user_count = count( $args ); + WP_CLI::error( "Only granted super-admin capabilities to {$successes} of {$user_count} users." ); + } else { + $message = $successes > 1 ? 'users' : 'user'; + WP_CLI::success( "Granted super-admin capabilities to {$successes} {$message}." ); + } } else { WP_CLI::error( 'Site options update failed.' ); }