Skip to content

Commit

Permalink
Merge pull request #20 from WebFiori/dev
Browse files Browse the repository at this point in the history
feat: Added Upload Even if in Test Env
  • Loading branch information
usernane authored Apr 15, 2024
2 parents 41518d4 + f187504 commit 19aca84
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
28 changes: 17 additions & 11 deletions tests/webfiori/framework/test/UploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public function testUpload00() {
$this->assertEquals([
[
'name' => 'testUpload.txt',
'size' => 51,
'size' => '51',
'upload-path' => str_replace('/', DS, str_replace('\\', DS, __DIR__)),
'upload-error' => 'temp_file_not_moved',
'upload-error' => '',
'mime' => 'text/plain',
'is-exist' => false,
'is-replace' => false,
'uploaded' => false
'uploaded' => true
]
], $r);
return $u;
Expand All @@ -111,16 +111,21 @@ public function testUpload01(FileUploader $u) {
$this->assertTrue($file instanceof UploadedFile);
$this->assertEquals('testUpload.txt',$file->getName());
$this->assertEquals('testUpload',$file->getNameWithNoExt());
$this->assertFalse($file->isUploaded());
$this->assertTrue($file->isUploaded());
$this->assertFalse($file->isReplace());
$this->assertEquals('text/plain',$file->getMIME());
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)),$file->getDir());
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)).DS.'testUpload.txt',$file->getAbsolutePath());

$this->assertEquals("temp_file_not_moved",$file->getUploadError());
$this->assertEquals("",$file->getUploadError());
$this->assertEquals("{\"id\":-1,\"mime\":\"text\/plain\",\"name\":\"testUpload.txt\""
. ",\"directory\":\"".Json::escapeJSONSpecialChars($file->getDir())."\",\"sizeInBytes\":0,"
. "\"sizeInKBytes\":0,\"sizeInMBytes\":0,\"uploaded\":false,\"isReplace\":false,\"uploadError\":\"temp_file_not_moved\"}", $file.'');
. ",\"directory\":\"".Json::escapeJSONSpecialChars($file->getDir())."\",\"sizeInBytes\":51,"
. "\"sizeInKBytes\":0.0498046875,"
. "\"sizeInMBytes\":4.8637390136719E-5,"
. "\"uploaded\":true,"
. "\"isReplace\":false,"
. "\"uploadError\":\"\"}", $file.'');
$file->remove();
}
public function testUpload02() {
$this->expectException(FileException::class);
Expand Down Expand Up @@ -148,13 +153,14 @@ public function testToJson00() {
$this->assertTrue($file1 instanceof UploadedFile);
$this->assertEquals('testUpload.txt',$file1->getName());
$this->assertEquals('testUpload',$file1->getNameWithNoExt());
$this->assertFalse($file1->isUploaded());
$this->assertTrue($file1->isUploaded());
$this->assertFalse($file1->isReplace());
$this->assertEquals('text/plain',$file1->getMIME());
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)),$file1->getDir());
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)).DS.'testUpload.txt',$file1->getAbsolutePath());

$this->assertEquals("temp_file_not_moved",$file1->getUploadError());
$this->assertEquals("",$file1->getUploadError());
$file1->remove();

$file2 = $r[1];
$this->assertTrue($file2 instanceof UploadedFile);
Expand All @@ -180,9 +186,9 @@ public function testToJson00() {
. '"sizeInBytes":0,'
. '"sizeInKBytes":0,'
. '"sizeInMBytes":0,'
. '"uploaded":false,'
. '"uploaded":true,'
. '"isReplace":false,'
. '"uploadError":"temp_file_not_moved"},'
. '"uploadError":""},'
. '{"id":-1,"mime":'
. '"application\/octet-stream",'
. '"name":"not-allowed.xp",'
Expand Down
22 changes: 15 additions & 7 deletions webfiori/file/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ public function upload(bool $replaceIfExist = false) : array {
if (strtoupper($reqMeth) == 'POST') {
$fileOrFiles = null;
$associatedInputName = filter_input(INPUT_POST, 'file');

if (gettype($associatedInputName) != 'string' && isset($_POST['file'])) {
//Probably in cli (test env)
$associatedInputName = filter_var($_POST['file']);
}

if ($associatedInputName !== null) {
$this->setAssociatedFileName($associatedInputName);
Expand Down Expand Up @@ -464,7 +469,8 @@ private function getFileArr($fileOrFiles,$replaceIfExist, $idx = null): array {
$fileInfoArr[UploaderConst::ERR_INDEX] = '';
$nameSplit = explode('.', $fileInfoArr[UploaderConst::NAME_INDEX]);
$fileInfoArr[UploaderConst::MIME_INDEX] = MIME::getType($nameSplit[count($nameSplit) - 1]);

$fileInfoArr[UploaderConst::UPLOADED_INDEX] = false;

$isErr = $idx === null ? $this->isError($fileOrFiles[$errIdx]) : $this->isError($fileOrFiles[$errIdx][$idx]);

if (!$isErr) {
Expand All @@ -478,12 +484,14 @@ private function getFileArr($fileOrFiles,$replaceIfExist, $idx = null): array {
$fileInfoArr[UploaderConst::REPLACE_INDEX] = false;
$name = $idx === null ? $fileOrFiles[$tempIdx] : $fileOrFiles[$tempIdx][$idx];
$sanitizedName = filter_var($name, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if (move_uploaded_file($sanitizedName, $filePath)) {
$fileInfoArr[UploaderConst::UPLOADED_INDEX] = true;
} else {
$fileInfoArr[UploaderConst::UPLOADED_INDEX] = false;
//If in CLI, use copy (testing env)
$moveFunc = http_response_code() === false ? 'copy' : 'move_uploaded_file';

if (!($moveFunc($sanitizedName, $filePath))) {
$fileInfoArr[UploaderConst::ERR_INDEX] = UploaderConst::ERR_MOVE_TEMP;
} else {
$fileInfoArr[UploaderConst::UPLOADED_INDEX] = true;
}
} else {
$fileInfoArr[UploaderConst::EXIST_INDEX] = true;
Expand All @@ -501,7 +509,7 @@ private function getFileArr($fileOrFiles,$replaceIfExist, $idx = null): array {
}
} else {
$fileInfoArr[UploaderConst::REPLACE_INDEX] = false;
$fileInfoArr[UploaderConst::ERR_INDEX] = false;
$fileInfoArr[UploaderConst::ERR_INDEX] = UploaderConst::ALREADY_EXIST;
}
}
} else {
Expand Down
4 changes: 4 additions & 0 deletions webfiori/file/UploaderConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class UploaderConst {
* A constant that is used to indicate uploaded file type is not allowed.
*/
const ERR_NOT_ALLOWED = 'not_allowed_type';
/**
* A constant that is used to indicate uploaded file with same name was already uploaded.
*/
const ALREADY_EXIST = 'already_uploaded';
/**
* One of the constants which is used to initialize uploaded file array.
*/
Expand Down

0 comments on commit 19aca84

Please sign in to comment.