<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[31002] trunk/tests/phpunit/tests/admin/includesPlugin.php: Improved tests for wp-admin/includes/plugin.php.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/31002">31002</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/31002","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-12-30 19:23:49 +0000 (Tue, 30 Dec 2014)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Improved tests for wp-admin/includes/plugin.php.

- Improved cleanup for existing tests, by ensuring that plugins are deactivated.
- New tests for `get_plugin_files()`, `get_mu_plugins()`, `_sort_uname_callback()`, `get_dropins()`, `is_network_only_plugin()`, `activate_plugins()`, `validate_active_plugins()`, and `is_uninstallable_plugin()`.

Props sgrant.
See <a href="https://core.trac.wordpress.org/ticket/30860">#30860</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsadminincludesPluginphp">trunk/tests/phpunit/tests/admin/includesPlugin.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsadminincludesPluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/admin/includesPlugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/admin/includesPlugin.php        2014-12-30 10:29:18 UTC (rev 31001)
+++ trunk/tests/phpunit/tests/admin/includesPlugin.php  2014-12-30 19:23:49 UTC (rev 31002)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -61,6 +61,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                activate_plugin( 'hello.php' );
</span><span class="cx" style="display: block; padding: 0 10px">                $test = is_plugin_active( 'hello.php' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( $test );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               deactivate_plugins( 'hello.php' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function test_is_plugin_active_false() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,5 +81,331 @@
</span><span class="cx" style="display: block; padding: 0 10px">                activate_plugin( 'hello.php' );
</span><span class="cx" style="display: block; padding: 0 10px">                $test = is_plugin_inactive( 'hello.php' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( $test );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               deactivate_plugins( 'hello.php' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @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( '<?php\n//Silence is golden.', 'index.php', WPMU_PLUGIN_DIR );
+               $this->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( '<?php\n//Silence is not golden.', 'index.php', WPMU_PLUGIN_DIR );
+               $found = get_mu_plugins();
+               $this->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( '<?php\n//Test', 'foo.php', WPMU_PLUGIN_DIR );
+               $this->_create_plugin( '<?php\n//Test 2', 'bar.txt', WPMU_PLUGIN_DIR );
+               $found = get_mu_plugins();
+               $this->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( "<?php\n//Test", 'advanced-cache.php', WP_CONTENT_DIR );
+               $p2 = $this->_create_plugin( "<?php\n//Test", 'not-a-dropin.php', WP_CONTENT_DIR );
+
+               $dropins = get_dropins();
+               $this->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( "<?php\n/*\nPlugin Name: test\nNetwork: true" );
+
+               $this->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 = "<?php\n/*\nPlugin Name: Test\n*/", $filename = false, $dir_path = false ) {
+               if ( false === $filename ) {
+                       $filename = rand_str() . '.php';
+               }
+
+               if ( false === $dir_path ) {
+                       $dir_path = WP_PLUGIN_DIR;
+               }
+
+               $full_name = $dir_path . '/' . wp_unique_filename( $dir_path, $filename );
+
+               $file = fopen( $full_name, 'w' );
+               fwrite( $file, $data );
+               fclose( $file );
+
+               return array( $filename, $full_name );
+       }
+
+       /**
+        * Move existing mu-plugins to wp-content/mu-plugin/backup.
+        *
+        * @since 4.2.0
+        *
+        * @access private
+        */
+       private function _back_up_mu_plugins() {
+               if ( is_dir( WPMU_PLUGIN_DIR ) ) {
+                       $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
+                       if ( ! is_dir( $mu_bu_dir ) ) {
+                               mkdir( $mu_bu_dir );
+                       }
+
+                       $files_to_move = array();
+                       if ( $mu_plugins = opendir( WPMU_PLUGIN_DIR ) ) {
+                               while ( false !== $plugin = readdir( $mu_plugins ) ) {
+                                       if ( 0 !== strpos( $plugin, '.' ) ) {
+                                               $files_to_move[] = $plugin;
+                                       }
+                               }
+                       }
+
+                       @closedir( $mu_plugins );
+
+                       foreach ( $files_to_move as $file_to_move ) {
+                               $f = rename( WPMU_PLUGIN_DIR . '/' . $file_to_move, $mu_bu_dir . '/' . $file_to_move );
+                       }
+               }
+       }
+
+       /**
+        * Restore backed-up mu-plugins.
+        *
+        * @since 4.2.0
+        *
+        * @access private
+        */
+       private function _restore_mu_plugins() {
+               $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
+               $files_to_move = array();
+               if ( is_dir( $mu_bu_dir ) && $mu_plugins = opendir( $mu_bu_dir ) ) {
+                       while ( false !== $plugin = readdir( $mu_plugins ) ) {
+                               if ( 0 !== strpos( $plugin, '.' ) ) {
+                                       $files_to_move[] = $plugin;
+                               }
+                       }
+               }
+
+               @closedir( $mu_plugins );
+
+               foreach ( $files_to_move as $file_to_move ) {
+                       rename( $mu_bu_dir . '/' . $file_to_move, WPMU_PLUGIN_DIR . '/' . $file_to_move );
+               }
+
+               if ( is_dir( $mu_bu_dir ) ) {
+                       rmdir( $mu_bu_dir );
+               }
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>