Skip to content

Commit 97a28da

Browse files
CopilotswissspidyCopilot
authored
Show literal "true"/"false" for boolean config values instead of "1"/empty string (#213)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 7e17246 commit 97a28da

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

features/config-get-field.feature

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,59 @@ Feature: Get the value of a constant or variable defined in wp-config.php and wp
293293
---
294294
- wp_cli_test
295295
"""
296+
297+
Scenario: Get a boolean true constant from wp-config.php shows literal "true"
298+
Given a WP install
299+
And a wp-config.php file:
300+
"""
301+
define( 'WP_DEBUG', true );
302+
define( 'WP_DEBUG_LOG', false );
303+
require_once( ABSPATH . 'wp-settings.php' );
304+
"""
305+
306+
When I run `wp config get WP_DEBUG`
307+
Then STDOUT should be:
308+
"""
309+
true
310+
"""
311+
312+
When I run `wp config get WP_DEBUG_LOG`
313+
Then STDOUT should be:
314+
"""
315+
false
316+
"""
317+
318+
Scenario: Get a boolean constant preserves native type for json and yaml formats
319+
Given a WP install
320+
And a wp-config.php file:
321+
"""
322+
define( 'WP_DEBUG', true );
323+
define( 'WP_DEBUG_LOG', false );
324+
require_once( ABSPATH . 'wp-settings.php' );
325+
"""
326+
327+
When I run `wp config get WP_DEBUG --format=json`
328+
Then STDOUT should be:
329+
"""
330+
true
331+
"""
332+
333+
When I run `wp config get WP_DEBUG_LOG --format=json`
334+
Then STDOUT should be:
335+
"""
336+
false
337+
"""
338+
339+
When I run `wp config get WP_DEBUG --format=yaml`
340+
Then STDOUT should be:
341+
"""
342+
---
343+
- true
344+
"""
345+
346+
When I run `wp config get WP_DEBUG_LOG --format=yaml`
347+
Then STDOUT should be:
348+
"""
349+
---
350+
- false
351+
"""

features/config-list.feature

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,70 @@ Feature: List the values of a wp-config.php file
412412
SECURE_AUTH_SALT='VNH|C>w-z?*dtP4ofy!v%RumM.}ug]mx7$QZW|C-R4T`d-~x|xvL{Xc_5C89K(,^'
413413
LOGGED_IN_SALT='Iwtez|Q`M l7lup; x&ml8^C|Lk&X[3/-l!$`P3GM$7:WI&X$Hn)unjZ9u~g4m[c'
414414
NONCE_SALT='QxcY|80 $f_dRkn*Liu|Ak*aas41g(q5X_h+m8Z$)tf6#TZ+Q,D#%n]g -{=mj1)'
415-
WP_ALLOW_MULTISITE=1
416-
MULTISITE=1
417-
SUBDOMAIN_INSTALL=''
415+
WP_ALLOW_MULTISITE=true
416+
MULTISITE=true
417+
SUBDOMAIN_INSTALL=false
418418
DOMAIN_CURRENT_SITE='example.com'
419419
PATH_CURRENT_SITE='/'
420420
SITE_ID_CURRENT_SITE=1
421421
BLOG_ID_CURRENT_SITE=1
422422
"""
423+
424+
Scenario: List boolean constants shows literal "true" and "false" in table format
425+
Given a WP install
426+
And a wp-config.php file:
427+
"""
428+
define( 'WP_DEBUG', true );
429+
define( 'WP_DEBUG_LOG', true );
430+
define( 'WP_DEBUG_DISPLAY', false );
431+
require_once( ABSPATH . 'wp-settings.php' );
432+
"""
433+
434+
When I run `wp config list --fields=name,value,type --format=table`
435+
Then STDOUT should be a table containing rows:
436+
| name | value | type |
437+
| WP_DEBUG | true | constant |
438+
| WP_DEBUG_LOG | true | constant |
439+
| WP_DEBUG_DISPLAY | false | constant |
440+
441+
Scenario: List boolean constants preserves native boolean type for json format
442+
Given a WP install
443+
And a wp-config.php file:
444+
"""
445+
define( 'WP_DEBUG', true );
446+
define( 'WP_DEBUG_DISPLAY', false );
447+
require_once( ABSPATH . 'wp-settings.php' );
448+
"""
449+
450+
When I run `wp config list WP_DEBUG --strict --format=json`
451+
Then STDOUT should contain:
452+
"""
453+
{"name":"WP_DEBUG","value":true,"type":"constant"}
454+
"""
455+
456+
When I run `wp config list WP_DEBUG_DISPLAY --strict --format=json`
457+
Then STDOUT should contain:
458+
"""
459+
{"name":"WP_DEBUG_DISPLAY","value":false,"type":"constant"}
460+
"""
461+
462+
Scenario: List boolean constants shows literal "true" and "false" in dotenv format
463+
Given a WP install
464+
And a wp-config.php file:
465+
"""
466+
define( 'WP_DEBUG', true );
467+
define( 'WP_DEBUG_DISPLAY', false );
468+
require_once( ABSPATH . 'wp-settings.php' );
469+
"""
470+
471+
When I run `wp config list WP_DEBUG --strict --format=dotenv`
472+
Then STDOUT should be:
473+
"""
474+
WP_DEBUG=true
475+
"""
476+
477+
When I run `wp config list WP_DEBUG_DISPLAY --strict --format=dotenv`
478+
Then STDOUT should be:
479+
"""
480+
WP_DEBUG_DISPLAY=false
481+
"""

src/Config_Command.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,14 @@ public function list_( $args, $assoc_args ) {
467467
return array_walk( $values, array( $this, 'print_dotenv' ) );
468468
}
469469

470+
if ( ! in_array( $assoc_args['format'], [ 'json', 'yaml' ], true ) ) {
471+
foreach ( $values as $index => $value ) {
472+
if ( is_bool( $value['value'] ) ) {
473+
$values[ $index ]['value'] = $value['value'] ? 'true' : 'false';
474+
}
475+
}
476+
}
477+
470478
Utils\format_items( $assoc_args['format'], $values, $assoc_args['fields'] );
471479
}
472480

@@ -514,6 +522,12 @@ public function list_( $args, $assoc_args ) {
514522
*/
515523
public function get( $args, $assoc_args ) {
516524
$value = $this->get_value( $assoc_args, $args );
525+
if ( is_bool( $value ) ) {
526+
$format = Utils\get_flag_value( $assoc_args, 'format' );
527+
if ( ! in_array( $format, [ 'json', 'yaml' ], true ) ) {
528+
$value = $value ? 'true' : 'false';
529+
}
530+
}
517531
WP_CLI::print_value( $value, $assoc_args );
518532
}
519533

@@ -1451,6 +1465,11 @@ private function print_dotenv( array $value ) {
14511465
$name = strtoupper( $value['name'] );
14521466
$variable_value = isset( $value['value'] ) ? $value['value'] : '';
14531467

1468+
if ( is_bool( $variable_value ) ) {
1469+
WP_CLI::line( "{$name}=" . ( $variable_value ? 'true' : 'false' ) );
1470+
return;
1471+
}
1472+
14541473
$variable_value = str_replace( "'", "\'", $variable_value );
14551474

14561475
if ( ! is_numeric( $variable_value ) ) {

0 commit comments

Comments
 (0)