diff --git a/tests/phpunit/data/functions/dummy.txt b/tests/phpunit/data/functions/dummy.txt new file mode 100644 index 0000000000..7614ea709c --- /dev/null +++ b/tests/phpunit/data/functions/dummy.txt @@ -0,0 +1 @@ +This is a dummy text file which is only used by the `Tests_Functions_CleanDirsizeCache::test_recurse_dirsize_without_transient()` test. diff --git a/tests/phpunit/tests/functions/cleanDirsizeCache.php b/tests/phpunit/tests/functions/cleanDirsizeCache.php index d600bdca54..27aacda62b 100644 --- a/tests/phpunit/tests/functions/cleanDirsizeCache.php +++ b/tests/phpunit/tests/functions/cleanDirsizeCache.php @@ -1 +1,140 @@ -expectNotice(); $this->expectNoticeMessage( $expected_message ); clean_dirsize_cache( $path ); } /** * Data provider. * * @return array */ public function data_clean_dirsize_cache_with_invalid_inputs() { return array( 'null' => array( 'path' => null, 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received NULL.', ), 'bool false' => array( 'path' => false, 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received boolean.', ), 'empty string' => array( 'path' => '', 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received string.', ), 'array' => array( 'path' => array( '.', './second/path/' ), 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received array.', ), ); } /** * Test the handling of a non-path text string passed as the $path parameter. * * @ticket 52241 * * @covers ::clean_dirsize_cache * * @dataProvider data_clean_dirsize_cache_with_non_path_string * * @param string $path Path input to use in the test. * @param int $expected_count Expected number of paths in the cache after cleaning. */ public function test_clean_dirsize_cache_with_non_path_string( $path, $expected_count ) { // Set the dirsize cache to our mock. set_transient( 'dirsize_cache', $this->mock_dirsize_cache_with_non_path_string() ); clean_dirsize_cache( $path ); $cache = get_transient( 'dirsize_cache' ); $this->assertIsArray( $cache ); $this->assertCount( $expected_count, $cache ); } /** * Data provider. * * @return array */ public function data_clean_dirsize_cache_with_non_path_string() { return array( 'single dot' => array( 'path' => '.', 'expected_count' => 1, ), 'non-path' => array( 'path' => 'string', 'expected_count' => 1, ), 'non-existant string, but non-path' => array( 'path' => 'doesnotexist', 'expected_count' => 2, ), ); } private function mock_dirsize_cache_with_non_path_string() { return array( '.' => array( 'size' => 50 ), 'string' => array( 'size' => 42 ), ); } /** * Test the behaviour of the function when the transient doesn't exist. * * @ticket 52241 * @ticket 53635 * * @covers ::recurse_dirsize */ public function test_recurse_dirsize_without_transient() { delete_transient( 'dirsize_cache' ); $size = recurse_dirsize( __DIR__ . '/fixtures' ); $this->assertGreaterThan( 10, $size ); } /** * Test the behaviour of the function when the transient does exist, but is not an array. * * In particular, this tests that no PHP TypeErrors are being thrown. * * @ticket 52241 * @ticket 53635 * * @covers ::recurse_dirsize */ public function test_recurse_dirsize_with_invalid_transient() { set_transient( 'dirsize_cache', 'this is not a valid transient for dirsize cache' ); $size = recurse_dirsize( __DIR__ . '/fixtures' ); $this->assertGreaterThan( 10, $size ); } } \ No newline at end of file +expectNotice(); + $this->expectNoticeMessage( $expected_message ); + + clean_dirsize_cache( $path ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_clean_dirsize_cache_with_invalid_inputs() { + return array( + 'null' => array( + 'path' => null, + 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received NULL.', + ), + 'bool false' => array( + 'path' => false, + 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received boolean.', + ), + 'empty string' => array( + 'path' => '', + 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received string.', + ), + 'array' => array( + 'path' => array( '.', './second/path/' ), + 'expected_message' => 'clean_dirsize_cache() only accepts a non-empty path string, received array.', + ), + ); + } + + /** + * Test the handling of a non-path text string passed as the $path parameter. + * + * @ticket 52241 + * + * @covers ::clean_dirsize_cache + * + * @dataProvider data_clean_dirsize_cache_with_non_path_string + * + * @param string $path Path input to use in the test. + * @param int $expected_count Expected number of paths in the cache after cleaning. + */ + public function test_clean_dirsize_cache_with_non_path_string( $path, $expected_count ) { + // Set the dirsize cache to our mock. + set_transient( 'dirsize_cache', $this->mock_dirsize_cache_with_non_path_string() ); + + clean_dirsize_cache( $path ); + + $cache = get_transient( 'dirsize_cache' ); + $this->assertIsArray( $cache ); + $this->assertCount( $expected_count, $cache ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_clean_dirsize_cache_with_non_path_string() { + return array( + 'single dot' => array( + 'path' => '.', + 'expected_count' => 1, + ), + 'non-path' => array( + 'path' => 'string', + 'expected_count' => 1, + ), + 'non-existant string, but non-path' => array( + 'path' => 'doesnotexist', + 'expected_count' => 2, + ), + ); + } + + private function mock_dirsize_cache_with_non_path_string() { + return array( + '.' => array( 'size' => 50 ), + 'string' => array( 'size' => 42 ), + ); + } + + /** + * Test the behaviour of the function when the transient doesn't exist. + * + * @ticket 52241 + * @ticket 53635 + * + * @covers ::recurse_dirsize + */ + public function test_recurse_dirsize_without_transient() { + delete_transient( 'dirsize_cache' ); + + $size = recurse_dirsize( DIR_TESTDATA . '/functions' ); + + $this->assertGreaterThan( 10, $size ); + } + + /** + * Test the behaviour of the function when the transient does exist, but is not an array. + * + * In particular, this tests that no PHP TypeErrors are being thrown. + * + * @ticket 52241 + * @ticket 53635 + * + * @covers ::recurse_dirsize + */ + public function test_recurse_dirsize_with_invalid_transient() { + set_transient( 'dirsize_cache', 'this is not a valid transient for dirsize cache' ); + + $size = recurse_dirsize( DIR_TESTDATA . '/functions' ); + + $this->assertGreaterThan( 10, $size ); + } +} diff --git a/tests/phpunit/tests/functions/fixtures/dummy.txt b/tests/phpunit/tests/functions/fixtures/dummy.txt deleted file mode 100644 index 7cc0f577ee..0000000000 --- a/tests/phpunit/tests/functions/fixtures/dummy.txt +++ /dev/null @@ -1 +0,0 @@ -This is a dummy text file which is only used by the `Tests_Multisite_CleanDirsizeCache::test_recurse_dirsize_without_transient()` test. \ No newline at end of file