Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ composer.lock
phpunit.xml
phpcs.xml
.phpcs.xml
.phpunit.result.cache
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": ">=5.6",
"behat/behat": "^3.7",
"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 @@ -63,9 +63,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
83 changes: 83 additions & 0 deletions tests/test-wp-version-resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

use WP_CLI\Utils;
use WP_CLI\Tests\TestCase;

class WPVersionResolverTest extends TestCase {

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();
}

private function wp_versions_data() {
return array(
'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',
);
}

private function data_wp_version_resolver() {
return array(
array( '5.0', '5.0' ), // Does not match any version. So return as it is.
array( '5', '6.5.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.
);
}

/**
* @dataProvider data_wp_version_resolver
*/
public function test_wp_version_resolver( $env, $expected ) {
if ( $env ) {
putenv( "WP_VERSION=$env" );
}

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

$this->assertEquals( $expected, $output );

// Reset the environment variable.
putenv( 'WP_VERSION' );
}
}
66 changes: 66 additions & 0 deletions utils/wp-version-resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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 );
curl_close( $ch );
fclose( $fp );
}

$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.