diff --git a/features/checksum-core.feature b/features/checksum-core.feature index 03e697e8..c3c7c016 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -260,3 +260,55 @@ Feature: Validate checksums for WordPress install Error: WordPress installation doesn't verify against checksums. """ And the return code should be 1 + + Scenario: Core checksums verify with format parameter + Given a WP install + And "WordPress" replaced with "Modified WordPress" in the wp-includes/functions.php file + And a wp-includes/test.log file: + """ + log content + """ + + When I try `wp core verify-checksums --format=table` + Then STDOUT should be a table containing rows: + | file | message | + | wp-includes/functions.php | File doesn't verify against checksum | + | wp-includes/test.log | File should not exist | + And the return code should be 1 + + When I try `wp core verify-checksums --format=csv` + Then STDOUT should contain: + """ + file,message + wp-includes/functions.php,"File doesn't verify against checksum" + wp-includes/test.log,"File should not exist" + """ + And the return code should be 1 + + When I try `wp core verify-checksums --format=json` + Then STDOUT should contain: + """ + "file":"wp-includes\/functions.php","message":"File doesn't verify against checksum" + """ + And the return code should be 1 + + When I try `wp core verify-checksums --format=count` + Then STDOUT should be: + """ + 2 + """ + And the return code should be 1 + + When I try `wp core verify-checksums --format=json --exclude=wp-includes/test.log` + Then STDOUT should contain: + """ + "file":"wp-includes\/functions.php","message":"File doesn't verify against checksum" + """ + And the return code should be 1 + + When I try `wp core verify-checksums --format=json --exclude=wp-includes/functions.php,wp-includes/test.log` + Then STDOUT should be: + """ + Success: WordPress installation verifies against checksums. + """ + And the return code should be 0 diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 43f5e965..8bca3518 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -1,5 +1,6 @@ ] * : Exclude specific files from the checksum verification. Provide a comma-separated list of file paths. * + * [--format=] + * : Render output in a specific format. When provided, messages are displayed in the chosen format. + * --- + * default: plain + * options: + * - plain + * - table + * - json + * - csv + * - yaml + * - count + * --- + * * ## EXAMPLES * * # Verify checksums @@ -79,6 +100,11 @@ class Checksum_Core_Command extends Checksum_Base_Command { * $ wp core verify-checksums --exclude="readme.html" * Success: WordPress installation verifies against checksums. * + * # Verify checksums with formatted output + * $ wp core verify-checksums --format=json + * [{"file":"readme.html","message":"File doesn't verify against checksum"}] + * Error: WordPress installation doesn't verify against checksums. + * * @when before_wp_load */ public function __invoke( $args, $assoc_args ) { @@ -137,14 +163,23 @@ public function __invoke( $args, $assoc_args ) { } if ( ! file_exists( ABSPATH . $file ) ) { - WP_CLI::warning( "File doesn't exist: {$file}" ); + $this->errors[] = [ + 'file' => $file, + 'message' => "File doesn't exist", + ]; + $has_errors = true; + continue; } $md5_file = md5_file( ABSPATH . $file ); - if ( $md5_file !== $checksum ) { - WP_CLI::warning( "File doesn't verify against checksum: {$file}" ); + if ( $checksum !== $md5_file ) { + $this->errors[] = [ + 'file' => $file, + 'message' => "File doesn't verify against checksum", + ]; + $has_errors = true; } } @@ -158,7 +193,25 @@ public function __invoke( $args, $assoc_args ) { if ( in_array( $additional_file, $this->exclude_files, true ) ) { continue; } - WP_CLI::warning( "File should not exist: {$additional_file}" ); + + $this->errors[] = [ + 'file' => $additional_file, + 'message' => 'File should not exist', + ]; + } + } + + if ( ! empty( $this->errors ) ) { + if ( ! isset( $assoc_args['format'] ) || 'plain' === $assoc_args['format'] ) { + foreach ( $this->errors as $error ) { + WP_CLI::warning( sprintf( '%s: %s', $error['message'], $error['file'] ) ); + } + } else { + $formatter = new Formatter( + $assoc_args, + array( 'file', 'message' ) + ); + $formatter->display_items( $this->errors ); } } @@ -234,7 +287,7 @@ private static function get_wp_details() { private static function find_var( $var_name, $code ) { $start = strpos( $code, '$' . $var_name . ' = ' ); - if ( ! $start ) { + if ( false === $start ) { return null; }