diff --git a/tests/phpunit/tests/functions/getDirsize.php b/tests/phpunit/tests/functions/getDirsize.php new file mode 100644 index 0000000000000..02c19f948978b --- /dev/null +++ b/tests/phpunit/tests/functions/getDirsize.php @@ -0,0 +1,58 @@ +test_dir = get_temp_dir() . 'get_dirsize_test_' . uniqid(); + if ( ! is_dir( $this->test_dir ) ) { + mkdir( $this->test_dir ); + } + } + + public function tear_down() { + $this->recursive_rmdir( $this->test_dir ); + parent::tear_down(); + } + + private function recursive_rmdir( $dir ) { + if ( ! is_dir( $dir ) ) { + return; + } + $files = array_diff( scandir( $dir ), array( '.', '..' ) ); + foreach ( $files as $file ) { + $path = "$dir/$file"; + is_dir( $path ) ? $this->recursive_rmdir( $path ) : unlink( $path ); + } + rmdir( $dir ); + } + + /** + * Tests basic directory size calculation. + * + * @ticket 65654 + */ + public function test_get_dirsize_basic() { + file_put_contents( $this->test_dir . '/file1.txt', '12345' ); + file_put_contents( $this->test_dir . '/file2.txt', '1234567890' ); + + $this->assertSame( 15, get_dirsize( $this->test_dir ) ); + } + + /** + * Tests that get_dirsize() returns false for non-existent directories. + * + * @ticket 65654 + */ + public function test_get_dirsize_non_existent() { + $this->assertFalse( get_dirsize( $this->test_dir . '/non_existent' ) ); + } +} diff --git a/tests/phpunit/tests/functions/recurseDirsize.php b/tests/phpunit/tests/functions/recurseDirsize.php new file mode 100644 index 0000000000000..71bfe49a1b452 --- /dev/null +++ b/tests/phpunit/tests/functions/recurseDirsize.php @@ -0,0 +1,101 @@ +test_dir = get_temp_dir() . 'recurse_dirsize_test_' . uniqid(); + if ( ! is_dir( $this->test_dir ) ) { + mkdir( $this->test_dir ); + } + } + + public function tear_down() { + $this->recursive_rmdir( $this->test_dir ); + delete_transient( 'dirsize_cache' ); + parent::tear_down(); + } + + private function recursive_rmdir( $dir ) { + if ( ! is_dir( $dir ) ) { + return; + } + $files = array_diff( scandir( $dir ), array( '.', '..' ) ); + foreach ( $files as $file ) { + $path = "$dir/$file"; + is_dir( $path ) ? $this->recursive_rmdir( $path ) : unlink( $path ); + } + rmdir( $dir ); + } + + /** + * Tests basic recursive directory size calculation. + * + * @ticket 65654 + */ + public function test_recurse_dirsize_basic() { + mkdir( $this->test_dir . '/sub' ); + file_put_contents( $this->test_dir . '/file1.txt', '12345' ); // 5 bytes. + file_put_contents( $this->test_dir . '/sub/file2.txt', '1234567890' ); // 10 bytes. + + $this->assertSame( 15, recurse_dirsize( $this->test_dir ) ); + } + + /** + * Tests that recurse_dirsize() respects the exclude parameter. + * + * @ticket 65654 + */ + public function test_recurse_dirsize_exclude() { + mkdir( $this->test_dir . '/exclude_me' ); + file_put_contents( $this->test_dir . '/file1.txt', '12345' ); // 5 bytes. + file_put_contents( $this->test_dir . '/exclude_me/file2.txt', '1234567890' ); // 10 bytes. + + $this->assertSame( 5, recurse_dirsize( $this->test_dir, $this->test_dir . '/exclude_me' ) ); + } + + /** + * Tests the pre_recurse_dirsize filter. + * + * @ticket 65654 + */ + public function test_pre_recurse_dirsize_filter() { + add_filter( + 'pre_recurse_dirsize', + function() { + return 1234; + } + ); + $this->assertSame( 1234, recurse_dirsize( $this->test_dir ) ); + } + + /** + * Tests that the result is cached in a transient. + * + * @ticket 65654 + */ + public function test_recurse_dirsize_caching() { + file_put_contents( $this->test_dir . '/file1.txt', '12345' ); + + // First call populates cache. + recurse_dirsize( $this->test_dir ); + + $cache = get_transient( 'dirsize_cache' ); + $this->assertIsArray( $cache ); + $path = untrailingslashit( $this->test_dir ); + $this->assertArrayHasKey( $path, $cache ); + $this->assertSame( 5, $cache[ $path ] ); + + // Modify file and check that cached value is still returned. + file_put_contents( $this->test_dir . '/file1.txt', '1234567890' ); + $this->assertSame( 5, recurse_dirsize( $this->test_dir ), 'Cached value should be returned.' ); + } +}