From eac393c7ca2c65d88a8cd16e6ba5de8a4a90f86b Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Mon, 15 Jul 2024 16:58:02 +0545 Subject: [PATCH 01/12] Add severity level in check --- docs/CLI.md | 3 + includes/CLI/Plugin_Check_Command.php | 43 ++++++++++++- includes/Checker/Check_Result.php | 11 ++-- includes/Traits/Amend_Check_Result.php | 62 ++++++++++--------- .../tests/Checker/Check_Result_Tests.php | 28 +++++---- 5 files changed, 99 insertions(+), 48 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 51bc584d0..b12ff179f 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -47,6 +47,9 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded. [--exclude-files=] : Additional files to exclude from checks. + +[--severity=] +: Severity level. ``` ## EXAMPLES ``` diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index e84b7f41f..7ae711a99 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -20,6 +20,8 @@ /** * Plugin check command. + * + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ final class Plugin_Check_Command { @@ -101,6 +103,9 @@ public function __construct( Plugin_Context $plugin_context ) { * [--exclude-files=] * : Additional files to exclude from checks. * + * [--severity=] + * : Severity level. + * * ## EXAMPLES * * wp plugin check akismet @@ -118,6 +123,7 @@ public function __construct( Plugin_Context $plugin_context ) { * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function check( $args, $assoc_args ) { // Get options based on the CLI arguments. @@ -129,6 +135,7 @@ public function check( $args, $assoc_args ) { 'ignore-warnings' => false, 'ignore-errors' => false, 'include-experimental' => false, + 'severity' => '', ) ); @@ -237,13 +244,27 @@ static function ( $dirs ) use ( $excluded_files ) { unset( $warnings[ $file_name ] ); } $file_results = $this->flatten_file_results( $file_errors, $file_warnings ); - $this->display_results( $formatter, $file_name, $file_results ); + + if ( '' !== $options['severity'] ) { + $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $options['severity'] ) ); + } + + if ( ! empty( $file_results ) ) { + $this->display_results( $formatter, $file_name, $file_results ); + } } // If there are any files left with only warnings, print those next. foreach ( $warnings as $file_name => $file_warnings ) { $file_results = $this->flatten_file_results( array(), $file_warnings ); - $this->display_results( $formatter, $file_name, $file_results ); + + if ( '' !== $options['severity'] ) { + $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $options['severity'] ) ); + } + + if ( ! empty( $file_results ) ) { + $this->display_results( $formatter, $file_name, $file_results ); + } } } @@ -599,4 +620,22 @@ private function has_runtime_check( array $checks ) { return false; } + + /** + * Returns check results filtered by severity level. + * + * @since 1.1.0 + * + * @param array $results Check results. + * @param int $severity Severity level. + * @return array Filtered results. + */ + private function get_filtered_results_by_severity( $results, $severity ) { + return array_filter( + $results, + function ( $item ) use ( $severity ) { + return ( $item['severity'] >= $severity ); + } + ); + } } diff --git a/includes/Checker/Check_Result.php b/includes/Checker/Check_Result.php index 88633f1aa..e79e5270d 100644 --- a/includes/Checker/Check_Result.php +++ b/includes/Checker/Check_Result.php @@ -95,11 +95,12 @@ public function plugin() { */ public function add_message( $error, $message, $args = array() ) { $defaults = array( - 'code' => '', - 'file' => '', - 'line' => 0, - 'column' => 0, - 'link' => '', + 'code' => '', + 'file' => '', + 'line' => 0, + 'column' => 0, + 'link' => '', + 'severity' => 5, ); $data = array_merge( diff --git a/includes/Traits/Amend_Check_Result.php b/includes/Traits/Amend_Check_Result.php index 8da3ae64e..75d1ab071 100644 --- a/includes/Traits/Amend_Check_Result.php +++ b/includes/Traits/Amend_Check_Result.php @@ -23,24 +23,26 @@ trait Amend_Check_Result { * * @since 1.0.0 * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param bool $error Whether it is an error or notice. - * @param string $message Error message. - * @param string $code Error code. - * @param string $file Absolute path to the file where the issue was found. - * @param int $line The line on which the message occurred. Default is 0 (unknown line). - * @param int $column The column on which the message occurred. Default is 0 (unknown column). + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param bool $error Whether it is an error or notice. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the issue was found. + * @param int $line The line on which the message occurred. Default is 0 (unknown line). + * @param int $column The column on which the message occurred. Default is 0 (unknown column). + * @param int $severity Severity level. Default is 5. */ - protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0 ) { + protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0, $severity = 5 ) { $result->add_message( (bool) $error, $message, array( - 'code' => $code, - 'file' => str_replace( $result->plugin()->path(), '', $file ), - 'line' => $line, - 'column' => $column, - 'link' => $this->get_file_editor_url( $result, $file, $line ), + 'code' => $code, + 'file' => str_replace( $result->plugin()->path(), '', $file ), + 'line' => $line, + 'column' => $column, + 'link' => $this->get_file_editor_url( $result, $file, $line ), + 'severity' => $severity, ) ); } @@ -50,15 +52,16 @@ protected function add_result_message_for_file( Check_Result $result, $error, $m * * @since 1.0.0 * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param string $message Error message. - * @param string $code Error code. - * @param string $file Absolute path to the file where the error was found. - * @param int $line The line on which the error occurred. Default is 0 (unknown line). - * @param int $column The column on which the error occurred. Default is 0 (unknown column). + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the error was found. + * @param int $line The line on which the error occurred. Default is 0 (unknown line). + * @param int $column The column on which the error occurred. Default is 0 (unknown column). + * @param int $severity Severity level. Default is 5. */ - protected function add_result_error_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0 ) { - $this->add_result_message_for_file( $result, true, $message, $code, $file, $line, $column ); + protected function add_result_error_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0, $severity = 5 ) { + $this->add_result_message_for_file( $result, true, $message, $code, $file, $line, $column, $severity ); } /** @@ -66,14 +69,15 @@ protected function add_result_error_for_file( Check_Result $result, $message, $c * * @since 1.0.0 * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param string $message Error message. - * @param string $code Error code. - * @param string $file Absolute path to the file where the warning was found. - * @param int $line The line on which the warning occurred. Default is 0 (unknown line). - * @param int $column The column on which the warning occurred. Default is 0 (unknown column). + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the warning was found. + * @param int $line The line on which the warning occurred. Default is 0 (unknown line). + * @param int $column The column on which the warning occurred. Default is 0 (unknown column). + * @param int $severity Severity level. Default is 5. */ - protected function add_result_warning_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0 ) { - $this->add_result_message_for_file( $result, false, $message, $code, $file, $line, $column ); + protected function add_result_warning_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0, $severity = 5 ) { + $this->add_result_message_for_file( $result, false, $message, $code, $file, $line, $column, $severity ); } } diff --git a/tests/phpunit/tests/Checker/Check_Result_Tests.php b/tests/phpunit/tests/Checker/Check_Result_Tests.php index 98398307d..2bc623caa 100644 --- a/tests/phpunit/tests/Checker/Check_Result_Tests.php +++ b/tests/phpunit/tests/Checker/Check_Result_Tests.php @@ -57,9 +57,10 @@ public function test_add_message_with_warning() { // Tests the warning exists in the array. $expected = array( - 'message' => 'Warning message', - 'code' => 'test_warning', - 'link' => '', + 'message' => 'Warning message', + 'code' => 'test_warning', + 'link' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $warnings['test-plugin.php'][12][40][0] ); @@ -91,9 +92,10 @@ public function test_add_message_with_error() { // Tests the error exists in the array. $expected = array( - 'message' => 'Error message', - 'code' => 'test_error', - 'link' => '', + 'message' => 'Error message', + 'code' => 'test_error', + 'link' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] ); @@ -122,9 +124,10 @@ public function test_get_errors_with_errors() { // Tests the error exists in the array. $expected = array( - 'message' => 'Error message', - 'code' => 'test_error', - 'link' => '', + 'message' => 'Error message', + 'code' => 'test_error', + 'link' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] ); @@ -153,9 +156,10 @@ public function test_get_warnings_with_warnings() { // Tests the warning exists in the array. $expected = array( - 'message' => 'Warning message', - 'code' => 'test_warning', - 'link' => '', + 'message' => 'Warning message', + 'code' => 'test_warning', + 'link' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $warnings['test-plugin.php'][22][30][0] ); From 4f33828483ba2c37c666b345c3bee81855141612 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 19 Jul 2024 11:35:57 +0545 Subject: [PATCH 02/12] Add missing severity parameter in CS runner --- includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index ebb7e8365..0f2ded28a 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -122,7 +122,8 @@ final public function run( Check_Result $result ) { $file_message['source'], $file_name, $file_message['line'], - $file_message['column'] + $file_message['column'], + $file_message['severity'] ); } } From 8f58d3a649d48fddbe59c6bceef68943b3d27504 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 19 Jul 2024 12:07:04 +0545 Subject: [PATCH 03/12] Add feature test for severity --- tests/behat/features/plugin-severity.feature | 246 +++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 tests/behat/features/plugin-severity.feature diff --git a/tests/behat/features/plugin-severity.feature b/tests/behat/features/plugin-severity.feature new file mode 100644 index 000000000..a2d0184c9 --- /dev/null +++ b/tests/behat/features/plugin-severity.feature @@ -0,0 +1,246 @@ +Feature: Test that the WP-CLI command works. + + Background: + Given a WP install with the Plugin Check plugin + And a wp-content/plugins/pcp-addon/class-postsperpage-check.php file: + """ + 'php', + 'standard' => plugin_dir_path( __FILE__ ) . 'postsperpage.xml', + ); + } + } + """ + And a wp-content/plugins/pcp-addon/class-prohibited-text-check.php file: + """ + add_result_error_for_file( + $result, + __( 'Prohibited text found.', 'pcp-addon' ), + 'prohibited_text_detected', + $file, + 0, + 0, + 8 + ); + } + } + } + """ + + And a wp-content/plugins/pcp-addon/pcp-addon.php file: + """ + esc_html__( 'New Category', 'pcp-addon' ) ) ); + } + ); + + add_filter( + 'wp_plugin_check_checks', + function ( array $checks ) { + require_once plugin_dir_path( __FILE__ ) . 'class-prohibited-text-check.php'; + require_once plugin_dir_path( __FILE__ ) . 'class-postsperpage-check.php'; + + return array_merge( + $checks, + array( + 'prohibited_text' => new Prohibited_Text_Check(), + 'postsperpage' => new PostsPerPage_Check(), + ) + ); + } + ); + """ + And a wp-content/plugins/pcp-addon/postsperpage.xml file: + """ + + + + error + 9 + + + """ + And I run the WP-CLI command `plugin activate pcp-addon` + And a wp-content/plugins/foo-sample/foo-sample.php file: + """ + 'post', + 'post_status' => 'publish', + 'posts_per_page' => 1000, + 'no_found_rows' => true, + ); + } + ); + """ + + Scenario: Basic checks with addon + When I run the WP-CLI command `plugin list --field=name --status=active` + Then STDOUT should contain: + """ + pcp-addon + """ + And STDOUT should contain: + """ + plugin-check + """ + + When I run the WP-CLI command `plugin list-check-categories --fields=slug,name --format=csv` + Then STDOUT should contain: + """ + new_category,"New Category" + """ + + When I run the WP-CLI command `plugin list-checks --fields=slug,category,stability --format=csv` + Then STDOUT should contain: + """ + prohibited_text,new_category,stable + """ + And STDOUT should contain: + """ + postsperpage,new_category,stable + """ + + When I run the WP-CLI command `plugin list-checks --fields=slug,category --format=csv --categories=new_category` + Then STDOUT should contain: + """ + prohibited_text,new_category + """ + And STDOUT should contain: + """ + postsperpage,new_category + """ + And STDOUT should not contain: + """ + plugin_review_phpcs,plugin_repo + """ + + Scenario: Check no severity level + When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme` + Then STDOUT should contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR + """ + And STDOUT should contain: + """ + prohibited_text_detected,ERROR + """ + And STDOUT should contain: + """ + WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR + """ + + Scenario: Check severity level 5 + When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme --severity=5` + Then STDOUT should contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR + """ + And STDOUT should contain: + """ + prohibited_text_detected,ERROR + """ + And STDOUT should contain: + """ + WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR + """ + + Scenario: Check severity level 8 + When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme --severity=8` + Then STDOUT should contain: + """ + prohibited_text_detected,ERROR + """ + And STDOUT should contain: + """ + WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR + """ + And STDOUT should not contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR + """ + + Scenario: Check severity level 9 + When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme --severity=9` + Then STDOUT should contain: + """ + WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR + """ + And STDOUT should not contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR + """ + And STDOUT should not contain: + """ + prohibited_text_detected,ERROR + """ + + Scenario: Check severity level 10 + When I run the WP-CLI command `plugin check foo-sample --exclude-checks=plugin_readme --severity=10` + Then STDOUT should be empty From bf583a62efb21202231378e7250a8a8b810467dc Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 7 Aug 2024 16:55:42 +0200 Subject: [PATCH 04/12] updated the readme --- docs/README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/README.md b/docs/README.md index ea6026835..8a2f66381 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,3 +6,39 @@ * [CLI Commands](CLI.md) * [Running Unit tests](running-unit-tests.md) * [Releasing a New Version of Plugin](releasing.md) + +## Checks Developed in the Plugin + +These are the results and severity levels for each check. + +| Category | Check | Severity | +| -------- | ------- | ------- | +| General | Internationalization | 5 | +| Performace | Enqueued Resources | 5 | +| Performace | Scripts in Footer | 5 | +| Performace | Scripts Scope | 5 | +| Performace | Scripts Size | 5 | +| Performace | Styles Scope | 5 | +| Performace | Styles Size | 5 | +| Performace | Query Parameters | 5 | +| Plugin Repo | Code Obfuscation | 8 | +| Plugin Repo | File Type Check | 8 | +| Plugin Repo | LocalHost | 8 | +| Plugin Repo | Unfiltered Uploads | 7 | +| Plugin Repo | Plugin Header TextDomain | 6 | +| Plugin Repo | Readme Headers | 9 | +| Plugin Repo | Readme Default Text | 7 | +| Plugin Repo | Readme Check License | 9 | +| Plugin Repo | Readme Readme Stable Tag | 9 | +| Plugin Repo | Readme Upgrade Notice | 9 | +| Plugin Repo | Readme Contributor Ignored | 5 | +| Plugin Repo | Readme PHP Header Ignored | 5 | +| Plugin Repo | Readme Tested up to | 7 | +| Plugin Repo | Readme Too many tags | 5 | +| Plugin Repo | Readme Ignored Tags | 5 | +| Plugin Repo | Readme No short description | 6 | +| Plugin Repo | Readme Trimmed short description | 6 | +| Plugin Repo | Readme No short description | 6 | +| Plugin Repo | PHP Coding Standards | 5 | +| Plugin Repo | Updater Check | 9 | +| Plugin Repo | Trademarks Check | 9 | \ No newline at end of file From 9c1d8d38287f4a19f6182d8e870403e81dd3996b Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Wed, 7 Aug 2024 17:00:19 +0200 Subject: [PATCH 05/12] updated trademarks severity --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 8a2f66381..fcd502cf9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -41,4 +41,4 @@ These are the results and severity levels for each check. | Plugin Repo | Readme No short description | 6 | | Plugin Repo | PHP Coding Standards | 5 | | Plugin Repo | Updater Check | 9 | -| Plugin Repo | Trademarks Check | 9 | \ No newline at end of file +| Plugin Repo | Trademarks Check | 5 | \ No newline at end of file From 7ab10a92d338d9e085d8301035f49513ee00f99f Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 7 Aug 2024 21:26:26 +0545 Subject: [PATCH 06/12] Removing separate feature file. Need to rewrite tests. --- docs/README.md | 16 +- tests/behat/features/plugin-severity.feature | 246 ------------------- 2 files changed, 8 insertions(+), 254 deletions(-) delete mode 100644 tests/behat/features/plugin-severity.feature diff --git a/docs/README.md b/docs/README.md index fcd502cf9..eb9feb8db 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,13 +14,13 @@ These are the results and severity levels for each check. | Category | Check | Severity | | -------- | ------- | ------- | | General | Internationalization | 5 | -| Performace | Enqueued Resources | 5 | -| Performace | Scripts in Footer | 5 | -| Performace | Scripts Scope | 5 | -| Performace | Scripts Size | 5 | -| Performace | Styles Scope | 5 | -| Performace | Styles Size | 5 | -| Performace | Query Parameters | 5 | +| Performance | Enqueued Resources | 5 | +| Performance | Scripts in Footer | 5 | +| Performance | Scripts Scope | 5 | +| Performance | Scripts Size | 5 | +| Performance | Styles Scope | 5 | +| Performance | Styles Size | 5 | +| Performance | Query Parameters | 5 | | Plugin Repo | Code Obfuscation | 8 | | Plugin Repo | File Type Check | 8 | | Plugin Repo | LocalHost | 8 | @@ -41,4 +41,4 @@ These are the results and severity levels for each check. | Plugin Repo | Readme No short description | 6 | | Plugin Repo | PHP Coding Standards | 5 | | Plugin Repo | Updater Check | 9 | -| Plugin Repo | Trademarks Check | 5 | \ No newline at end of file +| Plugin Repo | Trademarks Check | 5 | diff --git a/tests/behat/features/plugin-severity.feature b/tests/behat/features/plugin-severity.feature deleted file mode 100644 index a2d0184c9..000000000 --- a/tests/behat/features/plugin-severity.feature +++ /dev/null @@ -1,246 +0,0 @@ -Feature: Test that the WP-CLI command works. - - Background: - Given a WP install with the Plugin Check plugin - And a wp-content/plugins/pcp-addon/class-postsperpage-check.php file: - """ - 'php', - 'standard' => plugin_dir_path( __FILE__ ) . 'postsperpage.xml', - ); - } - } - """ - And a wp-content/plugins/pcp-addon/class-prohibited-text-check.php file: - """ - add_result_error_for_file( - $result, - __( 'Prohibited text found.', 'pcp-addon' ), - 'prohibited_text_detected', - $file, - 0, - 0, - 8 - ); - } - } - } - """ - - And a wp-content/plugins/pcp-addon/pcp-addon.php file: - """ - esc_html__( 'New Category', 'pcp-addon' ) ) ); - } - ); - - add_filter( - 'wp_plugin_check_checks', - function ( array $checks ) { - require_once plugin_dir_path( __FILE__ ) . 'class-prohibited-text-check.php'; - require_once plugin_dir_path( __FILE__ ) . 'class-postsperpage-check.php'; - - return array_merge( - $checks, - array( - 'prohibited_text' => new Prohibited_Text_Check(), - 'postsperpage' => new PostsPerPage_Check(), - ) - ); - } - ); - """ - And a wp-content/plugins/pcp-addon/postsperpage.xml file: - """ - - - - error - 9 - - - """ - And I run the WP-CLI command `plugin activate pcp-addon` - And a wp-content/plugins/foo-sample/foo-sample.php file: - """ - 'post', - 'post_status' => 'publish', - 'posts_per_page' => 1000, - 'no_found_rows' => true, - ); - } - ); - """ - - Scenario: Basic checks with addon - When I run the WP-CLI command `plugin list --field=name --status=active` - Then STDOUT should contain: - """ - pcp-addon - """ - And STDOUT should contain: - """ - plugin-check - """ - - When I run the WP-CLI command `plugin list-check-categories --fields=slug,name --format=csv` - Then STDOUT should contain: - """ - new_category,"New Category" - """ - - When I run the WP-CLI command `plugin list-checks --fields=slug,category,stability --format=csv` - Then STDOUT should contain: - """ - prohibited_text,new_category,stable - """ - And STDOUT should contain: - """ - postsperpage,new_category,stable - """ - - When I run the WP-CLI command `plugin list-checks --fields=slug,category --format=csv --categories=new_category` - Then STDOUT should contain: - """ - prohibited_text,new_category - """ - And STDOUT should contain: - """ - postsperpage,new_category - """ - And STDOUT should not contain: - """ - plugin_review_phpcs,plugin_repo - """ - - Scenario: Check no severity level - When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme` - Then STDOUT should contain: - """ - WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR - """ - And STDOUT should contain: - """ - prohibited_text_detected,ERROR - """ - And STDOUT should contain: - """ - WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR - """ - - Scenario: Check severity level 5 - When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme --severity=5` - Then STDOUT should contain: - """ - WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR - """ - And STDOUT should contain: - """ - prohibited_text_detected,ERROR - """ - And STDOUT should contain: - """ - WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR - """ - - Scenario: Check severity level 8 - When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme --severity=8` - Then STDOUT should contain: - """ - prohibited_text_detected,ERROR - """ - And STDOUT should contain: - """ - WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR - """ - And STDOUT should not contain: - """ - WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR - """ - - Scenario: Check severity level 9 - When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --exclude-checks=plugin_readme --severity=9` - Then STDOUT should contain: - """ - WordPress.WP.PostsPerPage.posts_per_page_posts_per_page,ERROR - """ - And STDOUT should not contain: - """ - WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR - """ - And STDOUT should not contain: - """ - prohibited_text_detected,ERROR - """ - - Scenario: Check severity level 10 - When I run the WP-CLI command `plugin check foo-sample --exclude-checks=plugin_readme --severity=10` - Then STDOUT should be empty From 825844d8de0d4943a7a5aa9e30f0115bbed24f91 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 7 Aug 2024 21:33:58 +0545 Subject: [PATCH 07/12] Correct function parameters for sniffer --- includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index 0f2ded28a..bf669dc9b 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -123,6 +123,7 @@ final public function run( Check_Result $result ) { $file_name, $file_message['line'], $file_message['column'], + '', $file_message['severity'] ); } From 3e5b4a4cb4e9bac07641a6056dfd27b6601278fa Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Thu, 8 Aug 2024 13:08:22 +0545 Subject: [PATCH 08/12] Update severity level for different checks --- docs/README.md | 1 - .../Plugin_Repo/Code_Obfuscation_Check.php | 9 ++-- .../Checks/Plugin_Repo/File_Type_Check.php | 30 +++++++++-- .../Checks/Plugin_Repo/Localhost_Check.php | 4 +- .../No_Unfiltered_Uploads_Check.php | 3 +- .../Plugin_Header_Text_Domain_Check.php | 6 ++- .../Plugin_Repo/Plugin_Readme_Check.php | 42 +++++++++++----- .../Plugin_Repo/Plugin_Updater_Check.php | 15 ++++-- includes/Traits/Amend_Check_Result.php | 50 +++++++++---------- 9 files changed, 106 insertions(+), 54 deletions(-) diff --git a/docs/README.md b/docs/README.md index eb9feb8db..2dc2f4792 100644 --- a/docs/README.md +++ b/docs/README.md @@ -38,7 +38,6 @@ These are the results and severity levels for each check. | Plugin Repo | Readme Ignored Tags | 5 | | Plugin Repo | Readme No short description | 6 | | Plugin Repo | Readme Trimmed short description | 6 | -| Plugin Repo | Readme No short description | 6 | | Plugin Repo | PHP Coding Standards | 5 | | Plugin Repo | Updater Check | 9 | | Plugin Repo | Trademarks Check | 5 | diff --git a/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php b/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php index a581a7b59..5756e2b1a 100644 --- a/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php @@ -110,7 +110,8 @@ protected function look_for_zendguard( Check_Result $result, array $php_files ) $file['file'], $file['line'], $file['column'], - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource', + 8 ); } } @@ -140,7 +141,8 @@ protected function look_for_sourceguardian( Check_Result $result, array $php_fil $file['file'], $file['line'], $file['column'], - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource', + 8 ); } } @@ -170,7 +172,8 @@ protected function look_for_ioncube( Check_Result $result, array $php_files ) { $file['file'], $file['line'], $file['column'], - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource', + 8 ); } } diff --git a/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php b/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php index 86660f4d6..dfdc435d0 100644 --- a/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php @@ -108,7 +108,11 @@ protected function look_for_compressed_files( Check_Result $result, array $files $result, __( 'Compressed files are not permitted.', 'plugin-check' ), 'compressed_files', - $file + $file, + 0, + 0, + '', + 8 ); } } @@ -130,7 +134,11 @@ protected function look_for_phar_files( Check_Result $result, array $files ) { $result, __( 'Phar files are not permitted.', 'plugin-check' ), 'phar_files', - $file + $file, + 0, + 0, + '', + 8 ); } } @@ -170,7 +178,11 @@ function ( $directory ) use ( $directories ) { $is_error, __( 'Version control checkouts should not be present.', 'plugin-check' ), 'vcs_present', - $dir + $dir, + 0, + 0, + '', + 8 ); } } @@ -193,7 +205,11 @@ protected function look_for_hidden_files( Check_Result $result, array $files ) { $result, __( 'Hidden files are not permitted.', 'plugin-check' ), 'hidden_files', - $file + $file, + 0, + 0, + '', + 8 ); } } @@ -218,7 +234,11 @@ protected function look_for_application_files( Check_Result $result, array $file $result, __( 'Application files are not permitted.', 'plugin-check' ), 'application_detected', - $file + $file, + 0, + 0, + '', + 8 ); } } diff --git a/includes/Checker/Checks/Plugin_Repo/Localhost_Check.php b/includes/Checker/Checks/Plugin_Repo/Localhost_Check.php index f1595c1d4..efc70e3a9 100644 --- a/includes/Checker/Checks/Plugin_Repo/Localhost_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Localhost_Check.php @@ -56,7 +56,9 @@ protected function check_files( Check_Result $result, array $files ) { 'localhost_code_detected', $file['file'], $file['line'], - $file['column'] + $file['column'], + '', + 8 ); } } diff --git a/includes/Checker/Checks/Plugin_Repo/No_Unfiltered_Uploads_Check.php b/includes/Checker/Checks/Plugin_Repo/No_Unfiltered_Uploads_Check.php index 7357eb4c3..0a4ecfb44 100644 --- a/includes/Checker/Checks/Plugin_Repo/No_Unfiltered_Uploads_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/No_Unfiltered_Uploads_Check.php @@ -59,7 +59,8 @@ protected function check_files( Check_Result $result, array $files ) { $file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#files-unfiltered-uploads' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#files-unfiltered-uploads', + 7 ); } } diff --git a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Text_Domain_Check.php b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Text_Domain_Check.php index 272730955..767675739 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Text_Domain_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Header_Text_Domain_Check.php @@ -75,7 +75,11 @@ public function run( Check_Result $result ) { esc_html( $plugin_slug ) ), 'textdomain_mismatch', - $plugin_main_file + $plugin_main_file, + 0, + 0, + '', + 6 ); } } diff --git a/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php b/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php index fa688feee..6b31c0e3d 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php @@ -67,7 +67,11 @@ protected function check_files( Check_Result $result, array $files ) { $result, __( 'The plugin readme.txt does not exist.', 'plugin-check' ), 'no_plugin_readme', - 'readme.txt' + 'readme.txt', + 0, + 0, + '', + 9 ); return; @@ -121,7 +125,8 @@ private function check_name( Check_Result $result, string $readme_file, Parser $ $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incomplete-readme' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incomplete-readme', + 9 ); } elseif ( empty( $parser->name ) ) { $this->add_result_error_for_file( @@ -135,7 +140,8 @@ private function check_name( Check_Result $result, string $readme_file, Parser $ $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incomplete-readme' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incomplete-readme', + 9 ); } } @@ -184,7 +190,8 @@ private function check_headers( Check_Result $result, string $readme_file, Parse $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information' + 'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information', + 7 ); } } else { @@ -234,7 +241,8 @@ private function check_default_text( Check_Result $result, string $readme_file, $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incomplete-readme' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incomplete-readme', + 7 ); } } @@ -262,7 +270,8 @@ private function check_license( Check_Result $result, string $readme_file, Parse $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared', + 9 ); return; @@ -279,7 +288,8 @@ private function check_license( Check_Result $result, string $readme_file, Parse $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared', + 9 ); } @@ -293,7 +303,8 @@ private function check_license( Check_Result $result, string $readme_file, Parse $plugin_main_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared', + 9 ); } else { $plugin_license = $this->normalize_licenses( $matches_license[1] ); @@ -308,7 +319,8 @@ private function check_license( Check_Result $result, string $readme_file, Parse $plugin_main_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#no-gpl-compatible-license-declared', + 9 ); } @@ -321,7 +333,8 @@ private function check_license( Check_Result $result, string $readme_file, Parse $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#declared-license-mismatched' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#declared-license-mismatched', + 9 ); } } @@ -389,7 +402,8 @@ private function check_stable_tag( Check_Result $result, string $readme_file, Pa $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incorrect-stable-tag' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incorrect-stable-tag', + 9 ); return; @@ -403,7 +417,8 @@ private function check_stable_tag( Check_Result $result, string $readme_file, Pa $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incorrect-stable-tag' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incorrect-stable-tag', + 9 ); } @@ -421,7 +436,8 @@ private function check_stable_tag( Check_Result $result, string $readme_file, Pa $readme_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incorrect-stable-tag' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#incorrect-stable-tag', + 9 ); } } diff --git a/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php b/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php index 38dccf51a..46dae0007 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php @@ -119,7 +119,8 @@ protected function look_for_update_uri_header( Check_Result $result ) { $plugin_main_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker', + 9 ); } } @@ -149,7 +150,8 @@ protected function look_for_updater_file( Check_Result $result, array $php_files $file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker', + 9 ); } } @@ -186,7 +188,11 @@ protected function look_for_plugin_updaters( Check_Result $result, array $php_fi esc_attr( $matches[0] ) ), 'plugin_updater_detected', - $updater_file + $updater_file, + 0, + 0, + '', + 9 ); } } @@ -223,7 +229,8 @@ protected function look_for_updater_routines( Check_Result $result, array $php_f $updater_file, 0, 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker' + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker', + 9 ); } } diff --git a/includes/Traits/Amend_Check_Result.php b/includes/Traits/Amend_Check_Result.php index 83aae366c..4dc5a1951 100644 --- a/includes/Traits/Amend_Check_Result.php +++ b/includes/Traits/Amend_Check_Result.php @@ -23,15 +23,15 @@ trait Amend_Check_Result { * * @since 1.0.0 * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param bool $error Whether it is an error or notice. - * @param string $message Error message. - * @param string $code Error code. - * @param string $file Absolute path to the file where the issue was found. - * @param int $line The line on which the message occurred. Default is 0 (unknown line). - * @param int $column The column on which the message occurred. Default is 0 (unknown column). - * @param string $docs URL for further information about the message. - * @param int $severity Severity level. Default is 5. + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param bool $error Whether it is an error or notice. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the issue was found. + * @param int $line The line on which the message occurred. Default is 0 (unknown line). + * @param int $column The column on which the message occurred. Default is 0 (unknown column). + * @param string $docs URL for further information about the message. + * @param int $severity Severity level. Default is 5. */ protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0, string $docs = '', $severity = 5 ) { @@ -55,14 +55,14 @@ protected function add_result_message_for_file( Check_Result $result, $error, $m * * @since 1.0.0 * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param string $message Error message. - * @param string $code Error code. - * @param string $file Absolute path to the file where the error was found. - * @param int $line The line on which the error occurred. Default is 0 (unknown line). - * @param int $column The column on which the error occurred. Default is 0 (unknown column). - * @param string $docs URL for further information about the message. - * @param int $severity Severity level. Default is 5. + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the error was found. + * @param int $line The line on which the error occurred. Default is 0 (unknown line). + * @param int $column The column on which the error occurred. Default is 0 (unknown column). + * @param string $docs URL for further information about the message. + * @param int $severity Severity level. Default is 5. */ protected function add_result_error_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0, string $docs = '', $severity = 5 ) { $this->add_result_message_for_file( $result, true, $message, $code, $file, $line, $column, $docs, $severity ); @@ -73,14 +73,14 @@ protected function add_result_error_for_file( Check_Result $result, $message, $c * * @since 1.0.0 * - * @param Check_Result $result The check result to amend, including the plugin context to check. - * @param string $message Error message. - * @param string $code Error code. - * @param string $file Absolute path to the file where the warning was found. - * @param int $line The line on which the warning occurred. Default is 0 (unknown line). - * @param int $column The column on which the warning occurred. Default is 0 (unknown column). - * @param string $docs URL for further information about the message. - * @param int $severity Severity level. Default is 5. + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the warning was found. + * @param int $line The line on which the warning occurred. Default is 0 (unknown line). + * @param int $column The column on which the warning occurred. Default is 0 (unknown column). + * @param string $docs URL for further information about the message. + * @param int $severity Severity level. Default is 5. */ protected function add_result_warning_for_file( Check_Result $result, $message, $code, $file, $line = 0, $column = 0, string $docs = '', $severity = 5 ) { $this->add_result_message_for_file( $result, false, $message, $code, $file, $line, $column, $docs, $severity ); From 3f021d0473987be6ca72be15fe27f84b094844f7 Mon Sep 17 00:00:00 2001 From: davidperezgar Date: Thu, 8 Aug 2024 16:20:16 +0200 Subject: [PATCH 09/12] updated after team discussion --- docs/README.md | 2 +- .../Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2dc2f4792..a5818be30 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,7 +21,7 @@ These are the results and severity levels for each check. | Performance | Styles Scope | 5 | | Performance | Styles Size | 5 | | Performance | Query Parameters | 5 | -| Plugin Repo | Code Obfuscation | 8 | +| Plugin Repo | Code Obfuscation | 6 | | Plugin Repo | File Type Check | 8 | | Plugin Repo | LocalHost | 8 | | Plugin Repo | Unfiltered Uploads | 7 | diff --git a/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php b/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php index 5756e2b1a..e6e4bd97e 100644 --- a/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php @@ -111,7 +111,7 @@ protected function look_for_zendguard( Check_Result $result, array $php_files ) $file['line'], $file['column'], 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource', - 8 + 6 ); } } @@ -142,7 +142,7 @@ protected function look_for_sourceguardian( Check_Result $result, array $php_fil $file['line'], $file['column'], 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource', - 8 + 6 ); } } @@ -173,7 +173,7 @@ protected function look_for_ioncube( Check_Result $result, array $php_files ) { $file['line'], $file['column'], 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#gpl-no-publicly-documented-resource', - 8 + 6 ); } } From 0a21389db42e707b9a9de6547ceac25112f05e82 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 9 Aug 2024 10:52:46 +0545 Subject: [PATCH 10/12] Add feature test for checks with severity level --- .../features/plugin-check-severity.feature | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 tests/behat/features/plugin-check-severity.feature diff --git a/tests/behat/features/plugin-check-severity.feature b/tests/behat/features/plugin-check-severity.feature new file mode 100644 index 000000000..28d2514ec --- /dev/null +++ b/tests/behat/features/plugin-check-severity.feature @@ -0,0 +1,144 @@ +Feature: Test that the severity level in plugin check works. + + Scenario: Check a plugin different severity levels + Given a WP install with the Plugin Check plugin + And a wp-content/plugins/foo-bar-wp/foo-bar-wp.php file: + """ + 'post', + 'post_status' => 'publish', + 'posts_per_page' => 1000, + 'no_found_rows' => true, + ); + } + ); + """ + And a wp-content/plugins/foo-bar-wp/readme.txt file: + """ + === Foo Bar WP === + + Contributors: wordpressdotorg + Tags: foo, bar, tag1 + Tested up to: 6.5 + Stable tag: 0.1.0 + License: GPLv2 or later + License URI: http://www.gnu.org/licenses/gpl-2.0.html + + Short description will be here. + + == Description == + + Long description will be here. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + == Upgrade Notice == + + Long upgrade notice here. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + """ + + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity` + Then STDOUT should contain: + """ + allow_unfiltered_uploads_detected,ERROR,7 + """ + And STDOUT should contain: + """ + obfuscated_code_detected,ERROR,6 + """ + And STDOUT should contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR,5 + """ + And STDOUT should contain: + """ + outdated_tested_upto_header,ERROR,7 + """ + And STDOUT should contain: + """ + default_readme_text,WARNING,7 + """ + And STDOUT should contain: + """ + upgrade_notice_limit,WARNING,5 + """ + + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --severity=7` + Then STDOUT should contain: + """ + allow_unfiltered_uploads_detected,ERROR,7 + """ + And STDOUT should not contain: + """ + obfuscated_code_detected,ERROR,6 + """ + And STDOUT should not contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR,5 + """ + And STDOUT should contain: + """ + outdated_tested_upto_header,ERROR,7 + """ + And STDOUT should contain: + """ + default_readme_text,WARNING,7 + """ + And STDOUT should not contain: + """ + upgrade_notice_limit,WARNING,5 + """ + + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --severity=6` + Then STDOUT should contain: + """ + allow_unfiltered_uploads_detected,ERROR,7 + """ + And STDOUT should contain: + """ + obfuscated_code_detected,ERROR,6 + """ + And STDOUT should not contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR,5 + """ + And STDOUT should contain: + """ + outdated_tested_upto_header,ERROR,7 + """ + And STDOUT should contain: + """ + default_readme_text,WARNING,7 + """ + And STDOUT should not contain: + """ + upgrade_notice_limit,WARNING,5 + """ + + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --severity=10` + Then STDOUT should be empty From b47281d264fc5844c6909f77137d32403613700d Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 9 Aug 2024 12:49:05 +0545 Subject: [PATCH 11/12] Add separate severity level arguments --- includes/CLI/Plugin_Check_Command.php | 42 +++++++++++---- .../features/plugin-check-severity.feature | 52 +++++++++++++++++++ 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index f25d7c4cc..164bf3f95 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -107,6 +107,12 @@ public function __construct( Plugin_Context $plugin_context ) { * [--severity=] * : Severity level. * + * [--error-severity=] + * : Error severity level. + * + * [--warning-severity=] + * : Warning severity level. + * * ## EXAMPLES * * wp plugin check akismet @@ -137,6 +143,8 @@ public function check( $args, $assoc_args ) { 'ignore-errors' => false, 'include-experimental' => false, 'severity' => '', + 'error-severity' => '', + 'warning-severity' => '', ) ); @@ -236,6 +244,10 @@ static function ( $dirs ) use ( $excluded_files ) { // Get formatter. $formatter = $this->get_formatter( $assoc_args, $default_fields ); + // Severity. + $error_severity = ! empty( $options['error-severity'] ) ? $options['error-severity'] : $options['severity']; + $warning_severity = ! empty( $options['warning-severity'] ) ? $options['warning-severity'] : $options['severity']; + // Print the formatted results. // Go over all files with errors first and print them, combined with any warnings in the same file. foreach ( $errors as $file_name => $file_errors ) { @@ -246,8 +258,8 @@ static function ( $dirs ) use ( $excluded_files ) { } $file_results = $this->flatten_file_results( $file_errors, $file_warnings ); - if ( '' !== $options['severity'] ) { - $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $options['severity'] ) ); + if ( '' !== $error_severity || '' !== $warning_severity ) { + $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ) ); } if ( ! empty( $file_results ) ) { @@ -259,8 +271,8 @@ static function ( $dirs ) use ( $excluded_files ) { foreach ( $warnings as $file_name => $file_warnings ) { $file_results = $this->flatten_file_results( array(), $file_warnings ); - if ( '' !== $options['severity'] ) { - $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $options['severity'] ) ); + if ( '' !== $error_severity || '' !== $warning_severity ) { + $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ) ); } if ( ! empty( $file_results ) ) { @@ -644,16 +656,26 @@ private function has_runtime_check( array $checks ) { * * @since 1.1.0 * - * @param array $results Check results. - * @param int $severity Severity level. + * @param array $results Check results. + * @param int $error_severity Error severity level. + * @param int $warning_severity Warning severity level. * @return array Filtered results. */ - private function get_filtered_results_by_severity( $results, $severity ) { - return array_filter( + private function get_filtered_results_by_severity( $results, $error_severity, $warning_severity ) { + $errors = array_filter( + $results, + function ( $item ) use ( $error_severity ) { + return ( 'ERROR' === $item['type'] && $item['severity'] >= $error_severity ); + } + ); + + $warnings = array_filter( $results, - function ( $item ) use ( $severity ) { - return ( $item['severity'] >= $severity ); + function ( $item ) use ( $warning_severity ) { + return ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity ); } ); + + return array_merge( $errors, $warnings ); } } diff --git a/tests/behat/features/plugin-check-severity.feature b/tests/behat/features/plugin-check-severity.feature index 28d2514ec..3761e6f15 100644 --- a/tests/behat/features/plugin-check-severity.feature +++ b/tests/behat/features/plugin-check-severity.feature @@ -140,5 +140,57 @@ Feature: Test that the severity level in plugin check works. upgrade_notice_limit,WARNING,5 """ + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --error-severity=6` + Then STDOUT should contain: + """ + allow_unfiltered_uploads_detected,ERROR,7 + """ + And STDOUT should contain: + """ + obfuscated_code_detected,ERROR,6 + """ + And STDOUT should not contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR,5 + """ + And STDOUT should contain: + """ + outdated_tested_upto_header,ERROR,7 + """ + And STDOUT should contain: + """ + default_readme_text,WARNING,7 + """ + And STDOUT should contain: + """ + upgrade_notice_limit,WARNING,5 + """ + + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --warning-severity=7` + Then STDOUT should contain: + """ + allow_unfiltered_uploads_detected,ERROR,7 + """ + And STDOUT should contain: + """ + obfuscated_code_detected,ERROR,6 + """ + And STDOUT should contain: + """ + WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR,5 + """ + And STDOUT should contain: + """ + outdated_tested_upto_header,ERROR,7 + """ + And STDOUT should contain: + """ + default_readme_text,WARNING,7 + """ + And STDOUT should not contain: + """ + upgrade_notice_limit,WARNING,5 + """ + When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity --severity=10` Then STDOUT should be empty From c84b0750513f820a93f2add04d3e9d898f808fb7 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 9 Aug 2024 13:17:59 +0545 Subject: [PATCH 12/12] Update CLI docs --- docs/CLI.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index b12ff179f..fe31e4640 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -1,6 +1,6 @@ [Back to overview](./README.md) -# wp plugin check +# wp plugin check Runs plugin check. @@ -50,6 +50,12 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded. [--severity=] : Severity level. + +[--error-severity=] +: Error severity level. + +[--warning-severity=] +: Warning severity level. ``` ## EXAMPLES ``` @@ -58,7 +64,7 @@ wp plugin check akismet --checks=late_escaping wp plugin check akismet --format=json ``` -# wp plugin list-checks +# wp plugin list-checks Lists the available checks for plugins. @@ -89,7 +95,7 @@ wp plugin list-checks wp plugin list-checks --format=json ``` -# wp plugin list-check-categories +# wp plugin list-check-categories Lists the available check categories for plugins.