Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,26 @@ Feature: Manage WordPress users
| Field | Value |
| roles | |

Scenario: Invalid User Role
Given a WP install
When I run `wp user create testuser4 [email protected]`
And I try `wp user update testuser4 --role=banana`
Then STDERR should be:
"""
Warning: Role doesn't exist: banana
"""
And STDOUT should contain:
"""
Success:
"""
And the return code should be 0

When I run `wp user get admin --field=roles`
Then STDOUT should be:
"""
administrator
"""

Scenario: Managing user capabilities
Given a WP install

Expand Down
15 changes: 12 additions & 3 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ public function update( $args, $assoc_args ) {
unset( $assoc_args['user_login'] );
}

if ( isset( $assoc_args['role'] ) ) {
self::validate_role( $assoc_args['role'], true );
}

$user_ids = [];
foreach ( $this->fetcher->get_many( $args ) as $user ) {
$user_ids[] = $user->ID;
Expand Down Expand Up @@ -1156,12 +1160,17 @@ public function reset_password( $args, $assoc_args ) {
/**
* Checks whether the role is valid
*
* @param string
* @param $role string
* @param $warn_user_only bool
*/
private static function validate_role( $role ) {
private static function validate_role( $role, $warn_user_only = false ) {

if ( ! empty( $role ) && null === get_role( $role ) ) {
WP_CLI::error( "Role doesn't exist: {$role}" );
if ( $warn_user_only ) {
WP_CLI::warning( "Role doesn't exist: {$role}" );
} else {
WP_CLI::error( "Role doesn't exist: {$role}" );
}
}

}
Expand Down