<!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>[49212] trunk: Multisite: More specific caching for `get_dirsize`.</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/49212">49212</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/49212","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-10-19 21:49:58 +0000 (Mon, 19 Oct 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 specific caching for `get_dirsize`.

Instead of one cache entry for all upload folders for a site on multisite, this now caches for each folder and invalidates that cache based on context. In multisite, this should speed up `get_dirsize` calls since older directories that are much less likely to change will no longer have the size recalculated.

Props janthiel, A5hleyRich, batmoo.
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="#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-10-19 21:14:06 UTC (rev 49211)
+++ trunk/src/wp-admin/includes/file.php        2020-10-19 21:49:58 UTC (rev 49212)
</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">-                delete_transient( 'dirsize_cache' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         invalidate_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-10-19 21:14:06 UTC (rev 49211)
+++ trunk/src/wp-includes/functions.php 2020-10-19 21:49:58 UTC (rev 49212)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2740,6 +2740,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Compute the URL.
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( is_multisite() ) {
+               invalidate_dirsize_cache( $new_file );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /** This filter is documented in wp-admin/includes/file.php */
</span><span class="cx" style="display: block; padding: 0 10px">        return apply_filters(
</span><span class="cx" style="display: block; padding: 0 10px">                'wp_handle_upload',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7560,26 +7564,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_dirsize( $directory, $max_execution_time = null ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $dirsize = get_transient( 'dirsize_cache' );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) {
-               return $dirsize[ $directory ]['size'];
-       }
-
-       if ( ! is_array( $dirsize ) ) {
-               $dirsize = array();
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         // Exclude individual site directories from the total when checking the main site of a network,
</span><span class="cx" style="display: block; padding: 0 10px">        // as they are subdirectories and should not be counted.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_multisite() && is_main_site() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $size = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
</ins><span class="cx" style="display: block; padding: 0 10px">         } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, null, $max_execution_time );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $size = recurse_dirsize( $directory, null, $max_execution_time );
</ins><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">-        set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
-       return $dirsize[ $directory ]['size'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $size;
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7591,6 +7585,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since MU (3.0.0)
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.3.0 $exclude parameter added.
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.2.0 $max_execution_time parameter added.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.6.0 $directory_cache parameter added.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string       $directory          Full path of a directory.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string|array $exclude            Optional. Full path of a subdirectory to exclude from the total,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7597,13 +7592,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                                         or array of paths. Expected without trailing slash(es).
</span><span class="cx" style="display: block; padding: 0 10px">  * @param int          $max_execution_time Maximum time to run before giving up. In seconds. The timeout is global
</span><span class="cx" style="display: block; padding: 0 10px">  *                                         and is measured from the moment WordPress started to load.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array        $directory_cache    Optional. Array of cached directory paths.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout.
</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 recurse_dirsize( $directory, $exclude = null, $max_execution_time = null ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null, &$directory_cache = null ) {
</ins><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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $cache_path = normalize_dirsize_cache_path( $directory );
+       $save_cache = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( ! isset( $directory_cache ) ) {
+               $directory_cache = get_transient( 'dirsize_cache' );
+               $save_cache      = true;
+       }
+
+       if ( isset( $directory_cache[ $cache_path ] ) ) {
+               return $directory_cache[ $cache_path ];
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7630,33 +7638,97 @@
</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">-        $handle = opendir( $directory );
-       if ( $handle ) {
-               while ( ( $file = readdir( $handle ) ) !== false ) {
-                       $path = $directory . '/' . $file;
-                       if ( '.' !== $file && '..' !== $file ) {
-                               if ( is_file( $path ) ) {
-                                       $size += filesize( $path );
-                               } elseif ( is_dir( $path ) ) {
-                                       $handlesize = recurse_dirsize( $path, $exclude, $max_execution_time );
-                                       if ( $handlesize > 0 ) {
-                                               $size += $handlesize;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+       * Filters the amount of storage space used by one directory and all it's children, in megabytes.
+       * Return the actual used space to shortcircuit the recursive PHP file size calculation and use something else
+       * like a CDN API or native operating system tools for better performance
+       *
+       * @since 5.6.0
+       *
+       * @param int|false $space_used The amount of used space, in bytes. Default 0.
+       */
+       $size = apply_filters( 'calculate_current_dirsize', $size, $directory, $exclude, $max_execution_time, $directory_cache );
+
+       if ( 0 === $size ) {
+               $handle = opendir( $directory );
+               if ( $handle ) {
+                       while ( ( $file = readdir( $handle ) ) !== false ) {
+                               $path = $directory . '/' . $file;
+                               if ( '.' !== $file && '..' !== $file ) {
+                                       if ( is_file( $path ) ) {
+                                               $size += filesize( $path );
+                                       } elseif ( is_dir( $path ) ) {
+                                               $handlesize = recurse_dirsize( $path, $exclude, $max_execution_time, $directory_cache );
+                                               if ( $handlesize > 0 ) {
+                                                       $size += $handlesize;
+                                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) {
-                                       // Time exceeded. Give up instead of risking a fatal timeout.
-                                       $size = null;
-                                       break;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) {
+                                               // Time exceeded. Give up instead of risking a fatal timeout.
+                                               $size = null;
+                                               break;
+                                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        closedir( $handle );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                closedir( $handle );
</del><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $directory_cache[ $cache_path ] = $size;
+
+       // Only write the transient on the top level call and not on recursive calls
+       if ( $save_cache ) {
+               set_transient( 'dirsize_cache', $directory_cache );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         return $size;
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Invalidates entries within the dirsize_cache
+ *
+ * Remove the current directory and all parent directories
+ * from the dirsize_cache transient.
+ *
+ * @since 5.6.0
+ *
+ * @param string $path Full path of a directory or file.
+ */
+function invalidate_dirsize_cache( $path ) {
+       $directory_cache = get_transient( 'dirsize_cache' );
+
+       if ( empty( $directory_cache ) ) {
+               return;
+       }
+
+       $cache_path = normalize_dirsize_cache_path( $path );
+       unset( $directory_cache[ $cache_path ] );
+
+       while ( DIRECTORY_SEPARATOR !== $cache_path && '.' !== $cache_path && '..' !== $cache_path ) {
+               $cache_path = dirname( $cache_path );
+               unset( $directory_cache[ $cache_path ] );
+       }
+
+       set_transient( 'dirsize_cache', $directory_cache );
+}
+
+/**
+ * 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 );
+}
+
+/**
</ins><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-10-19 21:14:06 UTC (rev 49211)
+++ trunk/src/wp-includes/post.php      2020-10-19 21:49:58 UTC (rev 49212)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5914,7 +5914,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">-                delete_transient( 'dirsize_cache' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         invalidate_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="trunktestsphpunittestsmultisitedirsizeCachephp"></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/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                              (rev 0)
+++ trunk/tests/phpunit/tests/multisite/dirsizeCache.php        2020-10-19 21:49:58 UTC (rev 49212)
</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_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;
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/multisite/dirsizeCache.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></div>

</body>
</html>