Make WordPress Core

Changeset 31002


Ignore:
Timestamp:
12/30/2014 07:23:49 PM (10 years ago)
Author:
boonebgorges
Message:

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 #30860.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPlugin.php

    r30557 r31002  
    6262        $test = is_plugin_active( 'hello.php' );
    6363        $this->assertTrue( $test );
     64
     65        deactivate_plugins( 'hello.php' );
    6466    }
    6567
     
    8082        $test = is_plugin_inactive( 'hello.php' );
    8183        $this->assertFalse( $test );
     84
     85        deactivate_plugins( 'hello.php' );
     86    }
     87
     88    /**
     89     * @covers ::get_plugin_files
     90     */
     91    public function test_get_plugin_files_single() {
     92        $name = 'hello.php';
     93        $this->assertEquals( array( $name ), get_plugin_files( $name ) );
     94    }
     95
     96    /**
     97     * @covers ::get_mu_plugins
     98     */
     99    public function test_get_mu_plugins_when_mu_plugins_exists_but_is_empty() {
     100        if ( is_dir( WPMU_PLUGIN_DIR ) ) {
     101            $exists = true;
     102            $this->_back_up_mu_plugins();
     103        } else {
     104            $exists = false;
     105            mkdir( WPMU_PLUGIN_DIR );
     106        }
     107
     108        $this->assertEquals( array(), get_mu_plugins() );
     109
     110        // Clean up.
     111        if ( $exists ) {
     112            $this->_restore_mu_plugins();
     113        } else {
     114            rmdir( WPMU_PLUGIN_DIR );
     115        }
     116    }
     117
     118    /**
     119     * @covers ::get_mu_plugins
     120     */
     121    public function test_get_mu_plugins_when_mu_plugins_directory_does_not_exist() {
     122        $exists = false;
     123        if ( is_dir( WPMU_PLUGIN_DIR ) ) {
     124            $exists = true;
     125            $this->_back_up_mu_plugins();
     126            rmdir( WPMU_PLUGIN_DIR );
     127        }
     128
     129        $this->assertEquals( array(), get_mu_plugins() );
     130
     131        // Clean up.
     132        if ( $exists ) {
     133            mkdir( WPMU_PLUGIN_DIR );
     134            $this->_restore_mu_plugins();
     135        }
     136    }
     137
     138    /**
     139     * @covers ::get_mu_plugins
     140     */
     141    public function test_get_mu_plugins_should_ignore_index_php_containing_silence_is_golden() {
     142        if ( is_dir( WPMU_PLUGIN_DIR ) ) {
     143            $exists = true;
     144            $this->_back_up_mu_plugins();
     145        } else {
     146            $exists = false;
     147            mkdir( WPMU_PLUGIN_DIR );
     148        }
     149
     150        $this->_create_plugin( '<?php\n//Silence is golden.', 'index.php', WPMU_PLUGIN_DIR );
     151        $this->assertEquals( array(), get_mu_plugins() );
     152
     153        // Clean up.
     154        unlink( WPMU_PLUGIN_DIR . '/index.php' );
     155        if ( $exists ) {
     156            $this->_restore_mu_plugins();
     157        } else {
     158            rmdir( WPMU_PLUGIN_DIR );
     159        }
     160    }
     161
     162    /**
     163     * @covers ::get_mu_plugins
     164     */
     165    public function test_get_mu_plugins_should_not_ignore_index_php_containing_something_other_than_silence_is_golden() {
     166        if ( is_dir( WPMU_PLUGIN_DIR ) ) {
     167            $exists = true;
     168            $this->_back_up_mu_plugins();
     169        } else {
     170            $exists = false;
     171            mkdir( WPMU_PLUGIN_DIR );
     172        }
     173
     174        $this->_create_plugin( '<?php\n//Silence is not golden.', 'index.php', WPMU_PLUGIN_DIR );
     175        $found = get_mu_plugins();
     176        $this->assertEquals( array( 'index.php' ), array_keys( $found ) );
     177
     178        // Clean up.
     179        unlink( WPMU_PLUGIN_DIR . '/index.php' );
     180        if ( $exists ) {
     181            $this->_restore_mu_plugins();
     182        } else {
     183            rmdir( WPMU_PLUGIN_DIR );
     184        }
     185    }
     186
     187    /**
     188     * @covers ::get_mu_plugins
     189     */
     190    public function test_get_mu_plugins_should_ignore_files_without_php_extensions() {
     191        if ( is_dir( WPMU_PLUGIN_DIR ) ) {
     192            $exists = true;
     193            $this->_back_up_mu_plugins();
     194        } else {
     195            $exists = false;
     196            mkdir( WPMU_PLUGIN_DIR );
     197        }
     198
     199        $this->_create_plugin( '<?php\n//Test', 'foo.php', WPMU_PLUGIN_DIR );
     200        $this->_create_plugin( '<?php\n//Test 2', 'bar.txt', WPMU_PLUGIN_DIR );
     201        $found = get_mu_plugins();
     202        $this->assertEquals( array( 'foo.php' ), array_keys( $found ) );
     203
     204        // Clean up.
     205        unlink( WPMU_PLUGIN_DIR . '/foo.php' );
     206        unlink( WPMU_PLUGIN_DIR . '/bar.txt' );
     207        if ( $exists ) {
     208            $this->_restore_mu_plugins();
     209        } else {
     210            rmdir( WPMU_PLUGIN_DIR );
     211        }
     212    }
     213
     214    /**
     215     * @covers ::_sort_uname_callback
     216     */
     217    public function test__sort_uname_callback() {
     218        $this->assertLessThan( 0, _sort_uname_callback( array( 'Name' => 'a' ), array( 'Name' => 'b' ) ) );
     219        $this->assertGreaterThan( 0, _sort_uname_callback( array( 'Name' => 'c' ), array( 'Name' => 'b' ) ) );
     220        $this->assertEquals( 0, _sort_uname_callback( array( 'Name' => 'a' ), array( 'Name' => 'a' ) ) );
     221    }
     222
     223    /**
     224     * @covers ::get_dropins
     225     */
     226    public function test_get_dropins_empty() {
     227        $this->assertEquals( array(), get_dropins() );
     228    }
     229
     230    /**
     231     * @covers ::get_dropins
     232     */
     233    public function test_get_dropins_not_empty() {
     234        $p1 = $this->_create_plugin( "<?php\n//Test", 'advanced-cache.php', WP_CONTENT_DIR );
     235        $p2 = $this->_create_plugin( "<?php\n//Test", 'not-a-dropin.php', WP_CONTENT_DIR );
     236
     237        $dropins = get_dropins();
     238        $this->assertEquals( array( 'advanced-cache.php' ), array_keys( $dropins ) );
     239
     240        unlink( $p1[1] );
     241        unlink( $p2[1] );
     242    }
     243
     244    /**
     245     * @covers ::is_network_only_plugin
     246     */
     247    public function test_is_network_only_plugin_hello() {
     248        $this->assertFalse( is_network_only_plugin( 'hello.php' ) );
     249    }
     250
     251    /**
     252     * @covers ::is_network_only_plugin
     253     */
     254    public function test_is_network_only_plugin() {
     255        $p = $this->_create_plugin( "<?php\n/*\nPlugin Name: test\nNetwork: true" );
     256
     257        $this->assertTrue( is_network_only_plugin( $p[0] ) );
     258
     259        unlink( $p[1] );
     260    }
     261
     262    /**
     263     * @covers ::activate_plugins
     264     */
     265    public function test_activate_plugins_single_no_array() {
     266        $name = 'hello.php';
     267        activate_plugins( $name );
     268        $this->assertTrue( is_plugin_active( $name ) );
     269        deactivate_plugins( $name );
     270    }
     271
     272    /**
     273     * @covers ::activate_plugins
     274     */
     275    public function test_activate_plugins_single_array() {
     276        $name = 'hello.php';
     277        activate_plugins( array( $name ) );
     278        $this->assertTrue( is_plugin_active( $name ) );
     279        deactivate_plugins( $name );
     280    }
     281
     282    /**
     283     * @covers ::validate_active_plugins
     284     */
     285    public function test_validate_active_plugins_remove_invalid() {
     286        $plugin = $this->_create_plugin();
     287
     288        activate_plugin( $plugin[ 0 ] );
     289        unlink( $plugin[ 1 ] );
     290
     291        $result = validate_active_plugins();
     292        $this->assertTrue( isset( $result[ $plugin[ 0 ] ] ) );
     293    }
     294
     295    /**
     296     * @covers ::is_uninstallable_plugin
     297     */
     298    public function test_is_uninstallable_plugin() {
     299        $this->assertFalse( is_uninstallable_plugin( 'hello' ) );
     300    }
     301
     302    /**
     303     * @covers ::is_uninstallable_plugin
     304     */
     305    public function test_is_uninstallable_plugin_true() {
     306        $plugin = $this->_create_plugin();
     307
     308        $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
     309        $uninstallable_plugins[ $plugin[0] ] = true;
     310        update_option( 'uninstall_plugins', $uninstallable_plugins );
     311
     312        $this->assertTrue( is_uninstallable_plugin( $plugin[0] ) );
     313
     314        unset( $uninstallable_plugins[ $plugin[0] ] );
     315        update_option( 'uninstall_plugins', $uninstallable_plugins );
     316
     317        unlink( $plugin[1] );
     318    }
     319
     320    /**
     321     * Generate a plugin.
     322     *
     323     * This creates a single-file plugin.
     324     *
     325     * @since 4.2.0
     326     *
     327     * @access private
     328     *
     329     * @param string $data     Optional. Data for the plugin file. Default is a dummy plugin header.
     330     * @param string $filename Optional. Filename for the plugin file. Default is a random string.
     331     * @param string $dir_path Optional. Path for directory where the plugin should live.
     332     * @return array Two-membered array of filename and full plugin path.
     333     */
     334    private function _create_plugin( $data = "<?php\n/*\nPlugin Name: Test\n*/", $filename = false, $dir_path = false ) {
     335        if ( false === $filename ) {
     336            $filename = rand_str() . '.php';
     337        }
     338
     339        if ( false === $dir_path ) {
     340            $dir_path = WP_PLUGIN_DIR;
     341        }
     342
     343        $full_name = $dir_path . '/' . wp_unique_filename( $dir_path, $filename );
     344
     345        $file = fopen( $full_name, 'w' );
     346        fwrite( $file, $data );
     347        fclose( $file );
     348
     349        return array( $filename, $full_name );
     350    }
     351
     352    /**
     353     * Move existing mu-plugins to wp-content/mu-plugin/backup.
     354     *
     355     * @since 4.2.0
     356     *
     357     * @access private
     358     */
     359    private function _back_up_mu_plugins() {
     360        if ( is_dir( WPMU_PLUGIN_DIR ) ) {
     361            $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
     362            if ( ! is_dir( $mu_bu_dir ) ) {
     363                mkdir( $mu_bu_dir );
     364            }
     365
     366            $files_to_move = array();
     367            if ( $mu_plugins = opendir( WPMU_PLUGIN_DIR ) ) {
     368                while ( false !== $plugin = readdir( $mu_plugins ) ) {
     369                    if ( 0 !== strpos( $plugin, '.' ) ) {
     370                        $files_to_move[] = $plugin;
     371                    }
     372                }
     373            }
     374
     375            @closedir( $mu_plugins );
     376
     377            foreach ( $files_to_move as $file_to_move ) {
     378                $f = rename( WPMU_PLUGIN_DIR . '/' . $file_to_move, $mu_bu_dir . '/' . $file_to_move );
     379            }
     380        }
     381    }
     382
     383    /**
     384     * Restore backed-up mu-plugins.
     385     *
     386     * @since 4.2.0
     387     *
     388     * @access private
     389     */
     390    private function _restore_mu_plugins() {
     391        $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
     392        $files_to_move = array();
     393        if ( is_dir( $mu_bu_dir ) && $mu_plugins = opendir( $mu_bu_dir ) ) {
     394            while ( false !== $plugin = readdir( $mu_plugins ) ) {
     395                if ( 0 !== strpos( $plugin, '.' ) ) {
     396                    $files_to_move[] = $plugin;
     397                }
     398            }
     399        }
     400
     401        @closedir( $mu_plugins );
     402
     403        foreach ( $files_to_move as $file_to_move ) {
     404            rename( $mu_bu_dir . '/' . $file_to_move, WPMU_PLUGIN_DIR . '/' . $file_to_move );
     405        }
     406
     407        if ( is_dir( $mu_bu_dir ) ) {
     408            rmdir( $mu_bu_dir );
     409        }
    82410    }
    83411}
Note: See TracChangeset for help on using the changeset viewer.