Fixtures
2 years ago
MimeType
2 years ago
FakeFile.php
2 years ago
FileTest.php
2 years ago
UploadedFileTest.php
2 years ago
UploadedFileTest.php
302 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | * |
| 11 | * Modified by impress-org on 23-January-2024 using Strauss. |
| 12 | * @see https://github.com/BrianHenryIE/strauss |
| 13 | */ |
| 14 | |
| 15 | namespace Give\Vendors\Symfony\Component\HttpFoundation\Tests\File; |
| 16 | |
| 17 | use PHPUnit\Framework\TestCase; |
| 18 | use Give\Vendors\Symfony\Component\HttpFoundation\File\UploadedFile; |
| 19 | |
| 20 | class UploadedFileTest extends TestCase |
| 21 | { |
| 22 | protected function setUp() |
| 23 | { |
| 24 | if (!ini_get('file_uploads')) { |
| 25 | $this->markTestSkipped('file_uploads is disabled in php.ini'); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public function testConstructWhenFileNotExists() |
| 30 | { |
| 31 | $this->expectException('Give\Vendors\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException'); |
| 32 | |
| 33 | new UploadedFile( |
| 34 | __DIR__.'/Fixtures/not_here', |
| 35 | 'original.gif', |
| 36 | null |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | public function testFileUploadsWithNoMimeType() |
| 41 | { |
| 42 | $file = new UploadedFile( |
| 43 | __DIR__.'/Fixtures/test.gif', |
| 44 | 'original.gif', |
| 45 | null, |
| 46 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 47 | \UPLOAD_ERR_OK |
| 48 | ); |
| 49 | |
| 50 | $this->assertEquals('application/octet-stream', $file->getClientMimeType()); |
| 51 | |
| 52 | if (\extension_loaded('fileinfo')) { |
| 53 | $this->assertEquals('image/gif', $file->getMimeType()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public function testFileUploadsWithUnknownMimeType() |
| 58 | { |
| 59 | $file = new UploadedFile( |
| 60 | __DIR__.'/Fixtures/.unknownextension', |
| 61 | 'original.gif', |
| 62 | null, |
| 63 | filesize(__DIR__.'/Fixtures/.unknownextension'), |
| 64 | \UPLOAD_ERR_OK |
| 65 | ); |
| 66 | |
| 67 | $this->assertEquals('application/octet-stream', $file->getClientMimeType()); |
| 68 | } |
| 69 | |
| 70 | public function testGuessClientExtension() |
| 71 | { |
| 72 | $file = new UploadedFile( |
| 73 | __DIR__.'/Fixtures/test.gif', |
| 74 | 'original.gif', |
| 75 | 'image/gif', |
| 76 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 77 | null |
| 78 | ); |
| 79 | |
| 80 | $this->assertEquals('gif', $file->guessClientExtension()); |
| 81 | } |
| 82 | |
| 83 | public function testGuessClientExtensionWithIncorrectMimeType() |
| 84 | { |
| 85 | $file = new UploadedFile( |
| 86 | __DIR__.'/Fixtures/test.gif', |
| 87 | 'original.gif', |
| 88 | 'image/png', |
| 89 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 90 | null |
| 91 | ); |
| 92 | |
| 93 | $this->assertEquals('png', $file->guessClientExtension()); |
| 94 | } |
| 95 | |
| 96 | public function testCaseSensitiveMimeType() |
| 97 | { |
| 98 | $file = new UploadedFile( |
| 99 | __DIR__.'/Fixtures/case-sensitive-mime-type.xlsm', |
| 100 | 'test.xlsm', |
| 101 | 'application/vnd.ms-excel.sheet.macroEnabled.12', |
| 102 | filesize(__DIR__.'/Fixtures/case-sensitive-mime-type.xlsm'), |
| 103 | null |
| 104 | ); |
| 105 | |
| 106 | $this->assertEquals('xlsm', $file->guessClientExtension()); |
| 107 | } |
| 108 | |
| 109 | public function testErrorIsOkByDefault() |
| 110 | { |
| 111 | $file = new UploadedFile( |
| 112 | __DIR__.'/Fixtures/test.gif', |
| 113 | 'original.gif', |
| 114 | 'image/gif', |
| 115 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 116 | null |
| 117 | ); |
| 118 | |
| 119 | $this->assertEquals(\UPLOAD_ERR_OK, $file->getError()); |
| 120 | } |
| 121 | |
| 122 | public function testGetClientOriginalName() |
| 123 | { |
| 124 | $file = new UploadedFile( |
| 125 | __DIR__.'/Fixtures/test.gif', |
| 126 | 'original.gif', |
| 127 | 'image/gif', |
| 128 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 129 | null |
| 130 | ); |
| 131 | |
| 132 | $this->assertEquals('original.gif', $file->getClientOriginalName()); |
| 133 | } |
| 134 | |
| 135 | public function testGetClientOriginalExtension() |
| 136 | { |
| 137 | $file = new UploadedFile( |
| 138 | __DIR__.'/Fixtures/test.gif', |
| 139 | 'original.gif', |
| 140 | 'image/gif', |
| 141 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 142 | null |
| 143 | ); |
| 144 | |
| 145 | $this->assertEquals('gif', $file->getClientOriginalExtension()); |
| 146 | } |
| 147 | |
| 148 | public function testMoveLocalFileIsNotAllowed() |
| 149 | { |
| 150 | $this->expectException('Give\Vendors\Symfony\Component\HttpFoundation\File\Exception\FileException'); |
| 151 | $file = new UploadedFile( |
| 152 | __DIR__.'/Fixtures/test.gif', |
| 153 | 'original.gif', |
| 154 | 'image/gif', |
| 155 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 156 | \UPLOAD_ERR_OK |
| 157 | ); |
| 158 | |
| 159 | $file->move(__DIR__.'/Fixtures/directory'); |
| 160 | } |
| 161 | |
| 162 | public function testMoveLocalFileIsAllowedInTestMode() |
| 163 | { |
| 164 | $path = __DIR__.'/Fixtures/test.copy.gif'; |
| 165 | $targetDir = __DIR__.'/Fixtures/directory'; |
| 166 | $targetPath = $targetDir.'/test.copy.gif'; |
| 167 | @unlink($path); |
| 168 | @unlink($targetPath); |
| 169 | copy(__DIR__.'/Fixtures/test.gif', $path); |
| 170 | |
| 171 | $file = new UploadedFile( |
| 172 | $path, |
| 173 | 'original.gif', |
| 174 | 'image/gif', |
| 175 | filesize($path), |
| 176 | \UPLOAD_ERR_OK, |
| 177 | true |
| 178 | ); |
| 179 | |
| 180 | $movedFile = $file->move(__DIR__.'/Fixtures/directory'); |
| 181 | |
| 182 | $this->assertFileExists($targetPath); |
| 183 | $this->assertFileDoesNotExist($path); |
| 184 | $this->assertEquals(realpath($targetPath), $movedFile->getRealPath()); |
| 185 | |
| 186 | @unlink($targetPath); |
| 187 | } |
| 188 | |
| 189 | public function testGetClientOriginalNameSanitizeFilename() |
| 190 | { |
| 191 | $file = new UploadedFile( |
| 192 | __DIR__.'/Fixtures/test.gif', |
| 193 | '../../original.gif', |
| 194 | 'image/gif', |
| 195 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 196 | null |
| 197 | ); |
| 198 | |
| 199 | $this->assertEquals('original.gif', $file->getClientOriginalName()); |
| 200 | } |
| 201 | |
| 202 | public function testGetSize() |
| 203 | { |
| 204 | $file = new UploadedFile( |
| 205 | __DIR__.'/Fixtures/test.gif', |
| 206 | 'original.gif', |
| 207 | 'image/gif', |
| 208 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 209 | null |
| 210 | ); |
| 211 | |
| 212 | $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize()); |
| 213 | |
| 214 | $file = new UploadedFile( |
| 215 | __DIR__.'/Fixtures/test', |
| 216 | 'original.gif', |
| 217 | 'image/gif' |
| 218 | ); |
| 219 | |
| 220 | $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize()); |
| 221 | } |
| 222 | |
| 223 | public function testGetExtension() |
| 224 | { |
| 225 | $file = new UploadedFile( |
| 226 | __DIR__.'/Fixtures/test.gif', |
| 227 | 'original.gif', |
| 228 | null |
| 229 | ); |
| 230 | |
| 231 | $this->assertEquals('gif', $file->getExtension()); |
| 232 | } |
| 233 | |
| 234 | public function testIsValid() |
| 235 | { |
| 236 | $file = new UploadedFile( |
| 237 | __DIR__.'/Fixtures/test.gif', |
| 238 | 'original.gif', |
| 239 | null, |
| 240 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 241 | \UPLOAD_ERR_OK, |
| 242 | true |
| 243 | ); |
| 244 | |
| 245 | $this->assertTrue($file->isValid()); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @dataProvider uploadedFileErrorProvider |
| 250 | */ |
| 251 | public function testIsInvalidOnUploadError($error) |
| 252 | { |
| 253 | $file = new UploadedFile( |
| 254 | __DIR__.'/Fixtures/test.gif', |
| 255 | 'original.gif', |
| 256 | null, |
| 257 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 258 | $error |
| 259 | ); |
| 260 | |
| 261 | $this->assertFalse($file->isValid()); |
| 262 | } |
| 263 | |
| 264 | public function uploadedFileErrorProvider() |
| 265 | { |
| 266 | return [ |
| 267 | [\UPLOAD_ERR_INI_SIZE], |
| 268 | [\UPLOAD_ERR_FORM_SIZE], |
| 269 | [\UPLOAD_ERR_PARTIAL], |
| 270 | [\UPLOAD_ERR_NO_TMP_DIR], |
| 271 | [\UPLOAD_ERR_EXTENSION], |
| 272 | ]; |
| 273 | } |
| 274 | |
| 275 | public function testIsInvalidIfNotHttpUpload() |
| 276 | { |
| 277 | $file = new UploadedFile( |
| 278 | __DIR__.'/Fixtures/test.gif', |
| 279 | 'original.gif', |
| 280 | null, |
| 281 | filesize(__DIR__.'/Fixtures/test.gif'), |
| 282 | \UPLOAD_ERR_OK |
| 283 | ); |
| 284 | |
| 285 | $this->assertFalse($file->isValid()); |
| 286 | } |
| 287 | |
| 288 | public function testGetMaxFilesize() |
| 289 | { |
| 290 | $size = UploadedFile::getMaxFilesize(); |
| 291 | |
| 292 | $this->assertIsInt($size); |
| 293 | $this->assertGreaterThan(0, $size); |
| 294 | |
| 295 | if (0 === (int) ini_get('post_max_size') && 0 === (int) ini_get('upload_max_filesize')) { |
| 296 | $this->assertSame(\PHP_INT_MAX, $size); |
| 297 | } else { |
| 298 | $this->assertLessThan(\PHP_INT_MAX, $size); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 |