- Timestamp:
- 12/03/2020 08:37:43 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php
r49630 r49744 94 94 95 95 $upload_dir = wp_upload_dir(); 96 $cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ));96 $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] ); 97 97 98 98 // Clear the dirsize cache. … … 142 142 143 143 $upload_dir = wp_upload_dir(); 144 $cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ));144 $cache_key_prefix = untrailingslashit( $upload_dir['basedir'] ); 145 145 146 146 // Clear the dirsize cache. … … 206 206 207 207 // `dirsize_cache` should now be filled after upload and recurse_dirsize() call. 208 $cache_path = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['path'] ));208 $cache_path = untrailingslashit( $upload_dir['path'] ); 209 209 $this->assertSame( true, is_array( get_transient( 'dirsize_cache' ) ) ); 210 210 $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] ); … … 234 234 235 235 function _get_mock_dirsize_cache_for_site( $site_id ) { 236 $prefix = wp_upload_dir()['basedir']; 237 236 238 return array( 237 " wp-content/uploads/sites/$site_id/2/2"=> 22,238 " wp-content/uploads/sites/$site_id/2/1"=> 21,239 " wp-content/uploads/sites/$site_id/2"=> 2,240 " wp-content/uploads/sites/$site_id/1/3"=> 13,241 " wp-content/uploads/sites/$site_id/1/2"=> 12,242 " wp-content/uploads/sites/$site_id/1/1"=> 11,243 " wp-content/uploads/sites/$site_id/1"=> 1,244 " wp-content/uploads/sites/$site_id/custom_directory" => 42,239 "$prefix/2/2" => 22, 240 "$prefix/2/1" => 21, 241 "$prefix/2" => 2, 242 "$prefix/1/3" => 13, 243 "$prefix/1/2" => 12, 244 "$prefix/1/1" => 11, 245 "$prefix/1" => 1, 246 "$prefix/custom_directory" => 42, 245 247 ); 246 248 } 249 250 /* 251 * Test that 5.6+ gracefully handles the old 5.5 transient structure. 252 * 253 * @ticket 51913 254 */ 255 function test_5_5_transient_structure_compat() { 256 $blog_id = self::factory()->blog->create(); 257 switch_to_blog( $blog_id ); 258 259 /* 260 * Our comparison of space relies on an initial value of 0. If a previous test has failed 261 * or if the `src` directory already contains a directory with site content, then the initial 262 * expectation will be polluted. We create sites until an empty one is available. 263 */ 264 while ( 0 !== get_space_used() ) { 265 restore_current_blog(); 266 $blog_id = self::factory()->blog->create(); 267 switch_to_blog( $blog_id ); 268 } 269 270 // Clear the dirsize cache. 271 delete_transient( 'dirsize_cache' ); 272 273 // Set the dirsize cache to our mock. 274 set_transient( 'dirsize_cache', $this->_get_mock_5_5_dirsize_cache( $blog_id ) ); 275 276 $upload_dir = wp_upload_dir(); 277 278 /* 279 * The cached size should be ignored, because it's in the old format. The function 280 * will try to fetch a live value, but in this case the folder doesn't actually 281 * exist on disk, so the function should fail. 282 */ 283 $this->assertSame( false, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) ); 284 285 /* 286 * Now that it's confirmed that old cached values aren't being returned, create the 287 * folder on disk, so that the the rest of the function can be tested. 288 */ 289 wp_mkdir_p( $upload_dir['basedir'] . '/2/1' ); 290 $filename = $upload_dir['basedir'] . '/2/1/this-needs-to-exist.txt'; 291 file_put_contents( $filename, 'this file is 21 bytes' ); 292 293 // Clear the dirsize cache. 294 delete_transient( 'dirsize_cache' ); 295 296 // Set the dirsize cache to our mock. 297 set_transient( 'dirsize_cache', $this->_get_mock_5_5_dirsize_cache( $blog_id ) ); 298 299 /* 300 * Now that the folder exists, the old cached value should be overwritten 301 * with the size, using the current format. 302 */ 303 $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) ); 304 $this->assertSame( 21, get_transient( 'dirsize_cache' )[ $upload_dir['basedir'] . '/2/1' ] ); 305 306 // No cache match on non existing directory should return false. 307 $this->assertSame( false, recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) ); 308 309 // Cleanup. 310 $this->remove_added_uploads(); 311 rmdir( $upload_dir['basedir'] . '/2/1' ); 312 313 restore_current_blog(); 314 } 315 316 function _get_mock_5_5_dirsize_cache( $site_id ) { 317 $prefix = untrailingslashit( wp_upload_dir()['basedir'] ); 318 319 return array( 320 "$prefix/2/2" => array( 'size' => 22 ), 321 "$prefix/2/1" => array( 'size' => 21 ), 322 "$prefix/2" => array( 'size' => 2 ), 323 "$prefix/1/3" => array( 'size' => 13 ), 324 "$prefix/1/2" => array( 'size' => 12 ), 325 "$prefix/1/1" => array( 'size' => 11 ), 326 "$prefix/1" => array( 'size' => 1 ), 327 "$prefix/custom_directory" => array( 'size' => 42 ), 328 ); 329 } 247 330 } 248 331
Note: See TracChangeset
for help on using the changeset viewer.