UploadedFileTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\UploadedFile;
  13. class UploadedFileTest extends TestCase
  14. {
  15. protected function setUp()
  16. {
  17. if (!ini_get('file_uploads')) {
  18. $this->markTestSkipped('file_uploads is disabled in php.ini');
  19. }
  20. }
  21. public function testConstructWhenFileNotExists()
  22. {
  23. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  24. new UploadedFile(
  25. __DIR__.'/Fixtures/not_here',
  26. 'original.gif',
  27. null
  28. );
  29. }
  30. public function testFileUploadsWithNoMimeType()
  31. {
  32. $file = new UploadedFile(
  33. __DIR__.'/Fixtures/test.gif',
  34. 'original.gif',
  35. null,
  36. filesize(__DIR__.'/Fixtures/test.gif'),
  37. UPLOAD_ERR_OK
  38. );
  39. $this->assertEquals('application/octet-stream', $file->getClientMimeType());
  40. if (\extension_loaded('fileinfo')) {
  41. $this->assertEquals('image/gif', $file->getMimeType());
  42. }
  43. }
  44. public function testFileUploadsWithUnknownMimeType()
  45. {
  46. $file = new UploadedFile(
  47. __DIR__.'/Fixtures/.unknownextension',
  48. 'original.gif',
  49. null,
  50. filesize(__DIR__.'/Fixtures/.unknownextension'),
  51. UPLOAD_ERR_OK
  52. );
  53. $this->assertEquals('application/octet-stream', $file->getClientMimeType());
  54. }
  55. public function testGuessClientExtension()
  56. {
  57. $file = new UploadedFile(
  58. __DIR__.'/Fixtures/test.gif',
  59. 'original.gif',
  60. 'image/gif',
  61. filesize(__DIR__.'/Fixtures/test.gif'),
  62. null
  63. );
  64. $this->assertEquals('gif', $file->guessClientExtension());
  65. }
  66. public function testGuessClientExtensionWithIncorrectMimeType()
  67. {
  68. $file = new UploadedFile(
  69. __DIR__.'/Fixtures/test.gif',
  70. 'original.gif',
  71. 'image/jpeg',
  72. filesize(__DIR__.'/Fixtures/test.gif'),
  73. null
  74. );
  75. $this->assertEquals('jpeg', $file->guessClientExtension());
  76. }
  77. public function testCaseSensitiveMimeType()
  78. {
  79. $file = new UploadedFile(
  80. __DIR__.'/Fixtures/case-sensitive-mime-type.xlsm',
  81. 'test.xlsm',
  82. 'application/vnd.ms-excel.sheet.macroEnabled.12',
  83. filesize(__DIR__.'/Fixtures/case-sensitive-mime-type.xlsm'),
  84. null
  85. );
  86. $this->assertEquals('xlsm', $file->guessClientExtension());
  87. }
  88. public function testErrorIsOkByDefault()
  89. {
  90. $file = new UploadedFile(
  91. __DIR__.'/Fixtures/test.gif',
  92. 'original.gif',
  93. 'image/gif',
  94. filesize(__DIR__.'/Fixtures/test.gif'),
  95. null
  96. );
  97. $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
  98. }
  99. public function testGetClientOriginalName()
  100. {
  101. $file = new UploadedFile(
  102. __DIR__.'/Fixtures/test.gif',
  103. 'original.gif',
  104. 'image/gif',
  105. filesize(__DIR__.'/Fixtures/test.gif'),
  106. null
  107. );
  108. $this->assertEquals('original.gif', $file->getClientOriginalName());
  109. }
  110. public function testGetClientOriginalExtension()
  111. {
  112. $file = new UploadedFile(
  113. __DIR__.'/Fixtures/test.gif',
  114. 'original.gif',
  115. 'image/gif',
  116. filesize(__DIR__.'/Fixtures/test.gif'),
  117. null
  118. );
  119. $this->assertEquals('gif', $file->getClientOriginalExtension());
  120. }
  121. /**
  122. * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
  123. */
  124. public function testMoveLocalFileIsNotAllowed()
  125. {
  126. $file = new UploadedFile(
  127. __DIR__.'/Fixtures/test.gif',
  128. 'original.gif',
  129. 'image/gif',
  130. filesize(__DIR__.'/Fixtures/test.gif'),
  131. UPLOAD_ERR_OK
  132. );
  133. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  134. }
  135. public function testMoveLocalFileIsAllowedInTestMode()
  136. {
  137. $path = __DIR__.'/Fixtures/test.copy.gif';
  138. $targetDir = __DIR__.'/Fixtures/directory';
  139. $targetPath = $targetDir.'/test.copy.gif';
  140. @unlink($path);
  141. @unlink($targetPath);
  142. copy(__DIR__.'/Fixtures/test.gif', $path);
  143. $file = new UploadedFile(
  144. $path,
  145. 'original.gif',
  146. 'image/gif',
  147. filesize($path),
  148. UPLOAD_ERR_OK,
  149. true
  150. );
  151. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  152. $this->assertFileExists($targetPath);
  153. $this->assertFileNotExists($path);
  154. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  155. @unlink($targetPath);
  156. }
  157. public function testGetClientOriginalNameSanitizeFilename()
  158. {
  159. $file = new UploadedFile(
  160. __DIR__.'/Fixtures/test.gif',
  161. '../../original.gif',
  162. 'image/gif',
  163. filesize(__DIR__.'/Fixtures/test.gif'),
  164. null
  165. );
  166. $this->assertEquals('original.gif', $file->getClientOriginalName());
  167. }
  168. public function testGetSize()
  169. {
  170. $file = new UploadedFile(
  171. __DIR__.'/Fixtures/test.gif',
  172. 'original.gif',
  173. 'image/gif',
  174. filesize(__DIR__.'/Fixtures/test.gif'),
  175. null
  176. );
  177. $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  178. $file = new UploadedFile(
  179. __DIR__.'/Fixtures/test',
  180. 'original.gif',
  181. 'image/gif'
  182. );
  183. $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
  184. }
  185. public function testGetExtension()
  186. {
  187. $file = new UploadedFile(
  188. __DIR__.'/Fixtures/test.gif',
  189. 'original.gif',
  190. null
  191. );
  192. $this->assertEquals('gif', $file->getExtension());
  193. }
  194. public function testIsValid()
  195. {
  196. $file = new UploadedFile(
  197. __DIR__.'/Fixtures/test.gif',
  198. 'original.gif',
  199. null,
  200. filesize(__DIR__.'/Fixtures/test.gif'),
  201. UPLOAD_ERR_OK,
  202. true
  203. );
  204. $this->assertTrue($file->isValid());
  205. }
  206. /**
  207. * @dataProvider uploadedFileErrorProvider
  208. */
  209. public function testIsInvalidOnUploadError($error)
  210. {
  211. $file = new UploadedFile(
  212. __DIR__.'/Fixtures/test.gif',
  213. 'original.gif',
  214. null,
  215. filesize(__DIR__.'/Fixtures/test.gif'),
  216. $error
  217. );
  218. $this->assertFalse($file->isValid());
  219. }
  220. public function uploadedFileErrorProvider()
  221. {
  222. return [
  223. [UPLOAD_ERR_INI_SIZE],
  224. [UPLOAD_ERR_FORM_SIZE],
  225. [UPLOAD_ERR_PARTIAL],
  226. [UPLOAD_ERR_NO_TMP_DIR],
  227. [UPLOAD_ERR_EXTENSION],
  228. ];
  229. }
  230. public function testIsInvalidIfNotHttpUpload()
  231. {
  232. $file = new UploadedFile(
  233. __DIR__.'/Fixtures/test.gif',
  234. 'original.gif',
  235. null,
  236. filesize(__DIR__.'/Fixtures/test.gif'),
  237. UPLOAD_ERR_OK
  238. );
  239. $this->assertFalse($file->isValid());
  240. }
  241. }