Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

### Fixed

- Restored support for the `allow-root` configuration parameter in the `WPCLI` module.

## [4.5.3] 2025-05-08;

### Fixed
Expand Down
1 change: 1 addition & 0 deletions docs/modules/WPCLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This module should be with [Cest][2] and [Cept][3] test cases.
* `packages-dir` - the directory to use to store the packages downloaded by the `wp package` command. Equivalent to
setting the `WP_CLI_PACKAGES_DIR` environment variable.
* `bin` - the path to a custom WP-CLI binary.
* `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.

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

Expand Down
19 changes: 16 additions & 3 deletions src/Module/WPCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WPCLI extends Module
'no-color' => true,
'debug' => true,
'quiet' => true,
'allow-root' => true,
];
/**
* @var array<string>
Expand Down Expand Up @@ -77,7 +78,8 @@ class WPCLI extends Module
* config-path?: string,
* custom-shell?: string,
* packages-dir?: string,
* bin?: string
* bin?: string,
* allow-root?: bool
* }
*/
protected array $config = [
Expand Down Expand Up @@ -132,7 +134,8 @@ public function cli(string|array $command = ['core', 'version'], ?array $env = n
* config-path?: string,
* custom-shell?: string,
* packages-dir?: string,
* bin?: string
* bin?: string,
* allow-root?: bool
* } $config
*/
$config = $this->config;
Expand Down Expand Up @@ -501,7 +504,17 @@ protected function validateConfig(): void
}
}

foreach (['skip-plugins', 'skip-themes', 'skip-packages', 'debug', 'quiet', 'color', 'no-color'] as $boolKey) {
foreach ([
'skip-plugins',
'skip-themes',
'skip-packages',
'debug',
'quiet',
'color',
'no-color',
'allow-root'
] as $boolKey
) {
if (empty($this->config[$boolKey])) {
unset($this->config[$boolKey]);
} else {
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/lucatume/WPBrowser/Module/WPCLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,36 @@ public function should_allow_configuring_wp_cli_to_run_without_color(): void
$this->assertEquals($expected, $commandLine);
}

/**
* It should allow configuring wp-cli to run with allow-root
*
* @test
*/
public function should_allow_configuring_wp_cli_to_run_with_allow_root(): void
{
$wpcli = $this->module([
'path' => self::$installation->getWpRootDir(),
'allow-root' => true
]);

$wpcli->cli(['core', 'version']);

$commandLine = $wpcli->grabLastCliProcess()->getCommandLine();
$wpCliPhar = CliProcess::getWpCliPharPathname();

$expected = implode(
' ',
array_map('escapeshellarg', [
PHP_BINARY,
$wpCliPhar,
'--allow-root',
'core',
'version'
])
);
$this->assertEquals($expected, $commandLine);
}

/**
* It should inherit env from current session when env not specified
*
Expand Down