From 3520fc39cc167df48ee73782aec2fcaaf7d5ddab Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 20:14:35 +0600 Subject: [PATCH 1/2] Tests: Add unit tests for the plugin status functions and activate_plugins(). See #66555. --- .../admin/includes/plugin/activatePlugins.php | 261 ++++++++++++++++++ .../admin/includes/plugin/isPluginActive.php | 259 +++++++++++++++++ 2 files changed, 520 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/plugin/activatePlugins.php create mode 100644 tests/phpunit/tests/admin/includes/plugin/isPluginActive.php diff --git a/tests/phpunit/tests/admin/includes/plugin/activatePlugins.php b/tests/phpunit/tests/admin/includes/plugin/activatePlugins.php new file mode 100644 index 0000000000000..f4a44610aba40 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/plugin/activatePlugins.php @@ -0,0 +1,261 @@ +slugs = array( "activate-plugins-one-$suffix", "activate-plugins-two-$suffix" ); + $this->redirects = array(); + + add_filter( 'wp_redirect', array( $this, 'record_redirect' ) ); + } + + /** + * Removes the test plugins and restores global state. + */ + public function tear_down() { + global $wp_plugin_paths; + + foreach ( $this->slugs as $slug ) { + $this->remove_plugin( $slug ); + + if ( is_array( $wp_plugin_paths ) ) { + unset( $wp_plugin_paths[ WP_PLUGIN_DIR . "/$slug/$slug.php" ] ); + } + } + + wp_cache_delete( 'plugins', 'plugins' ); + unset( $_GET['networkwide'] ); + + parent::tear_down(); + } + + /** + * Tests that several plugins are activated. + * + * @ticket 66555 + */ + public function test_should_activate_several_plugins() { + $this->create_plugins(); + + $this->assertTrue( activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ) ) ); + $this->assertEqualSets( + array( 'zzz/zzz.php', $this->get_file( 0 ), $this->get_file( 1 ) ), + get_option( 'active_plugins' ), + 'Both plugins should be active.' + ); + } + + /** + * Tests that a plugin that is not in an array is activated. + * + * @ticket 66555 + */ + public function test_should_activate_a_single_plugin() { + $this->create_plugins(); + + $this->assertTrue( activate_plugins( $this->get_file( 0 ) ) ); + $this->assertContains( $this->get_file( 0 ), get_option( 'active_plugins' ), 'The plugin should be active.' ); + $this->assertNotContains( $this->get_file( 1 ), get_option( 'active_plugins' ), 'The other plugin should not be active.' ); + } + + /** + * Tests that an error is returned when a plugin cannot be activated, and that the other plugins are activated. + * + * @ticket 66555 + */ + public function test_should_return_the_errors_of_the_plugins_that_cannot_be_activated() { + $this->create_plugins( array( 1 => " * Requires PHP: 99.0\n" ) ); + + $result = activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ) ); + + $this->assertWPError( $result ); + $this->assertSame( 'plugins_invalid', $result->get_error_code() ); + $this->assertSame( 'One of the plugins is invalid.', $result->get_error_message() ); + + $errors = $result->get_error_data(); + + $this->assertSame( array( $this->get_file( 1 ) ), array_keys( $errors ), 'Only the plugin that failed should have an error.' ); + $this->assertWPError( $errors[ $this->get_file( 1 ) ] ); + $this->assertSame( 'plugin_php_incompatible', $errors[ $this->get_file( 1 ) ]->get_error_code() ); + $this->assertContains( $this->get_file( 0 ), get_option( 'active_plugins' ), 'The other plugin should be active.' ); + $this->assertNotContains( $this->get_file( 1 ), get_option( 'active_plugins' ), 'The plugin that failed should not be active.' ); + } + + /** + * Tests that the errors of all the plugins that cannot be activated are returned. + * + * @ticket 66555 + */ + public function test_should_return_the_errors_of_all_the_plugins_that_cannot_be_activated() { + $this->create_plugins( + array( + 0 => " * Requires PHP: 99.0\n", + 1 => " * Requires PHP: 99.0\n", + ) + ); + + $result = activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ) ); + + $this->assertWPError( $result ); + $this->assertSame( array( $this->get_file( 0 ), $this->get_file( 1 ) ), array_keys( $result->get_error_data() ) ); + $this->assertSame( array( 'zzz/zzz.php' ), get_option( 'active_plugins' ), 'No plugin should be activated.' ); + } + + /** + * Tests that the plugin is added to the redirect URL of each plugin. + * + * @ticket 66555 + */ + public function test_should_add_each_plugin_to_the_redirect() { + $this->create_plugins(); + + activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ), 'plugins.php?error=true' ); + + $this->assertSame( + array( + 'plugins.php?error=true&plugin=' . rawurlencode( $this->get_file( 0 ) ) . '&_error_nonce=' . wp_create_nonce( 'plugin-activation-error_' . $this->get_file( 0 ) ), + 'plugins.php?error=true&plugin=' . rawurlencode( $this->get_file( 1 ) ) . '&_error_nonce=' . wp_create_nonce( 'plugin-activation-error_' . $this->get_file( 1 ) ), + ), + $this->redirects + ); + } + + /** + * Tests that nothing is added to a redirect that is empty. + * + * @ticket 66555 + */ + public function test_should_not_redirect_without_a_redirect_url() { + $this->create_plugins(); + + activate_plugins( array( $this->get_file( 0 ) ) ); + + $this->assertSame( array(), $this->redirects ); + } + + /** + * Tests that the plugins are activated silently. + * + * @ticket 66555 + */ + public function test_should_pass_the_silent_argument() { + $this->create_plugins(); + + $mock = new MockAction(); + add_action( 'activated_plugin', array( $mock, 'action' ) ); + + $this->assertTrue( activate_plugins( array( $this->get_file( 0 ) ), '', false, true ) ); + $this->assertSame( array(), $mock->get_events(), 'The activation hooks should not fire.' ); + + $this->assertTrue( activate_plugins( array( $this->get_file( 1 ) ) ) ); + $this->assertCount( 1, $mock->get_events(), 'The activation hook should fire when the plugin is not activated silently.' ); + } + + /** + * Tests that the plugins are activated for the network. + * + * @ticket 66555 + * + * @group ms-required + */ + public function test_should_pass_the_network_wide_argument() { + $this->create_plugins(); + + $this->assertTrue( activate_plugins( array( $this->get_file( 0 ), $this->get_file( 1 ) ), '', true ) ); + $this->assertEqualSets( + array( $this->get_file( 0 ), $this->get_file( 1 ) ), + array_keys( get_site_option( 'active_sitewide_plugins' ) ), + 'Both plugins should be network active.' + ); + $this->assertSame( array( 'zzz/zzz.php' ), get_option( 'active_plugins' ), 'The site plugins should not change.' ); + } + + /** + * Records redirect locations and stops the redirect. + * + * @param string $location The redirect location. + * @return false False, so that no header is sent. + */ + public function record_redirect( $location ) { + $this->redirects[] = $location; + + return false; + } + + /** + * Returns the plugin file of a test plugin. + * + * @param int $index The index of the plugin. + * @return string The plugin file. + */ + private function get_file( $index ) { + $slug = $this->slugs[ $index ]; + + return "$slug/$slug.php"; + } + + /** + * Creates the test plugins. + * + * @param string[] $headers Optional. Additional plugin headers, by index of the plugin. Default none. + */ + private function create_plugins( array $headers = array() ) { + foreach ( $this->slugs as $index => $slug ) { + $dir = WP_PLUGIN_DIR . '/' . $slug; + + $this->remove_plugin( $slug ); + $this->assertTrue( mkdir( $dir ), 'The test plugin directory should be created.' ); + $this->assertNotFalse( + file_put_contents( "{$dir}/{$slug}.php", "rmdir( $dir ); + $this->delete_folders( $dir ); + } + } +} diff --git a/tests/phpunit/tests/admin/includes/plugin/isPluginActive.php b/tests/phpunit/tests/admin/includes/plugin/isPluginActive.php new file mode 100644 index 0000000000000..1463b5bfaca7c --- /dev/null +++ b/tests/phpunit/tests/admin/includes/plugin/isPluginActive.php @@ -0,0 +1,259 @@ +slug = 'is-active-test-plugin-' . wp_generate_password( 8, false ); + } + + /** + * Removes the test plugin. + */ + public function tear_down() { + $this->remove_plugin(); + wp_cache_delete( 'plugins', 'plugins' ); + + parent::tear_down(); + } + + /** + * Tests that a plugin listed in the active plugins is active. + * + * @ticket 66555 + * + * @covers ::is_plugin_active + */ + public function test_is_plugin_active_should_be_true_for_a_listed_plugin() { + $this->assertTrue( is_plugin_active( 'active/active.php' ) ); + } + + /** + * Tests the plugins that are not active. + * + * @ticket 66555 + * + * @covers ::is_plugin_active + * @covers ::is_plugin_inactive + * + * @dataProvider data_plugins_that_are_not_active + * + * @param string|bool|null $plugin The plugin file. + */ + public function test_should_report_a_plugin_that_is_not_listed_as_inactive( $plugin ) { + $this->assertFalse( is_plugin_active( $plugin ), 'The plugin should not be active.' ); + $this->assertTrue( is_plugin_inactive( $plugin ), 'The plugin should be inactive.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_plugins_that_are_not_active() { + return array( + 'another plugin' => array( 'other/other.php' ), + 'a plugin that is not there' => array( 'missing/missing.php' ), + 'the folder of a plugin' => array( 'active' ), + 'a different case' => array( 'ACTIVE/active.php' ), + 'an empty string' => array( '' ), + 'null' => array( null ), + 'true' => array( true ), + ); + } + + /** + * Tests that is_plugin_inactive() is the opposite of is_plugin_active() for a listed plugin. + * + * @ticket 66555 + * + * @covers ::is_plugin_inactive + */ + public function test_is_plugin_inactive_should_be_false_for_a_listed_plugin() { + $this->assertFalse( is_plugin_inactive( 'active/active.php' ) ); + } + + /** + * Tests that a plugin that is active for the network is active. + * + * @ticket 66555 + * + * @group ms-required + * + * @covers ::is_plugin_active + * @covers ::is_plugin_inactive + */ + public function test_a_network_active_plugin_should_be_active() { + update_site_option( 'active_sitewide_plugins', array( 'network/network.php' => time() ) ); + + $this->assertTrue( is_plugin_active( 'network/network.php' ), 'The plugin should be active.' ); + $this->assertFalse( is_plugin_inactive( 'network/network.php' ), 'The plugin should not be inactive.' ); + } + + /** + * Tests that no plugin is active for the network on a single site. + * + * @ticket 66555 + * + * @group ms-excluded + * + * @covers ::is_plugin_active_for_network + */ + public function test_is_plugin_active_for_network_should_be_false_on_a_single_site() { + update_site_option( 'active_sitewide_plugins', array( 'network/network.php' => time() ) ); + + $this->assertFalse( is_plugin_active_for_network( 'network/network.php' ) ); + } + + /** + * Tests that a plugin that is listed as active for the network is active for the network. + * + * @ticket 66555 + * + * @group ms-required + * + * @covers ::is_plugin_active_for_network + */ + public function test_is_plugin_active_for_network_should_be_true_for_a_listed_plugin() { + update_site_option( 'active_sitewide_plugins', array( 'network/network.php' => time() ) ); + + $this->assertTrue( is_plugin_active_for_network( 'network/network.php' ) ); + } + + /** + * Tests that a plugin that is not listed as active for the network is not active for it. + * + * @ticket 66555 + * + * @group ms-required + * + * @covers ::is_plugin_active_for_network + * + * @dataProvider data_plugins_that_are_not_network_active + * + * @param string $plugin The plugin file. + */ + public function test_is_plugin_active_for_network_should_be_false_for_a_plugin_that_is_not_listed( $plugin ) { + update_site_option( 'active_sitewide_plugins', array( 'network/network.php' => time() ) ); + + $this->assertFalse( is_plugin_active_for_network( $plugin ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_plugins_that_are_not_network_active() { + return array( + 'another plugin' => array( 'other/other.php' ), + 'a plugin that is active on a site' => array( 'active/active.php' ), + 'an empty string' => array( '' ), + ); + } + + /** + * Tests that a plugin with the Network header set to true is a network only plugin. + * + * @ticket 66555 + * + * @covers ::is_network_only_plugin + * + * @dataProvider data_network_header + * + * @param string $header The Network header of the plugin, or an empty string. + * @param bool $expected Whether the plugin is for the network only. + */ + public function test_is_network_only_plugin_should_read_the_network_header( $header, $expected ) { + $this->create_plugin( $header ); + + $this->assertSame( $expected, is_network_only_plugin( $this->get_file() ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_network_header() { + return array( + 'true' => array( " * Network: true\n", true ), + 'TRUE' => array( " * Network: TRUE\n", true ), + 'false' => array( " * Network: false\n", false ), + 'something else' => array( " * Network: yes\n", false ), + 'no header' => array( '', false ), + ); + } + + /** + * Tests that the plugin file is normalized before it is read. + * + * @ticket 66555 + * + * @covers ::is_network_only_plugin + */ + public function test_is_network_only_plugin_should_accept_a_full_path_with_whitespace() { + $this->create_plugin( " * Network: true\n" ); + + $this->assertTrue( is_network_only_plugin( ' ' . WP_PLUGIN_DIR . '/' . $this->get_file() . ' ' ) ); + } + + /** + * Returns the plugin file of the test plugin. + * + * @return string The plugin file. + */ + private function get_file() { + return "{$this->slug}/{$this->slug}.php"; + } + + /** + * Creates the test plugin. + * + * @param string $headers Additional plugin headers. + */ + private function create_plugin( $headers = '' ) { + $dir = WP_PLUGIN_DIR . '/' . $this->slug; + + $this->remove_plugin(); + $this->assertTrue( mkdir( $dir ), 'The test plugin directory should be created.' ); + $this->assertNotFalse( + file_put_contents( "{$dir}/{$this->slug}.php", "slug; + + if ( is_dir( $dir ) ) { + $this->rmdir( $dir ); + $this->delete_folders( $dir ); + } + } +} From 9f72ff3a65cfd1e7e4f40f8638137a919ebf47a5 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 21:23:30 +0600 Subject: [PATCH 2/2] Tests: Do not pass null to is_plugin_active(), which is deprecated as an array offset on PHP 8.5. See #66555. --- tests/phpunit/tests/admin/includes/plugin/isPluginActive.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/plugin/isPluginActive.php b/tests/phpunit/tests/admin/includes/plugin/isPluginActive.php index 1463b5bfaca7c..ee420b19e3eb4 100644 --- a/tests/phpunit/tests/admin/includes/plugin/isPluginActive.php +++ b/tests/phpunit/tests/admin/includes/plugin/isPluginActive.php @@ -58,7 +58,7 @@ public function test_is_plugin_active_should_be_true_for_a_listed_plugin() { * * @dataProvider data_plugins_that_are_not_active * - * @param string|bool|null $plugin The plugin file. + * @param string|bool $plugin The plugin file. */ public function test_should_report_a_plugin_that_is_not_listed_as_inactive( $plugin ) { $this->assertFalse( is_plugin_active( $plugin ), 'The plugin should not be active.' ); @@ -77,7 +77,6 @@ public static function data_plugins_that_are_not_active() { 'the folder of a plugin' => array( 'active' ), 'a different case' => array( 'ACTIVE/active.php' ), 'an empty string' => array( '' ), - 'null' => array( null ), 'true' => array( true ), ); }