Skip to content

Commit

Permalink
Merge pull request #239 from 10up/fix/readme-error-filename
Browse files Browse the repository at this point in the history
Incorrect display of `readme.txt` instead of `readme.md` in Plugin Readme check
  • Loading branch information
mukeshpanchal27 authored Aug 3, 2023
2 parents 0bf1608 + a21ea17 commit 1da4245
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 25 deletions.
61 changes: 36 additions & 25 deletions includes/Checker/Checks/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use WordPress\Plugin_Check\Traits\Stable_Check;

/**
* Check the plugins readme.txt file and contents.
* Check the plugins readme file and contents.
*
* @since n.e.x.t
*/
Expand All @@ -34,7 +34,7 @@ public function get_categories() {
}

/**
* Check the readme.txt file.
* Check the readme file.
*
* @since n.e.x.t
*
Expand All @@ -45,7 +45,7 @@ protected function check_files( Check_Result $result, array $files ) {
// Find the readme file.
$readme = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' );

// If the readme.txt does not exist, add a warning and skip other tests.
// If the readme file does not exist, add a warning and skip other tests.
if ( empty( $readme ) ) {
$result->add_message(
false,
Expand All @@ -59,43 +59,53 @@ protected function check_files( Check_Result $result, array $files ) {
return;
}

// Check the readme.txt for default text.
// Check the readme file for default text.
$this->check_default_text( $result, $readme );

// Check the readme.txt for a valid license.
// Check the readme file for a valid license.
$this->check_license( $result, $readme );

// Check the readme.txt for a valid version.
// Check the readme file for a valid version.
$this->check_stable_tag( $result, $readme );
}

/**
* Checks the readme.txt for default text.
* Checks the readme file for default text.
*
* @since n.e.x.t
*
* @param Check_Result $result The Check Result to amend.
* @param array $files Array of plugin files.
*/
private function check_default_text( Check_Result $result, array $files ) {
if (
self::file_str_contains( $files, 'Here is a short description of the plugin.' ) ||
self::file_str_contains( $files, 'Tags: tag1' ) ||
self::file_str_contains( $files, 'Donate link: http://example.com/' )
) {
$default_text_patterns = array(
'Here is a short description of the plugin.',
'Tags: tag1',
'Donate link: http://example.com/',
);

$file = '';
foreach ( $default_text_patterns as $pattern ) {
$file = self::file_str_contains( $files, $pattern );
if ( $file ) {
break;
}
}

if ( $file ) {
$result->add_message(
false,
__( 'The readme.txt appears to contain default text.', 'plugin-check' ),
__( 'The readme appears to contain default text.', 'plugin-check' ),
array(
'code' => 'default_readme_text',
'file' => $result->plugin()->path( '/readme.txt' ),
'file' => str_replace( $result->plugin()->path(), '', $file ),
)
);
}
}

/**
* Checks the readme.txt for a valid license.
* Checks the readme file for a valid license.
*
* @since n.e.x.t
*
Expand All @@ -105,7 +115,7 @@ private function check_default_text( Check_Result $result, array $files ) {
private function check_license( Check_Result $result, array $files ) {
$matches = array();
// Get the license from the readme file.
self::file_preg_match( '/(License:|License URI:)\s*(.+)*/i', $files, $matches );
$file = self::file_preg_match( '/(License:|License URI:)\s*(.+)*/i', $files, $matches );

if ( empty( $matches ) ) {
return;
Expand All @@ -115,17 +125,17 @@ private function check_license( Check_Result $result, array $files ) {
if ( ! preg_match( '/^([a-z0-9\-\+\.]+)(\sor\s([a-z0-9\-\+\.]+))*$/i', $matches[2] ) ) {
$result->add_message(
false,
__( 'Your plugin has an invalid license declared. Please update your readme.txt with a valid SPDX license identifier.', 'plugin-check' ),
__( 'Your plugin has an invalid license declared. Please update your readme with a valid SPDX license identifier.', 'plugin-check' ),
array(
'code' => 'invalid_license',
'file' => $result->plugin()->path( '/readme.txt' ),
'file' => str_replace( $result->plugin()->path(), '', $file ),
)
);
}
}

/**
* Checks the readme.txt stable tag.
* Checks the readme file stable tag.
*
* @since n.e.x.t
*
Expand All @@ -134,8 +144,9 @@ private function check_license( Check_Result $result, array $files ) {
*/
private function check_stable_tag( Check_Result $result, array $files ) {
$matches = array();
// Get the readme.txt Stable tag.
if ( ! self::file_preg_match( '/Stable tag:\s*([a-z0-9\.]+)/i', $files, $matches ) ) {
// Get the Stable tag from readme file.
$file = self::file_preg_match( '/Stable tag:\s*([a-z0-9\.]+)/i', $files, $matches );
if ( ! $file ) {
return;
}

Expand All @@ -147,12 +158,12 @@ private function check_stable_tag( Check_Result $result, array $files ) {
__( "It's recommended not to use 'Stable Tag: trunk'.", 'plugin-check' ),
array(
'code' => 'trunk_stable_tag',
'file' => $result->plugin()->path( '/readme.txt' ),
'file' => str_replace( $result->plugin()->path(), '', $file ),
)
);
}

// Check the readme.txt Stable tag against the plugin's main file version.
// Check the readme file Stable tag against the plugin's main file version.
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $result->plugin()->basename() );

if (
Expand All @@ -161,10 +172,10 @@ private function check_stable_tag( Check_Result $result, array $files ) {
) {
$result->add_message(
false,
__( 'The Stable Tag in your readme.txt file does not match the version in your main plugin file.', 'plugin-check' ),
__( 'The Stable Tag in your readme file does not match the version in your main plugin file.', 'plugin-check' ),
array(
'code' => 'stable_tag_mismatch',
'file' => $result->plugin()->path( '/readme.txt' ),
'file' => str_replace( $result->plugin()->path(), '', $file ),
)
);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,42 @@ public function test_run_md_without_errors() {
$this->assertEquals( 0, $check_result->get_error_count() );
$this->assertEquals( 0, $check_result->get_warning_count() );
}

public function test_run_md_with_errors() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-md-with-errors/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$warnings = $check_result->get_warnings();

$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.md', $warnings );
$this->assertEquals( 4, $check_result->get_warning_count() );

// Check for default text file warning.
$this->assertArrayHasKey( 0, $warnings['readme.md'] );
$this->assertArrayHasKey( 0, $warnings['readme.md'][0] );
$this->assertArrayHasKey( 'code', $warnings['readme.md'][0][0][0] );
$this->assertEquals( 'default_readme_text', $warnings['readme.md'][0][0][0]['code'] );

// Check for invalid license warning.
$this->assertArrayHasKey( 0, $warnings['readme.md'] );
$this->assertArrayHasKey( 0, $warnings['readme.md'][0] );
$this->assertArrayHasKey( 'code', $warnings['readme.md'][0][0][1] );
$this->assertEquals( 'invalid_license', $warnings['readme.md'][0][0][1]['code'] );

// Check for trunk stable tag warning.
$this->assertArrayHasKey( 0, $warnings['readme.md'] );
$this->assertArrayHasKey( 0, $warnings['readme.md'][0] );
$this->assertArrayHasKey( 'code', $warnings['readme.md'][0][0][2] );
$this->assertEquals( 'trunk_stable_tag', $warnings['readme.md'][0][0][2]['code'] );

// Check for stable tag mismatch file warning.
$this->assertArrayHasKey( 0, $warnings['readme.md'] );
$this->assertArrayHasKey( 0, $warnings['readme.md'][0] );
$this->assertArrayHasKey( 'code', $warnings['readme.md'][0][0][3] );
$this->assertEquals( 'stable_tag_mismatch', $warnings['readme.md'][0][0][3]['code'] );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Plugin Name: Test Plugin Readme.md check (errors)
* Plugin URI: https://github.com/wordpress/plugin-check
* Description: Test plugin for the Readme.md 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
*
* @package test-plugin-check-errors
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

=== Plugin Check ===

Contributors: wordpressdotorg
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: 5.6
Stable tag: trunk
License: Oculus VR Inc. Software Development Kit License
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: performance, testing, security

Here is a short description of the plugin.

0 comments on commit 1da4245

Please sign in to comment.