Skip to content

Commit

Permalink
Merge pull request #25 from WebFiori/dev
Browse files Browse the repository at this point in the history
 feat: Added a Method to Add Test Upload Files
  • Loading branch information
usernane committed Apr 29, 2024
2 parents 45882ce + cc9dd38 commit badb725
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 33 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit stderr="true" colors="true" bootstrap="tests/bootstrap.php">
<phpunit colors="true" bootstrap="tests/bootstrap.php">
<php>
</php>
<filter>
Expand Down
35 changes: 4 additions & 31 deletions tests/webfiori/framework/test/UploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testUpload00() {
$u = new FileUploader(__DIR__, [
'txt'
]);
$this->addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'testUpload.txt', true);
FileUploader::addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'testUpload.txt', true);
$r = $u->upload();
$this->assertEquals([
[
Expand Down Expand Up @@ -132,7 +132,7 @@ public function testUpload02() {
$this->expectExceptionMessage('Upload path is not set.');
$_SERVER['REQUEST_METHOD'] = 'post';
$u = new FileUploader();
$this->addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'testUpload.txt', true);
FileUploader::addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'testUpload.txt', true);
$r = $u->upload();
}
/**
Expand All @@ -145,8 +145,8 @@ public function testToJson00() {
$_SERVER['REQUEST_METHOD'] = 'post';
$this->assertTrue($u->addExt('txt'));
$this->assertFalse($u->addExt(' '));
$this->addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'testUpload.txt', true);
$this->addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'not-allowed.xp');
FileUploader::addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'testUpload.txt', true);
FileUploader::addTestFile('files', ROOT_PATH.'tests'.DS.'tmp'.DS.'not-allowed.xp');
$r = $u->uploadAsFileObj();

$file1 = $r[0];
Expand Down Expand Up @@ -222,31 +222,4 @@ public function testRemoveExt00() {
'txt', 'jpg', 'png'
], $u->getExts());
}
/**
* Adds a test file for testing upload functionality.
*
* @param string $fileIdx
* @param string $filePath
* @param type $reset
*/
public function addTestFile(string $fileIdx, string $filePath, $reset = false) {
if ($reset) {
$_FILES = [];
}
if (!isset($_FILES[$fileIdx])) {
$_FILES[$fileIdx] = [];
$_FILES[$fileIdx]['name'] = [];
$_FILES[$fileIdx]['type'] = [];
$_FILES[$fileIdx]['size'] = [];
$_FILES[$fileIdx]['tmp_name'] = [];
$_FILES[$fileIdx]['error'] = [];
}

$file = new File($filePath);
$_FILES[$fileIdx]['name'][] = $file->getName();
$_FILES[$fileIdx]['type'][] = $file->getMIME();
$_FILES[$fileIdx]['size'][] = $file->getSize();
$_FILES[$fileIdx]['tmp_name'][] = $file->getAbsolutePath();
$_FILES[$fileIdx]['error'][] = 0;
}
}
78 changes: 78 additions & 0 deletions webfiori/file/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,60 @@ public function addExts(array $arr) : array {

return $retVal;
}
/**
* Adds a file to the array $_FILES for testing files uploads.
*
* This method can be used to simulate the process of single file upload.
*
* @param string $fileIdx The name of the index that will hold the blob.
* This is usually represented by the attribute 'name' of file input in
* the front-end.
*
* @param string $filePath The path of the file within testing environment.
*
* @param bool $reset If set to true, the array $_FILES will be re-initialized.
*/
public static function addTestFile(string $fileIdx = '', string $filePath = '', bool $reset = false) {
if ($reset) {
$_FILES = [];
}

$trimmed = trim($fileIdx);

if (strlen($trimmed) == 0) {
return;
}

if (!isset($_FILES[$trimmed])) {
$_FILES[$trimmed] = [];
$_FILES[$trimmed]['name'] = [];
$_FILES[$trimmed]['type'] = [];
$_FILES[$trimmed]['size'] = [];
$_FILES[$trimmed]['tmp_name'] = [];
$_FILES[$trimmed]['error'] = [];
}
$info = self::extractPathAndName($filePath);
$path = $info['path'].DS.$info['name'];


if (!File::isFileExist($path)) {
throw new FileException('No file was found at \''.$path.'\'.');
}

$nameExpl = explode('.', $info['name']);
if (count($nameExpl) == 2) {
$ext = $nameExpl[1];
} else {
$ext = 'bin';
}

$_FILES[$trimmed]['name'][] = $info['name'];
$_FILES[$trimmed]['type'][] = MIME::getType($ext);
$_FILES[$trimmed]['size'][] = filesize($path);
$_FILES[$trimmed]['tmp_name'][] = $path;
$_FILES[$trimmed]['error'][] = 0;
}

/**
* Returns the name of the index at which the uploaded files will exist on in the array $_FILES.
*
Expand Down Expand Up @@ -459,6 +513,30 @@ private function createFileObjFromArray($arr) : UploadedFile {

return $file;
}
private static function extractPathAndName($absPath): array {
$DS = DIRECTORY_SEPARATOR;
$cleanPath = str_replace('\\', $DS, str_replace('/', $DS, trim($absPath)));
$pathArr = explode($DS, $cleanPath);

if (count($pathArr) != 0) {
$fPath = '';
$name = $pathArr[count($pathArr) - 1];

for ($x = 0 ; $x < count($pathArr) - 1 ; $x++) {
$fPath .= $pathArr[$x].$DS;
}

return [
'path' => $fPath,
'name' => $name
];
}

return [
'name' => $cleanPath,
'path' => ''
];
}
private function getFileArr($fileOrFiles,$replaceIfExist, $idx = null): array {
$errIdx = 'error';
$tempIdx = 'tmp_name';
Expand Down
2 changes: 1 addition & 1 deletion webfiori/file/MIME.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ class MIME {
*
*/
public static function getType(string $ext) : string {
$lowerCase = strtolower($ext);
$lowerCase = strtolower(trim($ext, ".\r\n\t\v\x00"));
$retVal = File::DEFAULT_MIME;

$types = self::TYPES;
Expand Down

0 comments on commit badb725

Please sign in to comment.