Skip to content

Commit 1ab4ad0

Browse files
Validate reassign user in user delete command (#482)
* Validate reassign user in user delete command * Split reassign messages * Avoid DB hit unless `$reassign` is provided --------- Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 0c41ec2 commit 1ab4ad0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

features/user.feature

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,49 @@ Feature: Manage WordPress users
136136
3
137137
"""
138138

139+
Scenario: Delete user with invalid reassign
140+
Given a WP install
141+
And a session_no file:
142+
"""
143+
n
144+
"""
145+
And a session_yes file:
146+
"""
147+
y
148+
"""
149+
150+
When I run `wp user create bobjones bob@example.com --role=author --porcelain`
151+
And save STDOUT as {BOB_ID}
152+
153+
When I run `wp post list --format=count`
154+
And save STDOUT as {TOTAL_POSTS}
155+
156+
When I run `wp post generate --count=3 --format=ids --post_author=bobjones`
157+
And I run `wp post list --author={BOB_ID} --format=count`
158+
Then STDOUT should be:
159+
"""
160+
3
161+
"""
162+
163+
When I run `wp user delete bobjones < session_no`
164+
Then STDOUT should contain:
165+
"""
166+
--reassign parameter not passed. All associated posts will be deleted. Proceed? [y/n]
167+
"""
168+
169+
When I run `wp user delete bobjones --reassign=99999 < session_no`
170+
Then STDOUT should contain:
171+
"""
172+
--reassign parameter is invalid. All associated posts will be deleted. Proceed? [y/n]
173+
"""
174+
175+
When I run `wp user delete bobjones < session_yes`
176+
And I run `wp post list --format=count`
177+
Then STDOUT should be:
178+
"""
179+
{TOTAL_POSTS}
180+
"""
181+
139182
Scenario: Deleting user from the whole network
140183
Given a WP multisite install
141184

src/User_Command.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,12 @@ public function delete( $args, $assoc_args ) {
280280
WP_CLI::error( 'Reassigning content to a different user is not supported on multisite.' );
281281
}
282282

283+
$is_reassign_valid = ( $reassign && false === get_userdata( $reassign ) ) ? false : true;
284+
283285
if ( ! $reassign ) {
284286
WP_CLI::confirm( '--reassign parameter not passed. All associated posts will be deleted. Proceed?', $assoc_args );
287+
} elseif ( ! $is_reassign_valid ) {
288+
WP_CLI::confirm( '--reassign parameter is invalid. All associated posts will be deleted. Proceed?', $assoc_args );
285289
}
286290

287291
$users = $this->fetcher->get_many( $args );

0 commit comments

Comments
 (0)