diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 8cb612c5b..c621088cc 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -541,6 +541,8 @@ private function get_check_exclude_slugs() { * @since 1.0.0 * * @return string The plugin basename to check. + * + * @throws Exception Thrown if an plugin basename could not be set. */ final public function get_plugin_basename() { if ( null === $this->plugin_basename ) { @@ -550,10 +552,16 @@ final public function get_plugin_basename() { $this->plugin_basename = Plugin_Request_Utility::download_plugin( $plugin ); $this->delete_plugin_folder = true; - } elseif ( Plugin_Request_Utility::is_directory_valid_plugin( $plugin ) ) { - $this->plugin_basename = $plugin; } else { - $this->plugin_basename = Plugin_Request_Utility::get_plugin_basename_from_input( $plugin ); + try { + $this->plugin_basename = Plugin_Request_Utility::get_plugin_basename_from_input( $plugin ); + } catch ( Exception $exception ) { + if ( Plugin_Request_Utility::is_directory_valid_plugin( $plugin ) ) { + $this->plugin_basename = $plugin; + } else { + throw $exception; + } + } } } diff --git a/includes/Plugin_Context.php b/includes/Plugin_Context.php index e0c4923a8..cb5eafd9e 100644 --- a/includes/Plugin_Context.php +++ b/includes/Plugin_Context.php @@ -67,6 +67,8 @@ public function __construct( $main_file ) { } } } + + $this->main_file = $this->main_file; } /** @@ -101,9 +103,9 @@ public function main_file() { */ public function path( $relative_path = '/' ) { if ( is_dir( $this->main_file ) ) { - return trailingslashit( $this->main_file ) . ltrim( $relative_path, '/' ); + return realpath( trailingslashit( $this->main_file ) . ltrim( $relative_path, '/' ) ) . '/'; } else { - return plugin_dir_path( $this->main_file ) . ltrim( $relative_path, '/' ); + return realpath( plugin_dir_path( $this->main_file ) . ltrim( $relative_path, '/' ) ) . '/'; } } diff --git a/tests/behat/features/plugin-check-remote.feature b/tests/behat/features/plugin-check-remote.feature index 6499f0663..4f1040a61 100644 --- a/tests/behat/features/plugin-check-remote.feature +++ b/tests/behat/features/plugin-check-remote.feature @@ -35,6 +35,10 @@ Feature: Test that the WP-CLI plugin check command works with remote ZIP url. Scenario: Test with valid ZIP When I run the WP-CLI command `plugin check https://github.com/WordPress/plugin-check/raw/trunk/tests/behat/testdata/foo-bar-wp.zip --fields=code,type --format=csv` Then STDOUT should contain: + """ + FILE: foo-bar-wp.php + """ + And STDOUT should contain: """ WordPress.WP.AlternativeFunctions.rand_mt_rand,ERROR """ diff --git a/tests/behat/features/plugin-check-severity.feature b/tests/behat/features/plugin-check-severity.feature index 38323ae62..bf86f9f28 100644 --- a/tests/behat/features/plugin-check-severity.feature +++ b/tests/behat/features/plugin-check-severity.feature @@ -64,6 +64,10 @@ Feature: Test that the severity level in plugin check works. When I run the WP-CLI command `plugin check foo-bar-wp --format=csv --fields=code,type,severity` Then STDOUT should contain: + """ + FILE: foo-bar-wp.php + """ + And STDOUT should contain: """ allow_unfiltered_uploads_detected,ERROR,7 """ diff --git a/tests/phpunit/testdata/plugins/test-plugin/test-plugin.php b/tests/phpunit/testdata/plugins/test-plugin/test-plugin.php new file mode 100644 index 000000000..2d88c1458 --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin/test-plugin.php @@ -0,0 +1,16 @@ +assertSame( WP_PLUGIN_DIR . '/' . $this->plugin_name . '/another/folder', $this->check_context->path( '/another/folder' ) ); + $this->assertSame( WP_PLUGIN_DIR . '/' . $this->plugin_name . '/test-content/themes', $this->check_context->path( '/test-content/themes' ) ); } public function test_url() { @@ -42,6 +42,6 @@ public function test_url() { } public function test_url_with_parameter() { - $this->assertSame( WP_PLUGIN_URL . '/' . $this->plugin_name . '/folder/file.css', $this->check_context->url( '/folder/file.css' ) ); + $this->assertSame( WP_PLUGIN_URL . '/' . $this->plugin_name . '/assets/js/plugin-check-admin.js', $this->check_context->url( '/assets/js/plugin-check-admin.js' ) ); } } diff --git a/tests/phpunit/tests/Checker/Check_Result_Tests.php b/tests/phpunit/tests/Checker/Check_Result_Tests.php index 3015eddc9..b8ca0799e 100644 --- a/tests/phpunit/tests/Checker/Check_Result_Tests.php +++ b/tests/phpunit/tests/Checker/Check_Result_Tests.php @@ -19,7 +19,7 @@ class Check_Result_Tests extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $check_context = new Check_Context( 'test-plugin/test-plugin.php' ); + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin/test-plugin.php' ); $this->check_result = new Check_Result( $check_context ); } @@ -28,7 +28,7 @@ public function test_plugin() { $this->assertInstanceOf( Check_Context::class, $this->check_result->plugin() ); // Check the Check_Context has the correct basename. - $this->assertSame( 'test-plugin/test-plugin.php', $this->check_result->plugin()->basename() ); + $this->assertStringEndsWith( 'test-plugin/test-plugin.php', $this->check_result->plugin()->basename() ); } public function test_add_message_with_warning() { @@ -37,7 +37,7 @@ public function test_add_message_with_warning() { 'Warning message', array( 'code' => 'test_warning', - 'file' => 'test-plugin/test-plugin.php', + 'file' => UNIT_TESTS_PLUGIN_DIR . 'test-plugin/test-plugin.php', 'line' => 12, 'column' => 40, ) @@ -73,7 +73,7 @@ public function test_add_message_with_error() { 'Error message', array( 'code' => 'test_error', - 'file' => 'test-plugin/test-plugin.php', + 'file' => UNIT_TESTS_PLUGIN_DIR . 'test-plugin/test-plugin.php', 'line' => 22, 'column' => 30, ) @@ -113,7 +113,7 @@ public function test_get_errors_with_errors() { 'Error message', array( 'code' => 'test_error', - 'file' => 'test-plugin/test-plugin.php', + 'file' => UNIT_TESTS_PLUGIN_DIR . 'test-plugin/test-plugin.php', 'line' => 22, 'column' => 30, ) @@ -146,7 +146,7 @@ public function test_get_warnings_with_warnings() { 'Warning message', array( 'code' => 'test_warning', - 'file' => 'test-plugin/test-plugin.php', + 'file' => UNIT_TESTS_PLUGIN_DIR . 'test-plugin/test-plugin.php', 'line' => 22, 'column' => 30, ) diff --git a/tests/phpunit/tests/Plugin_Context_Tests.php b/tests/phpunit/tests/Plugin_Context_Tests.php index e930aecb0..db5f01a53 100644 --- a/tests/phpunit/tests/Plugin_Context_Tests.php +++ b/tests/phpunit/tests/Plugin_Context_Tests.php @@ -34,7 +34,7 @@ public function test_path() { } public function test_path_with_parameter() { - $this->assertSame( WP_PLUGIN_DIR . '/' . $this->plugin_name . '/another/folder', $this->plugin_context->path( '/another/folder' ) ); + $this->assertSame( WP_PLUGIN_DIR . '/' . $this->plugin_name . '/test-content/themes', $this->plugin_context->path( '/test-content/themes' ) ); } public function test_url() { @@ -42,7 +42,7 @@ public function test_url() { } public function test_url_with_parameter() { - $this->assertSame( WP_PLUGIN_URL . '/' . $this->plugin_name . '/folder/file.css', $this->plugin_context->url( '/folder/file.css' ) ); + $this->assertSame( WP_PLUGIN_URL . '/' . $this->plugin_name . '/assets/js/plugin-check-admin.js', $this->plugin_context->url( '/assets/js/plugin-check-admin.js' ) ); } public function test_location() {