Skip to content

Commit f83ecd0

Browse files
authored
Merge pull request #780 from lucatume/fix/restore-wpcli-allow-root
fix(WPCLI) restore allow-root support
2 parents fb157cd + 1eba916 commit f83ecd0

4 files changed

Lines changed: 51 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [unreleased] Unreleased
77

8+
### Fixed
9+
10+
- Restored support for the `allow-root` configuration parameter in the `WPCLI` module.
11+
812
## [4.5.3] 2025-05-08;
913

1014
### Fixed

docs/modules/WPCLI.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This module should be with [Cest][2] and [Cept][3] test cases.
4747
* `packages-dir` - the directory to use to store the packages downloaded by the `wp package` command. Equivalent to
4848
setting the `WP_CLI_PACKAGES_DIR` environment variable.
4949
* `bin` - the path to a custom WP-CLI binary.
50+
* `allow-root` - a boolean value to indicate if the `wp` command should be run with the `--allow-root` flag. Equivalent to the `--allow-root` option of the `wp` command. This is useful when running wp-cli commands as the root user.
5051

5152
The following is an example of the module configuration to run WPCLI commands on the `/var/wordpress` directory:
5253

src/Module/WPCLI.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class WPCLI extends Module
4242
'no-color' => true,
4343
'debug' => true,
4444
'quiet' => true,
45+
'allow-root' => true,
4546
];
4647
/**
4748
* @var array<string>
@@ -77,7 +78,8 @@ class WPCLI extends Module
7778
* config-path?: string,
7879
* custom-shell?: string,
7980
* packages-dir?: string,
80-
* bin?: string
81+
* bin?: string,
82+
* allow-root?: bool
8183
* }
8284
*/
8385
protected array $config = [
@@ -132,7 +134,8 @@ public function cli(string|array $command = ['core', 'version'], ?array $env = n
132134
* config-path?: string,
133135
* custom-shell?: string,
134136
* packages-dir?: string,
135-
* bin?: string
137+
* bin?: string,
138+
* allow-root?: bool
136139
* } $config
137140
*/
138141
$config = $this->config;
@@ -501,7 +504,17 @@ protected function validateConfig(): void
501504
}
502505
}
503506

504-
foreach (['skip-plugins', 'skip-themes', 'skip-packages', 'debug', 'quiet', 'color', 'no-color'] as $boolKey) {
507+
foreach ([
508+
'skip-plugins',
509+
'skip-themes',
510+
'skip-packages',
511+
'debug',
512+
'quiet',
513+
'color',
514+
'no-color',
515+
'allow-root'
516+
] as $boolKey
517+
) {
505518
if (empty($this->config[$boolKey])) {
506519
unset($this->config[$boolKey]);
507520
} else {

tests/unit/lucatume/WPBrowser/Module/WPCLITest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,36 @@ public function should_allow_configuring_wp_cli_to_run_without_color(): void
847847
$this->assertEquals($expected, $commandLine);
848848
}
849849

850+
/**
851+
* It should allow configuring wp-cli to run with allow-root
852+
*
853+
* @test
854+
*/
855+
public function should_allow_configuring_wp_cli_to_run_with_allow_root(): void
856+
{
857+
$wpcli = $this->module([
858+
'path' => self::$installation->getWpRootDir(),
859+
'allow-root' => true
860+
]);
861+
862+
$wpcli->cli(['core', 'version']);
863+
864+
$commandLine = $wpcli->grabLastCliProcess()->getCommandLine();
865+
$wpCliPhar = CliProcess::getWpCliPharPathname();
866+
867+
$expected = implode(
868+
' ',
869+
array_map('escapeshellarg', [
870+
PHP_BINARY,
871+
$wpCliPhar,
872+
'--allow-root',
873+
'core',
874+
'version'
875+
])
876+
);
877+
$this->assertEquals($expected, $commandLine);
878+
}
879+
850880
/**
851881
* It should inherit env from current session when env not specified
852882
*

0 commit comments

Comments
 (0)