Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/getSpaceUsed.php

    r38762 r42343  
    33if ( is_multisite() ) :
    44
    5 /**
    6  * @group multisite
    7  * @covers ::get_space_used
    8  */
    9 class Tests_Multisite_Get_Space_Used extends WP_UnitTestCase {
    10     protected $suppress = false;
     5    /**
     6    * @group multisite
     7    * @covers ::get_space_used
     8    */
     9    class Tests_Multisite_Get_Space_Used extends WP_UnitTestCase {
     10        protected $suppress = false;
    1111
    12     function setUp() {
    13         global $wpdb;
    14         parent::setUp();
    15         $this->suppress = $wpdb->suppress_errors();
    16     }
     12        function setUp() {
     13            global $wpdb;
     14            parent::setUp();
     15            $this->suppress = $wpdb->suppress_errors();
     16        }
    1717
    18     function tearDown() {
    19         global $wpdb;
    20         $wpdb->suppress_errors( $this->suppress );
    21         parent::tearDown();
    22     }
     18        function tearDown() {
     19            global $wpdb;
     20            $wpdb->suppress_errors( $this->suppress );
     21            parent::tearDown();
     22        }
    2323
    24     function test_get_space_used_switched_site() {
    25         $blog_id = self::factory()->blog->create();
    26         switch_to_blog( $blog_id );
    27 
    28         // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
    29         // src directory already contains a content directory with site content, then the initial expectation
    30         // will be polluted. We create sites until an empty one is available.
    31         while ( 0 != get_space_used() ) {
    32             restore_current_blog();
     24        function test_get_space_used_switched_site() {
    3325            $blog_id = self::factory()->blog->create();
    3426            switch_to_blog( $blog_id );
     27
     28            // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     29            // src directory already contains a content directory with site content, then the initial expectation
     30            // will be polluted. We create sites until an empty one is available.
     31            while ( 0 != get_space_used() ) {
     32                restore_current_blog();
     33                $blog_id = self::factory()->blog->create();
     34                switch_to_blog( $blog_id );
     35            }
     36
     37            // Upload a file to the new site.
     38            $filename = __FUNCTION__ . '.jpg';
     39            $contents = __FUNCTION__ . '_contents';
     40            $file     = wp_upload_bits( $filename, null, $contents );
     41
     42            // get_space_used() is measures in MB, get the size of the new file in MB.
     43            $size = filesize( $file['file'] ) / 1024 / 1024;
     44
     45            delete_transient( 'dirsize_cache' );
     46
     47            $this->assertEquals( $size, get_space_used() );
     48            $upload_dir = wp_upload_dir();
     49            $this->remove_added_uploads();
     50            $this->delete_folders( $upload_dir['basedir'] );
     51            restore_current_blog();
    3552        }
    3653
    37         // Upload a file to the new site.
    38         $filename = __FUNCTION__ . '.jpg';
    39         $contents = __FUNCTION__ . '_contents';
    40         $file = wp_upload_bits( $filename, null, $contents );
     54        /**
     55         * Directories of sub sites on a network should not count against the same spaced used total for
     56         * the main site.
     57         */
     58        function test_get_space_used_main_site() {
     59            $space_used = get_space_used();
    4160
    42         // get_space_used() is measures in MB, get the size of the new file in MB.
    43         $size = filesize( $file['file'] ) / 1024 / 1024;
    44 
    45         delete_transient( 'dirsize_cache' );
    46 
    47         $this->assertEquals( $size, get_space_used() );
    48         $upload_dir = wp_upload_dir();
    49         $this->remove_added_uploads();
    50         $this->delete_folders( $upload_dir['basedir'] );
    51         restore_current_blog();
    52     }
    53 
    54     /**
    55      * Directories of sub sites on a network should not count against the same spaced used total for
    56      * the main site.
    57      */
    58     function test_get_space_used_main_site() {
    59         $space_used = get_space_used();
    60 
    61         $blog_id = self::factory()->blog->create();
    62         switch_to_blog( $blog_id );
    63 
    64         // We don't rely on an initial value of 0 for space used, but should have a clean space available
    65         // so that we can remove any uploaded files and directories without concern of a conflict with
    66         // existing content directories in src.
    67         while ( 0 != get_space_used() ) {
    68             restore_current_blog();
    6961            $blog_id = self::factory()->blog->create();
    7062            switch_to_blog( $blog_id );
     63
     64            // We don't rely on an initial value of 0 for space used, but should have a clean space available
     65            // so that we can remove any uploaded files and directories without concern of a conflict with
     66            // existing content directories in src.
     67            while ( 0 != get_space_used() ) {
     68                restore_current_blog();
     69                $blog_id = self::factory()->blog->create();
     70                switch_to_blog( $blog_id );
     71            }
     72
     73            // Upload a file to the new site.
     74            $filename = __FUNCTION__ . '.jpg';
     75            $contents = __FUNCTION__ . '_contents';
     76            wp_upload_bits( $filename, null, $contents );
     77
     78            restore_current_blog();
     79
     80            delete_transient( 'dirsize_cache' );
     81
     82            $this->assertEquals( $space_used, get_space_used() );
     83
     84            // Switch back to the new site to remove the uploaded file.
     85            switch_to_blog( $blog_id );
     86            $upload_dir = wp_upload_dir();
     87            $this->remove_added_uploads();
     88            $this->delete_folders( $upload_dir['basedir'] );
     89            restore_current_blog();
    7190        }
    7291
    73         // Upload a file to the new site.
    74         $filename = __FUNCTION__ . '.jpg';
    75         $contents = __FUNCTION__ . '_contents';
    76         wp_upload_bits( $filename, null, $contents );
     92        function test_get_space_used_pre_get_spaced_used_filter() {
     93            add_filter( 'pre_get_space_used', array( $this, '_filter_space_used' ) );
    7794
    78         restore_current_blog();
     95            $this->assertEquals( 300, get_space_used() );
    7996
    80         delete_transient( 'dirsize_cache' );
     97            remove_filter( 'pre_get_space_used', array( $this, '_filter_space_used' ) );
     98        }
    8199
    82         $this->assertEquals( $space_used, get_space_used() );
    83 
    84         // Switch back to the new site to remove the uploaded file.
    85         switch_to_blog( $blog_id );
    86         $upload_dir = wp_upload_dir();
    87         $this->remove_added_uploads();
    88         $this->delete_folders( $upload_dir['basedir'] );
    89         restore_current_blog();
     100        function _filter_space_used() {
     101            return 300;
     102        }
    90103    }
    91 
    92     function test_get_space_used_pre_get_spaced_used_filter() {
    93         add_filter( 'pre_get_space_used', array( $this, '_filter_space_used' ) );
    94 
    95         $this->assertEquals( 300, get_space_used() );
    96 
    97         remove_filter( 'pre_get_space_used', array( $this, '_filter_space_used' ) );
    98     }
    99 
    100     function _filter_space_used() {
    101         return 300;
    102     }
    103 }
    104104endif;
Note: See TracChangeset for help on using the changeset viewer.