From 23856fb555927ffc41d6b5a6384f834f30537be0 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 11:49:42 +0600 Subject: [PATCH 01/21] feat: add --format param for core checksums verification --- src/Checksum_Core_Command.php | 52 ++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 43f5e965..bcbf0c7f 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. + * --- + * default: table + * options: + * - table + * - json + * - csv + * - yaml + * - count + * --- + * * ## EXAMPLES * * # Verify checksums @@ -79,6 +99,10 @@ 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 + * Success: WordPress installation verifies against checksums. + * * @when before_wp_load */ public function __invoke( $args, $assoc_args ) { @@ -137,14 +161,14 @@ public function __invoke( $args, $assoc_args ) { } if ( ! file_exists( ABSPATH . $file ) ) { - WP_CLI::warning( "File doesn't exist: {$file}" ); + $this->add_error( $file, "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}" ); + $this->add_error( $file, "File doesn't verify against checksum" ); $has_errors = true; } } @@ -158,10 +182,18 @@ 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->add_error( $additional_file, 'File should not exist' ); } } + if ( ! empty( $this->errors ) ) { + $formatter = new Formatter( + $assoc_args, + array( 'file', 'message' ) + ); + $formatter->display_items( $this->errors ); + } + if ( ! $has_errors ) { WP_CLI::success( 'WordPress installation verifies against checksums.' ); } else { @@ -203,7 +235,7 @@ private static function get_wp_details() { if ( ! is_readable( $versions_path ) ) { WP_CLI::error( "This does not seem to be a WordPress install.\n" . - 'Pass --path=`path/to/wordpress` or run `wp core download`.' + 'Pass --path=`path/to/wordpress` or run `wp core download`.' ); } @@ -245,4 +277,16 @@ private static function find_var( $var_name, $code ) { return trim( $value, " '" ); } + + /** + * Adds a new error to the array of detected errors. + * + * @param string $file Relative path to the file that had the error. + * @param string $message Message explaining the error. + */ + private function add_error( $file, $message ) { + $error['file'] = $file; + $error['message'] = $message; + $this->errors[] = $error; + } } From 0f0586c865d09c956d1903593fd175bccb8cdf25 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 15:03:55 +0600 Subject: [PATCH 02/21] fix: improve error handling output format in checksum verification --- src/Checksum_Core_Command.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index bcbf0c7f..8091df35 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -63,9 +63,8 @@ class Checksum_Core_Command extends Checksum_Base_Command { * : Exclude specific files from the checksum verification. Provide a comma-separated list of file paths. * * [--format=] - * : Render output in a specific format. + * : Render output in a specific format. When provided, errors are displayed in tabular format instead of individual warning messages. * --- - * default: table * options: * - table * - json @@ -161,14 +160,22 @@ public function __invoke( $args, $assoc_args ) { } if ( ! file_exists( ABSPATH . $file ) ) { - $this->add_error( $file, "File doesn't exist" ); + if ( isset( $assoc_args['format'] ) ) { + $this->add_error( $file, "File doesn't exist" ); + } else { + WP_CLI::warning( "File doesn't exist: {$file}" ); + } $has_errors = true; continue; } $md5_file = md5_file( ABSPATH . $file ); if ( $md5_file !== $checksum ) { - $this->add_error( $file, "File doesn't verify against checksum" ); + if ( isset( $assoc_args['format'] ) ) { + $this->add_error( $file, "File doesn't verify against checksum" ); + } else { + WP_CLI::warning( "File doesn't verify against checksum: {$file}" ); + } $has_errors = true; } } @@ -182,11 +189,15 @@ public function __invoke( $args, $assoc_args ) { if ( in_array( $additional_file, $this->exclude_files, true ) ) { continue; } - $this->add_error( $additional_file, 'File should not exist' ); + if ( isset( $assoc_args['format'] ) ) { + $this->add_error( $additional_file, 'File should not exist' ); + } else { + WP_CLI::warning( "File should not exist: {$additional_file}" ); + } } } - if ( ! empty( $this->errors ) ) { + if ( ! empty( $this->errors ) && isset( $assoc_args['format'] ) ) { $formatter = new Formatter( $assoc_args, array( 'file', 'message' ) From 0b19b82c739ef61aab90825963d7f7d1cb26e8fa Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 15:29:22 +0600 Subject: [PATCH 03/21] feat: add scenarios for verifying core checksums with format parameter --- features/checksum-core.feature | 160 +++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index 03e697e8..f6081891 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -260,3 +260,163 @@ Feature: Validate checksums for WordPress install Error: WordPress installation doesn't verify against checksums. """ And the return code should be 1 + + @require-php-7.0 + Scenario: Verify core checksums with format parameter - success case + Given a WP install + + When I run `wp core update` + Then STDOUT should not be empty + + When I run `wp core verify-checksums --format=json` + Then STDOUT should be: + """ + Success: WordPress installation verifies against checksums. + """ + And the return code should be 0 + + When I run `wp core verify-checksums --format=table` + Then STDOUT should be: + """ + Success: WordPress installation verifies against checksums. + """ + And the return code should be 0 + + Scenario: Verify core checksums with format parameter - error cases + Given a WP install + And "WordPress" replaced with "Wordpress" in the readme.html file + + When I try `wp core verify-checksums --format=json` + Then STDOUT should contain: + """ + {"file":"readme.html","message":"File doesn't verify against checksum"} + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + When I try `wp core verify-checksums --format=table` + Then STDOUT should contain: + """ + readme.html + """ + And STDOUT should contain: + """ + File doesn't verify against checksum + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + Scenario: Verify core checksums with format parameter - missing and extra files + Given a WP install + And a wp-includes/extra-file.txt file: + """ + hello world + """ + + When I run `rm readme.html` + Then STDERR should be empty + + When I try `wp core verify-checksums --format=csv` + Then STDOUT should contain: + """ + file,message + """ + And STDOUT should contain: + """ + readme.html,"File doesn't exist" + """ + And STDOUT should contain: + """ + wp-includes/extra-file.txt,"File should not exist" + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + Scenario: Verify core checksums with format parameter - yaml format + Given a WP install + And "WordPress" replaced with "PressWord" in the readme.html file + + When I try `wp core verify-checksums --format=yaml` + Then STDOUT should contain: + """ + --- + """ + And STDOUT should contain: + """ + file: readme.html + """ + And STDOUT should contain: + """ + message: "File doesn't verify against checksum" + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + Scenario: Verify core checksums with format parameter - count format + Given a WP install + And "WordPress" replaced with "PressWord" in the readme.html file + And a wp-includes/extra-file.txt file: + """ + hello world + """ + + When I try `wp core verify-checksums --format=count` + Then STDOUT should be: + """ + 2 + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + Scenario: Verify core checksums with format parameter and exclude functionality + Given a WP install + And "WordPress" replaced with "PressWord" in the readme.html file + And a wp-includes/extra-file.txt file: + """ + hello world + """ + + When I try `wp core verify-checksums --format=json --exclude='readme.html'` + Then STDOUT should contain: + """ + {"file":"wp-includes/extra-file.txt","message":"File should not exist"} + """ + And STDOUT should not contain: + """ + readme.html + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + When I try `wp core verify-checksums --format=table --exclude='wp-includes/extra-file.txt'` + Then STDOUT should contain: + """ + readme.html + """ + And STDOUT should not contain: + """ + wp-includes/extra-file.txt + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 From 805292fce376176fecef6f3a895e60e1cda03b44 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 16:21:08 +0600 Subject: [PATCH 04/21] refactor: remove redundant scenarios for verifying core checksums with format parameter --- features/checksum-core.feature | 160 --------------------------------- 1 file changed, 160 deletions(-) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index f6081891..03e697e8 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -260,163 +260,3 @@ Feature: Validate checksums for WordPress install Error: WordPress installation doesn't verify against checksums. """ And the return code should be 1 - - @require-php-7.0 - Scenario: Verify core checksums with format parameter - success case - Given a WP install - - When I run `wp core update` - Then STDOUT should not be empty - - When I run `wp core verify-checksums --format=json` - Then STDOUT should be: - """ - Success: WordPress installation verifies against checksums. - """ - And the return code should be 0 - - When I run `wp core verify-checksums --format=table` - Then STDOUT should be: - """ - Success: WordPress installation verifies against checksums. - """ - And the return code should be 0 - - Scenario: Verify core checksums with format parameter - error cases - Given a WP install - And "WordPress" replaced with "Wordpress" in the readme.html file - - When I try `wp core verify-checksums --format=json` - Then STDOUT should contain: - """ - {"file":"readme.html","message":"File doesn't verify against checksum"} - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. - """ - And the return code should be 1 - - When I try `wp core verify-checksums --format=table` - Then STDOUT should contain: - """ - readme.html - """ - And STDOUT should contain: - """ - File doesn't verify against checksum - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. - """ - And the return code should be 1 - - Scenario: Verify core checksums with format parameter - missing and extra files - Given a WP install - And a wp-includes/extra-file.txt file: - """ - hello world - """ - - When I run `rm readme.html` - Then STDERR should be empty - - When I try `wp core verify-checksums --format=csv` - Then STDOUT should contain: - """ - file,message - """ - And STDOUT should contain: - """ - readme.html,"File doesn't exist" - """ - And STDOUT should contain: - """ - wp-includes/extra-file.txt,"File should not exist" - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. - """ - And the return code should be 1 - - Scenario: Verify core checksums with format parameter - yaml format - Given a WP install - And "WordPress" replaced with "PressWord" in the readme.html file - - When I try `wp core verify-checksums --format=yaml` - Then STDOUT should contain: - """ - --- - """ - And STDOUT should contain: - """ - file: readme.html - """ - And STDOUT should contain: - """ - message: "File doesn't verify against checksum" - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. - """ - And the return code should be 1 - - Scenario: Verify core checksums with format parameter - count format - Given a WP install - And "WordPress" replaced with "PressWord" in the readme.html file - And a wp-includes/extra-file.txt file: - """ - hello world - """ - - When I try `wp core verify-checksums --format=count` - Then STDOUT should be: - """ - 2 - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. - """ - And the return code should be 1 - - Scenario: Verify core checksums with format parameter and exclude functionality - Given a WP install - And "WordPress" replaced with "PressWord" in the readme.html file - And a wp-includes/extra-file.txt file: - """ - hello world - """ - - When I try `wp core verify-checksums --format=json --exclude='readme.html'` - Then STDOUT should contain: - """ - {"file":"wp-includes/extra-file.txt","message":"File should not exist"} - """ - And STDOUT should not contain: - """ - readme.html - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. - """ - And the return code should be 1 - - When I try `wp core verify-checksums --format=table --exclude='wp-includes/extra-file.txt'` - Then STDOUT should contain: - """ - readme.html - """ - And STDOUT should not contain: - """ - wp-includes/extra-file.txt - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. - """ - And the return code should be 1 From 37e7679901c6d2eef83759c29a7a570069516f81 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 17:02:50 +0600 Subject: [PATCH 05/21] fix: optimize format handling in checksum verification errors --- src/Checksum_Core_Command.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 8091df35..ad225963 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -105,8 +105,9 @@ class Checksum_Core_Command extends Checksum_Base_Command { * @when before_wp_load */ public function __invoke( $args, $assoc_args ) { - $wp_version = ''; - $locale = ''; + $wp_version = ''; + $locale = ''; + $has_formatter = isset( $assoc_args['format'] ); if ( ! empty( $assoc_args['version'] ) ) { $wp_version = $assoc_args['version']; @@ -160,7 +161,7 @@ public function __invoke( $args, $assoc_args ) { } if ( ! file_exists( ABSPATH . $file ) ) { - if ( isset( $assoc_args['format'] ) ) { + if ( $has_formatter ) { $this->add_error( $file, "File doesn't exist" ); } else { WP_CLI::warning( "File doesn't exist: {$file}" ); @@ -171,7 +172,7 @@ public function __invoke( $args, $assoc_args ) { $md5_file = md5_file( ABSPATH . $file ); if ( $md5_file !== $checksum ) { - if ( isset( $assoc_args['format'] ) ) { + if ( $has_formatter ) { $this->add_error( $file, "File doesn't verify against checksum" ); } else { WP_CLI::warning( "File doesn't verify against checksum: {$file}" ); @@ -189,7 +190,7 @@ public function __invoke( $args, $assoc_args ) { if ( in_array( $additional_file, $this->exclude_files, true ) ) { continue; } - if ( isset( $assoc_args['format'] ) ) { + if ( $has_formatter ) { $this->add_error( $additional_file, 'File should not exist' ); } else { WP_CLI::warning( "File should not exist: {$additional_file}" ); @@ -197,7 +198,7 @@ public function __invoke( $args, $assoc_args ) { } } - if ( ! empty( $this->errors ) && isset( $assoc_args['format'] ) ) { + if ( ! empty( $this->errors ) && $has_formatter ) { $formatter = new Formatter( $assoc_args, array( 'file', 'message' ) From a4665e3c28defbbc99a6275f17385e1436c91b6f Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 17:02:55 +0600 Subject: [PATCH 06/21] feat: add scenarios for verifying core checksums with various format parameters --- features/checksum-core.feature | 119 +++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index 03e697e8..2b1b918f 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -260,3 +260,122 @@ 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/version.php file + And a wp-includes/extra-file.txt file: + """ + This is an extra file + """ + And "WordPress" replaced with "PressWord" in the readme.html file + + When I try `wp core verify-checksums --format=json` + Then STDOUT should be: + """ + [{"file":"readme.html","message":"File doesn't verify against checksum"},{"file":"wp-includes\/version.php","message":"File doesn't verify against checksum"},{"file":"wp-includes\/extra-file.txt","message":"File should not exist"}] + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + Scenario: Core checksums verify with table format + Given a WP install + And "WordPress" replaced with "Modified" in the wp-includes/functions.php file + + 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 | + And the return code should be 1 + + Scenario: Core checksums verify with csv format + Given a WP install + And a wp-includes/test.php file: + """ + + """ + + When I try `wp core verify-checksums --format=csv` + Then STDOUT should be: + """ + file,message + wp-includes/test.php,"File should not exist" + Success: WordPress installation verifies against checksums. + """ + And the return code should be 0 + + Scenario: Core checksums verify format parameter with missing core files + Given a WP install + When I run `rm wp-includes/widgets.php wp-includes/rest-api.php` + And I try `wp core verify-checksums --format=json` + Then STDOUT should contain: + """ + [{"file":"wp-includes\/rest-api.php","message":"File doesn't exist"},{"file":"wp-includes\/widgets.php","message":"File doesn't exist"}] + """ + And STDERR should be: + """ + Error: WordPress installation doesn't verify against checksums. + """ + And the return code should be 1 + + Scenario: Core checksums verify format parameter with multiple file types + Given a WP install + And "Version" replaced with "v" in the wp-includes/version.php file + And I run `rm wp-includes/nav-menu.php` + And a wp-includes/extra.txt file: + """ + test file + """ + + When I try `wp core verify-checksums --format=json` + Then STDOUT should contain: + """ + [{"file":"wp-includes\/nav-menu.php","message":"File doesn't exist"},{"file":"wp-includes\/version.php","message":"File doesn't verify against checksum"},{"file":"wp-includes\/extra.txt","message":"File should not exist"}] + """ + And the return code should be 1 + + Scenario: Core checksums verify with count format + Given a WP install + And "WordPress" replaced with "Modified" in the wp-includes/post.php file + And I run `rm wp-includes/comment.php` + And a wp-includes/test.txt file: + """ + test content + """ + + When I try `wp core verify-checksums --format=count` + Then STDOUT should be: + """ + 3 + """ + And the return code should be 1 + + Scenario: Core checksums verify with format parameter and excluded files + Given a WP install + And "WordPress" replaced with "Modified" in the wp-includes/update.php file + And "WordPress" replaced with "Changed" in the wp-includes/meta.php file + And a wp-includes/test.log file: + """ + log content + """ + + When I try `wp core verify-checksums --format=json --exclude=wp-includes/meta.php,wp-includes/test.log` + Then STDOUT should contain: + """ + [{"file":"wp-includes\/update.php","message":"File doesn't verify against checksum"}] + """ + And the return code should be 1 + + Scenario: Core checksums verify format parameter with empty result after exclusion + Given a WP install + And "WordPress" replaced with "Changed" in the wp-includes/cache.php file + + When I try `wp core verify-checksums --format=json --exclude=wp-includes/cache.php` + Then STDOUT should be: + """ + Success: WordPress installation verifies against checksums. + """ + And the return code should be 0 From f9ffb783cc72c995ada6f51e4eaa645006c27a57 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 20:26:07 +0600 Subject: [PATCH 07/21] refactor: remove redundant scenario for verifying core checksums with multiple file types --- features/checksum-core.feature | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index 2b1b918f..53d1bb43 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -321,22 +321,6 @@ Feature: Validate checksums for WordPress install """ And the return code should be 1 - Scenario: Core checksums verify format parameter with multiple file types - Given a WP install - And "Version" replaced with "v" in the wp-includes/version.php file - And I run `rm wp-includes/nav-menu.php` - And a wp-includes/extra.txt file: - """ - test file - """ - - When I try `wp core verify-checksums --format=json` - Then STDOUT should contain: - """ - [{"file":"wp-includes\/nav-menu.php","message":"File doesn't exist"},{"file":"wp-includes\/version.php","message":"File doesn't verify against checksum"},{"file":"wp-includes\/extra.txt","message":"File should not exist"}] - """ - And the return code should be 1 - Scenario: Core checksums verify with count format Given a WP install And "WordPress" replaced with "Modified" in the wp-includes/post.php file From 01629b0dded15c59ff66ee5ee359d0658d3098d8 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Thu, 7 Aug 2025 20:42:38 +0600 Subject: [PATCH 08/21] fix: update error message for clarity in checksum verification --- features/checksum-core.feature | 6 +++--- src/Checksum_Core_Command.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index 53d1bb43..be549235 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -309,11 +309,11 @@ Feature: Validate checksums for WordPress install Scenario: Core checksums verify format parameter with missing core files Given a WP install - When I run `rm wp-includes/widgets.php wp-includes/rest-api.php` + When I run `rm wp-includes/widgets.php` And I try `wp core verify-checksums --format=json` Then STDOUT should contain: """ - [{"file":"wp-includes\/rest-api.php","message":"File doesn't exist"},{"file":"wp-includes\/widgets.php","message":"File doesn't exist"}] + "file":"wp-includes\/widgets.php","message":"File doesn't exist" """ And STDERR should be: """ @@ -349,7 +349,7 @@ Feature: Validate checksums for WordPress install When I try `wp core verify-checksums --format=json --exclude=wp-includes/meta.php,wp-includes/test.log` Then STDOUT should contain: """ - [{"file":"wp-includes\/update.php","message":"File doesn't verify against checksum"}] + "file":"wp-includes\/update.php","message":"File doesn't verify against checksum" """ And the return code should be 1 diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index ad225963..5be1a61c 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -247,7 +247,7 @@ private static function get_wp_details() { if ( ! is_readable( $versions_path ) ) { WP_CLI::error( "This does not seem to be a WordPress install.\n" . - 'Pass --path=`path/to/wordpress` or run `wp core download`.' + 'Pass --path=`path/to/wordpress` or run `wp core download`.' ); } From 263de93ef3b7c44dd10d127d5a68531d4faadf81 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Fri, 15 Aug 2025 21:32:42 +0600 Subject: [PATCH 09/21] fix: remove add_error method and use inline array item push --- src/Checksum_Core_Command.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 5be1a61c..b864cd23 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -162,7 +162,10 @@ public function __invoke( $args, $assoc_args ) { if ( ! file_exists( ABSPATH . $file ) ) { if ( $has_formatter ) { - $this->add_error( $file, "File doesn't exist" ); + $this->errors[] = [ + 'file' => $file, + 'message' => "File doesn't exist" + ]; } else { WP_CLI::warning( "File doesn't exist: {$file}" ); } @@ -173,7 +176,10 @@ public function __invoke( $args, $assoc_args ) { $md5_file = md5_file( ABSPATH . $file ); if ( $md5_file !== $checksum ) { if ( $has_formatter ) { - $this->add_error( $file, "File doesn't verify against checksum" ); + $this->errors[] = [ + 'file' => $file, + 'message' => "File doesn't verify against checksum" + ]; } else { WP_CLI::warning( "File doesn't verify against checksum: {$file}" ); } @@ -191,7 +197,10 @@ public function __invoke( $args, $assoc_args ) { continue; } if ( $has_formatter ) { - $this->add_error( $additional_file, 'File should not exist' ); + $this->errors[] = [ + 'file' => $additional_file, + 'message' => 'File should not exist' + ]; } else { WP_CLI::warning( "File should not exist: {$additional_file}" ); } @@ -289,16 +298,4 @@ private static function find_var( $var_name, $code ) { return trim( $value, " '" ); } - - /** - * Adds a new error to the array of detected errors. - * - * @param string $file Relative path to the file that had the error. - * @param string $message Message explaining the error. - */ - private function add_error( $file, $message ) { - $error['file'] = $file; - $error['message'] = $message; - $this->errors[] = $error; - } } From 5f7b16351cb55bd5d27fcfa65e6d756a9e99a56c Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Fri, 15 Aug 2025 22:11:03 +0600 Subject: [PATCH 10/21] fix: improve error handling and output formatting in checksum verification --- src/Checksum_Core_Command.php | 75 ++++++++++++++++------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index b864cd23..61e22d99 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -65,7 +65,9 @@ class Checksum_Core_Command extends Checksum_Base_Command { * [--format=] * : Render output in a specific format. When provided, errors are displayed in tabular format instead of individual warning messages. * --- + * default: plain * options: + * - plain * - table * - json * - csv @@ -100,14 +102,14 @@ class Checksum_Core_Command extends Checksum_Base_Command { * * # Verify checksums with formatted output * $ wp core verify-checksums --format=json - * Success: WordPress installation verifies against checksums. + * [{"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 ) { - $wp_version = ''; - $locale = ''; - $has_formatter = isset( $assoc_args['format'] ); + $wp_version = ''; + $locale = ''; if ( ! empty( $assoc_args['version'] ) ) { $wp_version = $assoc_args['version']; @@ -149,7 +151,6 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::error( "Couldn't get checksums from WordPress.org." ); } - $has_errors = false; foreach ( $checksums as $file => $checksum ) { // Skip files which get updated if ( 'wp-content' === substr( $file, 0, 10 ) ) { @@ -161,29 +162,20 @@ public function __invoke( $args, $assoc_args ) { } if ( ! file_exists( ABSPATH . $file ) ) { - if ( $has_formatter ) { - $this->errors[] = [ - 'file' => $file, - 'message' => "File doesn't exist" - ]; - } else { - WP_CLI::warning( "File doesn't exist: {$file}" ); - } - $has_errors = true; + $this->errors[] = [ + 'file' => $file, + 'message' => "File doesn't exist", + ]; + continue; } $md5_file = md5_file( ABSPATH . $file ); if ( $md5_file !== $checksum ) { - if ( $has_formatter ) { - $this->errors[] = [ - 'file' => $file, - 'message' => "File doesn't verify against checksum" - ]; - } else { - WP_CLI::warning( "File doesn't verify against checksum: {$file}" ); - } - $has_errors = true; + $this->errors[] = [ + 'file' => $file, + 'message' => "File doesn't verify against checksum", + ]; } } @@ -196,29 +188,30 @@ public function __invoke( $args, $assoc_args ) { if ( in_array( $additional_file, $this->exclude_files, true ) ) { continue; } - if ( $has_formatter ) { - $this->errors[] = [ - 'file' => $additional_file, - 'message' => 'File should not exist' - ]; - } else { - WP_CLI::warning( "File should not exist: {$additional_file}" ); - } + + $this->errors[] = [ + 'file' => $additional_file, + 'message' => 'File should not exist', + ]; } } - if ( ! empty( $this->errors ) && $has_formatter ) { - $formatter = new Formatter( - $assoc_args, - array( 'file', 'message' ) - ); - $formatter->display_items( $this->errors ); - } + if ( ! empty( $this->errors ) ) { + if ( ! isset( $assoc_args['format'] ) || $assoc_args['format'] === 'plain' ) { + foreach ( $this->errors as $error ) { + WP_CLI::error( sprintf( '%s: %s', $error['message'], $error['file'] ) ); + } + } else { + $formatter = new Formatter( + $assoc_args, + array( 'file', 'message' ) + ); + $formatter->display_items( $this->errors ); + } - if ( ! $has_errors ) { - WP_CLI::success( 'WordPress installation verifies against checksums.' ); - } else { WP_CLI::error( "WordPress installation doesn't verify against checksums." ); + } else { + WP_CLI::success( 'WordPress installation verifies against checksums.' ); } } From b51a64c3469e813d5bcdefba908de027008df012 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Fri, 15 Aug 2025 22:43:24 +0600 Subject: [PATCH 11/21] fix: update error handling to use warnings for checksum verification failures --- src/Checksum_Core_Command.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 61e22d99..dbaf5c52 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -102,7 +102,7 @@ class Checksum_Core_Command extends Checksum_Base_Command { * * # Verify checksums with formatted output * $ wp core verify-checksums --format=json - * [{"file":"readme.html","message":"File doesn't verify against checksum"}] + * [{"file":"readme.html","message":"File doesn't verify against checksum"}] * Error: WordPress installation doesn't verify against checksums. * * @when before_wp_load @@ -151,6 +151,7 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::error( "Couldn't get checksums from WordPress.org." ); } + $has_errors = false; foreach ( $checksums as $file => $checksum ) { // Skip files which get updated if ( 'wp-content' === substr( $file, 0, 10 ) ) { @@ -167,6 +168,8 @@ public function __invoke( $args, $assoc_args ) { 'message' => "File doesn't exist", ]; + $has_errors = true; + continue; } @@ -176,6 +179,8 @@ public function __invoke( $args, $assoc_args ) { 'file' => $file, 'message' => "File doesn't verify against checksum", ]; + + $has_errors = true; } } @@ -199,7 +204,7 @@ public function __invoke( $args, $assoc_args ) { if ( ! empty( $this->errors ) ) { if ( ! isset( $assoc_args['format'] ) || $assoc_args['format'] === 'plain' ) { foreach ( $this->errors as $error ) { - WP_CLI::error( sprintf( '%s: %s', $error['message'], $error['file'] ) ); + WP_CLI::warning( sprintf( '%s: %s', $error['message'], $error['file'] ) ); } } else { $formatter = new Formatter( @@ -208,10 +213,12 @@ public function __invoke( $args, $assoc_args ) { ); $formatter->display_items( $this->errors ); } + } - WP_CLI::error( "WordPress installation doesn't verify against checksums." ); - } else { + if ( ! $has_errors ) { WP_CLI::success( 'WordPress installation verifies against checksums.' ); + } else { + WP_CLI::error( "WordPress installation doesn't verify against checksums." ); } } From 5851d4b4a90a78c5a29c91e9736c46d2daaa4566 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Fri, 15 Aug 2025 22:43:31 +0600 Subject: [PATCH 12/21] fix: update scenarios for core checksums verification with format parameters --- features/checksum-core.feature | 40 +++++++--------------------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index be549235..6f81c727 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -281,9 +281,9 @@ Feature: Validate checksums for WordPress install """ And the return code should be 1 - Scenario: Core checksums verify with table format + Scenario: Core checksums verify with format parameter Given a WP install - And "WordPress" replaced with "Modified" in the wp-includes/functions.php file + And "WordPress" replaced with "Modified WordPress" in the wp-includes/functions.php file When I try `wp core verify-checksums --format=table` Then STDOUT should be a table containing rows: @@ -291,49 +291,25 @@ Feature: Validate checksums for WordPress install | wp-includes/functions.php | File doesn't verify against checksum | And the return code should be 1 - Scenario: Core checksums verify with csv format - Given a WP install - And a wp-includes/test.php file: - """ - - """ - When I try `wp core verify-checksums --format=csv` - Then STDOUT should be: + Then STDOUT should contain: """ file,message - wp-includes/test.php,"File should not exist" - Success: WordPress installation verifies against checksums. + wp-includes/functions.php,"File doesn't verify against checksum" """ - And the return code should be 0 + And the return code should be 1 - Scenario: Core checksums verify format parameter with missing core files - Given a WP install - When I run `rm wp-includes/widgets.php` - And I try `wp core verify-checksums --format=json` + When I try `wp core verify-checksums --format=json` Then STDOUT should contain: """ - "file":"wp-includes\/widgets.php","message":"File doesn't exist" - """ - And STDERR should be: - """ - Error: WordPress installation doesn't verify against checksums. + "file":"wp-includes\/functions.php","message":"File doesn't verify against checksum" """ And the return code should be 1 - Scenario: Core checksums verify with count format - Given a WP install - And "WordPress" replaced with "Modified" in the wp-includes/post.php file - And I run `rm wp-includes/comment.php` - And a wp-includes/test.txt file: - """ - test content - """ - When I try `wp core verify-checksums --format=count` Then STDOUT should be: """ - 3 + 1 """ And the return code should be 1 From e4d4f6f0ababf756d8e0b4c443db05d246fc3f51 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Fri, 15 Aug 2025 23:19:13 +0600 Subject: [PATCH 13/21] fix: yoda condition check --- src/Checksum_Core_Command.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index dbaf5c52..23ca18fa 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -174,7 +174,7 @@ public function __invoke( $args, $assoc_args ) { } $md5_file = md5_file( ABSPATH . $file ); - if ( $md5_file !== $checksum ) { + if ( $checksum !== $md5_file ) { $this->errors[] = [ 'file' => $file, 'message' => "File doesn't verify against checksum", @@ -202,7 +202,7 @@ public function __invoke( $args, $assoc_args ) { } if ( ! empty( $this->errors ) ) { - if ( ! isset( $assoc_args['format'] ) || $assoc_args['format'] === 'plain' ) { + if ( ! isset( $assoc_args['format'] ) || 'plain' === $assoc_args['format'] ) { foreach ( $this->errors as $error ) { WP_CLI::warning( sprintf( '%s: %s', $error['message'], $error['file'] ) ); } @@ -287,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; } From 1b3112e927ecc7b4399f91f985ad178c5378c3aa Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sat, 16 Aug 2025 18:13:49 +0600 Subject: [PATCH 14/21] fix: update checksum verification scenarios to include new file checks and format outputs --- features/checksum-core.feature | 45 +++++++--------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index 6f81c727..c3c7c016 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -263,32 +263,17 @@ Feature: Validate checksums for WordPress install Scenario: Core checksums verify with format parameter Given a WP install - And "WordPress" replaced with "Modified WordPress" in the wp-includes/version.php file - And a wp-includes/extra-file.txt file: - """ - This is an extra file - """ - And "WordPress" replaced with "PressWord" in the readme.html file - - When I try `wp core verify-checksums --format=json` - Then STDOUT should be: - """ - [{"file":"readme.html","message":"File doesn't verify against checksum"},{"file":"wp-includes\/version.php","message":"File doesn't verify against checksum"},{"file":"wp-includes\/extra-file.txt","message":"File should not exist"}] - """ - And STDERR should be: + And "WordPress" replaced with "Modified WordPress" in the wp-includes/functions.php file + And a wp-includes/test.log file: """ - Error: WordPress installation doesn't verify against checksums. + log content """ - 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 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` @@ -296,6 +281,7 @@ Feature: Validate checksums for WordPress install """ 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 @@ -309,31 +295,18 @@ Feature: Validate checksums for WordPress install When I try `wp core verify-checksums --format=count` Then STDOUT should be: """ - 1 + 2 """ And the return code should be 1 - Scenario: Core checksums verify with format parameter and excluded files - Given a WP install - And "WordPress" replaced with "Modified" in the wp-includes/update.php file - And "WordPress" replaced with "Changed" in the wp-includes/meta.php file - And a wp-includes/test.log file: - """ - log content - """ - - When I try `wp core verify-checksums --format=json --exclude=wp-includes/meta.php,wp-includes/test.log` + When I try `wp core verify-checksums --format=json --exclude=wp-includes/test.log` Then STDOUT should contain: """ - "file":"wp-includes\/update.php","message":"File doesn't verify against checksum" + "file":"wp-includes\/functions.php","message":"File doesn't verify against checksum" """ And the return code should be 1 - Scenario: Core checksums verify format parameter with empty result after exclusion - Given a WP install - And "WordPress" replaced with "Changed" in the wp-includes/cache.php file - - When I try `wp core verify-checksums --format=json --exclude=wp-includes/cache.php` + 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. From 098bb259bb672787063caf8669804f25a1b21bf7 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sat, 16 Aug 2025 18:31:08 +0600 Subject: [PATCH 15/21] wip --- features/checksum-core.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index c3c7c016..e27497d0 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -311,4 +311,4 @@ Feature: Validate checksums for WordPress install """ Success: WordPress installation verifies against checksums. """ - And the return code should be 0 + And the return code should be 00 From f0601d7f180908880ac3d86120dabab0a04b0351 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sat, 16 Aug 2025 18:31:14 +0600 Subject: [PATCH 16/21] wip --- features/checksum-core.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/checksum-core.feature b/features/checksum-core.feature index e27497d0..c3c7c016 100644 --- a/features/checksum-core.feature +++ b/features/checksum-core.feature @@ -311,4 +311,4 @@ Feature: Validate checksums for WordPress install """ Success: WordPress installation verifies against checksums. """ - And the return code should be 00 + And the return code should be 0 From e2845ba845e6ffa6e759d0e3f73b32e6bfe424bb Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sat, 16 Aug 2025 21:25:06 +0600 Subject: [PATCH 17/21] fix: standardize error flag assignment spacing in checksum verification --- src/Checksum_Core_Command.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 23ca18fa..613510dc 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -167,8 +167,7 @@ public function __invoke( $args, $assoc_args ) { 'file' => $file, 'message' => "File doesn't exist", ]; - - $has_errors = true; + $has_errors = true; continue; } @@ -179,8 +178,7 @@ public function __invoke( $args, $assoc_args ) { 'file' => $file, 'message' => "File doesn't verify against checksum", ]; - - $has_errors = true; + $has_errors = true; } } From 967e0ddc00e56d90261b0c4d7478b392be22497e Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sat, 16 Aug 2025 21:25:35 +0600 Subject: [PATCH 18/21] fix: standardize spacing for error flag assignment in checksum verification --- src/Checksum_Core_Command.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 613510dc..23ca18fa 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -167,7 +167,8 @@ public function __invoke( $args, $assoc_args ) { 'file' => $file, 'message' => "File doesn't exist", ]; - $has_errors = true; + + $has_errors = true; continue; } @@ -178,7 +179,8 @@ public function __invoke( $args, $assoc_args ) { 'file' => $file, 'message' => "File doesn't verify against checksum", ]; - $has_errors = true; + + $has_errors = true; } } From 9518886152f576e4ab2f15915b4508a24e01c6a0 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sun, 17 Aug 2025 10:52:32 +0600 Subject: [PATCH 19/21] wip --- src/Checksum_Core_Command.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 23ca18fa..b7d10341 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -165,7 +165,7 @@ public function __invoke( $args, $assoc_args ) { if ( ! file_exists( ABSPATH . $file ) ) { $this->errors[] = [ 'file' => $file, - 'message' => "File doesn't exist", + 'message' => "File doesn't exist" ]; $has_errors = true; @@ -177,7 +177,7 @@ public function __invoke( $args, $assoc_args ) { if ( $checksum !== $md5_file ) { $this->errors[] = [ 'file' => $file, - 'message' => "File doesn't verify against checksum", + 'message' => "File doesn't verify against checksum" ]; $has_errors = true; @@ -196,7 +196,7 @@ public function __invoke( $args, $assoc_args ) { $this->errors[] = [ 'file' => $additional_file, - 'message' => 'File should not exist', + 'message' => 'File should not exist' ]; } } From 92ecea1fbd14a1b1cd431ec8a93e87bba2546b38 Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sun, 17 Aug 2025 10:52:47 +0600 Subject: [PATCH 20/21] fix: add missing commas in error messages for clarity --- src/Checksum_Core_Command.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index b7d10341..23ca18fa 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -165,7 +165,7 @@ public function __invoke( $args, $assoc_args ) { if ( ! file_exists( ABSPATH . $file ) ) { $this->errors[] = [ 'file' => $file, - 'message' => "File doesn't exist" + 'message' => "File doesn't exist", ]; $has_errors = true; @@ -177,7 +177,7 @@ public function __invoke( $args, $assoc_args ) { if ( $checksum !== $md5_file ) { $this->errors[] = [ 'file' => $file, - 'message' => "File doesn't verify against checksum" + 'message' => "File doesn't verify against checksum", ]; $has_errors = true; @@ -196,7 +196,7 @@ public function __invoke( $args, $assoc_args ) { $this->errors[] = [ 'file' => $additional_file, - 'message' => 'File should not exist' + 'message' => 'File should not exist', ]; } } From 1b4e576c16749372e42ec7083ae424bf13a56c8a Mon Sep 17 00:00:00 2001 From: Al-Amin Firdows Date: Sun, 17 Aug 2025 16:57:20 +0600 Subject: [PATCH 21/21] fix: clarify output format description in checksum verification options --- src/Checksum_Core_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Checksum_Core_Command.php b/src/Checksum_Core_Command.php index 23ca18fa..8bca3518 100644 --- a/src/Checksum_Core_Command.php +++ b/src/Checksum_Core_Command.php @@ -63,7 +63,7 @@ class Checksum_Core_Command extends Checksum_Base_Command { * : Exclude specific files from the checksum verification. Provide a comma-separated list of file paths. * * [--format=] - * : Render output in a specific format. When provided, errors are displayed in tabular format instead of individual warning messages. + * : Render output in a specific format. When provided, messages are displayed in the chosen format. * --- * default: plain * options: