diff --git a/features/config-get-field.feature b/features/config-get-field.feature index 27309cad..c7efc84a 100644 --- a/features/config-get-field.feature +++ b/features/config-get-field.feature @@ -293,3 +293,59 @@ Feature: Get the value of a constant or variable defined in wp-config.php and wp --- - wp_cli_test """ + + Scenario: Get a boolean true constant from wp-config.php shows literal "true" + Given a WP install + And a wp-config.php file: + """ + define( 'WP_DEBUG', true ); + define( 'WP_DEBUG_LOG', false ); + require_once( ABSPATH . 'wp-settings.php' ); + """ + + When I run `wp config get WP_DEBUG` + Then STDOUT should be: + """ + true + """ + + When I run `wp config get WP_DEBUG_LOG` + Then STDOUT should be: + """ + false + """ + + Scenario: Get a boolean constant preserves native type for json and yaml formats + Given a WP install + And a wp-config.php file: + """ + define( 'WP_DEBUG', true ); + define( 'WP_DEBUG_LOG', false ); + require_once( ABSPATH . 'wp-settings.php' ); + """ + + When I run `wp config get WP_DEBUG --format=json` + Then STDOUT should be: + """ + true + """ + + When I run `wp config get WP_DEBUG_LOG --format=json` + Then STDOUT should be: + """ + false + """ + + When I run `wp config get WP_DEBUG --format=yaml` + Then STDOUT should be: + """ + --- + - true + """ + + When I run `wp config get WP_DEBUG_LOG --format=yaml` + Then STDOUT should be: + """ + --- + - false + """ diff --git a/features/config-list.feature b/features/config-list.feature index cdc669c8..8f6097ec 100644 --- a/features/config-list.feature +++ b/features/config-list.feature @@ -412,11 +412,70 @@ Feature: List the values of a wp-config.php file SECURE_AUTH_SALT='VNH|C>w-z?*dtP4ofy!v%RumM.}ug]mx7$QZW|C-R4T`d-~x|xvL{Xc_5C89K(,^' LOGGED_IN_SALT='Iwtez|Q`M l7lup; x&ml8^C|Lk&X[3/-l!$`P3GM$7:WI&X$Hn)unjZ9u~g4m[c' NONCE_SALT='QxcY|80 $f_dRkn*Liu|Ak*aas41g(q5X_h+m8Z$)tf6#TZ+Q,D#%n]g -{=mj1)' - WP_ALLOW_MULTISITE=1 - MULTISITE=1 - SUBDOMAIN_INSTALL='' + WP_ALLOW_MULTISITE=true + MULTISITE=true + SUBDOMAIN_INSTALL=false DOMAIN_CURRENT_SITE='example.com' PATH_CURRENT_SITE='/' SITE_ID_CURRENT_SITE=1 BLOG_ID_CURRENT_SITE=1 """ + + Scenario: List boolean constants shows literal "true" and "false" in table format + Given a WP install + And a wp-config.php file: + """ + define( 'WP_DEBUG', true ); + define( 'WP_DEBUG_LOG', true ); + define( 'WP_DEBUG_DISPLAY', false ); + require_once( ABSPATH . 'wp-settings.php' ); + """ + + When I run `wp config list --fields=name,value,type --format=table` + Then STDOUT should be a table containing rows: + | name | value | type | + | WP_DEBUG | true | constant | + | WP_DEBUG_LOG | true | constant | + | WP_DEBUG_DISPLAY | false | constant | + + Scenario: List boolean constants preserves native boolean type for json format + Given a WP install + And a wp-config.php file: + """ + define( 'WP_DEBUG', true ); + define( 'WP_DEBUG_DISPLAY', false ); + require_once( ABSPATH . 'wp-settings.php' ); + """ + + When I run `wp config list WP_DEBUG --strict --format=json` + Then STDOUT should contain: + """ + {"name":"WP_DEBUG","value":true,"type":"constant"} + """ + + When I run `wp config list WP_DEBUG_DISPLAY --strict --format=json` + Then STDOUT should contain: + """ + {"name":"WP_DEBUG_DISPLAY","value":false,"type":"constant"} + """ + + Scenario: List boolean constants shows literal "true" and "false" in dotenv format + Given a WP install + And a wp-config.php file: + """ + define( 'WP_DEBUG', true ); + define( 'WP_DEBUG_DISPLAY', false ); + require_once( ABSPATH . 'wp-settings.php' ); + """ + + When I run `wp config list WP_DEBUG --strict --format=dotenv` + Then STDOUT should be: + """ + WP_DEBUG=true + """ + + When I run `wp config list WP_DEBUG_DISPLAY --strict --format=dotenv` + Then STDOUT should be: + """ + WP_DEBUG_DISPLAY=false + """ diff --git a/src/Config_Command.php b/src/Config_Command.php index ffd72851..378fff13 100644 --- a/src/Config_Command.php +++ b/src/Config_Command.php @@ -467,6 +467,14 @@ public function list_( $args, $assoc_args ) { return array_walk( $values, array( $this, 'print_dotenv' ) ); } + if ( ! in_array( $assoc_args['format'], [ 'json', 'yaml' ], true ) ) { + foreach ( $values as $index => $value ) { + if ( is_bool( $value['value'] ) ) { + $values[ $index ]['value'] = $value['value'] ? 'true' : 'false'; + } + } + } + Utils\format_items( $assoc_args['format'], $values, $assoc_args['fields'] ); } @@ -514,6 +522,12 @@ public function list_( $args, $assoc_args ) { */ public function get( $args, $assoc_args ) { $value = $this->get_value( $assoc_args, $args ); + if ( is_bool( $value ) ) { + $format = Utils\get_flag_value( $assoc_args, 'format' ); + if ( ! in_array( $format, [ 'json', 'yaml' ], true ) ) { + $value = $value ? 'true' : 'false'; + } + } WP_CLI::print_value( $value, $assoc_args ); } @@ -1451,6 +1465,11 @@ private function print_dotenv( array $value ) { $name = strtoupper( $value['name'] ); $variable_value = isset( $value['value'] ) ? $value['value'] : ''; + if ( is_bool( $variable_value ) ) { + WP_CLI::line( "{$name}=" . ( $variable_value ? 'true' : 'false' ) ); + return; + } + $variable_value = str_replace( "'", "\'", $variable_value ); if ( ! is_numeric( $variable_value ) ) {