Skip to content

Commit

Permalink
Merge pull request #395 from ernilambar/394-parser-warning-message
Browse files Browse the repository at this point in the history
Make parser warnings more clear
  • Loading branch information
mukeshpanchal27 authored Jan 29, 2024
2 parents 3c5e0d3 + 65240fc commit a50f3f1
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 31 deletions.
36 changes: 18 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 99 additions & 10 deletions includes/Checker/Checks/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,67 @@ private function check_stable_tag( Check_Result $result, string $readme_file, Pa
private function check_for_warnings( Check_Result $result, string $readme_file, Parser $parser ) {
$warnings = $parser->warnings ? $parser->warnings : array();

// This should be ERROR rather than WARNING. So ignoring here to handle separately.
unset( $warnings['invalid_plugin_name_header'] );

$warning_keys = array_keys( $warnings );

$latest_wordpress_version = (float) $this->get_wordpress_stable_version();

$ignored_warnings = array(
'contributor_ignored',
);

$messages = array(
'contributor_ignored' => sprintf(
/* translators: %s: plugin header tag */
__( 'One or more contributors listed were ignored. The "%s" field should only contain WordPress.org usernames. Remember that usernames are case-sensitive.', 'plugin-check' ),
'Contributors'
),
'requires_php_header_ignored' => sprintf(
/* translators: 1: plugin header tag; 2: Example version 5.2.4. 3: Example version 7.0. */
__( 'The "%1$s" field was ignored. This field should only contain a PHP version such as "%2$s" or "%3$s".', 'plugin-check' ),
'Requires PHP',
'5.2.4',
'7.0'
),
'tested_header_ignored' => sprintf(
/* translators: 1: plugin header tag; 2: Example version 5.0. 3: Example version 5.1. */
__( 'The "%1$s" field was ignored. This field should only contain a valid WordPress version such as "%2$s" or "%3$s".', 'plugin-check' ),
'Tested up to',
number_format( $latest_wordpress_version, 1 ),
number_format( $latest_wordpress_version + 0.1, 1 )
),
'requires_header_ignored' => sprintf(
/* translators: 1: plugin header tag; 2: Example version 5.0. 3: Example version 4.9. */
__( 'The "%1$s" field was ignored. This field should only contain a valid WordPress version such as "%2$s" or "%3$s".', 'plugin-check' ),
'Requires at least',
number_format( $latest_wordpress_version, 1 ),
number_format( $latest_wordpress_version - 0.1, 1 )
),
'too_many_tags' => sprintf(
/* translators: %d: maximum tags limit */
__( 'One or more tags were ignored. Please limit your plugin to %d tags.', 'plugin-check' ),
5
),
'ignored_tags' => sprintf(
/* translators: %s: list of tags not supported */
__( 'One or more tags were ignored. The following tags are not permitted: %s', 'plugin-check' ),
'"' . implode( '", "', $parser->ignore_tags ) . '"'
),
'no_short_description_present' => sprintf(
/* translators: %s: section title */
__( 'The "%s" section is missing. An excerpt was generated from your main plugin description.', 'plugin-check' ),
'Short Description'
),
'trimmed_short_description' => sprintf(
/* translators: 1: section title; 2: maximum limit */
_n( 'The "%1$s" section is too long and was truncated. A maximum of %2$d character is supported.', 'The "%1$s" section is too long and was truncated. A maximum of %2$d characters is supported.', 150, 'plugin-check' ),
'Short Description',
150
),
);

/**
* Filter the list of ignored readme parser warnings.
*
Expand All @@ -217,16 +272,50 @@ private function check_for_warnings( Check_Result $result, string $readme_file,
$warning_keys = array_diff( $warning_keys, $ignored_warnings );

if ( ! empty( $warning_keys ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: list of warnings */
esc_html__( 'The following readme parser warnings were detected: %s', 'plugin-check' ),
esc_html( implode( ', ', $warning_keys ) )
),
'readme_parser_warnings',
$readme_file
);
foreach ( $warning_keys as $warning ) {
$this->add_result_warning_for_file( $result, $messages[ $warning ], 'readme_parser_warnings', $readme_file );
}
}
}

/**
* Returns current major WordPress version.
*
* @since n.e.x.t
*
* @return string Stable WordPress version.
*/
private function get_wordpress_stable_version() {
$version = get_transient( 'wp_plugin_check_latest_wp_version' );

if ( false === $version ) {
$response = wp_remote_get( 'https://api.wordpress.org/core/version-check/1.7/' );

if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( isset( $body['offers'] ) && ! empty( $body['offers'] ) ) {
$latest_release = reset( $body['offers'] );

$version = $latest_release['new_bundled'];

set_transient( 'wp_plugin_check_latest_wp_version', $version, DAY_IN_SECONDS );
}
}
}

