Skip to content

Commit 36b6df0

Browse files
Copilotswissspidy
andauthored
Normalize user update editor preference flags to core string literals (#657)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 9408392 commit 36b6df0

2 files changed

Lines changed: 153 additions & 6 deletions

File tree

‎features/user.feature‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,3 +919,119 @@ Feature: Manage WordPress users
919919
"""
920920
false
921921
"""
922+
923+
Scenario: The rich editing preference is normalized when updating a user
924+
Given a WP install
925+
926+
When I run `wp user create frank frank@example.com --porcelain`
927+
Then STDOUT should be a number
928+
And save STDOUT as {USER_ID}
929+
930+
When I run `wp user update {USER_ID} --rich_editing=0`
931+
Then STDOUT should contain:
932+
"""
933+
Success: Updated user {USER_ID}.
934+
"""
935+
936+
When I run `wp user meta get {USER_ID} rich_editing`
937+
Then STDOUT should be:
938+
"""
939+
false
940+
"""
941+
942+
When I run `wp user update {USER_ID} --rich_editing=1`
943+
Then STDOUT should contain:
944+
"""
945+
Success: Updated user {USER_ID}.
946+
"""
947+
948+
When I run `wp user meta get {USER_ID} rich_editing`
949+
Then STDOUT should be:
950+
"""
951+
true
952+
"""
953+
954+
Scenario: The syntax highlighting preference is stored as the value core checks for
955+
Given a WP install
956+
957+
# wp_get_user_editor() compares the stored meta against the string 'false',
958+
# so other spellings of a boolean have to be normalised.
959+
When I run `wp user create sasha sasha@example.com --syntax_highlighting=0 --porcelain`
960+
Then STDOUT should be a number
961+
And save STDOUT as {USER_ID}
962+
963+
When I run `wp user meta get {USER_ID} syntax_highlighting`
964+
Then STDOUT should be:
965+
"""
966+
false
967+
"""
968+
969+
When I run `wp user create rosa rosa@example.com --syntax_highlighting=1 --porcelain`
970+
Then STDOUT should be a number
971+
And save STDOUT as {USER_ID}
972+
973+
When I run `wp user meta get {USER_ID} syntax_highlighting`
974+
Then STDOUT should be:
975+
"""
976+
true
977+
"""
978+
979+
Scenario: The syntax highlighting preference is normalized when updating a user
980+
Given a WP install
981+
982+
When I run `wp user create steve steve@example.com --porcelain`
983+
Then STDOUT should be a number
984+
And save STDOUT as {USER_ID}
985+
986+
When I run `wp user update {USER_ID} --syntax_highlighting=0`
987+
Then STDOUT should contain:
988+
"""
989+
Success: Updated user {USER_ID}.
990+
"""
991+
992+
When I run `wp user meta get {USER_ID} syntax_highlighting`
993+
Then STDOUT should be:
994+
"""
995+
false
996+
"""
997+
998+
When I run `wp user update {USER_ID} --syntax_highlighting=1`
999+
Then STDOUT should contain:
1000+
"""
1001+
Success: Updated user {USER_ID}.
1002+
"""
1003+
1004+
When I run `wp user meta get {USER_ID} syntax_highlighting`
1005+
Then STDOUT should be:
1006+
"""
1007+
true
1008+
"""
1009+
1010+
Scenario: Editor preferences that are not a boolean are rejected
1011+
Given a WP install
1012+
1013+
When I try `wp user create tina tina@example.com --rich_editing=maybe --porcelain`
1014+
Then STDERR should contain:
1015+
"""
1016+
Error: Invalid value for --rich_editing. Accepts 'true' or 'false', or their numeric forms 1 and 0.
1017+
"""
1018+
And STDOUT should be empty
1019+
And the return code should be 1
1020+
1021+
When I run `wp user create tina tina@example.com --porcelain`
1022+
Then STDOUT should be a number
1023+
And save STDOUT as {USER_ID}
1024+
1025+
When I try `wp user update {USER_ID} --syntax_highlighting=maybe`
1026+
Then STDERR should contain:
1027+
"""
1028+
Error: Invalid value for --syntax_highlighting. Accepts 'true' or 'false', or their numeric forms 1 and 0.
1029+
"""
1030+
And the return code should be 1
1031+
1032+
# The rejected update must not have changed the preference.
1033+
When I run `wp user meta get {USER_ID} syntax_highlighting`
1034+
Then STDOUT should be:
1035+
"""
1036+
true
1037+
"""

‎src/User_Command.php‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ function ( $user ) use ( $network, $reassign ) {
385385
* or their numeric forms 1 and 0.
386386
* Default: true
387387
*
388+
* [--syntax_highlighting=<syntax_highlighting>]
389+
* : Whether to enable the rich code editor for the user. Accepts 'true' or 'false',
390+
* or their numeric forms 1 and 0.
391+
* Default: true
392+
*
388393
* [--send-email]
389394
* : Send an email to the user with their new account details.
390395
*
@@ -403,7 +408,7 @@ function ( $user ) use ( $network, $reassign ) {
403408
* 4
404409
*
405410
* @param array{0: string, 1: string} $args Positional arguments.
406-
* @param array{role?: string, user_pass?: string, user_registered?: string, display_name?: string, user_nicename?: string, user_url?: string, nickname?: string, first_name?: string, last_name?: string, description?: string, rich_editing?: string, send_email?: bool, porcelain?: bool} $assoc_args Associative arguments.
411+
* @param array{role?: string, user_pass?: string, user_registered?: string, display_name?: string, user_nicename?: string, user_url?: string, nickname?: string, first_name?: string, last_name?: string, description?: string, rich_editing?: string, syntax_highlighting?: string, send_email?: bool, porcelain?: bool} $assoc_args Associative arguments.
407412
*/
408413
public function create( $args, $assoc_args ) {
409414
$user = new stdClass();
@@ -443,7 +448,10 @@ public function create( $args, $assoc_args ) {
443448
// Core compares the stored value against the string 'true', so any other
444449
// truthy spelling would disable the editor.
445450
$rich_editing = Utils\get_flag_value( $assoc_args, 'rich_editing', true );
446-
$user->rich_editing = filter_var( $rich_editing, FILTER_VALIDATE_BOOLEAN ) ? 'true' : 'false';
451+
$user->rich_editing = self::normalize_boolean_preference( 'rich_editing', $rich_editing );
452+
453+
$syntax_highlighting = Utils\get_flag_value( $assoc_args, 'syntax_highlighting', true );
454+
$user->syntax_highlighting = self::normalize_boolean_preference( 'syntax_highlighting', $syntax_highlighting );
447455

448456
if ( isset( $assoc_args['user_pass'] ) ) {
449457
$user->user_pass = $assoc_args['user_pass'];
@@ -544,12 +552,12 @@ public function create( $args, $assoc_args ) {
544552
* : A string containing content about the user.
545553
*
546554
* [--rich_editing=<rich_editing>]
547-
* : Whether to enable the rich editor for the user. Accepts 'true' or
548-
* 'false' as a string literal, not boolean.
555+
* : Whether to enable the rich editor for the user. Accepts 'true' or 'false',
556+
* or their numeric forms 1 and 0.
549557
*
550558
* [--syntax_highlighting=<syntax_highlighting>]
551-
* : Whether to enable the rich code editor for the user. Accepts 'true' or
552-
* 'false' as a string literal, not boolean.
559+
* : Whether to enable the rich code editor for the user. Accepts 'true' or 'false',
560+
* or their numeric forms 1 and 0.
553561
*
554562
* [--comment_shortcuts=<comment_shortcuts>]
555563
* : Whether to enable comment moderation keyboard shortcuts for the user.
@@ -608,6 +616,12 @@ public function update( $args, $assoc_args ) {
608616
self::validate_role( $assoc_args['role'], true );
609617
}
610618

619+
foreach ( [ 'rich_editing', 'syntax_highlighting' ] as $preference ) {
620+
if ( isset( $assoc_args[ $preference ] ) ) {
621+
$assoc_args[ $preference ] = self::normalize_boolean_preference( $preference, $assoc_args[ $preference ] );
622+
}
623+
}
624+
611625
$user_ids = [];
612626
foreach ( $this->fetcher->get_many( $args ) as $user ) {
613627
$user_ids[] = $user->ID;
@@ -1413,6 +1427,23 @@ public function reset_password( $args, $assoc_args ) {
14131427
}
14141428
}
14151429

1430+
/**
1431+
* Normalizes a user preference to the string literal core checks for.
1432+
*
1433+
* @param string $preference Name of the preference, used for error reporting.
1434+
* @param mixed $value Value the preference was given on the command line.
1435+
* @return string Either 'true' or 'false'.
1436+
*/
1437+
private static function normalize_boolean_preference( $preference, $value ) {
1438+
$normalized = filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
1439+
1440+
if ( null === $normalized ) {
1441+
WP_CLI::error( "Invalid value for --{$preference}. Accepts 'true' or 'false', or their numeric forms 1 and 0." );
1442+
}
1443+
1444+
return $normalized ? 'true' : 'false';
1445+
}
1446+
14161447
/**
14171448
* Checks whether the role is valid
14181449
*

0 commit comments

Comments
 (0)