Skip to content

Commit cc38427

Browse files
authored
Merge pull request #140 from alaminfirdows/fix/core-checksums-format
2 parents 8e6d7e5 + 1b4e576 commit cc38427

2 files changed

Lines changed: 110 additions & 5 deletions

File tree

features/checksum-core.feature

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,55 @@ Feature: Validate checksums for WordPress install
260260
Error: WordPress installation doesn't verify against checksums.
261261
"""
262262
And the return code should be 1
263+
264+
Scenario: Core checksums verify with format parameter
265+
Given a WP install
266+
And "WordPress" replaced with "Modified WordPress" in the wp-includes/functions.php file
267+
And a wp-includes/test.log file:
268+
"""
269+
log content
270+
"""
271+
272+
When I try `wp core verify-checksums --format=table`
273+
Then STDOUT should be a table containing rows:
274+
| file | message |
275+
| wp-includes/functions.php | File doesn't verify against checksum |
276+
| wp-includes/test.log | File should not exist |
277+
And the return code should be 1
278+
279+
When I try `wp core verify-checksums --format=csv`
280+
Then STDOUT should contain:
281+
"""
282+
file,message
283+
wp-includes/functions.php,"File doesn't verify against checksum"
284+
wp-includes/test.log,"File should not exist"
285+
"""
286+
And the return code should be 1
287+
288+
When I try `wp core verify-checksums --format=json`
289+
Then STDOUT should contain:
290+
"""
291+
"file":"wp-includes\/functions.php","message":"File doesn't verify against checksum"
292+
"""
293+
And the return code should be 1
294+
295+
When I try `wp core verify-checksums --format=count`
296+
Then STDOUT should be:
297+
"""
298+
2
299+
"""
300+
And the return code should be 1
301+
302+
When I try `wp core verify-checksums --format=json --exclude=wp-includes/test.log`
303+
Then STDOUT should contain:
304+
"""
305+
"file":"wp-includes\/functions.php","message":"File doesn't verify against checksum"
306+
"""
307+
And the return code should be 1
308+
309+
When I try `wp core verify-checksums --format=json --exclude=wp-includes/functions.php,wp-includes/test.log`
310+
Then STDOUT should be:
311+
"""
312+
Success: WordPress installation verifies against checksums.
313+
"""
314+
And the return code should be 0

src/Checksum_Core_Command.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use WP_CLI\Formatter;
34
use WP_CLI\Utils;
45
use WP_CLI\WpOrgApi;
56

@@ -24,6 +25,13 @@ class Checksum_Core_Command extends Checksum_Base_Command {
2425
*/
2526
private $exclude_files = [];
2627

28+
/**
29+
* Array of detected errors.
30+
*
31+
* @var array
32+
*/
33+
private $errors = [];
34+
2735
/**
2836
* Verifies WordPress files against WordPress.org's checksums.
2937
*
@@ -54,6 +62,19 @@ class Checksum_Core_Command extends Checksum_Base_Command {
5462
* [--exclude=<files>]
5563
* : Exclude specific files from the checksum verification. Provide a comma-separated list of file paths.
5664
*
65+
* [--format=<format>]
66+
* : Render output in a specific format. When provided, messages are displayed in the chosen format.
67+
* ---
68+
* default: plain
69+
* options:
70+
* - plain
71+
* - table
72+
* - json
73+
* - csv
74+
* - yaml
75+
* - count
76+
* ---
77+
*
5778
* ## EXAMPLES
5879
*
5980
* # Verify checksums
@@ -79,6 +100,11 @@ class Checksum_Core_Command extends Checksum_Base_Command {
79100
* $ wp core verify-checksums --exclude="readme.html"
80101
* Success: WordPress installation verifies against checksums.
81102
*
103+
* # Verify checksums with formatted output
104+
* $ wp core verify-checksums --format=json
105+
* [{"file":"readme.html","message":"File doesn't verify against checksum"}]
106+
* Error: WordPress installation doesn't verify against checksums.
107+
*
82108
* @when before_wp_load
83109
*/
84110
public function __invoke( $args, $assoc_args ) {
@@ -137,14 +163,23 @@ public function __invoke( $args, $assoc_args ) {
137163
}
138164

139165
if ( ! file_exists( ABSPATH . $file ) ) {
140-
WP_CLI::warning( "File doesn't exist: {$file}" );
166+
$this->errors[] = [
167+
'file' => $file,
168+
'message' => "File doesn't exist",
169+
];
170+
141171
$has_errors = true;
172+
142173
continue;
143174
}
144175

145176
$md5_file = md5_file( ABSPATH . $file );
146-
if ( $md5_file !== $checksum ) {
147-
WP_CLI::warning( "File doesn't verify against checksum: {$file}" );
177+
if ( $checksum !== $md5_file ) {
178+
$this->errors[] = [
179+
'file' => $file,
180+
'message' => "File doesn't verify against checksum",
181+
];
182+
148183
$has_errors = true;
149184
}
150185
}
@@ -158,7 +193,25 @@ public function __invoke( $args, $assoc_args ) {
158193
if ( in_array( $additional_file, $this->exclude_files, true ) ) {
159194
continue;
160195
}
161-
WP_CLI::warning( "File should not exist: {$additional_file}" );
196+
197+
$this->errors[] = [
198+
'file' => $additional_file,
199+
'message' => 'File should not exist',
200+
];
201+
}
202+
}
203+
204+
if ( ! empty( $this->errors ) ) {
205+
if ( ! isset( $assoc_args['format'] ) || 'plain' === $assoc_args['format'] ) {
206+
foreach ( $this->errors as $error ) {
207+
WP_CLI::warning( sprintf( '%s: %s', $error['message'], $error['file'] ) );
208+
}
209+
} else {
210+
$formatter = new Formatter(
211+
$assoc_args,
212+
array( 'file', 'message' )
213+
);
214+
$formatter->display_items( $this->errors );
162215
}
163216
}
164217

@@ -234,7 +287,7 @@ private static function get_wp_details() {
234287
private static function find_var( $var_name, $code ) {
235288
$start = strpos( $code, '$' . $var_name . ' = ' );
236289

237-
if ( ! $start ) {
290+
if ( false === $start ) {
238291
return null;
239292
}
240293

0 commit comments

Comments
 (0)