diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index 7e7782386c..93a5ec7057 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -1,4 +1,5 @@ 0) && is_dir($source_dir . $file)) { $filedata[$file] = directory_map($source_dir . $file, $new_depth, $hidden); - } - else + } else { $filedata[] = $file; } @@ -94,9 +95,10 @@ if ( ! function_exists('directory_map')) closedir($fp); return $filedata; + } catch (Exception $fe) + { + return []; } - - return []; } } @@ -180,8 +182,7 @@ if ( ! function_exists('delete_files')) if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.') { delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1); - } - elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename)) + } elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename)) { @unlink($path . DIRECTORY_SEPARATOR . $filename); } @@ -230,8 +231,7 @@ if ( ! function_exists('get_filenames')) if (is_dir($source_dir . $file) && $file[0] !== '.') { get_filenames($source_dir . $file . DIRECTORY_SEPARATOR, $include_path, true); - } - elseif ($file[0] !== '.') + } elseif ($file[0] !== '.') { $filedata[] = ($include_path === true) ? $source_dir . $file : $file; } @@ -285,8 +285,7 @@ if ( ! function_exists('get_dir_file_info')) if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false) { get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true); - } - elseif ($file[0] !== '.') + } elseif ($file[0] !== '.') { $filedata[$file] = get_file_info($source_dir . $file); $filedata[$file]['relative_path'] = $relative_path; @@ -318,9 +317,9 @@ if ( ! function_exists('get_file_info')) * @param string $file Path to file * @param mixed $returned_values Array or comma separated string of information returned * - * @return array + * @return array|null */ - function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date']): array + function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date']) { if ( ! file_exists($file)) { @@ -334,8 +333,7 @@ if ( ! function_exists('get_file_info')) foreach ($returned_values as $key) { - switch ($key) - { + switch ($key) { case 'name': $fileinfo['name'] = basename($file); break; @@ -387,32 +385,25 @@ if ( ! function_exists('symbolic_permissions')) if (($perms & 0xC000) === 0xC000) { $symbolic = 's'; // Socket - } - elseif (($perms & 0xA000) === 0xA000) + } elseif (($perms & 0xA000) === 0xA000) { $symbolic = 'l'; // Symbolic Link - } - elseif (($perms & 0x8000) === 0x8000) + } elseif (($perms & 0x8000) === 0x8000) { $symbolic = '-'; // Regular - } - elseif (($perms & 0x6000) === 0x6000) + } elseif (($perms & 0x6000) === 0x6000) { $symbolic = 'b'; // Block special - } - elseif (($perms & 0x4000) === 0x4000) + } elseif (($perms & 0x4000) === 0x4000) { $symbolic = 'd'; // Directory - } - elseif (($perms & 0x2000) === 0x2000) + } elseif (($perms & 0x2000) === 0x2000) { $symbolic = 'c'; // Character special - } - elseif (($perms & 0x1000) === 0x1000) + } elseif (($perms & 0x1000) === 0x1000) { $symbolic = 'p'; // FIFO pipe - } - else + } else { $symbolic = 'u'; // Unknown } @@ -483,8 +474,7 @@ if ( ! function_exists('set_realpath')) if (realpath($path) !== false) { $path = realpath($path); - } - elseif ($checkExistance && ! is_dir($path) && ! is_file($path)) + } elseif ($checkExistance && ! is_dir($path) && ! is_file($path)) { throw new InvalidArgumentException('Not a valid path: ' . $path); } diff --git a/tests/system/Helpers/FilesystemHelperTest.php b/tests/system/Helpers/FilesystemHelperTest.php index 6418516503..4e72410883 100644 --- a/tests/system/Helpers/FilesystemHelperTest.php +++ b/tests/system/Helpers/FilesystemHelperTest.php @@ -1,205 +1,249 @@ -structure = [ - 'foo' => [ - 'bar' => 'Once upon a midnight dreary', - 'baz' => 'While I pondered weak and weary' - ], - 'boo' => [ - 'far' => 'Upon a tome of long-forgotten lore', - 'faz' => 'There came a tapping up on the door' - ], - 'AnEmptyFolder' => [], - 'simpleFile' => 'A tap-tap-tapping upon my door', - '.hidden' => 'There is no spoon' - ]; - } + $this->structure = [ + 'foo' => [ + 'bar' => 'Once upon a midnight dreary', + 'baz' => 'While I pondered weak and weary' + ], + 'boo' => [ + 'far' => 'Upon a tome of long-forgotten lore', + 'faz' => 'There came a tapping up on the door' + ], + 'AnEmptyFolder' => [], + 'simpleFile' => 'A tap-tap-tapping upon my door', + '.hidden' => 'There is no spoon' + ]; + } - //-------------------------------------------------------------------- + //-------------------------------------------------------------------- - public function testDirectoryMapDefaults() - { - helper('filesystem'); - $this->assertTrue(function_exists('directory_map')); + public function testDirectoryMapDefaults() { + helper('filesystem'); + $this->assertTrue(function_exists('directory_map')); - $expected = [ - 'foo' . DIRECTORY_SEPARATOR => [ - 'bar', - 'baz' - ], - 'boo' . DIRECTORY_SEPARATOR => [ - 'far', - 'faz' - ], - 'AnEmptyFolder' . DIRECTORY_SEPARATOR => [], - 'simpleFile' - ]; + $expected = [ + 'foo' . DIRECTORY_SEPARATOR => [ + 'bar', + 'baz' + ], + 'boo' . DIRECTORY_SEPARATOR => [ + 'far', + 'faz' + ], + 'AnEmptyFolder' . DIRECTORY_SEPARATOR => [], + 'simpleFile' + ]; - $root = vfsStream::setup('root', null, $this->structure); - $this->assertTrue($root->hasChild('foo')); + $root = vfsStream::setup('root', null, $this->structure); + $this->assertTrue($root->hasChild('foo')); - $this->assertEquals($expected, directory_map(vfsStream::url('root'))); - } + $this->assertEquals($expected, directory_map(vfsStream::url('root'))); + } - public function testDirectoryMapShowsHiddenFiles() - { - helper('filesystem'); - $this->assertTrue(function_exists('directory_map')); + public function testDirectoryMapShowsHiddenFiles() { + helper('filesystem'); + $this->assertTrue(function_exists('directory_map')); - $expected = [ - 'foo' . DIRECTORY_SEPARATOR => [ - 'bar', - 'baz' - ], - 'boo' . DIRECTORY_SEPARATOR => [ - 'far', - 'faz' - ], - 'AnEmptyFolder' . DIRECTORY_SEPARATOR => [], - 'simpleFile', - '.hidden' - ]; + $expected = [ + 'foo' . DIRECTORY_SEPARATOR => [ + 'bar', + 'baz' + ], + 'boo' . DIRECTORY_SEPARATOR => [ + 'far', + 'faz' + ], + 'AnEmptyFolder' . DIRECTORY_SEPARATOR => [], + 'simpleFile', + '.hidden' + ]; - $root = vfsStream::setup('root', null, $this->structure); - $this->assertTrue($root->hasChild('foo')); + $root = vfsStream::setup('root', null, $this->structure); + $this->assertTrue($root->hasChild('foo')); - $this->assertEquals($expected, directory_map(vfsStream::url('root'), false, true)); - } + $this->assertEquals($expected, directory_map(vfsStream::url('root'), false, true)); + } - public function testDirectoryMapLimitsRecursion() - { - $this->assertTrue(function_exists('directory_map')); + public function testDirectoryMapLimitsRecursion() { + $this->assertTrue(function_exists('directory_map')); - $expected = [ - 'foo' . DIRECTORY_SEPARATOR, - 'boo' . DIRECTORY_SEPARATOR, - 'AnEmptyFolder' . DIRECTORY_SEPARATOR, - 'simpleFile', - '.hidden' - ]; + $expected = [ + 'foo' . DIRECTORY_SEPARATOR, + 'boo' . DIRECTORY_SEPARATOR, + 'AnEmptyFolder' . DIRECTORY_SEPARATOR, + 'simpleFile', + '.hidden' + ]; - $root = vfsStream::setup('root', null, $this->structure); - $this->assertTrue($root->hasChild('foo')); + $root = vfsStream::setup('root', null, $this->structure); + $this->assertTrue($root->hasChild('foo')); - $this->assertEquals($expected, directory_map(vfsStream::url('root'), 1, true)); - } + $this->assertEquals($expected, directory_map(vfsStream::url('root'), 1, true)); + } - //-------------------------------------------------------------------- + public function testDirectoryMapHandlesNotfound() { + $this->assertEquals([], directory_map(SUPPORTPATH . 'Files/shaker/')); + } - public function testWriteFileSuccess() - { - $vfs = vfsStream::setup('root'); + //-------------------------------------------------------------------- - $this->assertTrue(write_file(vfsStream::url('root/test.php'), 'Simple')); - $this->assertFileExists($vfs->getChild('test.php')->url()); - } + public function testWriteFileSuccess() { + $vfs = vfsStream::setup('root'); - //-------------------------------------------------------------------- + $this->assertTrue(write_file(vfsStream::url('root/test.php'), 'Simple')); + $this->assertFileExists($vfs->getChild('test.php')->url()); + } - public function testDeleteFilesDefaultsToOneLevelDeep() - { - $this->assertTrue(function_exists('delete_files')); + //-------------------------------------------------------------------- - $vfs = vfsStream::setup('root', null, $this->structure); + public function testDeleteFilesDefaultsToOneLevelDeep() { + $this->assertTrue(function_exists('delete_files')); - delete_files(vfsStream::url('root')); + $vfs = vfsStream::setup('root', null, $this->structure); - $this->assertFalse($vfs->hasChild('simpleFile')); - $this->assertFalse($vfs->hasChild('.hidden')); - $this->assertTrue($vfs->hasChild('foo')); - $this->assertTrue($vfs->hasChild('boo')); - $this->assertTrue($vfs->hasChild('AnEmptyFolder')); - } + delete_files(vfsStream::url('root')); - public function testDeleteFilesHandlesRecursion() - { - $this->assertTrue(function_exists('delete_files')); + $this->assertFalse($vfs->hasChild('simpleFile')); + $this->assertFalse($vfs->hasChild('.hidden')); + $this->assertTrue($vfs->hasChild('foo')); + $this->assertTrue($vfs->hasChild('boo')); + $this->assertTrue($vfs->hasChild('AnEmptyFolder')); + } - $vfs = vfsStream::setup('root', null, $this->structure); + public function testDeleteFilesHandlesRecursion() { + $this->assertTrue(function_exists('delete_files')); - delete_files(vfsStream::url('root'), true); + $vfs = vfsStream::setup('root', null, $this->structure); - $this->assertFalse($vfs->hasChild('simpleFile')); - $this->assertFalse($vfs->hasChild('.hidden')); - $this->assertFalse($vfs->hasChild('foo')); - $this->assertFalse($vfs->hasChild('boo')); - $this->assertFalse($vfs->hasChild('AnEmptyFolder')); - } + delete_files(vfsStream::url('root'), true); - public function testDeleteFilesLeavesHTFiles() - { - $structure = array_merge($this->structure, [ - '.htaccess' => 'Deny All', - 'index.html' => 'foo', - 'index.php' => 'blah' - ]); + $this->assertFalse($vfs->hasChild('simpleFile')); + $this->assertFalse($vfs->hasChild('.hidden')); + $this->assertFalse($vfs->hasChild('foo')); + $this->assertFalse($vfs->hasChild('boo')); + $this->assertFalse($vfs->hasChild('AnEmptyFolder')); + } - $vfs = vfsStream::setup('root', null, $structure); + public function testDeleteFilesLeavesHTFiles() { + $structure = array_merge($this->structure, [ + '.htaccess' => 'Deny All', + 'index.html' => 'foo', + 'index.php' => 'blah' + ]); - delete_files(vfsStream::url('root'), true, true); + $vfs = vfsStream::setup('root', null, $structure); - $this->assertFalse($vfs->hasChild('simpleFile')); - $this->assertFalse($vfs->hasChild('foo')); - $this->assertFalse($vfs->hasChild('boo')); - $this->assertFalse($vfs->hasChild('AnEmptyFolder')); - $this->assertTrue($vfs->hasChild('.htaccess')); - $this->assertTrue($vfs->hasChild('index.html')); - $this->assertTrue($vfs->hasChild('index.php')); - } + delete_files(vfsStream::url('root'), true, true); - //-------------------------------------------------------------------- + $this->assertFalse($vfs->hasChild('simpleFile')); + $this->assertFalse($vfs->hasChild('foo')); + $this->assertFalse($vfs->hasChild('boo')); + $this->assertFalse($vfs->hasChild('AnEmptyFolder')); + $this->assertTrue($vfs->hasChild('.htaccess')); + $this->assertTrue($vfs->hasChild('index.html')); + $this->assertTrue($vfs->hasChild('index.php')); + } - public function testGetFilenames() - { - $this->assertTrue(function_exists('delete_files')); + //-------------------------------------------------------------------- - // Not sure the directory names should actually show up - // here but this matches v3.x results. - $expected = [ - 'foo', - 'boo', - 'AnEmptyFolder', - 'simpleFile' - ]; + public function testGetFilenames() { + $this->assertTrue(function_exists('delete_files')); - $vfs = vfsStream::setup('root', null, $this->structure); + // Not sure the directory names should actually show up + // here but this matches v3.x results. + $expected = [ + 'foo', + 'boo', + 'AnEmptyFolder', + 'simpleFile' + ]; - $this->assertEquals($expected, get_filenames($vfs->url(), false)); - } + $vfs = vfsStream::setup('root', null, $this->structure); - public function testGetFilenamesWithSource() - { - $this->assertTrue(function_exists('delete_files')); + $this->assertEquals($expected, get_filenames($vfs->url(), false)); + } - // Not sure the directory names should actually show up - // here but this matches v3.x results. - $expected = [ - DIRECTORY_SEPARATOR . 'foo', - DIRECTORY_SEPARATOR . 'boo', - DIRECTORY_SEPARATOR . 'AnEmptyFolder', - DIRECTORY_SEPARATOR . 'simpleFile' - ]; + public function testGetFilenamesWithSource() { + $this->assertTrue(function_exists('delete_files')); - $vfs = vfsStream::setup('root', null, $this->structure); + // Not sure the directory names should actually show up + // here but this matches v3.x results. + $expected = [ + DIRECTORY_SEPARATOR . 'foo', + DIRECTORY_SEPARATOR . 'boo', + DIRECTORY_SEPARATOR . 'AnEmptyFolder', + DIRECTORY_SEPARATOR . 'simpleFile' + ]; - $this->assertEquals($expected, get_filenames($vfs->url(), true)); - } + $vfs = vfsStream::setup('root', null, $this->structure); - //-------------------------------------------------------------------- - - public function testGetFileInfo(){ - $vfs = vfsStream::setup('root', null, $this->structure); + $this->assertEquals($expected, get_filenames($vfs->url(), true)); + } + + //-------------------------------------------------------------------- + + public function testGetDirFileInfo() { + + $expected = [ + 'banana.php' => [ + 'name' => 'banana.php', + 'server_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker/banana.php', + 'size' => 193, + 'date' => 1529305930, + 'relative_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker', + ] + ]; + + + $this->assertEquals($expected, get_dir_file_info(SUPPORTPATH . 'Files/baker')); + } + + public function testGetFileInfo() { + + $expected = [ + 'name' => 'banana.php', + 'server_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker/banana.php', + 'size' => 193, + 'date' => 1529305930, + ]; + + + $this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/baker/banana.php')); + } + + public function testGetFileInfoCustom() { + + $expected = [ + 'readable' => true, + 'writable' => true, + 'executable' => false, + ]; + + $this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/baker/banana.php', 'readable,writable,executable')); + } + + public function testGetFileInfoPerms() { + + $expected = 0664; + + $stuff = get_file_info(SUPPORTPATH . 'Files/baker/banana.php', 'fileperms'); + $this->assertEquals($expected, $stuff['fileperms'] & 0777); + } + + public function testGetFileNotThereInfo() { + + $expected = null; + + $this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/icer')); + } - $this->assertEquals($expected, get_filenames($vfs->url(), true)); - - } }