From c9ce6e1bd03f53b394f7ef10b98fbd2bafc00928 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 26 Dec 2020 11:11:35 -0800 Subject: [PATCH] file_size_encode() test. --- e107_handlers/file_class.php | 57 +++++++++++++++++----------- e107_tests/tests/unit/e_fileTest.php | 18 ++++++++- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/e107_handlers/file_class.php b/e107_handlers/file_class.php index 842262fd9..5dd20653d 100644 --- a/e107_handlers/file_class.php +++ b/e107_handlers/file_class.php @@ -811,7 +811,12 @@ while(false !== ($file = readdir($handle))) { - if(($file != '.') && ($file != '..') && !in_array($file, $this->dirFilter) && !in_array($file, $omit) && is_dir($path . '/' . $file) && ($fmask == '' || preg_match("#" . $fmask . "#", $file))) + if($file === '.' || $file === '..' || in_array($file, $this->dirFilter) || in_array($file, $omit)) + { + continue; + } + + if(is_dir($path . '/' . $file) && ($fmask == '' || preg_match("#" . $fmask . "#", $file))) { $ret[] = $file; } @@ -829,7 +834,7 @@ function rmtree($dir) { - if(substr($dir, -1) != '/') + if(substr($dir, -1) !== '/') { $dir .= '/'; } @@ -837,23 +842,26 @@ { while($obj = readdir($handle)) { - if($obj != '.' && $obj != '..') + if($obj === '.' || $obj === '..') { - if(is_dir($dir . $obj)) + continue; + } + + if(is_dir($dir . $obj)) + { + if(!$this->rmtree($dir . $obj)) { - if(!$this->rmtree($dir . $obj)) - { - return false; - } - } - elseif(is_file($dir . $obj)) - { - if(!unlink($dir . $obj)) - { - return false; - } + return false; } } + elseif(is_file($dir . $obj)) + { + if(!unlink($dir . $obj)) + { + return false; + } + } + } closedir($handle); @@ -950,34 +958,39 @@ { $size = filesize($size); } + + $kb = 1024; $mb = 1024 * $kb; $gb = 1024 * $mb; $tb = 1024 * $gb; + if(!$size) { return '0 ' . CORE_LAN_B; } if($size < $kb) { - return $size . " " . CORE_LAN_B; + $ret = $size . " " . CORE_LAN_B; } elseif($size < $mb) { - return round($size / $kb, $decimal) . " " . CORE_LAN_KB; + $ret = round($size / $kb, $decimal) . " " . CORE_LAN_KB; } elseif($size < $gb) { - return round($size / $mb, $decimal) . " " . CORE_LAN_MB; + $ret = round($size / $mb, $decimal) . " " . CORE_LAN_MB; } elseif($size < $tb) { - return round($size / $gb, $decimal) . " " . CORE_LAN_GB; + $ret = round($size / $gb, $decimal) . " " . CORE_LAN_GB; } else { - return round($size / $tb, 2) . " " . CORE_LAN_TB; + $ret = round($size / $tb, 2) . " " . CORE_LAN_TB; } + + return $ret; } @@ -1571,7 +1584,7 @@ // print_a($headers); - return (stripos($headers[0], "200 OK") || strpos($headers[0], "302")) ? true : false; + return (stripos($headers[0], "200 OK") || strpos($headers[0], "302")); } @@ -1854,7 +1867,7 @@ } - return array('success' => $success, 'error' => $error, 'skipped' => $skipped); + return compact('success', 'error', 'skipped'); } diff --git a/e107_tests/tests/unit/e_fileTest.php b/e107_tests/tests/unit/e_fileTest.php index 9e4406f4f..79a52c8e8 100644 --- a/e107_tests/tests/unit/e_fileTest.php +++ b/e107_tests/tests/unit/e_fileTest.php @@ -128,12 +128,26 @@ class e_fileTest extends \Codeception\Test\Unit { } - +*/ public function testFile_size_encode() { + $arr = array( + '1 kB' => 1024, + '2 kB' => 2048, + '1 MB' => 1048576, + '1 GB' => 1073741824, + '1 TB' => 1099511627776, + ); + + foreach($arr as $expected => $bytes) + { + $result = $this->fl->file_size_encode($bytes); + $this->assertSame($expected, $result); + + } } - +/* public function testMkDir() {