Skip to content

Commit 6796f8d

Browse files
authored
Merge pull request #206 from wp-cli/copilot/add-config-add-and-update-commands
Add `wp config add` and `wp config update` commands
2 parents 487dcf1 + c98bbae commit 6796f8d

File tree

4 files changed

+592
-28
lines changed

4 files changed

+592
-28
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"bundled": true,
4242
"commands": [
4343
"config",
44+
"config add",
4445
"config edit",
4546
"config delete",
4647
"config create",
@@ -50,7 +51,8 @@
5051
"config list",
5152
"config path",
5253
"config set",
53-
"config shuffle-salts"
54+
"config shuffle-salts",
55+
"config update"
5456
]
5557
},
5658
"autoload": {

features/config-add.feature

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
Feature: Add a constant or variable to wp-config.php file
2+
3+
Scenario: Add a new constant to wp-config.php
4+
Given a WP install
5+
6+
When I run `wp config add NEW_CONSTANT constant_value`
7+
Then STDOUT should be:
8+
"""
9+
Success: Added the constant 'NEW_CONSTANT' to the 'wp-config.php' file with the value 'constant_value'.
10+
"""
11+
12+
When I run `wp config get NEW_CONSTANT`
13+
Then STDOUT should be:
14+
"""
15+
constant_value
16+
"""
17+
18+
Scenario: Add a new variable to wp-config.php
19+
Given a WP install
20+
21+
When I run `wp config add new_variable variable_value --type=variable`
22+
Then STDOUT should be:
23+
"""
24+
Success: Added the variable 'new_variable' to the 'wp-config.php' file with the value 'variable_value'.
25+
"""
26+
27+
When I run `wp config get new_variable`
28+
Then STDOUT should be:
29+
"""
30+
variable_value
31+
"""
32+
33+
Scenario: Add a raw constant to wp-config.php
34+
Given a WP install
35+
36+
When I run `wp config add WP_CUSTOM_CONSTANT true --raw`
37+
Then STDOUT should be:
38+
"""
39+
Success: Added the constant 'WP_CUSTOM_CONSTANT' to the 'wp-config.php' file with the raw value 'true'.
40+
"""
41+
42+
When I run `wp config list WP_CUSTOM_CONSTANT --strict --format=json`
43+
Then STDOUT should contain:
44+
"""
45+
{"name":"WP_CUSTOM_CONSTANT","value":true,"type":"constant"}
46+
"""
47+
48+
Scenario: Fail when trying to add an existing constant
49+
Given a WP install
50+
51+
When I run `wp config add TEST_CONSTANT test_value`
52+
Then STDOUT should be:
53+
"""
54+
Success: Added the constant 'TEST_CONSTANT' to the 'wp-config.php' file with the value 'test_value'.
55+
"""
56+
57+
When I try `wp config add TEST_CONSTANT another_value`
58+
Then STDERR should be:
59+
"""
60+
Error: The constant 'TEST_CONSTANT' already exists in the 'wp-config.php' file.
61+
"""
62+
63+
Scenario: Fail when trying to add an existing variable
64+
Given a WP install
65+
66+
When I run `wp config add test_variable test_value --type=variable`
67+
Then STDOUT should be:
68+
"""
69+
Success: Added the variable 'test_variable' to the 'wp-config.php' file with the value 'test_value'.
70+
"""
71+
72+
When I try `wp config add test_variable another_value --type=variable`
73+
Then STDERR should be:
74+
"""
75+
Error: The variable 'test_variable' already exists in the 'wp-config.php' file.
76+
"""
77+
78+
@custom-config-file
79+
Scenario: Add a new constant to wp-custom-config.php
80+
Given an empty directory
81+
And WP files
82+
83+
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check --config-file='wp-custom-config.php'`
84+
Then STDOUT should contain:
85+
"""
86+
Generated 'wp-custom-config.php' file.
87+
"""
88+
89+
When I run `wp config add NEW_CONSTANT constant_value --config-file='wp-custom-config.php'`
90+
Then STDOUT should be:
91+
"""
92+
Success: Added the constant 'NEW_CONSTANT' to the 'wp-custom-config.php' file with the value 'constant_value'.
93+
"""
94+
95+
When I run `wp config get NEW_CONSTANT --config-file='wp-custom-config.php'`
96+
Then STDOUT should be:
97+
"""
98+
constant_value
99+
"""
100+
101+
Scenario: Additions can be properly placed in wp-config.php
102+
Given a WP install
103+
And a wp-config.php file:
104+
"""
105+
define( 'CONST_A', 'val-a' );
106+
/** ANCHOR */
107+
define( 'CONST_B', 'val-b' );
108+
require_once( ABSPATH . 'wp-settings.php' );
109+
"""
110+
111+
When I run `wp config add SOME_NAME some_value --anchor="/** ANCHOR */" --placement=before --separator="\n"`
112+
Then STDOUT should be:
113+
"""
114+
Success: Added the constant 'SOME_NAME' to the 'wp-config.php' file with the value 'some_value'.
115+
"""
116+
And the wp-config.php file should be:
117+
"""
118+
define( 'CONST_A', 'val-a' );
119+
define( 'SOME_NAME', 'some_value' );
120+
/** ANCHOR */
121+
define( 'CONST_B', 'val-b' );
122+
require_once( ABSPATH . 'wp-settings.php' );
123+
"""
124+
125+
When I run `wp config add ANOTHER_NAME another_value --anchor="/** ANCHOR */" --placement=after --separator="\n"`
126+
Then STDOUT should be:
127+
"""
128+
Success: Added the constant 'ANOTHER_NAME' to the 'wp-config.php' file with the value 'another_value'.
129+
"""
130+
And the wp-config.php file should be:
131+
"""
132+
define( 'CONST_A', 'val-a' );
133+
define( 'SOME_NAME', 'some_value' );
134+
/** ANCHOR */
135+
define( 'ANOTHER_NAME', 'another_value' );
136+
define( 'CONST_B', 'val-b' );
137+
require_once( ABSPATH . 'wp-settings.php' );
138+
"""

features/config-update.feature

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
Feature: Update or add a constant or variable in wp-config.php file
2+
3+
Scenario: Update an existing constant in wp-config.php
4+
Given a WP install
5+
6+
When I run `wp config update DB_HOST db.example.com`
7+
Then STDOUT should be:
8+
"""
9+
Success: Updated the constant 'DB_HOST' in the 'wp-config.php' file with the value 'db.example.com'.
10+
"""
11+
12+
When I run `wp config get DB_HOST`
13+
Then STDOUT should be:
14+
"""
15+
db.example.com
16+
"""
17+
18+
Scenario: Add a new constant when it doesn't exist
19+
Given a WP install
20+
21+
When I run `wp config update NEW_CONSTANT constant_value`
22+
Then STDOUT should be:
23+
"""
24+
Success: Added the constant 'NEW_CONSTANT' to the 'wp-config.php' file with the value 'constant_value'.
25+
"""
26+
27+
When I run `wp config get NEW_CONSTANT`
28+
Then STDOUT should be:
29+
"""
30+
constant_value
31+
"""
32+
33+
Scenario: Update an existing constant then add it again
34+
Given a WP install
35+
36+
When I run `wp config update TEST_CONSTANT first_value`
37+
Then STDOUT should be:
38+
"""
39+
Success: Added the constant 'TEST_CONSTANT' to the 'wp-config.php' file with the value 'first_value'.
40+
"""
41+
42+
When I run `wp config update TEST_CONSTANT second_value`
43+
Then STDOUT should be:
44+
"""
45+
Success: Updated the constant 'TEST_CONSTANT' in the 'wp-config.php' file with the value 'second_value'.
46+
"""
47+
48+
When I run `wp config get TEST_CONSTANT`
49+
Then STDOUT should be:
50+
"""
51+
second_value
52+
"""
53+
54+
Scenario: Update a variable with --type=variable
55+
Given a WP install
56+
57+
When I run `wp config update new_variable variable_value --type=variable`
58+
Then STDOUT should be:
59+
"""
60+
Success: Added the variable 'new_variable' to the 'wp-config.php' file with the value 'variable_value'.
61+
"""
62+
63+
When I run `wp config update new_variable updated_value --type=variable`
64+
Then STDOUT should be:
65+
"""
66+
Success: Updated the variable 'new_variable' in the 'wp-config.php' file with the value 'updated_value'.
67+
"""
68+
69+
When I run `wp config get new_variable`
70+
Then STDOUT should be:
71+
"""
72+
updated_value
73+
"""
74+
75+
Scenario: Update raw values in wp-config.php
76+
Given a WP install
77+
78+
When I run `wp config update WP_DEBUG true --raw`
79+
Then STDOUT should be:
80+
"""
81+
Success: Updated the constant 'WP_DEBUG' in the 'wp-config.php' file with the raw value 'true'.
82+
"""
83+
84+
When I run `wp config list WP_DEBUG --strict --format=json`
85+
Then STDOUT should contain:
86+
"""
87+
{"name":"WP_DEBUG","value":true,"type":"constant"}
88+
"""
89+
90+
When I run `wp config update WP_DEBUG false --raw`
91+
Then STDOUT should be:
92+
"""
93+
Success: Updated the constant 'WP_DEBUG' in the 'wp-config.php' file with the raw value 'false'.
94+
"""
95+
96+
When I run `wp config list WP_DEBUG --strict --format=json`
97+
Then STDOUT should contain:
98+
"""
99+
{"name":"WP_DEBUG","value":false,"type":"constant"}
100+
"""
101+
102+
@custom-config-file
103+
Scenario: Update a constant in wp-custom-config.php
104+
Given an empty directory
105+
And WP files
106+
107+
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check --config-file='wp-custom-config.php'`
108+
Then STDOUT should contain:
109+
"""
110+
Generated 'wp-custom-config.php' file.
111+
"""
112+
113+
When I run `wp config update DB_HOST db.example.com --config-file='wp-custom-config.php'`
114+
Then STDOUT should be:
115+
"""
116+
Success: Updated the constant 'DB_HOST' in the 'wp-custom-config.php' file with the value 'db.example.com'.
117+
"""
118+
119+
When I run `wp config get DB_HOST --config-file='wp-custom-config.php'`
120+
Then STDOUT should be:
121+
"""
122+
db.example.com
123+
"""
124+
125+
Scenario: Ambiguous update requests throw errors
126+
Given a WP install
127+
128+
When I run `wp config update SOME_NAME some_value --type=constant`
129+
Then STDOUT should be:
130+
"""
131+
Success: Added the constant 'SOME_NAME' to the 'wp-config.php' file with the value 'some_value'.
132+
"""
133+
134+
When I run `wp config update SOME_NAME some_value --type=variable`
135+
Then STDOUT should be:
136+
"""
137+
Success: Added the variable 'SOME_NAME' to the 'wp-config.php' file with the value 'some_value'.
138+
"""
139+
140+
When I run `wp config list --fields=name,type SOME_NAME --strict`
141+
Then STDOUT should be a table containing rows:
142+
| name | type |
143+
| SOME_NAME | constant |
144+
| SOME_NAME | variable |
145+
146+
When I try `wp config update SOME_NAME some_value`
147+
Then STDERR should be:
148+
"""
149+
Error: Found both a constant and a variable 'SOME_NAME' in the 'wp-config.php' file. Use --type=<type> to disambiguate.
150+
"""
151+
152+
Scenario: Update with placement options for new constants
153+
Given a WP install
154+
And a wp-config.php file:
155+
"""
156+
define( 'CONST_A', 'val-a' );
157+
/** ANCHOR */
158+
define( 'CONST_B', 'val-b' );
159+
require_once( ABSPATH . 'wp-settings.php' );
160+
"""
161+
162+
When I run `wp config update SOME_NAME some_value --anchor="/** ANCHOR */" --placement=before --separator="\n"`
163+
Then STDOUT should be:
164+
"""
165+
Success: Added the constant 'SOME_NAME' to the 'wp-config.php' file with the value 'some_value'.
166+
"""
167+
And the wp-config.php file should be:
168+
"""
169+
define( 'CONST_A', 'val-a' );
170+
define( 'SOME_NAME', 'some_value' );
171+
/** ANCHOR */
172+
define( 'CONST_B', 'val-b' );
173+
require_once( ABSPATH . 'wp-settings.php' );
174+
"""
175+
176+
When I run `wp config update SOME_NAME updated_value --anchor="/** ANCHOR */" --placement=before --separator="\n"`
177+
Then STDOUT should be:
178+
"""
179+
Success: Updated the constant 'SOME_NAME' in the 'wp-config.php' file with the value 'updated_value'.
180+
"""
181+
And the wp-config.php file should be:
182+
"""
183+
define( 'CONST_A', 'val-a' );
184+
define( 'SOME_NAME', 'updated_value' );
185+
/** ANCHOR */
186+
define( 'CONST_B', 'val-b' );
187+
require_once( ABSPATH . 'wp-settings.php' );
188+
"""

0 commit comments

Comments
 (0)