| | 1 | <?php |
| | 2 | |
| | 3 | if ( is_multisite() ) : |
| | 4 | |
| | 5 | /** |
| | 6 | * @group multisite |
| | 7 | * @covers ::get_space_used |
| | 8 | */ |
| | 9 | class Tests_Multisite_Get_Space_Used extends WP_UnitTestCase { |
| | 10 | protected $suppress = false; |
| | 11 | |
| | 12 | function setUp() { |
| | 13 | global $wpdb; |
| | 14 | parent::setUp(); |
| | 15 | $this->suppress = $wpdb->suppress_errors(); |
| | 16 | |
| | 17 | $_SERVER['REMOTE_ADDR'] = ''; |
| | 18 | } |
| | 19 | |
| | 20 | function tearDown() { |
| | 21 | global $wpdb; |
| | 22 | $wpdb->suppress_errors( $this->suppress ); |
| | 23 | parent::tearDown(); |
| | 24 | } |
| | 25 | |
| | 26 | function test_get_space_used_switched_site() { |
| | 27 | $blog_id = $this->factory->blog->create(); |
| | 28 | switch_to_blog( $blog_id ); |
| | 29 | |
| | 30 | // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the |
| | 31 | // src directory already contains a content directory with site content, then the initial expectation |
| | 32 | // will be polluted. We create sites until an empty one is available. |
| | 33 | while ( 0 != get_space_used() ) { |
| | 34 | restore_current_blog(); |
| | 35 | $blog_id = $this->factory->blog->create(); |
| | 36 | switch_to_blog( $blog_id ); |
| | 37 | } |
| | 38 | |
| | 39 | // Upload a file to the new site. |
| | 40 | $filename = rand_str().'.jpg'; |
| | 41 | $contents = rand_str(); |
| | 42 | $file = wp_upload_bits( $filename, null, $contents ); |
| | 43 | |
| | 44 | // get_space_used() is measures in MB, get the size of the new file in MB. |
| | 45 | $size = filesize( $file['file'] ) / 1024 / 1024; |
| | 46 | |
| | 47 | delete_transient( 'dirsize_cache' ); |
| | 48 | |
| | 49 | $this->assertEquals( $size, get_space_used() ); |
| | 50 | $upload_dir = wp_upload_dir(); |
| | 51 | $this->remove_added_uploads(); |
| | 52 | $this->delete_folders( $upload_dir['basedir'] ); |
| | 53 | restore_current_blog(); |
| | 54 | } |
| | 55 | |
| | 56 | /** |
| | 57 | * Directories of sub sites on a network should not count against the same spaced used total for |
| | 58 | * the main site. |
| | 59 | */ |
| | 60 | function test_get_space_used_main_site() { |
| | 61 | $space_used = get_space_used(); |
| | 62 | |
| | 63 | $blog_id = $this->factory->blog->create(); |
| | 64 | switch_to_blog( $blog_id ); |
| | 65 | |
| | 66 | // We don't rely on an initial value of 0 for space used, but should have a clean space available |
| | 67 | // so that we can remove any uploaded files and directories without concern of a conflict with |
| | 68 | // existing content directories in src. |
| | 69 | while ( 0 != get_space_used() ) { |
| | 70 | restore_current_blog(); |
| | 71 | $blog_id = $this->factory->blog->create(); |
| | 72 | switch_to_blog( $blog_id ); |
| | 73 | } |
| | 74 | |
| | 75 | // Upload a file to the new site. |
| | 76 | $filename = rand_str().'.jpg'; |
| | 77 | $contents = rand_str(); |
| | 78 | wp_upload_bits( $filename, null, $contents ); |
| | 79 | |
| | 80 | restore_current_blog(); |
| | 81 | |
| | 82 | delete_transient( 'dirsize_cache' ); |
| | 83 | |
| | 84 | $this->assertEquals( $space_used, get_space_used() ); |
| | 85 | |
| | 86 | // Switch back to the new site to remove the uploaded file. |
| | 87 | switch_to_blog( $blog_id ); |
| | 88 | $upload_dir = wp_upload_dir(); |
| | 89 | $this->remove_added_uploads(); |
| | 90 | $this->delete_folders( $upload_dir['basedir'] ); |
| | 91 | restore_current_blog(); |
| | 92 | } |
| | 93 | } |
| | 94 | endif; |
| | 95 | No newline at end of file |