diff --git a/tests/phpunit/tests/admin/includesPlugin.php b/tests/phpunit/tests/admin/includesPlugin.php index 316d7c8dc0..d4d197de49 100644 --- a/tests/phpunit/tests/admin/includesPlugin.php +++ b/tests/phpunit/tests/admin/includesPlugin.php @@ -61,6 +61,8 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase { activate_plugin( 'hello.php' ); $test = is_plugin_active( 'hello.php' ); $this->assertTrue( $test ); + + deactivate_plugins( 'hello.php' ); } function test_is_plugin_active_false() { @@ -79,5 +81,331 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase { activate_plugin( 'hello.php' ); $test = is_plugin_inactive( 'hello.php' ); $this->assertFalse( $test ); + + deactivate_plugins( 'hello.php' ); + } + + /** + * @covers ::get_plugin_files + */ + public function test_get_plugin_files_single() { + $name = 'hello.php'; + $this->assertEquals( array( $name ), get_plugin_files( $name ) ); + } + + /** + * @covers ::get_mu_plugins + */ + public function test_get_mu_plugins_when_mu_plugins_exists_but_is_empty() { + if ( is_dir( WPMU_PLUGIN_DIR ) ) { + $exists = true; + $this->_back_up_mu_plugins(); + } else { + $exists = false; + mkdir( WPMU_PLUGIN_DIR ); + } + + $this->assertEquals( array(), get_mu_plugins() ); + + // Clean up. + if ( $exists ) { + $this->_restore_mu_plugins(); + } else { + rmdir( WPMU_PLUGIN_DIR ); + } + } + + /** + * @covers ::get_mu_plugins + */ + public function test_get_mu_plugins_when_mu_plugins_directory_does_not_exist() { + $exists = false; + if ( is_dir( WPMU_PLUGIN_DIR ) ) { + $exists = true; + $this->_back_up_mu_plugins(); + rmdir( WPMU_PLUGIN_DIR ); + } + + $this->assertEquals( array(), get_mu_plugins() ); + + // Clean up. + if ( $exists ) { + mkdir( WPMU_PLUGIN_DIR ); + $this->_restore_mu_plugins(); + } + } + + /** + * @covers ::get_mu_plugins + */ + public function test_get_mu_plugins_should_ignore_index_php_containing_silence_is_golden() { + if ( is_dir( WPMU_PLUGIN_DIR ) ) { + $exists = true; + $this->_back_up_mu_plugins(); + } else { + $exists = false; + mkdir( WPMU_PLUGIN_DIR ); + } + + $this->_create_plugin( 'assertEquals( array(), get_mu_plugins() ); + + // Clean up. + unlink( WPMU_PLUGIN_DIR . '/index.php' ); + if ( $exists ) { + $this->_restore_mu_plugins(); + } else { + rmdir( WPMU_PLUGIN_DIR ); + } + } + + /** + * @covers ::get_mu_plugins + */ + public function test_get_mu_plugins_should_not_ignore_index_php_containing_something_other_than_silence_is_golden() { + if ( is_dir( WPMU_PLUGIN_DIR ) ) { + $exists = true; + $this->_back_up_mu_plugins(); + } else { + $exists = false; + mkdir( WPMU_PLUGIN_DIR ); + } + + $this->_create_plugin( 'assertEquals( array( 'index.php' ), array_keys( $found ) ); + + // Clean up. + unlink( WPMU_PLUGIN_DIR . '/index.php' ); + if ( $exists ) { + $this->_restore_mu_plugins(); + } else { + rmdir( WPMU_PLUGIN_DIR ); + } + } + + /** + * @covers ::get_mu_plugins + */ + public function test_get_mu_plugins_should_ignore_files_without_php_extensions() { + if ( is_dir( WPMU_PLUGIN_DIR ) ) { + $exists = true; + $this->_back_up_mu_plugins(); + } else { + $exists = false; + mkdir( WPMU_PLUGIN_DIR ); + } + + $this->_create_plugin( '_create_plugin( 'assertEquals( array( 'foo.php' ), array_keys( $found ) ); + + // Clean up. + unlink( WPMU_PLUGIN_DIR . '/foo.php' ); + unlink( WPMU_PLUGIN_DIR . '/bar.txt' ); + if ( $exists ) { + $this->_restore_mu_plugins(); + } else { + rmdir( WPMU_PLUGIN_DIR ); + } + } + + /** + * @covers ::_sort_uname_callback + */ + public function test__sort_uname_callback() { + $this->assertLessThan( 0, _sort_uname_callback( array( 'Name' => 'a' ), array( 'Name' => 'b' ) ) ); + $this->assertGreaterThan( 0, _sort_uname_callback( array( 'Name' => 'c' ), array( 'Name' => 'b' ) ) ); + $this->assertEquals( 0, _sort_uname_callback( array( 'Name' => 'a' ), array( 'Name' => 'a' ) ) ); + } + + /** + * @covers ::get_dropins + */ + public function test_get_dropins_empty() { + $this->assertEquals( array(), get_dropins() ); + } + + /** + * @covers ::get_dropins + */ + public function test_get_dropins_not_empty() { + $p1 = $this->_create_plugin( "_create_plugin( "assertEquals( array( 'advanced-cache.php' ), array_keys( $dropins ) ); + + unlink( $p1[1] ); + unlink( $p2[1] ); + } + + /** + * @covers ::is_network_only_plugin + */ + public function test_is_network_only_plugin_hello() { + $this->assertFalse( is_network_only_plugin( 'hello.php' ) ); + } + + /** + * @covers ::is_network_only_plugin + */ + public function test_is_network_only_plugin() { + $p = $this->_create_plugin( "assertTrue( is_network_only_plugin( $p[0] ) ); + + unlink( $p[1] ); + } + + /** + * @covers ::activate_plugins + */ + public function test_activate_plugins_single_no_array() { + $name = 'hello.php'; + activate_plugins( $name ); + $this->assertTrue( is_plugin_active( $name ) ); + deactivate_plugins( $name ); + } + + /** + * @covers ::activate_plugins + */ + public function test_activate_plugins_single_array() { + $name = 'hello.php'; + activate_plugins( array( $name ) ); + $this->assertTrue( is_plugin_active( $name ) ); + deactivate_plugins( $name ); + } + + /** + * @covers ::validate_active_plugins + */ + public function test_validate_active_plugins_remove_invalid() { + $plugin = $this->_create_plugin(); + + activate_plugin( $plugin[ 0 ] ); + unlink( $plugin[ 1 ] ); + + $result = validate_active_plugins(); + $this->assertTrue( isset( $result[ $plugin[ 0 ] ] ) ); + } + + /** + * @covers ::is_uninstallable_plugin + */ + public function test_is_uninstallable_plugin() { + $this->assertFalse( is_uninstallable_plugin( 'hello' ) ); + } + + /** + * @covers ::is_uninstallable_plugin + */ + public function test_is_uninstallable_plugin_true() { + $plugin = $this->_create_plugin(); + + $uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); + $uninstallable_plugins[ $plugin[0] ] = true; + update_option( 'uninstall_plugins', $uninstallable_plugins ); + + $this->assertTrue( is_uninstallable_plugin( $plugin[0] ) ); + + unset( $uninstallable_plugins[ $plugin[0] ] ); + update_option( 'uninstall_plugins', $uninstallable_plugins ); + + unlink( $plugin[1] ); + } + + /** + * Generate a plugin. + * + * This creates a single-file plugin. + * + * @since 4.2.0 + * + * @access private + * + * @param string $data Optional. Data for the plugin file. Default is a dummy plugin header. + * @param string $filename Optional. Filename for the plugin file. Default is a random string. + * @param string $dir_path Optional. Path for directory where the plugin should live. + * @return array Two-membered array of filename and full plugin path. + */ + private function _create_plugin( $data = "