Skip to content

Commit 0940447

Browse files
rwagner00Ryan Wagnerpwtyler
authored
Add wp config is-true command (#161)
* CMSP-580-Is-True-Command: Provides a command for determining whether or not a variable is true. * CMSP-580-Is-True-Command: Resolves incorrect description. * CMSP-580-Is-True-Command: Renames command to is_true. * CMSP-580-Is-True-Command: Alters output to not use STDOUT and adjusts tests. * Reverse Exit Codes * change subcommand from is_true to is-true * Update usage example * Reverse exit codes in tests too * CMSP-580-Is-True-Command: Adds additional tests and updates to `boolval()`. * CMSP-580-Is-True-Command: Switches `is_true` for `is-true`. * CMSP-580-Is-True-Command: Cleans up test file with and adds DNE tests. * CMSP-580-Is-True-Command: Code sniff and switches `get_value()` to protected. * CMSP-580-Is-True-Command: Code sniff. * CMSP-580-Is-True-Command: Avoids downloading WP files for dramatic performance improvement. * Update features/config-is-true.feature description Co-authored-by: Phil Tyler <philip@tylerdigital.com> * CMSP-580-Is-True-Command: Code sniff fixes. * CMSP-580-Is-True-Command: Adds additional description. * CMSP-580-Is-True-Command: Adds additional example text. * CMSP-580-Is-True-Command: Reformats behat tests to not rely on config set and tests included files. --------- Co-authored-by: Ryan Wagner <ryan.wagner@pantheon.io> Co-authored-by: Phil Tyler <philip@tylerdigital.com>
1 parent 3292771 commit 0940447

2 files changed

Lines changed: 152 additions & 6 deletions

File tree

features/config-is-true.feature

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Feature: Determine whether the value of a constant or variable defined in wp-config.php is true.
2+
Background:
3+
Given an empty directory
4+
And a wp-includes/version.php file:
5+
"""
6+
<?php
7+
$wp_version = '6.3';
8+
"""
9+
And a wp-config.php file:
10+
"""
11+
<?php
12+
/* Truth tests. */
13+
define( 'WP_TRUTH', true );
14+
define( 'WP_STR_TRUTH', 'true' );
15+
define( 'WP_STR_MISC', 'foobar' );
16+
define( 'WP_STR_FALSE', 'false' );
17+
$wp_str_var_truth = 'true';
18+
$wp_str_var_false = 'false';
19+
$wp_str_var_misc = 'foobar';
20+
21+
/* False tests. */
22+
define( 'WP_FALSE', false );
23+
define( 'WP_STR_ZERO', '0' );
24+
define( 'WP_NUM_ZERO', 0 );
25+
$wp_variable_bool_false = false;
26+
27+
require_once ABSPATH . 'wp-settings.php';
28+
require_once ABSPATH . 'includes-file.php';
29+
"""
30+
And a includes-file.php file:
31+
"""
32+
<?php
33+
define( 'WP_INC_TRUTH', true );
34+
define( 'WP_INC_FALSE', false );
35+
"""
36+
37+
Scenario Outline: Get the value of a variable whose value is true
38+
When I try `wp config is-true <variable>`
39+
Then STDOUT should be empty
40+
Then STDERR should be empty
41+
And the return code should be 0
42+
43+
Examples:
44+
| variable |
45+
| WP_TRUTH |
46+
| WP_STR_TRUTH |
47+
| WP_STR_MISC |
48+
| WP_STR_FALSE |
49+
| wp_str_var_truth |
50+
| wp_str_var_false |
51+
| wp_str_var_misc |
52+
53+
Scenario Outline: Get the value of a variable whose value is not true
54+
When I try `wp config is-true <variable>`
55+
Then STDOUT should be empty
56+
And the return code should be 1
57+
58+
Examples:
59+
| variable |
60+
| WP_FALSE |
61+
| WP_STRZERO |
62+
| WP_NUMZERO |
63+
| wp_variable_bool_false |
64+
65+
Scenario Outline: Test for values which do not exist
66+
When I try `wp config is-true <variable> --type=<type>`
67+
Then STDOUT should be empty
68+
And the return code should be 1
69+
70+
Examples:
71+
| variable | type |
72+
| WP_TEST_CONSTANT_DNE | all |
73+
| wp_test_variable_dne | variable |
74+
75+
Scenario: Test for correct functionality with included PHP files.
76+
When I try `wp config is-true WP_INC_TRUTH`
77+
Then STDOUT should be empty
78+
Then STDERR should be empty
79+
And the return code should be 0
80+
81+
When I try `wp config is-true WP_INC_FALSE`
82+
Then STDOUT should be empty
83+
And the return code should be 1
84+

src/Config_Command.php

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,53 @@ public function list_( $args, $assoc_args ) {
421421
* @when before_wp_load
422422
*/
423423
public function get( $args, $assoc_args ) {
424-
$path = $this->get_config_path( $assoc_args );
425-
$wp_config_file_name = basename( $path );
426-
list( $name ) = $args;
427-
$type = Utils\get_flag_value( $assoc_args, 'type' );
428-
429-
$value = $this->return_value( $name, $type, self::get_wp_config_vars( $path ), $wp_config_file_name );
424+
$value = $this->get_value( $assoc_args, $args );
430425
WP_CLI::print_value( $value, $assoc_args );
431426
}
432427

428+
/**
429+
* Determines whether value of a specific constant or variable defined is truthy.
430+
* This determination is made by evaluating the retrieved value via boolval().
431+
*
432+
* ## OPTIONS
433+
*
434+
* <name>
435+
* : Name of the wp-config.php constant or variable.
436+
*
437+
* [--type=<type>]
438+
* : Type of config value to retrieve. Defaults to 'all'.
439+
* ---
440+
* default: all
441+
* options:
442+
* - constant
443+
* - variable
444+
* - all
445+
* ---
446+
*
447+
* [--config-file=<path>]
448+
* : Specify the file path to the config file to be read. Defaults to the root of the
449+
* WordPress installation and the filename "wp-config.php".
450+
*
451+
* ## EXAMPLES
452+
*
453+
* # Assert if MULTISITE is true
454+
* $ wp config is-true MULTISITE
455+
* echo $?
456+
* 0
457+
*
458+
* @subcommand is-true
459+
* @when before_wp_load
460+
*/
461+
public function is_true( $args, $assoc_args ) {
462+
$value = $this->get_value( $assoc_args, $args );
463+
464+
if ( boolval( $value ) ) {
465+
WP_CLI::halt( 0 );
466+
}
467+
WP_CLI::halt( 1 );
468+
469+
}
470+
433471
/**
434472
* Get the array of wp-config.php constants and variables.
435473
*
@@ -1008,6 +1046,30 @@ private function parse_separator( $separator ) {
10081046
return $separator;
10091047
}
10101048

1049+
/**
1050+
* Gets the value of a specific constant or variable defined in wp-config.php file.
1051+
*
1052+
* @param $assoc_args
1053+
* @param $args
1054+
*
1055+
* @return string
1056+
*/
1057+
protected function get_value( $assoc_args, $args ) {
1058+
$path = $this->get_config_path( $assoc_args );
1059+
$wp_config_file_name = basename( $path );
1060+
list( $name ) = $args;
1061+
$type = Utils\get_flag_value( $assoc_args, 'type' );
1062+
1063+
$value = $this->return_value(
1064+
$name,
1065+
$type,
1066+
self::get_wp_config_vars( $path ),
1067+
$wp_config_file_name
1068+
);
1069+
1070+
return $value;
1071+
}
1072+
10111073
/**
10121074
* Writes a provided variable's key and value to stdout, in dotenv format.
10131075
*

0 commit comments

Comments
 (0)