Skip to content

Commit 97baf02

Browse files
committed
test: add test and format for get_dir_file_info fix
1 parent b966329 commit 97baf02

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

‎system/Helpers/filesystem_helper.php‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ function directory_mirror(string $originDir, string $targetDir, bool $overwrite
8585
$dirLen = strlen($originDir);
8686

8787
/** @var SplFileInfo $file */
88-
foreach (new RecursiveIteratorIterator(
89-
new RecursiveDirectoryIterator($originDir, FilesystemIterator::SKIP_DOTS),
90-
RecursiveIteratorIterator::SELF_FIRST,
91-
) as $file) {
88+
foreach (
89+
new RecursiveIteratorIterator(
90+
new RecursiveDirectoryIterator($originDir, FilesystemIterator::SKIP_DOTS),
91+
RecursiveIteratorIterator::SELF_FIRST,
92+
) as $file
93+
) {
9294
$origin = $file->getPathname();
9395
$target = $targetDir . substr($origin, $dirLen);
9496

@@ -159,10 +161,12 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
159161
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
160162

161163
try {
162-
foreach (new RecursiveIteratorIterator(
163-
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
164-
RecursiveIteratorIterator::CHILD_FIRST,
165-
) as $object) {
164+
foreach (
165+
new RecursiveIteratorIterator(
166+
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
167+
RecursiveIteratorIterator::CHILD_FIRST,
168+
) as $object
169+
) {
166170
$filename = $object->getFilename();
167171
if (! $hidden && $filename[0] === '.') {
168172
continue;
@@ -212,10 +216,12 @@ function get_filenames(
212216
$sourceDir = rtrim($sourceDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
213217

214218
try {
215-
foreach (new RecursiveIteratorIterator(
216-
new RecursiveDirectoryIterator($sourceDir, RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
217-
RecursiveIteratorIterator::SELF_FIRST,
218-
) as $name => $object) {
219+
foreach (
220+
new RecursiveIteratorIterator(
221+
new RecursiveDirectoryIterator($sourceDir, RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
222+
RecursiveIteratorIterator::SELF_FIRST,
223+
) as $name => $object
224+
) {
219225
$basename = pathinfo($name, PATHINFO_BASENAME);
220226
if (! $hidden && $basename[0] === '.') {
221227
continue;
@@ -403,18 +409,18 @@ function symbolic_permissions(int $perms): string
403409

404410
// Owner
405411
$symbolic .= ((($perms & 0x0100) !== 0) ? 'r' : '-')
406-
. ((($perms & 0x0080) !== 0) ? 'w' : '-')
407-
. ((($perms & 0x0040) !== 0) ? ((($perms & 0x0800) !== 0) ? 's' : 'x') : ((($perms & 0x0800) !== 0) ? 'S' : '-'));
412+
. ((($perms & 0x0080) !== 0) ? 'w' : '-')
413+
. ((($perms & 0x0040) !== 0) ? ((($perms & 0x0800) !== 0) ? 's' : 'x') : ((($perms & 0x0800) !== 0) ? 'S' : '-'));
408414

409415
// Group
410416
$symbolic .= ((($perms & 0x0020) !== 0) ? 'r' : '-')
411-
. ((($perms & 0x0010) !== 0) ? 'w' : '-')
412-
. ((($perms & 0x0008) !== 0) ? ((($perms & 0x0400) !== 0) ? 's' : 'x') : ((($perms & 0x0400) !== 0) ? 'S' : '-'));
417+
. ((($perms & 0x0010) !== 0) ? 'w' : '-')
418+
. ((($perms & 0x0008) !== 0) ? ((($perms & 0x0400) !== 0) ? 's' : 'x') : ((($perms & 0x0400) !== 0) ? 'S' : '-'));
413419

414420
// World
415421
$symbolic .= ((($perms & 0x0004) !== 0) ? 'r' : '-')
416-
. ((($perms & 0x0002) !== 0) ? 'w' : '-')
417-
. ((($perms & 0x0001) !== 0) ? ((($perms & 0x0200) !== 0) ? 't' : 'x') : ((($perms & 0x0200) !== 0) ? 'T' : '-'));
422+
. ((($perms & 0x0002) !== 0) ? 'w' : '-')
423+
. ((($perms & 0x0001) !== 0) ? ((($perms & 0x0200) !== 0) ? 't' : 'x') : ((($perms & 0x0200) !== 0) ? 'T' : '-'));
418424

419425
return $symbolic;
420426
}

‎tests/system/Helpers/FilesystemHelperTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,25 @@ public function testGetDirFileInfoFailure(): void
477477
$this->assertSame($expected, get_dir_file_info(SUPPORTPATH . 'Files#baker'));
478478
}
479479

480+
public function testGetDirFileInfoIgnoresDirectoriesWhenTopLevelOnlyIsTrue(): void
481+
{
482+
$dir = WRITEPATH . 'test_get_dir_file_info';
483+
mkdir($dir . '/subdir', 0777, true);
484+
file_put_contents($dir . '/file.txt', 'test');
485+
486+
$result = get_dir_file_info($dir, true);
487+
488+
// Clean up
489+
unlink($dir . '/file.txt');
490+
rmdir($dir . '/subdir');
491+
rmdir($dir);
492+
493+
// 'subdir' is a directory. When $topLevelOnly is true, get_dir_file_info passes it to get_file_info,
494+
// which returns null because is_file() is false. The function should ignore it completely.
495+
$this->assertArrayHasKey('file.txt', $result);
496+
$this->assertArrayNotHasKey('subdir', $result);
497+
}
498+
480499
public function testGetFileInfo(): void
481500
{
482501
$file = SUPPORTPATH . 'Files/baker/banana.php';

0 commit comments

Comments
 (0)