Skip to content

Commit 14271c3

Browse files
committed
AC-479: Input validation for File Upload in Customer Address
* Unit test corrections for checkAllowedExtension
1 parent 1aa462e commit 14271c3

File tree

1 file changed

+69
-10
lines changed

1 file changed

+69
-10
lines changed

lib/internal/Magento/Framework/File/Test/Unit/UploaderTest.php

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use Magento\Framework\File\Uploader;
1111
use PHPUnit\Framework\TestCase;
1212
use PHPUnit\Framework\MockObject\MockObject;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Framework\Filesystem;
15+
use Magento\Framework\Filesystem\Directory\TargetDirectory;
16+
use Magento\Framework\Filesystem\DriverPool;
1317

1418
/**
1519
* Unit Test class for \Magento\Framework\File\Uploader
@@ -21,9 +25,37 @@ class UploaderTest extends TestCase
2125
*/
2226
private $uploader;
2327

28+
/**
29+
* Allowed extensions array
30+
*
31+
* @var array
32+
*/
33+
private $_allowedMimeTypes = [
34+
'php' => 'text/plain',
35+
'txt' => 'text/plain'
36+
];
37+
2438
protected function setUp(): void
2539
{
26-
$this->uploader = $this->createMock(Uploader::class);
40+
$class = new \ReflectionObject($this);
41+
$fileName = $class->getFilename();
42+
$fileType = 'php';
43+
$this->setupFiles(1230123, $fileName, $fileType);
44+
45+
$driverPool = $this->createMock(DriverPool::class);
46+
$directoryList = $this->createMock(DirectoryList::class);
47+
$filesystem = $this->createMock(Filesystem::class);
48+
$targetDirectory = $this->createMock(TargetDirectory::class);
49+
50+
$this->uploader = new Uploader(
51+
"fileId",
52+
null,
53+
$directoryList,
54+
$driverPool,
55+
$targetDirectory,
56+
$filesystem
57+
);
58+
$this->uploader->setAllowedExtensions(array_keys($this->_allowedMimeTypes));
2759
}
2860

2961
/**
@@ -87,11 +119,10 @@ public function getCorrectFileNameProvider()
87119
*/
88120
public function testCheckAllowedExtension(bool $isValid, string $extension)
89121
{
90-
$this->uploader
91-
->expects($this->never())
92-
->method('checkAllowedExtension')
93-
->with($extension)
94-
->willReturn($isValid);
122+
$this->assertEquals(
123+
$isValid,
124+
$this->uploader->checkAllowedExtension($extension)
125+
);
95126
}
96127

97128
/**
@@ -102,23 +133,51 @@ public function checkAllowedExtensionProvider(): array
102133
return [
103134
[
104135
true,
105-
'jpeg'
136+
'txt'
137+
],
138+
[
139+
false,
140+
'png'
106141
],
107142
[
108143
false,
109144
'$#@$#@$3'
110145
],
111146
[
112-
true,
113-
'4324324324jpeg'
147+
false,
148+
'4324324324txt'
114149
],
115150
[
116151
false,
117152
'$#$#$jpeg..$#2$#@$#@$'
118153
],
119154
[
120155
false,
121-
'../../jpeg'
156+
'../../txt'
157+
],
158+
[
159+
true,
160+
'php'
161+
]
162+
];
163+
}
164+
165+
/**
166+
* Setup global variable $_FILES.
167+
*
168+
* @param int $fileSize
169+
* @param string $fileName
170+
* @param string $fileType
171+
* @return void
172+
*/
173+
private function setupFiles($fileSize, $fileName, $fileType) {
174+
$_FILES = [
175+
'fileId' => [
176+
'name' => $fileName,
177+
'type' => $fileType,
178+
'tmp_name' => $fileName,
179+
'error' => 0,
180+
'size' => $fileSize,
122181
]
123182
];
124183
}

0 commit comments

Comments
 (0)