Skip to content
Open
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
11 changes: 6 additions & 5 deletions bin/run-behat-tests
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ if [[ "$@" == *"--help"* ]]; then
exit $ret
fi

# Turn WP_VERSION into an actual number to make sure our tags work correctly.
if [ "${WP_VERSION-latest}" = "latest" ]; then
export WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r ".offers[0].current")
fi

# To retrieve the WP-CLI tests package root folder, we start with this scripts
# location.
SOURCE="${BASH_SOURCE[0]}"
Expand All @@ -44,5 +39,11 @@ export WP_CLI_TESTS_ROOT
# Generate the tags to apply environment-specific filters.
BEHAT_TAGS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-tags.php)

# Resolve WP_VERSION.
WP_VERSION=$(php "$WP_CLI_TESTS_ROOT"/utils/wp-version-resolver.php)
export WP_VERSION

echo "Running Behat tests for WordPress $WP_VERSION"

# Run the functional tests.
vendor/bin/behat --format progress "$BEHAT_TAGS" --strict "$@"
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": ">=7.2.24",
"behat/behat": "^v3.15.0",
"composer/semver": "^3.4",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || ^0.5 || ^0.6.2 || ^0.7.1 || ^1.0.0",
"php-parallel-lint/php-console-highlighter": "^1.0",
"php-parallel-lint/php-parallel-lint": "^1.3.1",
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@
so this file does not have to comply with WP naming conventions. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
<exclude-pattern>*/utils/wp-version-resolver\.php$</exclude-pattern>
</rule>
<rule ref="WordPress.WP.GlobalVariablesOverride">
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
<exclude-pattern>*/utils/wp-version-resolver\.php$</exclude-pattern>
</rule>

<!-- This is a procedural stand-alone file that is adding polyfills when
Expand Down
98 changes: 98 additions & 0 deletions tests/tests/TestWPVersionResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

