Make WordPress Core


Ignore:
Timestamp:
04/09/2025 01:29:39 PM (5 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Use the ms-required group where appropriate.

This replaces the if ( is_multisite() ) conditional wrapping entire test classes with the ms-required group for more consistency across the test suite.

Follow-up to [40520].

See #63167.

File:
1 edited

Legend:

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

    r54304 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * Tests specific to the directory size caching in multisite.
    7      *
    8      * @ticket 19879
    9      * @group multisite
    10      */
    11     class Tests_Multisite_CleanDirsizeCache extends WP_UnitTestCase {
    12 
    13         /**
    14          * Test whether dirsize_cache values are used correctly with a more complex dirsize cache mock.
    15          *
    16          * @ticket 19879
    17          */
    18         public function test_get_dirsize_cache_in_recurse_dirsize_mock() {
    19             $blog_id = self::factory()->blog->create();
    20             switch_to_blog( $blog_id );
    21 
    22             /*
    23              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    24              * or if the `src` directory already contains a directory with site content, then the initial
    25              * expectation will be polluted. We create sites until an empty one is available.
    26              */
    27             while ( 0 !== get_space_used() ) {
    28                 restore_current_blog();
    29                 $blog_id = self::factory()->blog->create();
    30                 switch_to_blog( $blog_id );
    31             }
    32 
    33             // Clear the dirsize cache.
    34             delete_transient( 'dirsize_cache' );
    35 
    36             // Set the dirsize cache to our mock.
    37             set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
    38 
    39             $upload_dir = wp_upload_dir();
    40 
    41             // Check recurse_dirsize() against the mock. The cache should match.
    42             $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
    43             $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) );
    44             $this->assertSame( 2, recurse_dirsize( $upload_dir['basedir'] . '/2' ) );
    45             $this->assertSame( 11, recurse_dirsize( $upload_dir['basedir'] . '/1/1' ) );
    46             $this->assertSame( 12, recurse_dirsize( $upload_dir['basedir'] . '/1/2' ) );
    47             $this->assertSame( 13, recurse_dirsize( $upload_dir['basedir'] . '/1/3' ) );
    48             $this->assertSame( 1, recurse_dirsize( $upload_dir['basedir'] . '/1' ) );
    49             $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) );
    50 
    51             // No cache match, upload directory should be empty and return 0.
    52             $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) );
    53 
    54             // No cache match on non existing directory should return false.
    55             $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
    56 
    57             // Cleanup.
    58             $this->remove_added_uploads();
    59             restore_current_blog();
    60         }
    61 
    62         /**
    63          * Test whether the dirsize_cache invalidation works given a file path as input.
    64          *
    65          * @ticket 19879
    66          */
    67         public function test_clean_dirsize_cache_file_input_mock() {
    68             $blog_id = self::factory()->blog->create();
    69             switch_to_blog( $blog_id );
    70 
    71             /*
    72              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    73              * or if the `src` directory already contains a directory with site content, then the initial
    74              * expectation will be polluted. We create sites until an empty one is available.
    75              */
    76             while ( 0 !== get_space_used() ) {
    77                 restore_current_blog();
    78                 $blog_id = self::factory()->blog->create();
    79                 switch_to_blog( $blog_id );
    80             }
    81 
    82             $upload_dir       = wp_upload_dir();
    83             $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
    84 
    85             // Clear the dirsize cache.
    86             delete_transient( 'dirsize_cache' );
    87 
    88             // Set the dirsize cache to our mock.
    89             set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
    90 
    91             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    92             $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    93             $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    94 
    95             // Invalidation should also respect the directory tree up.
    96             // Should work fine with path to directory OR file.
    97             clean_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
    98 
    99             $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    100             $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    101 
    102             // Other cache paths should not be invalidated.
    103             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    104 
    105             // Cleanup.
    106             $this->remove_added_uploads();
    107             restore_current_blog();
    108         }
    109 
    110         /**
    111          * Test whether the dirsize_cache invalidation works given a directory path as input.
    112          *
    113          * @ticket 19879
    114          */
    115         public function test_clean_dirsize_cache_folder_input_mock() {
    116             $blog_id = self::factory()->blog->create();
    117             switch_to_blog( $blog_id );
    118 
    119             /*
    120              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    121              * or if the `src` directory already contains a directory with site content, then the initial
    122              * expectation will be polluted. We create sites until an empty one is available.
    123              */
    124             while ( 0 !== get_space_used() ) {
    125                 restore_current_blog();
    126                 $blog_id = self::factory()->blog->create();
    127                 switch_to_blog( $blog_id );
    128             }
    129 
    130             $upload_dir       = wp_upload_dir();
    131             $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
    132 
    133             // Clear the dirsize cache.
    134             delete_transient( 'dirsize_cache' );
    135 
    136             // Set the dirsize cache to our mock.
    137             set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
    138 
    139             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    140             $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    141             $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    142 
    143             // Invalidation should also respect the directory tree up.
    144             // Should work fine with path to directory OR file.
    145             clean_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
    146 
    147             $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
    148             $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
    149 
    150             // Other cache paths should not be invalidated.
    151             $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
    152 
    153             // Cleanup.
    154             $this->remove_added_uploads();
    155             restore_current_blog();
    156         }
    157 
    158         /**
    159          * Test whether dirsize_cache values are used correctly with a simple real upload.
    160          *
    161          * @ticket 19879
    162          */
    163         public function test_get_dirsize_cache_in_recurse_dirsize_upload() {
    164             $blog_id = self::factory()->blog->create();
    165             switch_to_blog( $blog_id );
    166 
    167             /*
    168              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    169              * or if the `src` directory already contains a directory with site content, then the initial
    170              * expectation will be polluted. We create sites until an empty one is available.
    171              */
    172             while ( 0 !== get_space_used() ) {
    173                 restore_current_blog();
    174                 $blog_id = self::factory()->blog->create();
    175                 switch_to_blog( $blog_id );
    176             }
    177 
    178             // Clear the dirsize cache.
    179             delete_transient( 'dirsize_cache' );
    180 
    181             $upload_dir = wp_upload_dir();
    182 
    183             $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) );
    184 
    185             // Upload a file to the new site using wp_upload_bits().
    186             $filename = __FUNCTION__ . '.jpg';
    187             $contents = __FUNCTION__ . '_contents';
    188             $file     = wp_upload_bits( $filename, null, $contents );
    189 
    190             $calc_size = recurse_dirsize( $upload_dir['path'] );
    191             $size      = filesize( $file['file'] );
    192             $this->assertSame( $size, $calc_size );
    193 
    194             // `dirsize_cache` should now be filled after upload and recurse_dirsize() call.
    195             $cache_path = untrailingslashit( $upload_dir['path'] );
    196             $this->assertIsArray( get_transient( 'dirsize_cache' ) );
    197             $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );
    198 
    199             // Cleanup.
    200             $this->remove_added_uploads();
    201             restore_current_blog();
    202         }
    203 
    204         /**
    205          * Test whether the filter to calculate space for an existing directory works as expected.
    206          *
    207          * @ticket 19879
    208          */
    209         public function test_pre_recurse_dirsize_filter() {
    210             add_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
    211 
    212             $upload_dir = wp_upload_dir();
    213             $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) );
    214 
    215             remove_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
    216         }
    217 
    218         public function filter_pre_recurse_dirsize() {
    219             return 1042;
    220         }
    221 
    222         private function get_mock_dirsize_cache_for_site( $site_id ) {
    223             $prefix = wp_upload_dir()['basedir'];
    224 
    225             return array(
    226                 "$prefix/2/2"              => 22,
    227                 "$prefix/2/1"              => 21,
    228                 "$prefix/2"                => 2,
    229                 "$prefix/1/3"              => 13,
    230                 "$prefix/1/2"              => 12,
    231                 "$prefix/1/1"              => 11,
    232                 "$prefix/1"                => 1,
    233                 "$prefix/custom_directory" => 42,
    234             );
    235         }
    236 
    237         /*
    238          * Test that 5.6+ gracefully handles the old 5.5 transient structure.
    239          *
    240          * @ticket 51913
    241          */
    242         public function test_5_5_transient_structure_compat() {
    243             $blog_id = self::factory()->blog->create();
    244             switch_to_blog( $blog_id );
    245 
    246             /*
    247              * Our comparison of space relies on an initial value of 0. If a previous test has failed
    248              * or if the `src` directory already contains a directory with site content, then the initial
    249              * expectation will be polluted. We create sites until an empty one is available.
    250              */
    251             while ( 0 !== get_space_used() ) {
    252                 restore_current_blog();
    253                 $blog_id = self::factory()->blog->create();
    254                 switch_to_blog( $blog_id );
    255             }
    256 
    257             // Clear the dirsize cache.
    258             delete_transient( 'dirsize_cache' );
    259 
    260             // Set the dirsize cache to our mock.
    261             set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
    262 
    263             $upload_dir = wp_upload_dir();
    264 
    265             /*
    266              * The cached size should be ignored, because it's in the old format. The function
    267              * will try to fetch a live value, but in this case the folder doesn't actually
    268              * exist on disk, so the function should fail.
    269              */
    270             $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
    271 
    272             /*
    273              * Now that it's confirmed that old cached values aren't being returned, create the
    274              * folder on disk, so that the the rest of the function can be tested.
    275              */
    276             wp_mkdir_p( $upload_dir['basedir'] . '/2/1' );
    277             $filename = $upload_dir['basedir'] . '/2/1/this-needs-to-exist.txt';
    278             file_put_contents( $filename, 'this file is 21 bytes' );
    279 
    280             // Clear the dirsize cache.
    281             delete_transient( 'dirsize_cache' );
    282 
    283             // Set the dirsize cache to our mock.
    284             set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
    285 
    286             /*
    287              * Now that the folder exists, the old cached value should be overwritten
    288              * with the size, using the current format.
    289              */
    290             $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
    291             $this->assertSame( 21, get_transient( 'dirsize_cache' )[ $upload_dir['basedir'] . '/2/1' ] );
    292 
    293             // No cache match on non existing directory should return false.
    294             $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
    295 
    296             // Cleanup.
    297             $this->remove_added_uploads();
    298             rmdir( $upload_dir['basedir'] . '/2/1' );
    299 
    300             restore_current_blog();
    301         }
    302 
    303         private function get_mock_5_5_dirsize_cache( $site_id ) {
    304             $prefix = untrailingslashit( wp_upload_dir()['basedir'] );
    305 
    306             return array(
    307                 "$prefix/2/2"              => array( 'size' => 22 ),
    308                 "$prefix/2/1"              => array( 'size' => 21 ),
    309                 "$prefix/2"                => array( 'size' => 2 ),
    310                 "$prefix/1/3"              => array( 'size' => 13 ),
    311                 "$prefix/1/2"              => array( 'size' => 12 ),
    312                 "$prefix/1/1"              => array( 'size' => 11 ),
    313                 "$prefix/1"                => array( 'size' => 1 ),
    314                 "$prefix/custom_directory" => array( 'size' => 42 ),
    315             );
    316         }
    317     }
    318 
    319 endif;
     3/**
     4 * Tests specific to the directory size caching in multisite.
     5 *
     6 * @ticket 19879
     7 * @group ms-required
     8 * @group multisite
     9 */
     10class Tests_Multisite_CleanDirsizeCache extends WP_UnitTestCase {
     11
     12    /**
     13     * Test whether dirsize_cache values are used correctly with a more complex dirsize cache mock.
     14     *
     15     * @ticket 19879
     16     */
     17    public function test_get_dirsize_cache_in_recurse_dirsize_mock() {
     18        $blog_id = self::factory()->blog->create();
     19        switch_to_blog( $blog_id );
     20
     21        /*
     22         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     23         * or if the `src` directory already contains a directory with site content, then the initial
     24         * expectation will be polluted. We create sites until an empty one is available.
     25         */
     26        while ( 0 !== get_space_used() ) {
     27            restore_current_blog();
     28            $blog_id = self::factory()->blog->create();
     29            switch_to_blog( $blog_id );
     30        }
     31
     32        // Clear the dirsize cache.
     33        delete_transient( 'dirsize_cache' );
     34
     35        // Set the dirsize cache to our mock.
     36        set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
     37
     38        $upload_dir = wp_upload_dir();
     39
     40        // Check recurse_dirsize() against the mock. The cache should match.
     41        $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
     42        $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) );
     43        $this->assertSame( 2, recurse_dirsize( $upload_dir['basedir'] . '/2' ) );
     44        $this->assertSame( 11, recurse_dirsize( $upload_dir['basedir'] . '/1/1' ) );
     45        $this->assertSame( 12, recurse_dirsize( $upload_dir['basedir'] . '/1/2' ) );
     46        $this->assertSame( 13, recurse_dirsize( $upload_dir['basedir'] . '/1/3' ) );
     47        $this->assertSame( 1, recurse_dirsize( $upload_dir['basedir'] . '/1' ) );
     48        $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) );
     49
     50        // No cache match, upload directory should be empty and return 0.
     51        $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) );
     52
     53        // No cache match on non existing directory should return false.
     54        $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
     55
     56        // Cleanup.
     57        $this->remove_added_uploads();
     58        restore_current_blog();
     59    }
     60
     61    /**
     62     * Test whether the dirsize_cache invalidation works given a file path as input.
     63     *
     64     * @ticket 19879
     65     */
     66    public function test_clean_dirsize_cache_file_input_mock() {
     67        $blog_id = self::factory()->blog->create();
     68        switch_to_blog( $blog_id );
     69
     70        /*
     71         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     72         * or if the `src` directory already contains a directory with site content, then the initial
     73         * expectation will be polluted. We create sites until an empty one is available.
     74         */
     75        while ( 0 !== get_space_used() ) {
     76            restore_current_blog();
     77            $blog_id = self::factory()->blog->create();
     78            switch_to_blog( $blog_id );
     79        }
     80
     81        $upload_dir       = wp_upload_dir();
     82        $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
     83
     84        // Clear the dirsize cache.
     85        delete_transient( 'dirsize_cache' );
     86
     87        // Set the dirsize cache to our mock.
     88        set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
     89
     90        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     91        $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     92        $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     93
     94        // Invalidation should also respect the directory tree up.
     95        // Should work fine with path to directory OR file.
     96        clean_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
     97
     98        $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     99        $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     100
     101        // Other cache paths should not be invalidated.
     102        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     103
     104        // Cleanup.
     105        $this->remove_added_uploads();
     106        restore_current_blog();
     107    }
     108
     109    /**
     110     * Test whether the dirsize_cache invalidation works given a directory path as input.
     111     *
     112     * @ticket 19879
     113     */
     114    public function test_clean_dirsize_cache_folder_input_mock() {
     115        $blog_id = self::factory()->blog->create();
     116        switch_to_blog( $blog_id );
     117
     118        /*
     119         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     120         * or if the `src` directory already contains a directory with site content, then the initial
     121         * expectation will be polluted. We create sites until an empty one is available.
     122         */
     123        while ( 0 !== get_space_used() ) {
     124            restore_current_blog();
     125            $blog_id = self::factory()->blog->create();
     126            switch_to_blog( $blog_id );
     127        }
     128
     129        $upload_dir       = wp_upload_dir();
     130        $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] );
     131
     132        // Clear the dirsize cache.
     133        delete_transient( 'dirsize_cache' );
     134
     135        // Set the dirsize cache to our mock.
     136        set_transient( 'dirsize_cache', $this->get_mock_dirsize_cache_for_site( $blog_id ) );
     137
     138        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     139        $this->assertArrayHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     140        $this->assertArrayHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     141
     142        // Invalidation should also respect the directory tree up.
     143        // Should work fine with path to directory OR file.
     144        clean_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
     145
     146        $this->assertArrayNotHasKey( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) );
     147        $this->assertArrayNotHasKey( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) );
     148
     149        // Other cache paths should not be invalidated.
     150        $this->assertArrayHasKey( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) );
     151
     152        // Cleanup.
     153        $this->remove_added_uploads();
     154        restore_current_blog();
     155    }
     156
     157    /**
     158     * Test whether dirsize_cache values are used correctly with a simple real upload.
     159     *
     160     * @ticket 19879
     161     */
     162    public function test_get_dirsize_cache_in_recurse_dirsize_upload() {
     163        $blog_id = self::factory()->blog->create();
     164        switch_to_blog( $blog_id );
     165
     166        /*
     167         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     168         * or if the `src` directory already contains a directory with site content, then the initial
     169         * expectation will be polluted. We create sites until an empty one is available.
     170         */
     171        while ( 0 !== get_space_used() ) {
     172            restore_current_blog();
     173            $blog_id = self::factory()->blog->create();
     174            switch_to_blog( $blog_id );
     175        }
     176
     177        // Clear the dirsize cache.
     178        delete_transient( 'dirsize_cache' );
     179
     180        $upload_dir = wp_upload_dir();
     181
     182        $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) );
     183
     184        // Upload a file to the new site using wp_upload_bits().
     185        $filename = __FUNCTION__ . '.jpg';
     186        $contents = __FUNCTION__ . '_contents';
     187        $file     = wp_upload_bits( $filename, null, $contents );
     188
     189        $calc_size = recurse_dirsize( $upload_dir['path'] );
     190        $size      = filesize( $file['file'] );
     191        $this->assertSame( $size, $calc_size );
     192
     193        // `dirsize_cache` should now be filled after upload and recurse_dirsize() call.
     194        $cache_path = untrailingslashit( $upload_dir['path'] );
     195        $this->assertIsArray( get_transient( 'dirsize_cache' ) );
     196        $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );
     197
     198        // Cleanup.
     199        $this->remove_added_uploads();
     200        restore_current_blog();
     201    }
     202
     203    /**
     204     * Test whether the filter to calculate space for an existing directory works as expected.
     205     *
     206     * @ticket 19879
     207     */
     208    public function test_pre_recurse_dirsize_filter() {
     209        add_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
     210
     211        $upload_dir = wp_upload_dir();
     212        $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) );
     213
     214        remove_filter( 'pre_recurse_dirsize', array( $this, 'filter_pre_recurse_dirsize' ) );
     215    }
     216
     217    public function filter_pre_recurse_dirsize() {
     218        return 1042;
     219    }
     220
     221    private function get_mock_dirsize_cache_for_site( $site_id ) {
     222        $prefix = wp_upload_dir()['basedir'];
     223
     224        return array(
     225            "$prefix/2/2"              => 22,
     226            "$prefix/2/1"              => 21,
     227            "$prefix/2"                => 2,
     228            "$prefix/1/3"              => 13,
     229            "$prefix/1/2"              => 12,
     230            "$prefix/1/1"              => 11,
     231            "$prefix/1"                => 1,
     232            "$prefix/custom_directory" => 42,
     233        );
     234    }
     235
     236    /*
     237     * Test that 5.6+ gracefully handles the old 5.5 transient structure.
     238     *
     239     * @ticket 51913
     240     */
     241    public function test_5_5_transient_structure_compat() {
     242        $blog_id = self::factory()->blog->create();
     243        switch_to_blog( $blog_id );
     244
     245        /*
     246         * Our comparison of space relies on an initial value of 0. If a previous test has failed
     247         * or if the `src` directory already contains a directory with site content, then the initial
     248         * expectation will be polluted. We create sites until an empty one is available.
     249         */
     250        while ( 0 !== get_space_used() ) {
     251            restore_current_blog();
     252            $blog_id = self::factory()->blog->create();
     253            switch_to_blog( $blog_id );
     254        }
     255
     256        // Clear the dirsize cache.
     257        delete_transient( 'dirsize_cache' );
     258
     259        // Set the dirsize cache to our mock.
     260        set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
     261
     262        $upload_dir = wp_upload_dir();
     263
     264        /*
     265         * The cached size should be ignored, because it's in the old format. The function
     266         * will try to fetch a live value, but in this case the folder doesn't actually
     267         * exist on disk, so the function should fail.
     268         */
     269        $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
     270
     271        /*
     272         * Now that it's confirmed that old cached values aren't being returned, create the
     273         * folder on disk, so that the the rest of the function can be tested.
     274         */
     275        wp_mkdir_p( $upload_dir['basedir'] . '/2/1' );
     276        $filename = $upload_dir['basedir'] . '/2/1/this-needs-to-exist.txt';
     277        file_put_contents( $filename, 'this file is 21 bytes' );
     278
     279        // Clear the dirsize cache.
     280        delete_transient( 'dirsize_cache' );
     281
     282        // Set the dirsize cache to our mock.
     283        set_transient( 'dirsize_cache', $this->get_mock_5_5_dirsize_cache( $blog_id ) );
     284
     285        /*
     286         * Now that the folder exists, the old cached value should be overwritten
     287         * with the size, using the current format.
     288         */
     289        $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
     290        $this->assertSame( 21, get_transient( 'dirsize_cache' )[ $upload_dir['basedir'] . '/2/1' ] );
     291
     292        // No cache match on non existing directory should return false.
     293        $this->assertFalse( recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
     294
     295        // Cleanup.
     296        $this->remove_added_uploads();
     297        rmdir( $upload_dir['basedir'] . '/2/1' );
     298
     299        restore_current_blog();
     300    }
     301
     302    private function get_mock_5_5_dirsize_cache( $site_id ) {
     303        $prefix = untrailingslashit( wp_upload_dir()['basedir'] );
     304
     305        return array(
     306            "$prefix/2/2"              => array( 'size' => 22 ),
     307            "$prefix/2/1"              => array( 'size' => 21 ),
     308            "$prefix/2"                => array( 'size' => 2 ),
     309            "$prefix/1/3"              => array( 'size' => 13 ),
     310            "$prefix/1/2"              => array( 'size' => 12 ),
     311            "$prefix/1/1"              => array( 'size' => 11 ),
     312            "$prefix/1"                => array( 'size' => 1 ),
     313            "$prefix/custom_directory" => array( 'size' => 42 ),
     314        );
     315    }
     316}
Note: See TracChangeset for help on using the changeset viewer.