Make WordPress Core

Ticket #19879: 19879_Unit_Tests_comment-fix.patch

File 19879_Unit_Tests_comment-fix.patch, 9.6 KB (added by janthiel, 5 years ago)

Updated function comment from copied test case to what the function really does. Missed that in the first place.

  • tests/phpunit/tests/multisite/dirsizeCache.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3if ( is_multisite() ) :
     4
     5        /**
     6         * Tests specific to the dirsize caching in multisites
     7         *
     8         * @group multisite
     9         */
     10        class Tests_Multisite_Dirsize_Cache extends WP_UnitTestCase {
     11                protected $suppress = false;
     12
     13                function setUp() {
     14                        global $wpdb;
     15                        parent::setUp();
     16                        $this->suppress = $wpdb->suppress_errors();
     17                }
     18
     19                function tearDown() {
     20                        global $wpdb;
     21                        $wpdb->suppress_errors( $this->suppress );
     22                        parent::tearDown();
     23                }
     24
     25                /*
     26                 * Test whether the values from the dirsize_cache will be used correctly using a more complex dirsize cache mock
     27                 */
     28                function test_get_dirsize_cache_in_recurse_dirsize_mock() {
     29                        $blog_id = self::factory()->blog->create();
     30                        switch_to_blog( $blog_id );
     31
     32                        // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     33                        // src directory already contains a content directory with site content, then the initial expectation
     34                        // will be polluted. We create sites until an empty one is available.
     35                        while ( 0 !== get_space_used() ) {
     36                                restore_current_blog();
     37                                $blog_id = self::factory()->blog->create();
     38                                switch_to_blog( $blog_id );
     39                        }
     40
     41                        // Clear the dirsize_cache
     42                        delete_transient( 'dirsize_cache' );
     43
     44                        // Set the dirsize cache to our mock
     45                        set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
     46
     47                        $upload_dir = wp_upload_dir();
     48
     49                        // Check recurse_dirsize against the mock. The cache should match
     50                        $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
     51                        $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) );
     52                        $this->assertSame( 2, recurse_dirsize( $upload_dir['basedir'] . '/2' ) );
     53                        $this->assertSame( 11, recurse_dirsize( $upload_dir['basedir'] . '/1/1' ) );
     54                        $this->assertSame( 12, recurse_dirsize( $upload_dir['basedir'] . '/1/2' ) );
     55                        $this->assertSame( 13, recurse_dirsize( $upload_dir['basedir'] . '/1/3' ) );
     56                        $this->assertSame( 1, recurse_dirsize( $upload_dir['basedir'] . '/1' ) );
     57                        $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) );
     58
     59                        // No cache match, upload folder should be empty and return 0
     60                        $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) );
     61
     62                        // No cache match on non existing folder should return false
     63                        $this->assertSame( false, recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
     64
     65                        // Cleanup
     66                        $this->remove_added_uploads();
     67                        restore_current_blog();
     68                }
     69
     70                /*
     71                 * Test whether the invalidation of the dirsize_cache works
     72                 * Given a file path as input
     73                 */
     74                function test_invalidate_dirsize_cache_file_input_mock() {
     75                        $blog_id = self::factory()->blog->create();
     76                        switch_to_blog( $blog_id );
     77
     78                        // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     79                        // src directory already contains a content directory with site content, then the initial expectation
     80                        // will be polluted. We create sites until an empty one is available.
     81                        while ( 0 !== get_space_used() ) {
     82                                restore_current_blog();
     83                                $blog_id = self::factory()->blog->create();
     84                                switch_to_blog( $blog_id );
     85                        }
     86
     87                        $upload_dir       = wp_upload_dir();
     88                        $cache_key_prefix = normalize_dirsize_cache_path( $upload_dir['basedir'] );
     89
     90                        // Clear the dirsize_cache
     91                        delete_transient( 'dirsize_cache' );
     92
     93                        // Set the dirsize cache to our mock
     94                        set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
     95
     96                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
     97                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
     98                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
     99
     100                        // Invalidation should also respect the directory tree up
     101                        // Should work fine with path to folder OR file
     102                        invalidate_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
     103
     104                        $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
     105                        $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
     106
     107                        // Other cache paths should not be invalidated
     108                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
     109
     110                        // Cleanup
     111                        $this->remove_added_uploads();
     112                        restore_current_blog();
     113                }
     114
     115                /*
     116                 * Test whether the invalidation of the dirsize_cache works
     117                 * Given a folder path as input
     118                 */
     119                function test_invalidate_dirsize_cache_folder_input_mock() {
     120                        $blog_id = self::factory()->blog->create();
     121                        switch_to_blog( $blog_id );
     122
     123                        // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     124                        // src directory already contains a content directory with site content, then the initial expectation
     125                        // will be polluted. We create sites until an empty one is available.
     126                        while ( 0 !== get_space_used() ) {
     127                                restore_current_blog();
     128                                $blog_id = self::factory()->blog->create();
     129                                switch_to_blog( $blog_id );
     130                        }
     131
     132                        $upload_dir       = wp_upload_dir();
     133                        $cache_key_prefix = normalize_dirsize_cache_path( $upload_dir['basedir'] );
     134
     135                        // Clear the dirsize_cache
     136                        delete_transient( 'dirsize_cache' );
     137
     138                        // Set the dirsize cache to our mock
     139                        set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
     140
     141                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
     142                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
     143                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
     144
     145                        // Invalidation should also respect the directory tree up
     146                        // Should work fine with path to folder OR file
     147                        invalidate_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
     148
     149                        $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
     150                        $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
     151
     152                        // Other cache paths should not be invalidated
     153                        $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
     154
     155                        // Cleanup
     156                        $this->remove_added_uploads();
     157                        restore_current_blog();
     158                }
     159
     160                /**
     161                 * Test whether the values from the dirsize_cache will be used correctly using a simple real upload
     162                 */
     163                function test_get_dirsize_cache_in_recurse_dirsize_upload() {
     164                        $blog_id = self::factory()->blog->create();
     165                        switch_to_blog( $blog_id );
     166
     167                        // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     168                        // src directory already contains a content directory with site content, then the initial expectation
     169                        // will be polluted. We create sites until an empty one is available.
     170                        while ( 0 !== get_space_used() ) {
     171                                restore_current_blog();
     172                                $blog_id = self::factory()->blog->create();
     173                                switch_to_blog( $blog_id );
     174                        }
     175
     176                        // Clear the dirsize_cache
     177                        delete_transient( 'dirsize_cache' );
     178
     179                        $upload_dir = wp_upload_dir();
     180
     181                        $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) );
     182
     183                        // Upload a file to the new site using wp_upload_bits.
     184                        $filename = __FUNCTION__ . '.jpg';
     185                        $contents = __FUNCTION__ . '_contents';
     186                        $file     = wp_upload_bits( $filename, null, $contents );
     187
     188                        $calc_size = recurse_dirsize( $upload_dir['path'] );
     189                        $size      = filesize( $file['file'] );
     190                        $this->assertSame( $size, $calc_size );
     191
     192                        // dirsize_cache should now be filled after upload and recurse_dirsize call
     193                        $cache_path = normalize_dirsize_cache_path( $upload_dir['path'] );
     194                        $this->assertSame( true, is_array( get_transient( 'dirsize_cache' ) ) );
     195                        $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );
     196
     197                        // Cleanup
     198                        $this->remove_added_uploads();
     199                        restore_current_blog();
     200                }
     201
     202                /*
     203                 * Test whether the filter to calculate space for an existing directory works as expected
     204                 */
     205                function test_recurse_dirsize_calculate_current_dirsize_filter() {
     206                        add_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) );
     207
     208                        $upload_dir = wp_upload_dir();
     209                        $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) );
     210
     211                        remove_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) );
     212                }
     213
     214                function _filter_calculate_current_dirsize() {
     215                        return 1042;
     216                }
     217
     218                function _get_mock_dirsize_cache_for_site( $site_id ) {
     219                        return
     220                                [
     221                                        "wp-content/uploads/sites/$site_id/2/2"              => 22,
     222                                        "wp-content/uploads/sites/$site_id/2/1"              => 21,
     223                                        "wp-content/uploads/sites/$site_id/2"                => 2,
     224                                        "wp-content/uploads/sites/$site_id/1/3"              => 13,
     225                                        "wp-content/uploads/sites/$site_id/1/2"              => 12,
     226                                        "wp-content/uploads/sites/$site_id/1/1"              => 11,
     227                                        "wp-content/uploads/sites/$site_id/1"                => 1,
     228                                        "wp-content/uploads/sites/$site_id/custom_directory" => 42
     229                                ];
     230                }
     231        }
     232endif;