use WP_CLI\Utils;
use WP_CLI\Tests\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class TestWPVersionResolverTest extends TestCase {
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class name has a redundant "Test" suffix. The class is already named TestWPVersionResolverTest, which is redundant. Consider renaming to TestWPVersionResolver or WPVersionResolverTest.

Suggested change
class TestWPVersionResolverTest extends TestCase {
class WPVersionResolverTest extends TestCase {

Copilot uses AI. Check for mistakes.

/**
* @var string
*/
private $temp_file = '';

protected function set_up() {
parent::set_up();

$this->temp_file = Utils\get_temp_dir() . 'wp-cli-tests-wp-versions.json';

$fp = fopen( $this->temp_file, 'w' );
fwrite( $fp, json_encode( $this->wp_versions_data() ) );
fclose( $fp );
}

protected function tear_down() {
if ( $this->temp_file && file_exists( $this->temp_file ) ) {
unlink( $this->temp_file );
}

parent::tear_down();
}

/**
* @return array<string, string>
*/
private function wp_versions_data(): array {
return array(
'5.4' => 'insecure',
'5.9' => 'insecure',
'5.9.1' => 'insecure',
'5.9.2' => 'insecure',
'6.0' => 'insecure',
'6.0.1' => 'insecure',
'6.0.2' => 'insecure',
'6.1' => 'insecure',
'6.1.1' => 'insecure',
'6.1.2' => 'insecure',
'6.2' => 'insecure',
'6.2.1' => 'insecure',
'6.2.2' => 'insecure',
'6.5' => 'insecure',
'6.5.2' => 'latest',
);
}

/**
* @return array<int, array{0: string, 1: string}>
*/
public static function data_wp_version_resolver(): array {
return array(
array( '5.0', '5.0' ), // Does not match any version. So return as it is.
array( '5', '5.9.2' ), // Return the latest major version.
array( '5.9', '5.9.2' ), // Return the latest patch version.
array( '5.9.1', '5.9.1' ), // Return the exact version.
array( '6', '6.5.2' ), // Return the latest minor version.
array( '6.0', '6.0.2' ), // Return the latest patch version.
array( '6.0.0', '6.0' ), // Return the requested version.
array( '', '6.5.2' ), // Return the latest version.
array( 'latest', '6.5.2' ), // Return the latest version.
array( 'some-mismatched-version', 'some-mismatched-version' ), // Does not match any version. So return as it is.
array( '6.5-alpha', '6.5.2' ), // Return the latest version.
array( '6.5-beta', '6.5.2' ), // Return the latest version.
array( '6.5-rc', '6.5.2' ), // Return the latest version.
array( '6.5-nightly', '6.5-nightly' ), // Does not match any version. So return as it is.
array( '6.5.0.0', '6.5' ), // Return the latest version.
array( '6.5.2.0', '6.5.2' ), // Return the latest version.
Comment on lines +74 to +75
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment incorrectly states "Return the latest version" for versions with 4 segments. The actual behavior based on the constraint logic is to return as-is (constraint = $wp_version_env at line 54). The comment should say "Does not match standard semver. Return as normalized version." or similar.

Suggested change
array( '6.5.0.0', '6.5' ), // Return the latest version.
array( '6.5.2.0', '6.5.2' ), // Return the latest version.
array( '6.5.0.0', '6.5' ), // Does not match standard semver. Return as normalized version.
array( '6.5.2.0', '6.5.2' ), // Does not match standard semver. Return as normalized version.

Copilot uses AI. Check for mistakes.
);
}

/**
* @dataProvider data_wp_version_resolver
*
* @param string $env
* @param string $expected
*/
#[DataProvider( 'data_wp_version_resolver' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function test_wp_version_resolver( $env, $expected ): void {
if ( $env ) {
putenv( "WP_VERSION=$env" );
}

$output = exec( 'php ' . dirname( dirname( __DIR__ ) ) . '/utils/wp-version-resolver.php' );

// Reset the environment variable.
putenv( 'WP_VERSION' );
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The environment variable is not properly unset. Using putenv('WP_VERSION') without a value may not work correctly on all systems. Consider using putenv('WP_VERSION=') with an empty string or checking if the variable needs to be restored to its previous value.

Suggested change
putenv( 'WP_VERSION' );
putenv( 'WP_VERSION=' );

Copilot uses AI. Check for mistakes.

Comment on lines +84 to +95
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test doesn't verify the actual error handling behavior when the JSON file doesn't exist initially. While the setup creates the file, consider adding a test case that verifies the script correctly downloads the JSON file when it's missing.

Copilot uses AI. Check for mistakes.
$this->assertSame( $expected, $output );
}
}
69 changes: 69 additions & 0 deletions utils/wp-version-resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Resolve the WP version to use for the tests.
*/

use Composer\Semver\Semver;

const WP_VERSIONS_JSON_FILE = '/wp-cli-tests-wp-versions.json';
const WP_VERSIONS_JSON_URL = 'https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json';

$wp_version_env = getenv( 'WP_VERSION' );
$wp_versions_file_path = sys_get_temp_dir() . WP_VERSIONS_JSON_FILE;

if ( ! file_exists( $wp_versions_file_path ) ) {
$ch = curl_init( WP_VERSIONS_JSON_URL );
$fp = fopen( $wp_versions_file_path, 'w' );
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of fopen() is not checked. If the file cannot be created (e.g., due to permissions), this will cause a fatal error when passed to curl_setopt(). Consider checking if $fp is valid before proceeding.

Suggested change
$fp = fopen( $wp_versions_file_path, 'w' );
$fp = fopen( $wp_versions_file_path, 'w' );
if ( false === $fp ) {
echo "Error: Unable to open file for writing: $wp_versions_file_path" . PHP_EOL;
exit( 1 );
}

Copilot uses AI. Check for mistakes.

Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of curl_init() is not checked. If curl fails to initialize, $ch will be false, causing subsequent curl_setopt() calls to fail. Consider checking if $ch is valid before using it.

Suggested change
if ( $ch === false ) {
if ( $fp ) {
fclose( $fp );
}
echo "Error: Failed to initialize cURL to fetch WP versions.\n";
exit( 1 );
}

Copilot uses AI. Check for mistakes.
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );

curl_exec( $ch );
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
// phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated
curl_close( $ch );
}
fclose( $fp );
Comment on lines +21 to +26
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of curl_exec() is not checked. If the curl request fails, the script will continue and potentially read an empty or invalid JSON file. Consider checking the return value and handling failure cases appropriately.