// If $version is still false at this point, use current installed WordPress version.
if ( false === $version ) {
$version = get_bloginfo( 'version' );

// Strip off any -alpha, -RC, -beta suffixes.
list( $version, ) = explode( '-', $version );

if ( preg_match( '#^\d.\d#', $version, $matches ) ) {
$version = $matches[0];
}
}

return $version;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Plugin Name: Test Plugin Readme Errors (Multiple Parser Warnings)
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Test plugin for the Readme check.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-check-errors-multiple-parser-warnings
*
* @package test-plugin-check-errors-multiple-parser-warnings
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== Test Plugin Readme Errors Multiple Parser Warnings ===

Contributors: plugin-check, first name
Requires at least: Previous
Tested up to: Latest
Requires PHP: PHP 5.6
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: testing, security, plugin, readme, validation, enhancement, performance

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vulputate nunc velit, vitae sollicitudin tortor viverra sed. Morbi sed nibh ex. Nulla vel augue velit. Maecenas vel lobortis orci. In eget metus dui. Vivamus eleifend volutpat diam. Duis viverra leo non eros auctor, nec placerat elit placerat. Nulla blandit odio risus. In eu dui non erat congue sagittis. Donec luctus tincidunt dolor, nec tempus erat pretium eu. Nunc a dictum odio. Vivamus vel faucibus lorem, eu finibus sem. Sed et vehicula ante, vel sodales augue. Donec tincidunt bibendum libero, ac tristique tellus rutrum nec. Curabitur viverra tincidunt eros non laoreet. Sed vitae consequat diam.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

Contributors: plugin-check
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: PHP 5.6
Tested up to: Latest
Requires PHP: 5.6
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down
82 changes: 81 additions & 1 deletion tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,86 @@ public function test_run_with_errors_parser_warnings() {
$this->assertEquals( 'readme_parser_warnings', $warnings['readme.txt'][0][0][0]['code'] );
}

public function test_run_with_errors_multiple_parser_warnings() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-multiple-parser-warnings/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $warnings );
$this->assertEquals( 6, $check_result->get_warning_count() );
$this->assertEmpty( $errors );
$this->assertEquals( 0, $check_result->get_error_count() );

// Check for parser warnings.
$this->assertArrayHasKey( 0, $warnings['readme.txt'] );
$this->assertArrayHasKey( 0, $warnings['readme.txt'][0] );

for ( $i = 0; $i < 6; ++$i ) {
$this->assertArrayHasKey( 'code', $warnings['readme.txt'][0][0][ $i ] );
$this->assertEquals( 'readme_parser_warnings', $warnings['readme.txt'][0][0][ $i ]['code'] );
}
}

public function test_run_with_errors_parser_warnings_with_custom_set_transient_version() {
$version = '5.0';

set_transient( 'wp_plugin_check_latest_wp_version', $version );

$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-parser-warnings/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertEmpty( $errors );
$this->assertNotEmpty( $warnings );
$this->assertEquals( 0, $check_result->get_error_count() );
$this->assertEquals( 1, $check_result->get_warning_count() );

$this->assertArrayHasKey( 0, $warnings['readme.txt'] );
$this->assertArrayHasKey( 0, $warnings['readme.txt'][0] );
$this->assertArrayHasKey( 'code', $warnings['readme.txt'][0][0][0] );
$this->assertEquals( 'readme_parser_warnings', $warnings['readme.txt'][0][0][0]['code'] );
$this->assertStringContainsString( 'The "Tested up to" field was ignored. This field should only contain a valid WordPress version such as "' . $version . '"', $warnings['readme.txt'][0][0][0]['message'] );

delete_transient( 'wp_plugin_check_latest_wp_version' );
}

public function test_run_with_errors_multiple_parser_warnings_and_empty_ignored_array() {
add_filter( 'wp_plugin_check_ignored_readme_warnings', '__return_empty_array' );

$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-multiple-parser-warnings/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $warnings );

/*
* Parser warning `contributor_ignored` is ignored by default. When empty array is returned for
* 'wp_plugin_check_ignored_readme_warnings' then that ignored warning is also added in the list of warnings.
*/
$this->assertEquals( 7, $check_result->get_warning_count() );
$this->assertEmpty( $errors );
$this->assertEquals( 0, $check_result->get_error_count() );

remove_filter( 'wp_plugin_check_ignored_readme_warnings', '__return_empty_array' );
}

public function test_filter_readme_warnings_ignored() {
// Define custom ignore for testing.
$custom_ignores = array(
Expand Down Expand Up @@ -273,7 +353,7 @@ static function () use ( $custom_ignores ) {
public function test_filter_wp_plugin_check_ignored_readme_warnings_will_return_no_error() {
// Define custom ignore for testing.
$custom_ignores = array(
'requires_php_header_ignored',
'tested_header_ignored',
'contributor_ignored',
);

Expand Down

0 comments on commit a50f3f1

Please sign in to comment.