Skip to content

Commit 19aca84

Browse files
authored
Merge pull request #20 from WebFiori/dev
feat: Added Upload Even if in Test Env
2 parents 41518d4 + f187504 commit 19aca84

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

tests/webfiori/framework/test/UploaderTest.php

+17-11
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public function testUpload00() {
9090
$this->assertEquals([
9191
[
9292
'name' => 'testUpload.txt',
93-
'size' => 51,
93+
'size' => '51',
9494
'upload-path' => str_replace('/', DS, str_replace('\\', DS, __DIR__)),
95-
'upload-error' => 'temp_file_not_moved',
95+
'upload-error' => '',
9696
'mime' => 'text/plain',
9797
'is-exist' => false,
9898
'is-replace' => false,
99-
'uploaded' => false
99+
'uploaded' => true
100100
]
101101
], $r);
102102
return $u;
@@ -111,16 +111,21 @@ public function testUpload01(FileUploader $u) {
111111
$this->assertTrue($file instanceof UploadedFile);
112112
$this->assertEquals('testUpload.txt',$file->getName());
113113
$this->assertEquals('testUpload',$file->getNameWithNoExt());
114-
$this->assertFalse($file->isUploaded());
114+
$this->assertTrue($file->isUploaded());
115115
$this->assertFalse($file->isReplace());
116116
$this->assertEquals('text/plain',$file->getMIME());
117117
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)),$file->getDir());
118118
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)).DS.'testUpload.txt',$file->getAbsolutePath());
119119

120-
$this->assertEquals("temp_file_not_moved",$file->getUploadError());
120+
$this->assertEquals("",$file->getUploadError());
121121
$this->assertEquals("{\"id\":-1,\"mime\":\"text\/plain\",\"name\":\"testUpload.txt\""
122-
. ",\"directory\":\"".Json::escapeJSONSpecialChars($file->getDir())."\",\"sizeInBytes\":0,"
123-
. "\"sizeInKBytes\":0,\"sizeInMBytes\":0,\"uploaded\":false,\"isReplace\":false,\"uploadError\":\"temp_file_not_moved\"}", $file.'');
122+
. ",\"directory\":\"".Json::escapeJSONSpecialChars($file->getDir())."\",\"sizeInBytes\":51,"
123+
. "\"sizeInKBytes\":0.0498046875,"
124+
. "\"sizeInMBytes\":4.8637390136719E-5,"
125+
. "\"uploaded\":true,"
126+
. "\"isReplace\":false,"
127+
. "\"uploadError\":\"\"}", $file.'');
128+
$file->remove();
124129
}
125130
public function testUpload02() {
126131
$this->expectException(FileException::class);
@@ -148,13 +153,14 @@ public function testToJson00() {
148153
$this->assertTrue($file1 instanceof UploadedFile);
149154
$this->assertEquals('testUpload.txt',$file1->getName());
150155
$this->assertEquals('testUpload',$file1->getNameWithNoExt());
151-
$this->assertFalse($file1->isUploaded());
156+
$this->assertTrue($file1->isUploaded());
152157
$this->assertFalse($file1->isReplace());
153158
$this->assertEquals('text/plain',$file1->getMIME());
154159
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)),$file1->getDir());
155160
$this->assertEquals(str_replace('/', DS, str_replace('\\', DS, __DIR__)).DS.'testUpload.txt',$file1->getAbsolutePath());
156161

157-
$this->assertEquals("temp_file_not_moved",$file1->getUploadError());
162+
$this->assertEquals("",$file1->getUploadError());
163+
$file1->remove();
158164

