From 53cb166fdb0429200824bebca51802b21a64ddf5 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 23:35:16 +0600 Subject: [PATCH 1/2] Tests: Add unit tests for the data and the sections of install_plugin_information(). See #66566. --- .../InstallPluginInformationSections_Test.php | 520 ++++++++++++++++++ 1 file changed, 520 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/plugin-install/InstallPluginInformationSections_Test.php diff --git a/tests/phpunit/tests/admin/includes/plugin-install/InstallPluginInformationSections_Test.php b/tests/phpunit/tests/admin/includes/plugin-install/InstallPluginInformationSections_Test.php new file mode 100644 index 0000000000000..09bc65bc15319 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/plugin-install/InstallPluginInformationSections_Test.php @@ -0,0 +1,520 @@ +user->create( array( 'role' => 'administrator' ) ); + + if ( is_multisite() ) { + grant_super_admin( self::$admin_id ); + } + } + + /** + * Revokes the super admin fixture. + */ + public static function wpTearDownAfterClass() { + if ( is_multisite() ) { + revoke_super_admin( self::$admin_id ); + } + } + + /** + * Saves the globals and the request. + */ + public function set_up() { + parent::set_up(); + + foreach ( array( 'tab', 'hook_suffix', 'current_screen' ) as $name ) { + $this->original['globals'][ $name ] = $GLOBALS[ $name ] ?? null; + } + + $this->original['request'] = $_REQUEST; + $this->api_requests = array(); + + wp_set_current_user( self::$admin_id ); + } + + /** + * Restores the globals and the request. + */ + public function tear_down() { + foreach ( $this->original['globals'] as $name => $value ) { + $GLOBALS[ $name ] = $value; + } + + $_REQUEST = $this->original['request']; + + parent::tear_down(); + } + + /** + * Returns the data of a plugin, as the API returns it. + * + * @param array $overrides Optional. Values that replace the defaults. Default none. + * @return object The data. + */ + private function get_api( array $overrides = array() ) { + return (object) array_merge( + array( + 'name' => 'My Plugin', + 'slug' => 'my-plugin', + 'version' => '1.0', + 'external' => true, + 'sections' => array( + 'description' => '

The description.

', + 'installation' => '

The installation.

', + 'faq' => '

The questions.

', + ), + ), + $overrides + ); + } + + /** + * Outputs the screen of a plugin, and returns the output. + * + * The screen ends the request, so an action of the footer stops it before. + * + * @param object|WP_Error $api The response of the API. + * @param array $request Optional. The request data besides the plugin. Default none. + * @return string The output. + */ + private function render( $api, array $request = array() ) { + $GLOBALS['tab'] = 'plugin-information'; + $GLOBALS['hook_suffix'] = 'plugin-install.php'; + + set_current_screen( 'plugin-install' ); + + $_REQUEST = array_merge( array( 'plugin' => $api->slug ?? 'my-plugin' ), $request ); + + add_filter( + 'plugins_api', + function ( $result, $action, $args ) use ( $api ) { + $this->api_requests[] = $args->slug; + + return $api; + }, + 10, + 3 + ); + add_action( + 'admin_footer', + static function () { + throw new RuntimeException( self::STOP ); + } + ); + + // The headers have been sent by the output of the tests. Other errors are left to the handler of the tests. + $previous_handler = set_error_handler( + static function ( $errno, $errstr, $errfile, $errline ) use ( &$previous_handler ) { + if ( str_contains( $errstr, 'headers already sent' ) ) { + return true; + } + + return $previous_handler ? $previous_handler( $errno, $errstr, $errfile, $errline ) : false; + }, + E_WARNING + ); + + ob_start(); + + try { + install_plugin_information(); + $this->fail( 'The screen should stop before it ends the request.' ); + } catch ( RuntimeException $e ) { + $this->assertSame( self::STOP, $e->getMessage() ); + } finally { + restore_error_handler(); + $output = ob_get_clean(); + } + + return $output; + } + + /** + * Tests that nothing is output without a plugin in the request. + * + * @ticket 66566 + */ + public function test_should_output_nothing_without_a_plugin() { + $_REQUEST = array(); + + add_filter( + 'plugins_api', + function () { + $this->api_requests[] = 'asked'; + + return $this->get_api(); + } + ); + + $this->assertSame( '', get_echo( 'install_plugin_information' ) ); + $this->assertSame( array(), $this->api_requests, 'The API should not be asked.' ); + } + + /** + * Tests that the screen dies with the error of the API. + * + * @ticket 66566 + */ + public function test_should_die_with_the_error_of_the_api() { + $_REQUEST['plugin'] = 'missing-plugin'; + + add_filter( + 'plugins_api', + static function () { + return new WP_Error( 'plugins_api_failed', 'Plugin not found.' ); + } + ); + + $this->expectException( 'WPDieException' ); + $this->expectExceptionMessage( 'Plugin not found.' ); + + install_plugin_information(); + } + + /** + * Tests the slug that the API is asked for. + * + * @ticket 66566 + */ + public function test_should_ask_the_api_for_the_unslashed_plugin_of_the_request() { + $this->render( $this->get_api(), array( 'plugin' => 'my\\\\-plugin' ) ); + + $this->assertSame( array( 'my\\-plugin' ), $this->api_requests ); + } + + /** + * Tests the title of the screen. + * + * @ticket 66566 + */ + public function test_should_output_the_title_of_the_screen_and_the_name_of_the_plugin() { + $output = $this->render( $this->get_api() ); + + $this->assertStringContainsString( '› Plugin Installation', $output, 'The title of the screen should be output.' ); + $this->assertStringContainsString( "

My Plugin

", $output ); + $this->assertStringContainsString( '
', str_replace( "'", '"', $output ) ); + } + + /** + * Tests that the markup that is not allowed is removed from the sections. + * + * @ticket 66566 + */ + public function test_should_remove_the_markup_that_is_not_allowed_from_the_sections() { + $output = $this->render( + $this->get_api( + array( + 'sections' => array( + 'description' => '

Safe text.

link', + ), + ) + ) + ); + + $this->assertStringNotContainsString( '', + 'author' => 'Jane', + ) + ) + ); + + $this->assertStringNotContainsString( '