<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[49616] trunk: Multisite: More consistency for `clean_dirsize_cache()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/49616">49616</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/49616","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>helen</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-11-16 22:29:42 +0000 (Mon, 16 Nov 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Multisite: More consistency for `clean_dirsize_cache()`.

Props SergeyBiryukov.
Fixes <a href="https://core.trac.wordpress.org/ticket/19879">#19879</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmultisitecleanDirsizeCachephp">trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php</a></li>
</ul>

<h3>Removed Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmultisitedirsizeCachephp">trunk/tests/phpunit/tests/multisite/dirsizeCache.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesfilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/file.php      2020-11-16 21:34:56 UTC (rev 49615)
+++ trunk/src/wp-admin/includes/file.php        2020-11-16 22:29:42 UTC (rev 49616)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -928,7 +928,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $url = $uploads['url'] . "/$filename";
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_multisite() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                invalidate_dirsize_cache( $new_file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         clean_dirsize_cache( $new_file );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php       2020-11-16 21:34:56 UTC (rev 49615)
+++ trunk/src/wp-includes/functions.php 2020-11-16 22:29:42 UTC (rev 49616)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2747,7 +2747,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $url = $upload['url'] . "/$filename";
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_multisite() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                invalidate_dirsize_cache( $new_file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         clean_dirsize_cache( $new_file );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /** This filter is documented in wp-admin/includes/file.php */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7628,7 +7628,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $size = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $directory  = untrailingslashit( $directory );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $cache_path = normalize_dirsize_cache_path( $directory );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cache_path = untrailingslashit( str_replace( ABSPATH, '', $directory ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $save_cache = false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! isset( $directory_cache ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7722,7 +7723,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $path Full path of a directory or file.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function invalidate_dirsize_cache( $path ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function clean_dirsize_cache( $path ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         $directory_cache = get_transient( 'dirsize_cache' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $directory_cache ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7729,7 +7730,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $cache_path = normalize_dirsize_cache_path( $path );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cache_path = untrailingslashit( str_replace( ABSPATH, '', $path ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         unset( $directory_cache[ $cache_path ] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        while ( DIRECTORY_SEPARATOR !== $cache_path && '.' !== $cache_path && '..' !== $cache_path ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7741,22 +7742,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Normalize dirsize cache path.
- *
- * Ensures array keys within the dirsize_cache transient follow the same format.
- *
- * @since 5.6.0
- *
- * @param string $path
- * @return string
- */
-function normalize_dirsize_cache_path( $path ) {
-       $path = str_replace( ABSPATH, '', $path );
-
-       return untrailingslashit( $path );
-}
-
-/**
</del><span class="cx" style="display: block; padding: 0 10px">  * Checks compatibility with the current WordPress version.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.2.0
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2020-11-16 21:34:56 UTC (rev 49615)
+++ trunk/src/wp-includes/post.php      2020-11-16 22:29:42 UTC (rev 49616)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5918,7 +5918,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $file         = get_attached_file( $post_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_multisite() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                invalidate_dirsize_cache( $file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         clean_dirsize_cache( $file );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestsmultisitecleanDirsizeCachephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php                         (rev 0)
+++ trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php   2020-11-16 22:29:42 UTC (rev 49616)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,231 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+if ( is_multisite() ) :
+
+       /**
+        * Tests specific to the dirsize caching in multisites
+        *
+        * @group multisite
+        */
+       class Tests_Multisite_Dirsize_Cache extends WP_UnitTestCase {
+               protected $suppress = false;
+
+               function setUp() {
+                       global $wpdb;
+                       parent::setUp();
+                       $this->suppress = $wpdb->suppress_errors();
+               }
+
+               function tearDown() {
+                       global $wpdb;
+                       $wpdb->suppress_errors( $this->suppress );
+                       parent::tearDown();
+               }
+
+               /*
+                * Test whether the values from the dirsize_cache will be used correctly using a more complex dirsize cache mock
+                */
+               function test_get_dirsize_cache_in_recurse_dirsize_mock() {
+                       $blog_id = self::factory()->blog->create();
+                       switch_to_blog( $blog_id );
+
+                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
+                       // src directory already contains a content directory with site content, then the initial expectation
+                       // will be polluted. We create sites until an empty one is available.
+                       while ( 0 !== get_space_used() ) {
+                               restore_current_blog();
+                               $blog_id = self::factory()->blog->create();
+                               switch_to_blog( $blog_id );
+                       }
+
+                       // Clear the dirsize_cache
+                       delete_transient( 'dirsize_cache' );
+
+                       // Set the dirsize cache to our mock
+                       set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
+
+                       $upload_dir = wp_upload_dir();
+
+                       // Check recurse_dirsize against the mock. The cache should match
+                       $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
+                       $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) );
+                       $this->assertSame( 2, recurse_dirsize( $upload_dir['basedir'] . '/2' ) );
+                       $this->assertSame( 11, recurse_dirsize( $upload_dir['basedir'] . '/1/1' ) );
+                       $this->assertSame( 12, recurse_dirsize( $upload_dir['basedir'] . '/1/2' ) );
+                       $this->assertSame( 13, recurse_dirsize( $upload_dir['basedir'] . '/1/3' ) );
+                       $this->assertSame( 1, recurse_dirsize( $upload_dir['basedir'] . '/1' ) );
+                       $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) );
+
+                       // No cache match, upload folder should be empty and return 0
+                       $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) );
+
+                       // No cache match on non existing folder should return false
+                       $this->assertSame( false, recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
+
+                       // Cleanup
+                       $this->remove_added_uploads();
+                       restore_current_blog();
+               }
+
+               /*
+                * Test whether the invalidation of the dirsize_cache works
+                * Given a file path as input
+                */
+               function test_clean_dirsize_cache_file_input_mock() {
+                       $blog_id = self::factory()->blog->create();
+                       switch_to_blog( $blog_id );
+
+                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
+                       // src directory already contains a content directory with site content, then the initial expectation
+                       // will be polluted. We create sites until an empty one is available.
+                       while ( 0 !== get_space_used() ) {
+                               restore_current_blog();
+                               $blog_id = self::factory()->blog->create();
+                               switch_to_blog( $blog_id );
+                       }
+
+                       $upload_dir       = wp_upload_dir();
+                       $cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ) );
+
+                       // Clear the dirsize_cache
+                       delete_transient( 'dirsize_cache' );
+
+                       // Set the dirsize cache to our mock
+                       set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
+
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
+
+                       // Invalidation should also respect the directory tree up
+                       // Should work fine with path to folder OR file
+                       clean_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
+
+                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
+                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
+
+                       // Other cache paths should not be invalidated
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
+
+                       // Cleanup
+                       $this->remove_added_uploads();
+                       restore_current_blog();
+               }
+
+               /*
+                * Test whether the invalidation of the dirsize_cache works
+                * Given a folder path as input
+                */
+               function test_clean_dirsize_cache_folder_input_mock() {
+                       $blog_id = self::factory()->blog->create();
+                       switch_to_blog( $blog_id );
+
+                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
+                       // src directory already contains a content directory with site content, then the initial expectation
+                       // will be polluted. We create sites until an empty one is available.
+                       while ( 0 !== get_space_used() ) {
+                               restore_current_blog();
+                               $blog_id = self::factory()->blog->create();
+                               switch_to_blog( $blog_id );
+                       }
+
+                       $upload_dir       = wp_upload_dir();
+                       $cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ) );
+
+                       // Clear the dirsize_cache
+                       delete_transient( 'dirsize_cache' );
+
+                       // Set the dirsize cache to our mock
+                       set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
+
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
+
+                       // Invalidation should also respect the directory tree up
+                       // Should work fine with path to folder OR file
+                       clean_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
+
+                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
+                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
+
+                       // Other cache paths should not be invalidated
+                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
+
+                       // Cleanup
+                       $this->remove_added_uploads();
+                       restore_current_blog();
+               }
+
+               /**
+                * Test whether the values from the dirsize_cache will be used correctly using a simple real upload
+                */
+               function test_get_dirsize_cache_in_recurse_dirsize_upload() {
+                       $blog_id = self::factory()->blog->create();
+                       switch_to_blog( $blog_id );
+
+                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
+                       // src directory already contains a content directory with site content, then the initial expectation
+                       // will be polluted. We create sites until an empty one is available.
+                       while ( 0 !== get_space_used() ) {
+                               restore_current_blog();
+                               $blog_id = self::factory()->blog->create();
+                               switch_to_blog( $blog_id );
+                       }
+
+                       // Clear the dirsize_cache
+                       delete_transient( 'dirsize_cache' );
+
+                       $upload_dir = wp_upload_dir();
+
+                       $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) );
+
+                       // Upload a file to the new site using wp_upload_bits.
+                       $filename = __FUNCTION__ . '.jpg';
+                       $contents = __FUNCTION__ . '_contents';
+                       $file     = wp_upload_bits( $filename, null, $contents );
+
+                       $calc_size = recurse_dirsize( $upload_dir['path'] );
+                       $size      = filesize( $file['file'] );
+                       $this->assertSame( $size, $calc_size );
+
+                       // dirsize_cache should now be filled after upload and recurse_dirsize call
+                       $cache_path = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['path'] ) );
+                       $this->assertSame( true, is_array( get_transient( 'dirsize_cache' ) ) );
+                       $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );
+
+                       // Cleanup
+                       $this->remove_added_uploads();
+                       restore_current_blog();
+               }
+
+               /*
+                * Test whether the filter to calculate space for an existing directory works as expected
+                */
+               function test_recurse_dirsize_calculate_current_dirsize_filter() {
+                       add_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) );
+
+                       $upload_dir = wp_upload_dir();
+                       $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) );
+
+                       remove_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) );
+               }
+
+               function _filter_calculate_current_dirsize() {
+                       return 1042;
+               }
+
+               function _get_mock_dirsize_cache_for_site( $site_id ) {
+                       return array(
+                               "wp-content/uploads/sites/$site_id/2/2" => 22,
+                               "wp-content/uploads/sites/$site_id/2/1" => 21,
+                               "wp-content/uploads/sites/$site_id/2"   => 2,
+                               "wp-content/uploads/sites/$site_id/1/3" => 13,
+                               "wp-content/uploads/sites/$site_id/1/2" => 12,
+                               "wp-content/uploads/sites/$site_id/1/1" => 11,
+                               "wp-content/uploads/sites/$site_id/1"   => 1,
+                               "wp-content/uploads/sites/$site_id/custom_directory" => 42,
+                       );
+               }
+       }
+endif;
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/multisite/cleanDirsizeCache.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsmultisitedirsizeCachephp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: trunk/tests/phpunit/tests/multisite/dirsizeCache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/dirsizeCache.php      2020-11-16 21:34:56 UTC (rev 49615)
+++ trunk/tests/phpunit/tests/multisite/dirsizeCache.php        2020-11-16 22:29:42 UTC (rev 49616)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,231 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<?php
-
-if ( is_multisite() ) :
-
-       /**
-        * Tests specific to the dirsize caching in multisites
-        *
-        * @group multisite
-        */
-       class Tests_Multisite_Dirsize_Cache extends WP_UnitTestCase {
-               protected $suppress = false;
-
-               function setUp() {
-                       global $wpdb;
-                       parent::setUp();
-                       $this->suppress = $wpdb->suppress_errors();
-               }
-
-               function tearDown() {
-                       global $wpdb;
-                       $wpdb->suppress_errors( $this->suppress );
-                       parent::tearDown();
-               }
-
-               /*
-                * Test whether the values from the dirsize_cache will be used correctly using a more complex dirsize cache mock
-                */
-               function test_get_dirsize_cache_in_recurse_dirsize_mock() {
-                       $blog_id = self::factory()->blog->create();
-                       switch_to_blog( $blog_id );
-
-                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
-                       // src directory already contains a content directory with site content, then the initial expectation
-                       // will be polluted. We create sites until an empty one is available.
-                       while ( 0 !== get_space_used() ) {
-                               restore_current_blog();
-                               $blog_id = self::factory()->blog->create();
-                               switch_to_blog( $blog_id );
-                       }
-
-                       // Clear the dirsize_cache
-                       delete_transient( 'dirsize_cache' );
-
-                       // Set the dirsize cache to our mock
-                       set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
-
-                       $upload_dir = wp_upload_dir();
-
-                       // Check recurse_dirsize against the mock. The cache should match
-                       $this->assertSame( 21, recurse_dirsize( $upload_dir['basedir'] . '/2/1' ) );
-                       $this->assertSame( 22, recurse_dirsize( $upload_dir['basedir'] . '/2/2' ) );
-                       $this->assertSame( 2, recurse_dirsize( $upload_dir['basedir'] . '/2' ) );
-                       $this->assertSame( 11, recurse_dirsize( $upload_dir['basedir'] . '/1/1' ) );
-                       $this->assertSame( 12, recurse_dirsize( $upload_dir['basedir'] . '/1/2' ) );
-                       $this->assertSame( 13, recurse_dirsize( $upload_dir['basedir'] . '/1/3' ) );
-                       $this->assertSame( 1, recurse_dirsize( $upload_dir['basedir'] . '/1' ) );
-                       $this->assertSame( 42, recurse_dirsize( $upload_dir['basedir'] . '/custom_directory' ) );
-
-                       // No cache match, upload folder should be empty and return 0
-                       $this->assertSame( 0, recurse_dirsize( $upload_dir['basedir'] ) );
-
-                       // No cache match on non existing folder should return false
-                       $this->assertSame( false, recurse_dirsize( $upload_dir['basedir'] . '/does_not_exist' ) );
-
-                       // Cleanup
-                       $this->remove_added_uploads();
-                       restore_current_blog();
-               }
-
-               /*
-                * Test whether the invalidation of the dirsize_cache works
-                * Given a file path as input
-                */
-               function test_invalidate_dirsize_cache_file_input_mock() {
-                       $blog_id = self::factory()->blog->create();
-                       switch_to_blog( $blog_id );
-
-                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
-                       // src directory already contains a content directory with site content, then the initial expectation
-                       // will be polluted. We create sites until an empty one is available.
-                       while ( 0 !== get_space_used() ) {
-                               restore_current_blog();
-                               $blog_id = self::factory()->blog->create();
-                               switch_to_blog( $blog_id );
-                       }
-
-                       $upload_dir       = wp_upload_dir();
-                       $cache_key_prefix = normalize_dirsize_cache_path( $upload_dir['basedir'] );
-
-                       // Clear the dirsize_cache
-                       delete_transient( 'dirsize_cache' );
-
-                       // Set the dirsize cache to our mock
-                       set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
-
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
-
-                       // Invalidation should also respect the directory tree up
-                       // Should work fine with path to folder OR file
-                       invalidate_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
-
-                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
-                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
-
-                       // Other cache paths should not be invalidated
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
-
-                       // Cleanup
-                       $this->remove_added_uploads();
-                       restore_current_blog();
-               }
-
-               /*
-                * Test whether the invalidation of the dirsize_cache works
-                * Given a folder path as input
-                */
-               function test_invalidate_dirsize_cache_folder_input_mock() {
-                       $blog_id = self::factory()->blog->create();
-                       switch_to_blog( $blog_id );
-
-                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
-                       // src directory already contains a content directory with site content, then the initial expectation
-                       // will be polluted. We create sites until an empty one is available.
-                       while ( 0 !== get_space_used() ) {
-                               restore_current_blog();
-                               $blog_id = self::factory()->blog->create();
-                               switch_to_blog( $blog_id );
-                       }
-
-                       $upload_dir       = wp_upload_dir();
-                       $cache_key_prefix = normalize_dirsize_cache_path( $upload_dir['basedir'] );
-
-                       // Clear the dirsize_cache
-                       delete_transient( 'dirsize_cache' );
-
-                       // Set the dirsize cache to our mock
-                       set_transient( 'dirsize_cache', $this->_get_mock_dirsize_cache_for_site( $blog_id ) );
-
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
-
-                       // Invalidation should also respect the directory tree up
-                       // Should work fine with path to folder OR file
-                       invalidate_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
-
-                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
-                       $this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
-
-                       // Other cache paths should not be invalidated
-                       $this->assertSame( true, array_key_exists( $cache_key_prefix . '/1/1', get_transient( 'dirsize_cache' ) ) );
-
-                       // Cleanup
-                       $this->remove_added_uploads();
-                       restore_current_blog();
-               }
-
-               /**
-                * Test whether the values from the dirsize_cache will be used correctly using a simple real upload
-                */
-               function test_get_dirsize_cache_in_recurse_dirsize_upload() {
-                       $blog_id = self::factory()->blog->create();
-                       switch_to_blog( $blog_id );
-
-                       // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
-                       // src directory already contains a content directory with site content, then the initial expectation
-                       // will be polluted. We create sites until an empty one is available.
-                       while ( 0 !== get_space_used() ) {
-                               restore_current_blog();
-                               $blog_id = self::factory()->blog->create();
-                               switch_to_blog( $blog_id );
-                       }
-
-                       // Clear the dirsize_cache
-                       delete_transient( 'dirsize_cache' );
-
-                       $upload_dir = wp_upload_dir();
-
-                       $this->assertSame( 0, recurse_dirsize( $upload_dir['path'] ) );
-
-                       // Upload a file to the new site using wp_upload_bits.
-                       $filename = __FUNCTION__ . '.jpg';
-                       $contents = __FUNCTION__ . '_contents';
-                       $file     = wp_upload_bits( $filename, null, $contents );
-
-                       $calc_size = recurse_dirsize( $upload_dir['path'] );
-                       $size      = filesize( $file['file'] );
-                       $this->assertSame( $size, $calc_size );
-
-                       // dirsize_cache should now be filled after upload and recurse_dirsize call
-                       $cache_path = normalize_dirsize_cache_path( $upload_dir['path'] );
-                       $this->assertSame( true, is_array( get_transient( 'dirsize_cache' ) ) );
-                       $this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );
-
-                       // Cleanup
-                       $this->remove_added_uploads();
-                       restore_current_blog();
-               }
-
-               /*
-                * Test whether the filter to calculate space for an existing directory works as expected
-                */
-               function test_recurse_dirsize_calculate_current_dirsize_filter() {
-                       add_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) );
-
-                       $upload_dir = wp_upload_dir();
-                       $this->assertSame( 1042, recurse_dirsize( $upload_dir['path'] ) );
-
-                       remove_filter( 'calculate_current_dirsize', array( $this, '_filter_calculate_current_dirsize' ) );
-               }
-
-               function _filter_calculate_current_dirsize() {
-                       return 1042;
-               }
-
-               function _get_mock_dirsize_cache_for_site( $site_id ) {
-                       return array(
-                               "wp-content/uploads/sites/$site_id/2/2" => 22,
-                               "wp-content/uploads/sites/$site_id/2/1" => 21,
-                               "wp-content/uploads/sites/$site_id/2"   => 2,
-                               "wp-content/uploads/sites/$site_id/1/3" => 13,
-                               "wp-content/uploads/sites/$site_id/1/2" => 12,
-                               "wp-content/uploads/sites/$site_id/1/1" => 11,
-                               "wp-content/uploads/sites/$site_id/1"   => 1,
-                               "wp-content/uploads/sites/$site_id/custom_directory" => 42,
-                       );
-               }
-       }
-endif;
</del></span></pre>
</div>
</div>

</body>
</html>