Skip to content

Commit 39c02df

Browse files
committed
Merge branch 'main' into 165-show-boolean-literal
2 parents 9f76add + a17b045 commit 39c02df

14 files changed

Lines changed: 418 additions & 329 deletions

.github/workflows/testing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Testing
22

33
on:
4+
workflow_dispatch:
45
pull_request:
56
push:
67
branches:

AGENTS.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Instructions
2+
3+
This package is part of WP-CLI, the official command line interface for WordPress. For a detailed explanation of the project structure and development workflow, please refer to the main @README.md file.
4+
5+
## Best Practices for Code Contributions
6+
7+
When contributing to this package, please adhere to the following guidelines:
8+
9+
* **Follow Existing Conventions:** Before writing any code, analyze the existing codebase in this package to understand the coding style, naming conventions, and architectural patterns.
10+
* **Focus on the Package's Scope:** All changes should be relevant to the functionality of the package.
11+
* **Write Tests:** All new features and bug fixes must be accompanied by acceptance tests using Behat. You can find the existing tests in the `features/` directory. There may be PHPUnit unit tests as well in the `tests/` directory.
12+
* **Update Documentation:** If your changes affect the user-facing functionality, please update the relevant inline code documentation.
13+
14+
### Building and running
15+
16+
Before submitting any changes, it is crucial to validate them by running the full suite of static code analysis and tests. To run the full suite of checks, execute the following command: `composer test`.
17+
18+
This single command ensures that your changes meet all the quality gates of the project. While you can run the individual steps separately, it is highly recommended to use this single command to ensure a comprehensive validation.
19+
20+
### Useful Composer Commands
21+
22+
The project uses Composer to manage dependencies and run scripts. The following commands are available:
23+
24+
* `composer install`: Install dependencies.
25+
* `composer test`: Run the full test suite, including linting, code style checks, static analysis, and unit/behavior tests.
26+
* `composer lint`: Check for syntax errors.
27+
* `composer phpcs`: Check for code style violations.
28+
* `composer phpcbf`: Automatically fix code style violations.
29+
* `composer phpstan`: Run static analysis.
30+
* `composer phpunit`: Run unit tests.
31+
* `composer behat`: Run behavior-driven tests.
32+
33+
### Coding Style
34+
35+
The project follows the `WP_CLI_CS` coding standard, which is enforced by PHP_CodeSniffer. The configuration can be found in `phpcs.xml.dist`. Before submitting any code, please run `composer phpcs` to check for violations and `composer phpcbf` to automatically fix them.
36+
37+
## Documentation
38+
39+
The `README.md` file might be generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). In that case, changes need to be made against the corresponding part of the codebase.
40+
41+
### Inline Documentation
42+
43+
Only write high-value comments if at all. Avoid talking to the user through comments.
44+
45+
## Testing
46+
47+
The project has a comprehensive test suite that includes unit tests, behavior-driven tests, and static analysis.
48+
49+
* **Unit tests** are written with PHPUnit and can be found in the `tests/` directory. The configuration is in `phpunit.xml.dist`.
50+
* **Behavior-driven tests** are written with Behat and can be found in the `features/` directory. The configuration is in `behat.yml`.
51+
* **Static analysis** is performed with PHPStan.
52+
53+
All tests are run on GitHub Actions for every pull request.
54+
55+
When writing tests, aim to follow existing patterns. Key conventions include:
56+
57+
* When adding tests, first examine existing tests to understand and conform to established conventions.
58+
* For unit tests, extend the base `WP_CLI\Tests\TestCase` test class.
59+
* For Behat tests, only WP-CLI commands installed in `composer.json` can be run.
60+
61+
### Behat Steps
62+
63+
WP-CLI makes use of a Behat-based testing framework and provides a set of custom step definitions to write feature tests.
64+
65+
> **Note:** If you are expecting an error output in a test, you need to use `When I try ...` instead of `When I run ...` .
66+
67+
#### Given
68+
69+
* `Given an empty directory` - Creates an empty directory.
70+
* `Given /^an? (empty|non-existent) ([^\s]+) directory$/` - Creates or deletes a specific directory.
71+
* `Given an empty cache` - Clears the WP-CLI cache directory.
72+
* `Given /^an? ([^\s]+) (file|cache file):$/` - Creates a file with the given contents.
73+
* `Given /^"([^"]+)" replaced with "([^"]+)" in the ([^\s]+) file$/` - Search and replace a string in a file using regex.
74+
* `Given /^that HTTP requests to (.*?) will respond with:$/` - Mock HTTP requests to a given URL.
75+
* `Given WP files` - Download WordPress files without installing.
76+
* `Given wp-config.php` - Create a wp-config.php file using `wp config create`.
77+
* `Given a database` - Creates an empty database.
78+
* `Given a WP install(ation)` - Installs WordPress.
79+
* `Given a WP install(ation) in :subdir` - Installs WordPress in a given directory.
80+
* `Given a WP install(ation) with Composer` - Installs WordPress with Composer.
81+
* `Given a WP install(ation) with Composer and a custom vendor directory :vendor_directory` - Installs WordPress with Composer and a custom vendor directory.
82+
* `Given /^a WP multisite (subdirectory|subdomain)?\s?(install|installation)$/` - Installs WordPress Multisite.
83+
* `Given these installed and active plugins:` - Installs and activates one or more plugins.
84+
* `Given a custom wp-content directory` - Configure a custom `wp-content` directory.
85+
* `Given download:` - Download multiple files into the given destinations.
86+
* `Given /^save (STDOUT|STDERR) ([\'].+[^\'])?\s?as \{(\w+)\}$/` - Store STDOUT or STDERR contents in a variable.
87+
* `Given /^a new Phar with (?:the same version|version "([^"]+)")$/` - Build a new WP-CLI Phar file with a given version.
88+
* `Given /^a downloaded Phar with (?:the same version|version "([^"]+)")$/` - Download a specific WP-CLI Phar version from GitHub.
89+
* `Given /^save the (.+) file ([\'].+[^\'])? as \{(\w+)\}$/` - Stores the contents of the given file in a variable.
90+
* `Given a misconfigured WP_CONTENT_DIR constant directory` - Modify wp-config.php to set `WP_CONTENT_DIR` to an empty string.
91+
* `Given a dependency on current wp-cli` - Add `wp-cli/wp-cli` as a Composer dependency.
92+
* `Given a PHP built-in web server` - Start a PHP built-in web server in the current directory.
93+
* `Given a PHP built-in web server to serve :subdir` - Start a PHP built-in web server in the given subdirectory.
94+
95+
#### When
96+
97+
* ``When /^I launch in the background `([^`]+)`$/`` - Launch a given command in the background.
98+
* ``When /^I (run|try) `([^`]+)`$/`` - Run or try a given command.
99+
* ``When /^I (run|try) `([^`]+)` from '([^\s]+)'$/`` - Run or try a given command in a subdirectory.
100+
* `When /^I (run|try) the previous command again$/` - Run or try the previous command again.
101+
102+
#### Then
103+
104+
* `Then /^the return code should( not)? be (\d+)$/` - Expect a specific exit code of the previous command.
105+
* `Then /^(STDOUT|STDERR) should( strictly)? (be|contain|not contain):$/` - Check the contents of STDOUT or STDERR.
106+
* `Then /^(STDOUT|STDERR) should be a number$/` - Expect STDOUT or STDERR to be a numeric value.
107+
* `Then /^(STDOUT|STDERR) should not be a number$/` - Expect STDOUT or STDERR to not be a numeric value.
108+
* `Then /^STDOUT should be a table containing rows:$/` - Expect STDOUT to be a table containing the given rows.
109+
* `Then /^STDOUT should end with a table containing rows:$/` - Expect STDOUT to end with a table containing the given rows.
110+
* `Then /^STDOUT should be JSON containing:$/` - Expect valid JSON output in STDOUT.
111+
* `Then /^STDOUT should be a JSON array containing:$/` - Expect valid JSON array output in STDOUT.
112+
* `Then /^STDOUT should be CSV containing:$/` - Expect STDOUT to be CSV containing certain values.
113+
* `Then /^STDOUT should be YAML containing:$/` - Expect STDOUT to be YAML containing certain content.
114+
* `Then /^(STDOUT|STDERR) should be empty$/` - Expect STDOUT or STDERR to be empty.
115+
* `Then /^(STDOUT|STDERR) should not be empty$/` - Expect STDOUT or STDERR not to be empty.
116+
* `Then /^(STDOUT|STDERR) should be a version string (<|<=|>|>=|==|=|<>) ([+\w.{}-]+)$/` - Expect STDOUT or STDERR to be a version string comparing to the given version.
117+
* `Then /^the (.+) (file|directory) should( strictly)? (exist|not exist|be:|contain:|not contain):$/` - Expect a certain file or directory to (not) exist or (not) contain certain contents.
118+
* `Then /^the contents of the (.+) file should( not)? match (((\/.*\/)|(#.#))([a-z]+)?)$/` - Match file contents against a regex.
119+
* `Then /^(STDOUT|STDERR) should( not)? match (((\/.*\/)|(#.#))([a-z]+)?)$/` - Match STDOUT or STDERR against a regex.
120+
* `Then /^an email should (be sent|not be sent)$/` - Expect an email to be sent (or not).
121+
* `Then the HTTP status code should be :code` - Expect the HTTP status code for visiting `http://localhost:8080`.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ wp config delete <name> [--type=<type>] [--config-file=<path>]
137137
Generates a wp-config.php file.
138138

139139
~~~
140-
wp config create --dbname=<dbname> --dbuser=<dbuser> [--dbpass=<dbpass>] [--dbhost=<dbhost>] [--dbprefix=<dbprefix>] [--dbcharset=<dbcharset>] [--dbcollate=<dbcollate>] [--locale=<locale>] [--extra-php] [--skip-salts] [--skip-check] [--force] [--config-file=<path>] [--insecure]
140+
wp config create --dbname=<dbname> --dbuser=<dbuser> [--dbpass=<dbpass>] [--dbhost=<dbhost>] [--dbprefix=<dbprefix>] [--dbcharset=<dbcharset>] [--dbcollate=<dbcollate>] [--locale=<locale>] [--extra-php] [--skip-salts] [--skip-check] [--force] [--config-file=<path>] [--insecure] [--ssl]
141141
~~~
142142

143143
Creates a new wp-config.php with database constants, and verifies that
@@ -200,6 +200,9 @@ the database constants are correct.
200200
[--insecure]
201201
Retry API download without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
202202

203+
[--ssl]
204+
Use SSL when checking the database connection.
205+
203206
**EXAMPLES**
204207

205208
# Standard wp-config.php file

composer.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717
}
1818
],
1919
"require": {
20-
"wp-cli/wp-cli": "^2.5",
21-
"wp-cli/wp-config-transformer": "^1.2.1"
20+
"wp-cli/wp-cli": "^2.12",
21+
"wp-cli/wp-config-transformer": "^1.4.0"
2222
},
2323
"require-dev": {
2424
"wp-cli/db-command": "^1.3 || ^2",
25-
"wp-cli/wp-cli-tests": "^4.2.8"
25+
"wp-cli/wp-cli-tests": "^5"
2626
},
2727
"config": {
2828
"process-timeout": 7200,
2929
"sort-packages": true,
3030
"allow-plugins": {
3131
"dealerdirect/phpcodesniffer-composer-installer": true,
32-
"johnpbloch/wordpress-core-installer": true
33-
}
32+
"johnpbloch/wordpress-core-installer": true,
33+
"phpstan/extension-installer": true
34+
},
35+
"lock": false
3436
},
3537
"extra": {
3638
"branch-alias": {
@@ -66,12 +68,14 @@
6668
"behat-rerun": "rerun-behat-tests",
6769
"lint": "run-linter-tests",
6870
"phpcs": "run-phpcs-tests",
71+
"phpstan": "run-phpstan-tests",
6972
"phpcbf": "run-phpcbf-cleanup",
7073
"phpunit": "run-php-unit-tests",
7174
"prepare-tests": "install-package-tests",
7275
"test": [
7376
"@lint",
7477
"@phpcs",
78+
"@phpstan",
7579
"@phpunit",
7680
"@behat"
7781
]

features/config-create.feature

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ Feature: Create a wp-config file
9090
9191
When I try `wp version`
9292
Then STDERR should not contain:
93-
"""
94-
Constant WP_DEBUG already defined
95-
"""
93+
"""
94+
Constant WP_DEBUG already defined
95+
"""
9696
9797
@require-wp-4.0
9898
Scenario: No wp-config.php and WPLANG
@@ -227,69 +227,63 @@ Feature: Create a wp-config file
227227
define( 'AUTH_SALT',
228228
"""
229229
230-
@less-than-php-7.0
231-
Scenario: Configure with salts fetched from WordPress.org
230+
Scenario: Values are properly escaped to avoid creating invalid config files
232231
Given an empty directory
233232
And WP files
234233
235-
When I run `wp config create {CORE_CONFIG_SETTINGS}`
234+
When I run `wp config create --skip-check --dbname=somedb --dbuser=someuser --dbpass="PasswordWith'SingleQuotes'"`
236235
Then the wp-config.php file should contain:
237236
"""
238-
define( 'AUTH_SALT',
237+
define( 'DB_PASSWORD', 'PasswordWith\'SingleQuotes\'' )
239238
"""
240239
241-
Scenario: Define WPLANG when running WP < 4.0
242-
Given an empty directory
243-
And I run `wp core download --version=3.9 --force`
244-
245-
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check`
246-
Then the wp-config.php file should contain:
240+
When I run `wp config get DB_PASSWORD`
241+
Then STDOUT should be:
247242
"""
248-
define( 'WPLANG', '' );
243+
PasswordWith'SingleQuotes'
249244
"""
250245
251-
When I try `wp config create {CORE_CONFIG_SETTINGS}`
252-
Then the return code should be 1
253-
And STDERR should contain:
254-
"""
255-
Error: The 'wp-config.php' file already exists.
256-
"""
246+
Scenario: Passwords with special characters and double quotes
247+
Given an empty directory
248+
And WP files
257249
258-
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check --locale=ja --force`
259-
Then the return code should be 0
260-
And STDOUT should contain:
261-
"""
262-
Success: Generated 'wp-config.php' file.
263-
"""
264-
And the wp-config.php file should contain:
250+
When I run `wp config create --skip-check --dbname=somedb --dbuser=someuser --dbpass='p@(ss){w0r?d><}"!With"DoubleQuotes'`
251+
Then the wp-config.php file should contain:
265252
"""
266-
define( 'WPLANG', 'ja' );
253+
define( 'DB_PASSWORD', 'p@(ss){w0r?d><}"!With"DoubleQuotes' )
267254
"""
268255
269-
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check --config-file=wp-custom-config.php --locale=ja --force`
270-
Then the return code should be 0
271-
And STDOUT should contain:
272-
"""
273-
Success: Generated 'wp-custom-config.php' file.
274-
"""
275-
And the wp-custom-config.php file should contain:
256+
When I run `wp config get DB_PASSWORD`
257+
Then STDOUT should be:
276258
"""
277-
define( 'WPLANG', 'ja' );
259+
p@(ss){w0r?d><}"!With"DoubleQuotes
278260
"""
279261
280-
Scenario: Values are properly escaped to avoid creating invalid config files
262+
Scenario: Passwords with backslash should properly escaped
281263
Given an empty directory
282264
And WP files
283-
284-
When I run `wp config create --skip-check --dbname=somedb --dbuser=someuser --dbpass="PasswordWith'SingleQuotes'"`
265+
266+
When I run `wp config create --skip-check --dbname=somedb --dbuser=someuser --dbpass='my\\password'`
285267
Then the wp-config.php file should contain:
286268
"""
287-
define( 'DB_PASSWORD', 'PasswordWith\'SingleQuotes\'' )
269+
define( 'DB_PASSWORD', 'my\\\\password' )
288270
"""
289-
271+
290272
When I run `wp config get DB_PASSWORD`
291273
Then STDOUT should be:
292274
"""
293-
PasswordWith'SingleQuotes'
275+
my\\password
294276
"""
295277
278+
@require-mysql @require-mysql-5.7
279+
Scenario: Configure with required SSL connection
280+
Given an empty directory
281+
And WP files
282+
And I run `MYSQL_PWD='{DB_ROOT_PASSWORD}' MYSQL_HOST='{MYSQL_HOST}' MYSQL_TCP_PORT='{MYSQL_PORT}' mysql -u root -e "CREATE USER IF NOT EXISTS 'wp_cli_test_ssl'@'%' IDENTIFIED BY 'password2' REQUIRE SSL;"`
283+
284+
When I try `wp config create --dbhost=127.0.0.1 --dbname=wp_cli_test --dbuser=wp_cli_test_ssl --dbpass=password2 --ssl`
285+
Then the return code should be 0
286+
And the wp-config.php file should contain:
287+
"""
288+
define( 'DB_USER', 'wp_cli_test_ssl' )
289+
"""

features/config-has.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ Feature: Check whether the wp-config.php file or the wp-custom-config.php file h
1515
Then STDOUT should be empty
1616
And the return code should be 0
1717

18+
Scenario: Check for the existance of an existing wp-config.php constant in a read-only file system
19+
Given a WP install
20+
21+
When I run `chmod -w wp-config.php`
22+
And I try `wp config has DB_NAME`
23+
Then STDOUT should be empty
24+
And the return code should be 0
25+
1826
@custom-config-file
1927
Scenario: Check the existence of an existing wp-custom-config.php constant or variable
2028
Given an empty directory

features/config-is-true.feature

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Feature: Determine whether the value of a constant or variable defined in wp-con
3737
Scenario Outline: Get the value of a variable whose value is true
3838
When I try `wp config is-true <variable>`
3939
Then STDOUT should be empty
40-
Then STDERR should be empty
40+
And STDERR should be empty
4141
And the return code should be 0
4242

4343
Examples:
@@ -75,10 +75,9 @@ Feature: Determine whether the value of a constant or variable defined in wp-con
7575
Scenario: Test for correct functionality with included PHP files.
7676
When I try `wp config is-true WP_INC_TRUTH`
7777
Then STDOUT should be empty
78-
Then STDERR should be empty
78+
And STDERR should be empty
7979
And the return code should be 0
8080

8181
When I try `wp config is-true WP_INC_FALSE`
8282
Then STDOUT should be empty
8383
And the return code should be 1
84-

0 commit comments

Comments
 (0)