Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/phpunit/tests/functions/getDirsize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Tests the get_dirsize() function.
*
* @group functions
*
* @covers ::get_dirsize
*/
class Tests_Functions_GetDirsize extends WP_UnitTestCase {
private $test_dir;

public function set_up() {
parent::set_up();
$this->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' ) );
}
}
101 changes: 101 additions & 0 deletions tests/phpunit/tests/functions/recurseDirsize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* Tests the recurse_dirsize() function.
*
* @group functions
*
* @covers ::recurse_dirsize
*/
class Tests_Functions_RecurseDirsize extends WP_UnitTestCase {
private $test_dir;

public function set_up() {
parent::set_up();
$this->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() {

Check failure on line 73 in tests/phpunit/tests/functions/recurseDirsize.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Expected 1 space after FUNCTION keyword; 0 found

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function() {
static 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.' );
}
}
Loading