Suggested change
curl_exec( $ch );
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
// phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated
curl_close( $ch );
}
fclose( $fp );
$result = curl_exec( $ch );
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
// phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated
curl_close( $ch );
}
fclose( $fp );
if ( $result === false ) {
// Remove incomplete file if it exists.
if ( file_exists( $wp_versions_file_path ) ) {
unlink( $wp_versions_file_path );
}
fwrite( STDERR, "Error: Failed to download WP versions JSON from " . WP_VERSIONS_JSON_URL . "\n" );
exit( 1 );
}

Copilot uses AI. Check for mistakes.
}

$wp_versions_json = json_decode( file_get_contents( $wp_versions_file_path ), true );

Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of json_decode() is not checked. If the JSON file is invalid or corrupted, this will result in null, causing errors in subsequent operations. Consider checking if decoding was successful and handle the error case appropriately.

Suggested change
if ( !is_array( $wp_versions_json ) ) {
echo "Error: Failed to decode WP versions JSON file.\n";
echo $wp_version_env;
exit( 1 );
}

Copilot uses AI. Check for mistakes.
if ( empty( $wp_version_env ) || 'latest' === $wp_version_env ) {
$wp_version = array_search( 'latest', $wp_versions_json, true );

if ( empty( $wp_version ) ) {
$wp_version = $wp_version_env;
}

echo $wp_version;
exit( 0 );
}
Comment on lines +31 to +40
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script doesn't handle the trunk value for WP_VERSION, which was previously supported according to the README documentation. The script should either handle trunk as a special case (similar to latest) or the documentation should be updated to reflect this breaking change.

Copilot uses AI. Check for mistakes.

$wp_version = '';
$constraint = '';
$wp_versions = array_keys( $wp_versions_json );
$version_count = count( explode( '.', $wp_version_env ) );

if ( 1 === $version_count ) {
$constraint = "^$wp_version_env"; // Get the latest minor version.
} elseif ( 2 === $version_count ) {
$constraint = "~$wp_version_env.0"; // Get the latest patch version.
Copy link
Member Author

@thelovekesh thelovekesh May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added .0 as patch version due to semver package API behavior:

The ~ operator is best explained by example: ~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0. Ref: https://getcomposer.org/doc/articles/versions.md#tilde-version-range-

} elseif ( 3 === $version_count ) {
$constraint = "=$wp_version_env"; // Get the exact version.
} else {
$constraint = $wp_version_env;
}

if ( ! class_exists( 'Composer\Semver\Semver' ) ) {
require_once __DIR__ . '/../vendor/autoload.php';
}

try {
$wp_satisfied_versions = Semver::satisfiedBy( $wp_versions, $constraint );
} catch ( Exception $e ) {
echo $wp_version_env;
exit( 0 );
}

$wp_version = end( $wp_satisfied_versions );
echo $wp_version ? : $wp_version_env;
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Missing space before the ternary operator. The expression should be $wp_version ? $wp_version : $wp_version_env with proper spacing for readability.

Suggested change
echo $wp_version ? : $wp_version_env;
echo $wp_version ? $wp_version : $wp_version_env;

Copilot uses AI. Check for mistakes.
Comment on lines +68 to +69
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The end() function modifies the internal pointer of the array. If $wp_satisfied_versions is empty, end() returns false, which is then correctly handled by the ternary operator. However, consider using a more explicit check or alternative approach like array_pop(array_slice($wp_satisfied_versions, -1)) or checking if the array is empty before calling end().

Suggested change
$wp_version = end( $wp_satisfied_versions );
echo $wp_version ? : $wp_version_env;
$wp_version = !empty($wp_satisfied_versions) ? $wp_satisfied_versions[count($wp_satisfied_versions) - 1] : false;
echo $wp_version ? $wp_version : $wp_version_env;

Copilot uses AI. Check for mistakes.