Skip to content

Commit

Permalink
move the version check to a utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzsequence committed Apr 3, 2024
1 parent 6844831 commit 597a6d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
19 changes: 19 additions & 0 deletions features/bootstrap/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,22 @@ function get_temp_dir() {

return $trailingslashit( $temp );
}

/**
* Get the latest WordPress version from the version check API endpoint.
*
* @access public
* @category System
*
* @return string
*/
function get_wp_version() {
// Fetch the latest WordPress version info from the WordPress.org API
$url = 'https://api.wordpress.org/core/version-check/1.7/';
$json = file_get_contents($url);
$data = json_decode($json, true);

// Extract the latest version number
$latestVersion = $data['offers'][0]['current'];
return trim($latestVersion);
}
12 changes: 3 additions & 9 deletions features/steps/given.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Behat\Gherkin\Node\TableNode,
WP_CLI\Process;

use function WP_CLI\Utils\get_wp_version;

$steps->Given( '/^an empty directory$/',
function ( $world ) {
$world->create_run_dir();
Expand Down Expand Up @@ -157,20 +159,12 @@ function($world) {
);

$steps->Given('/^the current WP version is not the latest$/', function ($world) {
// Fetch the latest WordPress version info from the WordPress.org API
$url = 'https://api.wordpress.org/core/version-check/1.7/';
$json = file_get_contents($url);
$data = json_decode($json, true);

// Extract the latest version number
$latestVersion = $data['offers'][0]['current'];

// Use wp-cli to get the currently installed WordPress version.
$currentVersion = $world->proc('wp core version')->run();

// Normalize versions (remove new lines).
$currentVersion = trim($currentVersion->stdout);
$latestVersion = trim($latestVersion);
$latestVersion = get_wp_version();

// If there's no update available or the current version is the latest, throw an exception to skip the test.
if (empty($latestVersion) || $currentVersion === $latestVersion) {
Expand Down

0 comments on commit 597a6d7

Please sign in to comment.