From 3f2950a7b6691f6ccb6e53ae4c1c693f5a8b689c Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 8 Mar 2024 14:41:49 +0545 Subject: [PATCH 1/4] Add format in core update --- src/Core_Command.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Core_Command.php b/src/Core_Command.php index 80139080..e93a044c 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; /** @@ -1064,6 +1065,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. * @@ -1109,6 +1119,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 @@ -1214,6 +1229,21 @@ public function update( $args, $assoc_args ) { $locale = (string) 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 { From 2c794a8eaefb7cf301819f4a22726f1a0e357b90 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Mon, 11 Mar 2024 10:27:06 +0545 Subject: [PATCH 2/4] Add feature test for format in core update --- features/core-update.feature | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/features/core-update.feature b/features/core-update.feature index 3f50c6ad..eb4a73a5 100644 --- a/features/core-update.feature +++ b/features/core-update.feature @@ -32,6 +32,27 @@ Feature: Update WordPress core 4.0 """ + @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.0 --force` + Then STDOUT should not be empty + + When I run `wp eval 'echo $GLOBALS["wp_version"];'` + Then STDOUT should be: + """ + 6.0 + """ + + When I run `wget http://wordpress.org/wordpress-6.1.zip --quiet` + And I run `wp core update wordpress-6.1.zip --format=json` + Then STDOUT should be: + """ + [{"name":"core","old_version":"6.0","new_version":"6.1","status":"Updated"}] + """ + # PHP 7.1 needs WP 3.9 (due to wp_check_php_mysql_versions(), see trac changeset [27257]), # and travis doesn't install mysql extension by default for PHP 7.0. @less-than-php-7 From 8c98130c6c1af894241b51948dcb83b4fb2d4326 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Thu, 14 Mar 2024 13:35:58 +0545 Subject: [PATCH 3/4] Add test for csv and table format --- features/core-update.feature | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/features/core-update.feature b/features/core-update.feature index eb4a73a5..dfb9b340 100644 --- a/features/core-update.feature +++ b/features/core-update.feature @@ -53,6 +53,48 @@ Feature: Update WordPress core [{"name":"core","old_version":"6.0","new_version":"6.1","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.0 --force` + Then STDOUT should not be empty + + When I run `wp eval 'echo $GLOBALS["wp_version"];'` + Then STDOUT should be: + """ + 6.0 + """ + + When I run `wget http://wordpress.org/wordpress-6.1.zip --quiet` + And I run `wp core update wordpress-6.1.zip --format=csv` + Then STDOUT should be: + """ + name,old_version,new_version,status + core,6.0,6.1,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.0 --force` + Then STDOUT should not be empty + + When I run `wp eval 'echo $GLOBALS["wp_version"];'` + Then STDOUT should be: + """ + 6.0 + """ + + When I run `wget http://wordpress.org/wordpress-6.1.zip --quiet` + And I run `wp core update wordpress-6.1.zip --format=table` + Then STDOUT should be a table containing rows: + | name | old_version | new_version | status | + | core | 6.0 | 6.1 | Updated | + # PHP 7.1 needs WP 3.9 (due to wp_check_php_mysql_versions(), see trac changeset [27257]), # and travis doesn't install mysql extension by default for PHP 7.0. @less-than-php-7 From 40be7266ac298808140754d14106194edc99c970 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Tue, 19 Mar 2024 13:22:03 +0545 Subject: [PATCH 4/4] Fix test for table format in core update --- features/core-update.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/core-update.feature b/features/core-update.feature index dfb9b340..d28263b6 100644 --- a/features/core-update.feature +++ b/features/core-update.feature @@ -91,7 +91,7 @@ Feature: Update WordPress core When I run `wget http://wordpress.org/wordpress-6.1.zip --quiet` And I run `wp core update wordpress-6.1.zip --format=table` - Then STDOUT should be a table containing rows: + Then STDOUT should end with a table containing rows: | name | old_version | new_version | status | | core | 6.0 | 6.1 | Updated |