UploadedFileTest.php 9.7 KB

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