Make WordPress Core


Ignore:
Timestamp:
03/01/2021 08:21:23 PM (6 years ago)
Author:
johnbillion
Message:

Build/Test Tools: PHPUnit test improvements:

  • Switch back to running the PHPUnit test suite against the src directory instead of build
  • Increase the reliability of backing up the mu-plugins directory during tests
  • Split the PHPUnit tests for PHP versions below 7.0 in half, allowing them to run in parallel and reduce the overall test run duration on GitHub Actions

Merges [50441-50444] to the 5.7 branch.

Fixes #51734, #51735, #52645

Location:
branches/5.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.7

  • branches/5.7/tests/phpunit/tests/admin/includesPlugin.php

    r49566 r50464  
    55 */
    66class Tests_Admin_includesPlugin extends WP_UnitTestCase {
     7        public static function wpSetUpBeforeClass( $factory ) {
     8                self::_back_up_mu_plugins();
     9        }
     10
     11        public static function wpTearDownAfterClass() {
     12                self::_restore_mu_plugins();
     13        }
     14
    715        function test_get_plugin_data() {
    816                $data = get_plugin_data( DIR_TESTDATA . '/plugins/hello.php' );
    … …  
    370378                        'list_files_test_plugin/subdir/subfile.php',
    371379                );
    372                 $this->assertSame( $expected, $plugin_files );
    373380
    374381                unlink( $sub_dir . '/subfile.php' );
    … …  
    376383                rmdir( $sub_dir );
    377384                rmdir( $plugin_dir );
     385
     386                $this->assertSame( $expected, $plugin_files );
    378387        }
    379388
    … …  
    382391         */
    383392        public function test_get_mu_plugins_when_mu_plugins_exists_but_is_empty() {
    384                 if ( is_dir( WPMU_PLUGIN_DIR ) ) {
    385                         $exists = true;
    386                         $this->_back_up_mu_plugins();
    387                 } else {
    388                         $exists = false;
    389                         mkdir( WPMU_PLUGIN_DIR );
    390                 }
    391 
     393                mkdir( WPMU_PLUGIN_DIR );
     394
     395                $mu_plugins = get_mu_plugins();
     396
     397                rmdir( WPMU_PLUGIN_DIR );
     398
     399                $this->assertSame( array(), $mu_plugins );
     400        }
     401
     402        /**
     403         * @covers ::get_mu_plugins
     404         */
     405        public function test_get_mu_plugins_when_mu_plugins_directory_does_not_exist() {
     406                $this->assertFileNotExists( WPMU_PLUGIN_DIR );
    392407                $this->assertSame( array(), get_mu_plugins() );
    393 
    394                 // Clean up.
    395                 if ( $exists ) {
    396                         $this->_restore_mu_plugins();
    397                 } else {
    398                         rmdir( WPMU_PLUGIN_DIR );
    399                 }
    400408        }
    401409
    … …  
    403411         * @covers ::get_mu_plugins
    404412         */
    405         public function test_get_mu_plugins_when_mu_plugins_directory_does_not_exist() {
    406                 $exists = false;
    407                 if ( is_dir( WPMU_PLUGIN_DIR ) ) {
    408                         $exists = true;
    409                         $this->_back_up_mu_plugins();
    410                         rmdir( WPMU_PLUGIN_DIR );
    411                 }
    412 
    413                 $this->assertSame( array(), get_mu_plugins() );
    414 
    415                 // Clean up.
    416                 if ( $exists ) {
    417                         mkdir( WPMU_PLUGIN_DIR );
    418                         $this->_restore_mu_plugins();
    419                 }
     413        public function test_get_mu_plugins_should_ignore_index_php_containing_silence_is_golden() {
     414                mkdir( WPMU_PLUGIN_DIR );
     415
     416                $this->_create_plugin( '<?php\n//Silence is golden.', 'index.php', WPMU_PLUGIN_DIR );
     417
     418                $mu_plugins = get_mu_plugins();
     419
     420                unlink( WPMU_PLUGIN_DIR . '/index.php' );
     421                rmdir( WPMU_PLUGIN_DIR );
     422
     423                $this->assertSame( array(), $mu_plugins );
    420424        }
    421425
    … …  
    423427         * @covers ::get_mu_plugins
    424428         */
    425         public function test_get_mu_plugins_should_ignore_index_php_containing_silence_is_golden() {
    426                 if ( is_dir( WPMU_PLUGIN_DIR ) ) {
    427                         $exists = true;
    428                         $this->_back_up_mu_plugins();
    429                 } else {
    430                         $exists = false;
    431                         mkdir( WPMU_PLUGIN_DIR );
    432                 }
    433 
    434                 $this->_create_plugin( '<?php\n//Silence is golden.', 'index.php', WPMU_PLUGIN_DIR );
    435                 $this->assertSame( array(), get_mu_plugins() );
     429        public function test_get_mu_plugins_should_not_ignore_index_php_containing_something_other_than_silence_is_golden() {
     430                mkdir( WPMU_PLUGIN_DIR );
     431
     432                $this->_create_plugin( '<?php\n//Silence is not golden.', 'index.php', WPMU_PLUGIN_DIR );
     433                $found = get_mu_plugins();
    436434
    437435                // Clean up.
    438436                unlink( WPMU_PLUGIN_DIR . '/index.php' );
    439                 if ( $exists ) {
    440                         $this->_restore_mu_plugins();
    441                 } else {
    442                         rmdir( WPMU_PLUGIN_DIR );
    443                 }
     437                rmdir( WPMU_PLUGIN_DIR );
     438
     439                $this->assertSame( array( 'index.php' ), array_keys( $found ) );
    444440        }
    445441
    … …  
    447443         * @covers ::get_mu_plugins
    448444         */
    449         public function test_get_mu_plugins_should_not_ignore_index_php_containing_something_other_than_silence_is_golden() {
    450                 if ( is_dir( WPMU_PLUGIN_DIR ) ) {
    451                         $exists = true;
    452                         $this->_back_up_mu_plugins();
    453                 } else {
    454                         $exists = false;
    455                         mkdir( WPMU_PLUGIN_DIR );
    456                 }
    457 
    458                 $this->_create_plugin( '<?php\n//Silence is not golden.', 'index.php', WPMU_PLUGIN_DIR );
    459                 $found = get_mu_plugins();
    460                 $this->assertSame( array( 'index.php' ), array_keys( $found ) );
    461 
    462                 // Clean up.
    463                 unlink( WPMU_PLUGIN_DIR . '/index.php' );
    464                 if ( $exists ) {
    465                         $this->_restore_mu_plugins();
    466                 } else {
    467                         rmdir( WPMU_PLUGIN_DIR );
    468                 }
    469         }
    470 
    471         /**
    472          * @covers ::get_mu_plugins
    473          */
    474445        public function test_get_mu_plugins_should_ignore_files_without_php_extensions() {
    475                 if ( is_dir( WPMU_PLUGIN_DIR ) ) {
    476                         $exists = true;
    477                         $this->_back_up_mu_plugins();
    478                 } else {
    479                         $exists = false;
    480                         mkdir( WPMU_PLUGIN_DIR );
    481                 }
     446                mkdir( WPMU_PLUGIN_DIR );
    482447
    483448                $this->_create_plugin( '<?php\n//Test', 'foo.php', WPMU_PLUGIN_DIR );
    484449                $this->_create_plugin( '<?php\n//Test 2', 'bar.txt', WPMU_PLUGIN_DIR );
    485450                $found = get_mu_plugins();
    486                 $this->assertSame( array( 'foo.php' ), array_keys( $found ) );
    487451
    488452                // Clean up.
    489453                unlink( WPMU_PLUGIN_DIR . '/foo.php' );
    490454                unlink( WPMU_PLUGIN_DIR . '/bar.txt' );
    491                 if ( $exists ) {
    492                         $this->_restore_mu_plugins();
    493                 } else {
    494                         rmdir( WPMU_PLUGIN_DIR );
    495                 }
     455
     456                $this->assertSame( array( 'foo.php' ), array_keys( $found ) );
    496457        }
    497458
    … …  
    652613
    653614        /**
    654          * Move existing mu-plugins to wp-content/mu-plugin/backup.
     615         * Move existing mu-plugins to wp-content/mu-plugin-backup.
    655616         *
    656617         * @since 4.2.0
    … …  
    658619         * @access private
    659620         */
    660         private function _back_up_mu_plugins() {
     621        private static function _back_up_mu_plugins() {
    661622                if ( is_dir( WPMU_PLUGIN_DIR ) ) {
    662623                        $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
    663                         if ( ! is_dir( $mu_bu_dir ) ) {
    664                                 mkdir( $mu_bu_dir );
    665                         }
    666 
    667                         $files_to_move = array();
    668                         $mu_plugins    = opendir( WPMU_PLUGIN_DIR );
    669                         if ( $mu_plugins ) {
    670                                 while ( false !== $plugin = readdir( $mu_plugins ) ) {
    671                                         if ( 0 !== strpos( $plugin, '.' ) ) {
    672                                                 $files_to_move[] = $plugin;
    673                                         }
    674                                 }
    675                         }
    676 
    677                         closedir( $mu_plugins );
    678 
    679                         foreach ( $files_to_move as $file_to_move ) {
    680                                 $f = rename( WPMU_PLUGIN_DIR . '/' . $file_to_move, $mu_bu_dir . '/' . $file_to_move );
    681                         }
     624                        rename( WPMU_PLUGIN_DIR, $mu_bu_dir );
    682625                }
    683626        }
    … …  
    690633         * @access private
    691634         */
    692         private function _restore_mu_plugins() {
    693                 $mu_bu_dir     = WP_CONTENT_DIR . '/mu-plugin-backup';
    694                 $files_to_move = array();
    695                 $mu_plugins    = @opendir( $mu_bu_dir );
    696                 if ( $mu_plugins ) {
    697                         while ( false !== $plugin = readdir( $mu_plugins ) ) {
    698                                 if ( 0 !== strpos( $plugin, '.' ) ) {
    699                                         $files_to_move[] = $plugin;
    700                                 }
    701                         }
    702                 }
    703 
    704                 closedir( $mu_plugins );
    705 
    706                 foreach ( $files_to_move as $file_to_move ) {
    707                         rename( $mu_bu_dir . '/' . $file_to_move, WPMU_PLUGIN_DIR . '/' . $file_to_move );
     635        private static function _restore_mu_plugins() {
     636                $mu_bu_dir = WP_CONTENT_DIR . '/mu-plugin-backup';
     637
     638                if ( is_dir( WPMU_PLUGIN_DIR ) ) {
     639                        rmdir( WPMU_PLUGIN_DIR );
    708640                }
    709641
    710642                if ( is_dir( $mu_bu_dir ) ) {
    711                         rmdir( $mu_bu_dir );
     643                        rename( $mu_bu_dir, WPMU_PLUGIN_DIR );
    712644                }
    713645        }
Note: See TracChangeset for help on using the changeset viewer.