diff --git a/features/core-update.feature b/features/core-update.feature index 44d0ad1e..a52bcd3b 100644 --- a/features/core-update.feature +++ b/features/core-update.feature @@ -30,6 +30,69 @@ Feature: Update WordPress core 6.2 """ + @require-php-7.0 + Scenario: Output in JSON format + Given a WP install + And I try `wp theme install twentytwenty --activate` + + When I run `wp core download --version=6.6 --force` + Then STDOUT should not be empty + + When I run `wp eval 'echo $GLOBALS["wp_version"];'` + Then STDOUT should be: + """ + 6.6 + """ + + When I run `wget http://wordpress.org/wordpress-6.8.zip --quiet` + And I run `wp core update wordpress-6.8.zip --format=json` + Then STDOUT should be: + """ + [{"name":"core","old_version":"6.6","new_version":"6.8","status":"Updated"}] + """ + + @require-php-7.0 + Scenario: Output in CSV format + Given a WP install + And I try `wp theme install twentytwenty --activate` + + When I run `wp core download --version=6.6 --force` + Then STDOUT should not be empty + + When I run `wp eval 'echo $GLOBALS["wp_version"];'` + Then STDOUT should be: + """ + 6.6 + """ + + When I run `wget http://wordpress.org/wordpress-6.8.zip --quiet` + And I run `wp core update wordpress-6.8.zip --format=csv` + Then STDOUT should be: + """ + name,old_version,new_version,status + core,6.6,6.8,Updated + """ + + @require-php-7.0 + Scenario: Output in table format + Given a WP install + And I try `wp theme install twentytwenty --activate` + + When I run `wp core download --version=6.6 --force` + Then STDOUT should not be empty + + When I run `wp eval 'echo $GLOBALS["wp_version"];'` + Then STDOUT should be: + """ + 6.6 + """ + + When I run `wget http://wordpress.org/wordpress-6.8.zip --quiet` + And I run `wp core update wordpress-6.8.zip --format=table` + Then STDOUT should end with a table containing rows: + | name | old_version | new_version | status | + | core | 6.6 | 6.8 | Updated | + # This test downgrades to an older WordPress version, but the SQLite plugin requires 6.4+ @require-mysql Scenario: Update to the latest minor release (PHP 7.2 compatible with WP >= 4.9) diff --git a/src/Core_Command.php b/src/Core_Command.php index 3dd85bf6..b9bbb2b4 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -5,6 +5,7 @@ use WP_CLI\Iterators\Table as TableIterator; use WP_CLI\Utils; use WP_CLI\Formatter; +use WP_CLI\Loggers; use WP_CLI\WpOrgApi; /** @@ -1073,6 +1074,15 @@ private static function get_core_checksums( $version, $locale, $insecure ) { * [--locale=] * : Select which language you want to download. * + * [--format=] + * : Render output in a particular format. + * --- + * options: + * - table + * - csv + * - json + * --- + * * [--insecure] * : Retry download without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack. * @@ -1104,7 +1114,7 @@ private static function get_core_checksums( $version, $locale, $insecure ) { * @alias upgrade * * @param array{0?: string} $args Positional arguments. - * @param array{minor?: bool, version?: string, force?: bool, locale?: string, insecure?: bool} $assoc_args Associative arguments. + * @param array{minor?: bool, version?: string, force?: bool, locale?: string, insecure?: bool, format?: string} $assoc_args Associative arguments. */ public function update( $args, $assoc_args ) { global $wp_version; @@ -1116,6 +1126,11 @@ public function update( $args, $assoc_args ) { $assoc_args['version'] = 'nightly'; } + if ( ! empty( $assoc_args['format'] ) && in_array( $assoc_args['format'], [ 'json', 'csv' ], true ) ) { + $logger = new Loggers\Quiet( WP_CLI::get_runner()->in_color() ); + WP_CLI::set_logger( $logger ); + } + if ( ! empty( $args[0] ) ) { // ZIP path or URL is given @@ -1237,6 +1252,21 @@ public function update( $args, $assoc_args ) { $locale = Utils\get_flag_value( $assoc_args, 'locale', get_locale() ); $this->cleanup_extra_files( $from_version, $to_version, $locale, $insecure ); + $data = [ + [ + 'name' => 'core', + 'old_version' => $from_version, + 'new_version' => $to_version, + 'status' => 'Updated', + ], + ]; + + $format = Utils\get_flag_value( $assoc_args, 'format' ); + + if ( ! empty( $format ) ) { + Utils\format_items( $format, $data, [ 'name', 'old_version', 'new_version', 'status' ] ); + } + WP_CLI::success( 'WordPress updated successfully.' ); } } else {