Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect display of readme.txt instead of readme.md in Plugin Readme check #239

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 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,57 @@ 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' ),
sprintf(
/* translators: %s: readme file */
__( 'The %s appears to contain default text.', 'plugin-check' ),
str_replace( $result->plugin()->path( '/' ), '', $file )
),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
array(
'code' => 'default_readme_text',
'file' => $result->plugin()->path( '/readme.txt' ),
'file' => $file,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
)
);
}
}

/**
* 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 +119,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 +129,21 @@ 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' ),
sprintf(
/* translators: %s: readme file */
__( 'Your plugin has an invalid license declared. Please update your %s with a valid SPDX license identifier.', 'plugin-check' ),
str_replace( $result->plugin()->path( '/' ), '', $file )
),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
array(
'code' => 'invalid_license',
'file' => $result->plugin()->path( '/readme.txt' ),
'file' => $file,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
)
);
}
}

/**
* Checks the readme.txt stable tag.
* Checks the readme file stable tag.
*
* @since n.e.x.t
*
Expand All @@ -134,8 +152,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 +166,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' => $file,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
)
);
}

// 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 +180,14 @@ 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' ),
sprintf(
/* translators: %s: readme file */
__( 'The Stable Tag in your %s file does not match the version in your main plugin file.', 'plugin-check' ),
str_replace( $result->plugin()->path( '/' ), '', $file )
),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
array(
'code' => 'stable_tag_mismatch',
'file' => $result->plugin()->path( '/readme.txt' ),
'file' => $file,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
)
);
}
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.