Changeset 49628
- Timestamp:
- 11/17/2020 03:36:02 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r49616 r49628 7608 7608 * Get the size of a directory recursively. 7609 7609 * 7610 * Used by get_dirsize() to get a directory's size when it contains 7611 * other directories. 7610 * Used by get_dirsize() to get a directory size when it contains other directories. 7612 7611 * 7613 7612 * @since MU (3.0.0) 7614 * @since 4.3.0 $exclude parameteradded.7615 * @since 5.2.0 $max_execution_time parameteradded.7616 * @since 5.6.0 $directory_cache parameteradded.7613 * @since 4.3.0 The `$exclude` parameter was added. 7614 * @since 5.2.0 The `$max_execution_time` parameter was added. 7615 * @since 5.6.0 The `$directory_cache` parameter was added. 7617 7616 * 7618 7617 * @param string $directory Full path of a directory. … … 7669 7668 7670 7669 /** 7671 * Filters the amount of storage space used by one directory and all it's children, in megabytes. 7672 * Return the actual used space to shortcircuit the recursive PHP file size calculation and use something else 7673 * like a CDN API or native operating system tools for better performance 7674 * 7675 * @since 5.6.0 7676 * 7677 * @param int|false $space_used The amount of used space, in bytes. Default 0. 7678 */ 7670 * Filters the amount of storage space used by one directory and all its children, in megabytes. 7671 * 7672 * Return the actual used space to short-circuit the recursive PHP file size calculation 7673 * and use something else, like a CDN API or native operating system tools for better performance. 7674 * 7675 * @since 5.6.0 7676 * 7677 * @param int|false $space_used The amount of used space, in bytes. Default 0. 7678 */ 7679 7679 $size = apply_filters( 'calculate_current_dirsize', $size, $directory, $exclude, $max_execution_time, $directory_cache ); 7680 7680 … … 7706 7706 $directory_cache[ $cache_path ] = $size; 7707 7707 7708 // Only write the transient on the top level call and not on recursive calls 7708 // Only write the transient on the top level call and not on recursive calls. 7709 7709 if ( $save_cache ) { 7710 7710 set_transient( 'dirsize_cache', $directory_cache ); … … 7715 7715 7716 7716 /** 7717 * Invalidates entries within the dirsize_cache 7718 * 7719 * Remove the current directory and all parent directories 7720 * from the dirsize_cache transient. 7717 * Cleans directory size cache used by recurse_dirsize(). 7718 * 7719 * Removes the current directory and all parent directories from the `dirsize_cache` transient. 7721 7720 * 7722 7721 * @since 5.6.0 -
trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php
r49616 r49628 4 4 5 5 /** 6 * Tests specific to the dir size caching in multisites6 * Tests specific to the directory size caching in multisite. 7 7 * 8 * @ticket 19879 8 9 * @group multisite 9 10 */ … … 23 24 } 24 25 25 /* 26 * Test whether the values from the dirsize_cache will be used correctly using a more complex dirsize cache mock 26 /** 27 * Test whether dirsize_cache values are used correctly with a more complex dirsize cache mock. 28 * 29 * @ticket 19879 27 30 */ 28 31 function test_get_dirsize_cache_in_recurse_dirsize_mock() { … … 30 33 switch_to_blog( $blog_id ); 31 34 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 35 /* 36 * Our comparison of space relies on an initial value of 0. If a previous test has failed 37 * or if the `src` directory already contains a content directory with site content, then 38 * the initial expectation will be polluted. We create sites until an empty one is available. 39 */ 40 while ( 0 !== get_space_used() ) { 41 restore_current_blog(); 42 $blog_id = self::factory()->blog->create(); 43 switch_to_blog( $blog_id ); 44 } 45 46 // Clear the dirsize cache. 47 delete_transient( 'dirsize_cache' ); 48 49 // Set the dirsize cache to our mock. 45 50 set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) ); 46 51 47 52 $upload_dir = wp_upload_dir(); 48 53 49 // Check recurse_dirsize against the mock. The cache should match54 // Check recurse_dirsize() against the mock. The cache should match. 50 55 $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) ); 51 56 $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) ); … … 57 62 $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) ); 58 63 59 // No cache match, upload folder should be empty and return 064 // No cache match, upload directory should be empty and return 0. 60 65 $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) ); 61 66 62 // No cache match on non existing folder should return false67 // No cache match on non existing directory should return false. 63 68 $this->assertSame( false, recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) ); 64 69 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 70 // Cleanup. 71 $this->remove_added_uploads(); 72 restore_current_blog(); 73 } 74 75 /** 76 * Test whether the dirsize_cache invalidation works given a file path as input. 77 * 78 * @ticket 19879 73 79 */ 74 80 function test_clean_dirsize_cache_file_input_mock() { … … 76 82 switch_to_blog( $blog_id ); 77 83 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. 84 /* 85 * Our comparison of space relies on an initial value of 0. If a previous test has failed 86 * or if the `src` directory already contains a content directory with site content, then 87 * the initial expectation will be polluted. We create sites until an empty one is available. 88 */ 81 89 while ( 0 !== get_space_used() ) { 82 90 restore_current_blog(); … … 88 96 $cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ) ); 89 97 90 // Clear the dirsize _cache91 delete_transient( 'dirsize_cache' ); 92 93 // Set the dirsize cache to our mock 98 // Clear the dirsize cache. 99 delete_transient( 'dirsize_cache' ); 100 101 // Set the dirsize cache to our mock. 94 102 set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) ); 95 103 … … 98 106 $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) ); 99 107 100 // Invalidation should also respect the directory tree up 101 // Should work fine with path to folder OR file108 // Invalidation should also respect the directory tree up. 109 // Should work fine with path to directory OR file. 102 110 clean_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' ); 103 111 … … 105 113 $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) ); 106 114 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 115 // Other cache paths should not be invalidated. 116 $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) ); 117 118 // Cleanup. 119 $this->remove_added_uploads(); 120 restore_current_blog(); 121 } 122 123 /** 124 * Test whether the dirsize_cache invalidation works given a directory path as input. 125 * 126 * @ticket 19879 118 127 */ 119 128 function test_clean_dirsize_cache_folder_input_mock() { … … 121 130 switch_to_blog( $blog_id ); 122 131 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. 132 /* 133 * Our comparison of space relies on an initial value of 0. If a previous test has failed 134 * or if the `src` directory already contains a content directory with site content, then 135 * the initial expectation will be polluted. We create sites until an empty one is available. 136 */ 126 137 while ( 0 !== get_space_used() ) { 127 138 restore_current_blog(); … … 133 144 $cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ) ); 134 145 135 // Clear the dirsize _cache136 delete_transient( 'dirsize_cache' ); 137 138 // Set the dirsize cache to our mock 146 // Clear the dirsize cache. 147 delete_transient( 'dirsize_cache' ); 148 149 // Set the dirsize cache to our mock. 139 150 set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) ); 140 151 … … 143 154 $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) ); 144 155 145 // Invalidation should also respect the directory tree up 146 // Should work fine with path to folder OR file156 // Invalidation should also respect the directory tree up. 157 // Should work fine with path to directory OR file. 147 158 clean_dirsize_cache( $upload_dir['basedir'] . '/2/1' ); 148 159 … … 150 161 $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) ); 151 162 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 163 // Other cache paths should not be invalidated. 164 $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) ); 165 166 // Cleanup. 167 $this->remove_added_uploads(); 168 restore_current_blog(); 169 } 170 171 /** 172 * Test whether dirsize_cache values are used correctly with a simple real upload. 173 * 174 * @ticket 19879 162 175 */ 163 176 function test_get_dirsize_cache_in_recurse_dirsize_upload() { … … 165 178 switch_to_blog( $blog_id ); 166 179 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 180 /* 181 * Our comparison of space relies on an initial value of 0. If a previous test has failed 182 * or if the `src` directory already contains a content directory with site content, then 183 * the initial expectation will be polluted. We create sites until an empty one is available. 184 */ 185 while ( 0 !== get_space_used() ) { 186 restore_current_blog(); 187 $blog_id = self::factory()->blog->create(); 188 switch_to_blog( $blog_id ); 189 } 190 191 // Clear the dirsize cache. 177 192 delete_transient( 'dirsize_cache' ); 178 193 … … 181 196 $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) ); 182 197 183 // Upload a file to the new site using wp_upload_bits .198 // Upload a file to the new site using wp_upload_bits(). 184 199 $filename = __FUNCTION__ . '.jpg'; 185 200 $contents = __FUNCTION__ . '_contents'; … … 190 205 $this->assertSame( $size, $calc_size ); 191 206 192 // dirsize_cache should now be filled after upload and recurse_dirsize call207 // `dirsize_cache` should now be filled after upload and recurse_dirsize() call. 193 208 $cache_path = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['path'] ) ); 194 209 $this->assertSame( true, is_array( get_transient( 'dirsize_cache' ) ) ); 195 210 $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] ); 196 211 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 212 // Cleanup. 213 $this->remove_added_uploads(); 214 restore_current_blog(); 215 } 216 217 /** 218 * Test whether the filter to calculate space for an existing directory works as expected. 219 * 220 * @ticket 19879 204 221 */ 205 222 function test_recurse_dirsize_calculate_current_dirsize_filter() { … … 229 246 } 230 247 } 248 231 249 endif;
Note: See TracChangeset
for help on using the changeset viewer.