159165
$file2 = $r[1];
160166
$this->assertTrue($file2 instanceof UploadedFile);
@@ -180,9 +186,9 @@ public function testToJson00() {
180186
. '"sizeInBytes":0,'
181187
. '"sizeInKBytes":0,'
182188
. '"sizeInMBytes":0,'
183-
. '"uploaded":false,'
189+
. '"uploaded":true,'
184190
. '"isReplace":false,'
185-
. '"uploadError":"temp_file_not_moved"},'
191+
. '"uploadError":""},'
186192
. '{"id":-1,"mime":'
187193
. '"application\/octet-stream",'
188194
. '"name":"not-allowed.xp",'

webfiori/file/FileUploader.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ public function upload(bool $replaceIfExist = false) : array {
389389
if (strtoupper($reqMeth) == 'POST') {
390390
$fileOrFiles = null;
391391
$associatedInputName = filter_input(INPUT_POST, 'file');
392+
393+
if (gettype($associatedInputName) != 'string' && isset($_POST['file'])) {
394+
//Probably in cli (test env)
395+
$associatedInputName = filter_var($_POST['file']);
396+
}
392397

393398
if ($associatedInputName !== null) {
394399
$this->setAssociatedFileName($associatedInputName);
@@ -464,7 +469,8 @@ private function getFileArr($fileOrFiles,$replaceIfExist, $idx = null): array {
464469
$fileInfoArr[UploaderConst::ERR_INDEX] = '';
465470
$nameSplit = explode('.', $fileInfoArr[UploaderConst::NAME_INDEX]);
466471
$fileInfoArr[UploaderConst::MIME_INDEX] = MIME::getType($nameSplit[count($nameSplit) - 1]);
467-
472+
$fileInfoArr[UploaderConst::UPLOADED_INDEX] = false;
473+
468474
$isErr = $idx === null ? $this->isError($fileOrFiles[$errIdx]) : $this->isError($fileOrFiles[$errIdx][$idx]);
469475

470476
if (!$isErr) {
@@ -478,12 +484,14 @@ private function getFileArr($fileOrFiles,$replaceIfExist, $idx = null): array {
478484
$fileInfoArr[UploaderConst::REPLACE_INDEX] = false;
479485
$name = $idx === null ? $fileOrFiles[$tempIdx] : $fileOrFiles[$tempIdx][$idx];
480486
$sanitizedName = filter_var($name, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
481-
482-
if (move_uploaded_file($sanitizedName, $filePath)) {
483-
$fileInfoArr[UploaderConst::UPLOADED_INDEX] = true;
484-
} else {
485-
$fileInfoArr[UploaderConst::UPLOADED_INDEX] = false;
487+
488+
//If in CLI, use copy (testing env)
489+
$moveFunc = http_response_code() === false ? 'copy' : 'move_uploaded_file';
490+
491+
if (!($moveFunc($sanitizedName, $filePath))) {
486492
$fileInfoArr[UploaderConst::ERR_INDEX] = UploaderConst::ERR_MOVE_TEMP;
493+
} else {
494+
$fileInfoArr[UploaderConst::UPLOADED_INDEX] = true;
487495
}
488496
} else {
489497
$fileInfoArr[UploaderConst::EXIST_INDEX] = true;
@@ -501,7 +509,7 @@ private function getFileArr($fileOrFiles,$replaceIfExist, $idx = null): array {
501509
}
502510
} else {
503511
$fileInfoArr[UploaderConst::REPLACE_INDEX] = false;
504-
$fileInfoArr[UploaderConst::ERR_INDEX] = false;
512+
$fileInfoArr[UploaderConst::ERR_INDEX] = UploaderConst::ALREADY_EXIST;
505513
}
506514
}
507515
} else {

webfiori/file/UploaderConst.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class UploaderConst {
2424
* A constant that is used to indicate uploaded file type is not allowed.
2525
*/
2626
const ERR_NOT_ALLOWED = 'not_allowed_type';
27+
/**
28+
* A constant that is used to indicate uploaded file with same name was already uploaded.
29+
*/
30+
const ALREADY_EXIST = 'already_uploaded';
2731
/**
2832
* One of the constants which is used to initialize uploaded file array.
2933
*/

0 commit comments

Comments
 (0)