From 5c141092e7d0940c27da29408d6b67d9db1d3934 Mon Sep 17 00:00:00 2001 From: Emmanuel Adumbire Date: Fri, 9 Jun 2023 08:27:58 -0400 Subject: [PATCH] Warn when deleting multisite user with no blog roles (#408) * Show error message when multisite user to delete has no blog roles(#403) * Fix tests and phpcs * Replace --yes removed to test * Specify expected output on subsite user delete * Make subsite user delete test less specific --- features/user.feature | 20 ++++++++++++++++++++ src/User_Command.php | 3 +++ 2 files changed, 23 insertions(+) diff --git a/features/user.feature b/features/user.feature index 8e93ca370..3537f0899 100644 --- a/features/user.feature +++ b/features/user.feature @@ -152,6 +152,26 @@ Feature: Manage WordPress users Then STDERR should not be empty And the return code should be 1 + Scenario: Trying to delete existing user with no roles from a subsite + Given a WP multisite install + + When I run `wp user create bobjones bob@example.com --role=author --url=https://example.com --porcelain` + And save STDOUT as {BOB_ID} + + When I run `wp user delete bobjones --yes` + Then STDOUT should contain: + """ + Success: Removed user + """ + And STDERR should be empty + + When I try `wp user delete bobjones --yes` + Then STDERR should be: + """ + Warning: No roles found for user {BOB_ID} on https://example.com, no users deleted. + """ + And the return code should be 1 + @require-wp-4.0 Scenario: Trying to delete super admin Given a WP multisite install diff --git a/src/User_Command.php b/src/User_Command.php index 1ba1d6db0..baf7a72c5 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -295,6 +295,9 @@ function ( $user ) use ( $network, $reassign ) { if ( $network ) { $result = wpmu_delete_user( $user_id ); $message = "Deleted user {$user_id}."; + } elseif ( is_multisite() && empty( $user->roles ) ) { + $message = "No roles found for user {$user_id} on " . home_url() . ', no users deleted.'; + return [ 'error', $message ]; } else { $result = wp_delete_user( $user_id, $reassign ); $message = "Removed user {$user_id} from " . home_url() . '.';