From c855b827072e5bae9035b798ef991dc582f8c0a9 Mon Sep 17 00:00:00 2001 From: costdev Date: Sun, 5 Feb 2023 03:41:41 +0000 Subject: [PATCH] Add tests for `::dirlist()`. --- .../filesystem/wpFilesystemDirect/dirlist.php | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tests/phpunit/tests/filesystem/wpFilesystemDirect/dirlist.php diff --git a/tests/phpunit/tests/filesystem/wpFilesystemDirect/dirlist.php b/tests/phpunit/tests/filesystem/wpFilesystemDirect/dirlist.php new file mode 100644 index 0000000000000..9168bc64efaab --- /dev/null +++ b/tests/phpunit/tests/filesystem/wpFilesystemDirect/dirlist.php @@ -0,0 +1,131 @@ +dirlist( self::$file_structure['test_dir']['path'] . $path, $include_hidden, $recursive ); + + if ( is_array( $expected ) ) { + $this->assertSameSets( + $expected, + array_keys( $actual ), + 'The array keys do not match.' + ); + } else { + $this->assertFalse( + $actual, + '`WP_Filesystem_Direct::dirlist()` did not return false.' + ); + } + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_get_dirlist() { + return array( + 'a directory that exists excluding hidden files' => array( + 'path' => '', + 'include_hidden' => false, + 'recursive' => false, + 'expected' => array( + 'a_file_that_exists.txt', + 'subdir', + ), + ), + 'a directory that exists including hidden files' => array( + 'path' => '', + 'include_hidden' => true, + 'recursive' => false, + 'expected' => array( + 'a_file_that_exists.txt', + '.a_hidden_file', + 'subdir', + ), + ), + 'a directory that does not exist' => array( + 'path' => 'a_directory_that_does_not_exist/', + 'include_hidden' => true, + 'recursive' => false, + 'expected' => false, + ), + 'a file that exists' => array( + 'path' => 'a_file_that_exists.txt', + 'include_hidden' => true, + 'recursive' => false, + 'expected' => array( + 'a_file_that_exists.txt', + ), + ), + 'a file that does not exist' => array( + 'path' => 'a_file_that_does_not_exist.txt', + 'include_hidden' => true, + 'recursive' => false, + 'expected' => false, + ), + ); + } + + /** + * Tests that `WP_Filesystem_Direct::dirlist()` recurses + * into a subdirectory. + * + * @ticket 57774 + */ + public function test_should_recurse_into_subdirectory() { + $actual = self::$filesystem->dirlist( self::$file_structure['test_dir']['path'], true, true ); + + $this->assertIsArray( $actual, 'Did not return an array.' ); + $this->assertArrayHasKey( 'subdir', $actual, 'The subdirectory was not detected.' ); + $this->assertArrayHasKey( 'files', $actual['subdir'], 'The subdirectory does not have a "files" key.' ); + $this->assertNotEmpty( $actual['subdir']['files'], "The subdirectory's contents were not retrieved." ); + $this->assertArrayHasKey( 'subfile.txt', $actual['subdir']['files'], 'The subfile was not detected.' ); + } + + /** + * Tests that `WP_Filesystem_Direct::dirlist()` should not recurse + * into a subdirectory. + * + * @ticket 57774 + */ + public function test_should_not_recurse_into_subdirectory() { + + $actual = self::$filesystem->dirlist( self::$file_structure['test_dir']['path'], true, false ); + + $this->assertIsArray( $actual, 'Did not return an array.' ); + $this->assertArrayHasKey( 'subdir', $actual, 'The subdirectory was not detected.' ); + $this->assertArrayHasKey( 'files', $actual['subdir'], 'The "files" key was not set.' ); + $this->assertIsArray( $actual['subdir']['files'], 'The "files" key was not set to an array.' ); + $this->assertEmpty( $actual['subdir']['files'], 'The "files" array was not empty.' ); + } + +}