| | 1 | <?php |
| | 2 | |
| | 3 | if ( 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 |
| | 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 | * Directories of sub sites on a network should not count against the same spaced used total for |
| | 162 | * the main site. |
| | 163 | */ |
| | 164 | function test_get_dirsize_cache_in_recurse_dirsize_upload() { |
| | 165 | $blog_id = self::factory()->blog->create(); |
| | 166 | switch_to_blog( $blog_id ); |
| | 167 | |
| | 168 | // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the |
| | 169 | // src directory already contains a content directory with site content, then the initial expectation |
| | 170 | // will be polluted. We create sites until an empty one is available. |
| | 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 = normalize_dirsize_cache_path( $upload_dir['path'] ); |
| | 195 | $this->assertSame( true, is_array( 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 | function test_recurse_dirsize_calculate_current_dirsize_filter() { |
| | 207 | add_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) ); |
| | 208 | |
| | 209 | $upload_dir = wp_upload_dir(); |
| | 210 | $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) ); |
| | 211 | |
| | 212 | remove_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) ); |
| | 213 | } |
| | 214 | |
| | 215 | function _filter_calculate_current_dirsize() { |
| | 216 | return 1042; |
| | 217 | } |
| | 218 | |
| | 219 | function _get_mock_dirsize_cache_for_site( $site_id ) { |
| | 220 | return |
| | 221 | [ |
| | 222 | "wp-content/uploads/sites/$site_id/2/2" => 22, |
| | 223 | "wp-content/uploads/sites/$site_id/2/1" => 21, |
| | 224 | "wp-content/uploads/sites/$site_id/2" => 2, |
| | 225 | "wp-content/uploads/sites/$site_id/1/3" => 13, |
| | 226 | "wp-content/uploads/sites/$site_id/1/2" => 12, |
| | 227 | "wp-content/uploads/sites/$site_id/1/1" => 11, |
| | 228 | "wp-content/uploads/sites/$site_id/1" => 1, |
| | 229 | "wp-content/uploads/sites/$site_id/custom_directory" => 42 |
| | 230 | ]; |
| | 231 | } |
| | 232 | } |
| | 233 | endif; |