diff --git a/docs/CLI.md b/docs/CLI.md index 51bc584d0..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. @@ -47,6 +47,15 @@ By default, `.git`, `vendor` and `node_modules` directories are excluded. [--exclude-files=] : Additional files to exclude from checks. + +[--severity=] +: Severity level. + +[--error-severity=] +: Error severity level. + +[--warning-severity=] +: Warning severity level. ``` ## EXAMPLES ``` @@ -55,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. @@ -86,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. diff --git a/docs/README.md b/docs/README.md index ea6026835..a5818be30 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,3 +6,38 @@ * [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 | +| 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 | 6 | +| 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 | PHP Coding Standards | 5 | +| Plugin Repo | Updater Check | 9 | +| Plugin Repo | Trademarks Check | 5 | diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 8b79518c1..164bf3f95 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -21,6 +21,8 @@ /** * Plugin check command. + * + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ final class Plugin_Check_Command { @@ -102,6 +104,15 @@ public function __construct( Plugin_Context $plugin_context ) { * [--exclude-files=] * : Additional files to exclude from checks. * + * [--severity=] + * : Severity level. + * + * [--error-severity=] + * : Error severity level. + * + * [--warning-severity=] + * : Warning severity level. + * * ## EXAMPLES * * wp plugin check akismet @@ -119,6 +130,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. @@ -130,6 +142,9 @@ public function check( $args, $assoc_args ) { 'ignore-warnings' => false, 'ignore-errors' => false, 'include-experimental' => false, + 'severity' => '', + 'error-severity' => '', + 'warning-severity' => '', ) ); @@ -229,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 ) { @@ -238,13 +257,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 ( '' !== $error_severity || '' !== $warning_severity ) { + $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_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 ( '' !== $error_severity || '' !== $warning_severity ) { + $file_results = $this->get_filtered_results_by_severity( $file_results, intval( $error_severity ), intval( $warning_severity ) ); + } + + if ( ! empty( $file_results ) ) { + $this->display_results( $formatter, $file_name, $file_results ); + } } } @@ -617,4 +650,32 @@ 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 $error_severity Error severity level. + * @param int $warning_severity Warning severity level. + * @return array Filtered results. + */ + 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 ( $warning_severity ) { + return ( 'WARNING' === $item['type'] && $item['severity'] >= $warning_severity ); + } + ); + + return array_merge( $errors, $warnings ); + } } diff --git a/includes/Checker/Check_Result.php b/includes/Checker/Check_Result.php index f6ba9bf50..389cb8217 100644 --- a/includes/Checker/Check_Result.php +++ b/includes/Checker/Check_Result.php @@ -95,12 +95,13 @@ public function plugin() { */ public function add_message( $error, $message, $args = array() ) { $defaults = array( - 'code' => '', - 'file' => '', - 'line' => 0, - 'column' => 0, - 'link' => '', - 'docs' => '', + 'code' => '', + 'file' => '', + 'line' => 0, + 'column' => 0, + 'link' => '', + 'docs' => '', + 'severity' => 5, ); $data = array_merge( diff --git a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php index ebb7e8365..bf669dc9b 100644 --- a/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php +++ b/includes/Checker/Checks/Abstract_PHP_CodeSniffer_Check.php @@ -122,7 +122,9 @@ final public function run( Check_Result $result ) { $file_message['source'], $file_name, $file_message['line'], - $file_message['column'] + $file_message['column'], + '', + $file_message['severity'] ); } } diff --git a/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php b/includes/Checker/Checks/Plugin_Repo/Code_Obfuscation_Check.php index a581a7b59..e6e4bd97e 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', + 6 ); } } @@ -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', + 6 ); } } @@ -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', + 6 ); } } 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 090a17620..4dc5a1951 100644 --- a/includes/Traits/Amend_Check_Result.php +++ b/includes/Traits/Amend_Check_Result.php @@ -23,26 +23,29 @@ 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 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 = '' ) { + protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0, string $docs = '', $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 ), - 'docs' => $docs, + 'code' => $code, + 'file' => str_replace( $result->plugin()->path(), '', $file ), + 'line' => $line, + 'column' => $column, + 'link' => $this->get_file_editor_url( $result, $file, $line ), + 'docs' => $docs, + 'severity' => $severity, ) ); } @@ -52,16 +55,17 @@ 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 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 = '' ) { - $this->add_result_message_for_file( $result, true, $message, $code, $file, $line, $column, $docs ); + 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 ); } /** @@ -69,15 +73,16 @@ 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 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 = '' ) { - $this->add_result_message_for_file( $result, false, $message, $code, $file, $line, $column, $docs ); + 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 ); } } diff --git a/tests/behat/features/plugin-check-severity.feature b/tests/behat/features/plugin-check-severity.feature new file mode 100644 index 000000000..3761e6f15 --- /dev/null +++ b/tests/behat/features/plugin-check-severity.feature @@ -0,0 +1,196 @@ +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 --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 diff --git a/tests/phpunit/tests/Checker/Check_Result_Tests.php b/tests/phpunit/tests/Checker/Check_Result_Tests.php index 560fcf0d4..3015eddc9 100644 --- a/tests/phpunit/tests/Checker/Check_Result_Tests.php +++ b/tests/phpunit/tests/Checker/Check_Result_Tests.php @@ -57,10 +57,11 @@ public function test_add_message_with_warning() { // Tests the warning exists in the array. $expected = array( - 'message' => 'Warning message', - 'code' => 'test_warning', - 'link' => '', - 'docs' => '', + 'message' => 'Warning message', + 'code' => 'test_warning', + 'link' => '', + 'docs' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $warnings['test-plugin.php'][12][40][0] ); @@ -92,10 +93,11 @@ public function test_add_message_with_error() { // Tests the error exists in the array. $expected = array( - 'message' => 'Error message', - 'code' => 'test_error', - 'link' => '', - 'docs' => '', + 'message' => 'Error message', + 'code' => 'test_error', + 'link' => '', + 'docs' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] ); @@ -124,10 +126,11 @@ public function test_get_errors_with_errors() { // Tests the error exists in the array. $expected = array( - 'message' => 'Error message', - 'code' => 'test_error', - 'link' => '', - 'docs' => '', + 'message' => 'Error message', + 'code' => 'test_error', + 'link' => '', + 'docs' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] ); @@ -156,10 +159,11 @@ public function test_get_warnings_with_warnings() { // Tests the warning exists in the array. $expected = array( - 'message' => 'Warning message', - 'code' => 'test_warning', - 'link' => '', - 'docs' => '', + 'message' => 'Warning message', + 'code' => 'test_warning', + 'link' => '', + 'docs' => '', + 'severity' => 5, ); $this->assertEquals( $expected, $warnings['test-plugin.php'][22][30][0] );