From 2884cadce34a70faa12db86e6572e08c515c9d50 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 3 Aug 2023 11:16:47 +0530 Subject: [PATCH 1/5] Check only root directory readme files --- .../Checker/Checks/Plugin_Readme_Check.php | 52 +++++++++++-------- .../Checks/Plugin_Readme_Check_Tests.php | 16 ++++++ .../load.php | 16 ++++++ .../readme.txt | 13 +++++ .../sub-directory/readme.txt | 14 +++++ 5 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 tests/phpunit/testdata/plugins/test-plugin-root-readme-without-errors/load.php create mode 100644 tests/phpunit/testdata/plugins/test-plugin-root-readme-without-errors/readme.txt create mode 100644 tests/phpunit/testdata/plugins/test-plugin-root-readme-without-errors/sub-directory/readme.txt diff --git a/includes/Checker/Checks/Plugin_Readme_Check.php b/includes/Checker/Checks/Plugin_Readme_Check.php index 8ce44fb8b..fd467b823 100644 --- a/includes/Checker/Checks/Plugin_Readme_Check.php +++ b/includes/Checker/Checks/Plugin_Readme_Check.php @@ -42,8 +42,17 @@ public function get_categories() { * @param array $files Array of plugin files. */ protected function check_files( Check_Result $result, array $files ) { + + $plugin_relative_path = $result->plugin()->path(); + $root_readme = array( + $plugin_relative_path . 'readme.txt', + $plugin_relative_path . 'readme.md', + ); + // Find the readme file. - $readme = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' ); + $readme_list = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' ); + + $readme = array_intersect( $root_readme, $readme_list ); // If the readme file does not exist, add a warning and skip other tests. if ( empty( $readme ) ) { @@ -60,13 +69,13 @@ protected function check_files( Check_Result $result, array $files ) { } // Check the readme file for default text. - $this->check_default_text( $result, $readme ); + $this->check_default_text( $result, $readme, $plugin_relative_path ); // Check the readme file for a valid license. - $this->check_license( $result, $readme ); + $this->check_license( $result, $readme, $plugin_relative_path ); // Check the readme file for a valid version. - $this->check_stable_tag( $result, $readme ); + $this->check_stable_tag( $result, $readme, $plugin_relative_path ); } /** @@ -76,32 +85,29 @@ protected function check_files( Check_Result $result, array $files ) { * * @param Check_Result $result The Check Result to amend. * @param array $files Array of plugin files. + * @param string $path Plugin relative path. */ - private function check_default_text( Check_Result $result, array $files ) { + private function check_default_text( Check_Result $result, array $files, $path ) { $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 ) { + $result->add_message( + false, + __( 'The readme appears to contain default text.', 'plugin-check' ), + array( + 'code' => 'default_readme_text', + 'file' => str_replace( $path, '', $file ), + ) + ); break; } } - - if ( $file ) { - $result->add_message( - false, - __( 'The readme appears to contain default text.', 'plugin-check' ), - array( - 'code' => 'default_readme_text', - 'file' => str_replace( $result->plugin()->path(), '', $file ), - ) - ); - } } /** @@ -111,8 +117,9 @@ private function check_default_text( Check_Result $result, array $files ) { * * @param Check_Result $result The Check Result to amend. * @param array $files Array of plugin files. + * @param string $path Plugin relative path. */ - private function check_license( Check_Result $result, array $files ) { + private function check_license( Check_Result $result, array $files, $path ) { $matches = array(); // Get the license from the readme file. $file = self::file_preg_match( '/(License:|License URI:)\s*(.+)*/i', $files, $matches ); @@ -128,7 +135,7 @@ private function check_license( Check_Result $result, array $files ) { __( 'Your plugin has an invalid license declared. Please update your readme with a valid SPDX license identifier.', 'plugin-check' ), array( 'code' => 'invalid_license', - 'file' => str_replace( $result->plugin()->path(), '', $file ), + 'file' => str_replace( $path, '', $file ), ) ); } @@ -141,8 +148,9 @@ private function check_license( Check_Result $result, array $files ) { * * @param Check_Result $result The Check Result to amend. * @param array $files Array of plugin files. + * @param string $path Plugin relative path. */ - private function check_stable_tag( Check_Result $result, array $files ) { + private function check_stable_tag( Check_Result $result, array $files, $path ) { $matches = array(); // Get the Stable tag from readme file. $file = self::file_preg_match( '/Stable tag:\s*([a-z0-9\.]+)/i', $files, $matches ); @@ -158,7 +166,7 @@ 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' => str_replace( $result->plugin()->path(), '', $file ), + 'file' => str_replace( $path, '', $file ), ) ); } @@ -175,7 +183,7 @@ private function check_stable_tag( Check_Result $result, array $files ) { __( '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' => str_replace( $result->plugin()->path(), '', $file ), + 'file' => str_replace( $path, '', $file ), ) ); } diff --git a/tests/phpunit/Checker/Checks/Plugin_Readme_Check_Tests.php b/tests/phpunit/Checker/Checks/Plugin_Readme_Check_Tests.php index bbdd93946..79635fdf1 100644 --- a/tests/phpunit/Checker/Checks/Plugin_Readme_Check_Tests.php +++ b/tests/phpunit/Checker/Checks/Plugin_Readme_Check_Tests.php @@ -166,4 +166,20 @@ public function test_run_md_with_errors() { $this->assertArrayHasKey( 'code', $warnings['readme.md'][0][0][3] ); $this->assertEquals( 'stable_tag_mismatch', $warnings['readme.md'][0][0][3]['code'] ); } + + public function test_run_root_readme_file_without_errors() { + $readme_check = new Plugin_Readme_Check(); + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-root-readme-without-errors/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->assertEmpty( $warnings ); + $this->assertEquals( 0, $check_result->get_error_count() ); + $this->assertEquals( 0, $check_result->get_warning_count() ); + } } diff --git a/tests/phpunit/testdata/plugins/test-plugin-root-readme-without-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-root-readme-without-errors/load.php new file mode 100644 index 000000000..27259d097 --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-root-readme-without-errors/load.php @@ -0,0 +1,16 @@ + Date: Thu, 3 Aug 2023 11:24:44 +0530 Subject: [PATCH 2/5] Rename readme.MD to readme.md --- .../{readme.MD => readme.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-without-errors/{readme.MD => readme.md} (100%) diff --git a/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-without-errors/readme.MD b/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-without-errors/readme.md similarity index 100% rename from tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-without-errors/readme.MD rename to tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-without-errors/readme.md From cae8aace9aee62ae530b2f92f1fe8455f532a894 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 4 Aug 2023 10:38:03 +0530 Subject: [PATCH 3/5] Revert function signature changes --- .../Checker/Checks/Plugin_Readme_Check.php | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/includes/Checker/Checks/Plugin_Readme_Check.php b/includes/Checker/Checks/Plugin_Readme_Check.php index fd467b823..8174818d6 100644 --- a/includes/Checker/Checks/Plugin_Readme_Check.php +++ b/includes/Checker/Checks/Plugin_Readme_Check.php @@ -69,13 +69,13 @@ protected function check_files( Check_Result $result, array $files ) { } // Check the readme file for default text. - $this->check_default_text( $result, $readme, $plugin_relative_path ); + $this->check_default_text( $result, $readme ); // Check the readme file for a valid license. - $this->check_license( $result, $readme, $plugin_relative_path ); + $this->check_license( $result, $readme ); // Check the readme file for a valid version. - $this->check_stable_tag( $result, $readme, $plugin_relative_path ); + $this->check_stable_tag( $result, $readme ); } /** @@ -85,9 +85,8 @@ protected function check_files( Check_Result $result, array $files ) { * * @param Check_Result $result The Check Result to amend. * @param array $files Array of plugin files. - * @param string $path Plugin relative path. */ - private function check_default_text( Check_Result $result, array $files, $path ) { + private function check_default_text( Check_Result $result, array $files ) { $default_text_patterns = array( 'Here is a short description of the plugin.', 'Tags: tag1', @@ -102,7 +101,7 @@ private function check_default_text( Check_Result $result, array $files, $path ) __( 'The readme appears to contain default text.', 'plugin-check' ), array( 'code' => 'default_readme_text', - 'file' => str_replace( $path, '', $file ), + 'file' => str_replace( $result->plugin()->path(), '', $file ), ) ); break; @@ -117,9 +116,8 @@ private function check_default_text( Check_Result $result, array $files, $path ) * * @param Check_Result $result The Check Result to amend. * @param array $files Array of plugin files. - * @param string $path Plugin relative path. */ - private function check_license( Check_Result $result, array $files, $path ) { + private function check_license( Check_Result $result, array $files ) { $matches = array(); // Get the license from the readme file. $file = self::file_preg_match( '/(License:|License URI:)\s*(.+)*/i', $files, $matches ); @@ -135,7 +133,7 @@ private function check_license( Check_Result $result, array $files, $path ) { __( 'Your plugin has an invalid license declared. Please update your readme with a valid SPDX license identifier.', 'plugin-check' ), array( 'code' => 'invalid_license', - 'file' => str_replace( $path, '', $file ), + 'file' => str_replace( $result->plugin()->path(), '', $file ), ) ); } @@ -148,9 +146,8 @@ private function check_license( Check_Result $result, array $files, $path ) { * * @param Check_Result $result The Check Result to amend. * @param array $files Array of plugin files. - * @param string $path Plugin relative path. */ - private function check_stable_tag( Check_Result $result, array $files, $path ) { + private function check_stable_tag( Check_Result $result, array $files ) { $matches = array(); // Get the Stable tag from readme file. $file = self::file_preg_match( '/Stable tag:\s*([a-z0-9\.]+)/i', $files, $matches ); @@ -166,7 +163,7 @@ private function check_stable_tag( Check_Result $result, array $files, $path ) { __( "It's recommended not to use 'Stable Tag: trunk'.", 'plugin-check' ), array( 'code' => 'trunk_stable_tag', - 'file' => str_replace( $path, '', $file ), + 'file' => str_replace( $result->plugin()->path(), '', $file ), ) ); } @@ -183,7 +180,7 @@ private function check_stable_tag( Check_Result $result, array $files, $path ) { __( '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' => str_replace( $path, '', $file ), + 'file' => str_replace( $result->plugin()->path(), '', $file ), ) ); } From 3c15ebeff992ca809841dc35e179f29735399d71 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 4 Aug 2023 11:28:18 +0530 Subject: [PATCH 4/5] Address review feedback --- .../Checker/Checks/Plugin_Readme_Check.php | 34 ++++++++++++++++--- .../readme.md | 13 +++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/testdata/plugins/test-plugin-plugin-readme-without-errors/readme.md diff --git a/includes/Checker/Checks/Plugin_Readme_Check.php b/includes/Checker/Checks/Plugin_Readme_Check.php index 8174818d6..223a5eeff 100644 --- a/includes/Checker/Checks/Plugin_Readme_Check.php +++ b/includes/Checker/Checks/Plugin_Readme_Check.php @@ -44,15 +44,39 @@ public function get_categories() { protected function check_files( Check_Result $result, array $files ) { $plugin_relative_path = $result->plugin()->path(); - $root_readme = array( - $plugin_relative_path . 'readme.txt', - $plugin_relative_path . 'readme.md', - ); // Find the readme file. $readme_list = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' ); - $readme = array_intersect( $root_readme, $readme_list ); + // Filter the readme files that located at rootx. + $potential_readme_files = array_filter( + $readme_list, + function ( $file ) use ( $plugin_relative_path ) { + $file = str_replace( $plugin_relative_path, '', $file ); + if ( ! strpos( $file, '/' ) ) { + return true; + } + } + ); + + // Find the .txt versions of the readme files. + $readme_txt = array_filter( + $potential_readme_files, + function ( $file ) { + return preg_match( '/^readme\.txt$/i', basename( $file ) ); + } + ); + + // Find the .md versions of the readme files. + $readme_md = array_filter( + $potential_readme_files, + function ( $file ) { + return preg_match( '/^readme\.md$/i', basename( $file ) ); + } + ); + + // If there's a .txt version, ignore .md versions. + $readme = ( ! empty( $readme_txt ) ) ? $readme_txt : $readme_md; // If the readme file does not exist, add a warning and skip other tests. if ( empty( $readme ) ) { diff --git a/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-without-errors/readme.md b/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-without-errors/readme.md new file mode 100644 index 000000000..06c49edf3 --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-without-errors/readme.md @@ -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. \ No newline at end of file From 557e12b4ac3e6e6c809ce82b297547d419a47757 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 4 Aug 2023 15:30:01 +0530 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Pascal Birchler --- includes/Checker/Checks/Plugin_Readme_Check.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Checker/Checks/Plugin_Readme_Check.php b/includes/Checker/Checks/Plugin_Readme_Check.php index 223a5eeff..f9951dc52 100644 --- a/includes/Checker/Checks/Plugin_Readme_Check.php +++ b/includes/Checker/Checks/Plugin_Readme_Check.php @@ -48,7 +48,7 @@ protected function check_files( Check_Result $result, array $files ) { // Find the readme file. $readme_list = self::filter_files_by_regex( $files, '/readme\.(txt|md)$/i' ); - // Filter the readme files that located at rootx. + // Filter the readme files located at root. $potential_readme_files = array_filter( $readme_list, function ( $file ) use ( $plugin_relative_path ) {