You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

299 lines
7.8 KiB

  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->expectException('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. public function testMoveLocalFileIsNotAllowed()
  122. {
  123. $this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileException');
  124. $file = new UploadedFile(
  125. __DIR__.'/Fixtures/test.gif',
  126. 'original.gif',
  127. 'image/gif',
  128. filesize(__DIR__.'/Fixtures/test.gif'),
  129. UPLOAD_ERR_OK
  130. );
  131. $file->move(__DIR__.'/Fixtures/directory');
  132. }
  133. public function testMoveLocalFileIsAllowedInTestMode()
  134. {
  135. $path = __DIR__.'/Fixtures/test.copy.gif';
  136. $targetDir = __DIR__.'/Fixtures/directory';
  137. $targetPath = $targetDir.'/test.copy.gif';
  138. @unlink($path);
  139. @unlink($targetPath);
  140. copy(__DIR__.'/Fixtures/test.gif', $path);
  141. $file = new UploadedFile(
  142. $path,
  143. 'original.gif',
  144. 'image/gif',
  145. filesize($path),
  146. UPLOAD_ERR_OK,
  147. true
  148. );
  149. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  150. $this->assertFileExists($targetPath);
  151. $this->assertFileNotExists($path);
  152. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  153. @unlink($targetPath);
  154. }
  155. public function testGetClientOriginalNameSanitizeFilename()
  156. {
  157. $file = new UploadedFile(
  158. __DIR__.'/Fixtures/test.gif',
  159. '../../original.gif',
  160. 'image/gif',
  161. filesize(__DIR__.'/Fixtures/test.gif'),
  162. null
  163. );
  164. $this->assertEquals('original.gif', $file->getClientOriginalName());
  165. }
  166. public function testGetSize()
  167. {
  168. $file = new UploadedFile(
  169. __DIR__.'/Fixtures/test.gif',
  170. 'original.gif',
  171. 'image/gif',
  172. filesize(__DIR__.'/Fixtures/test.gif'),
  173. null
  174. );
  175. $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  176. $file = new UploadedFile(
  177. __DIR__.'/Fixtures/test',
  178. 'original.gif',
  179. 'image/gif'
  180. );
  181. $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
  182. }
  183. public function testGetExtension()
  184. {
  185. $file = new UploadedFile(
  186. __DIR__.'/Fixtures/test.gif',
  187. 'original.gif',
  188. null
  189. );
  190. $this->assertEquals('gif', $file->getExtension());
  191. }
  192. public function testIsValid()
  193. {
  194. $file = new UploadedFile(
  195. __DIR__.'/Fixtures/test.gif',
  196. 'original.gif',
  197. null,
  198. filesize(__DIR__.'/Fixtures/test.gif'),
  199. UPLOAD_ERR_OK,
  200. true
  201. );
  202. $this->assertTrue($file->isValid());
  203. }
  204. /**
  205. * @dataProvider uploadedFileErrorProvider
  206. */
  207. public function testIsInvalidOnUploadError($error)
  208. {
  209. $file = new UploadedFile(
  210. __DIR__.'/Fixtures/test.gif',
  211. 'original.gif',
  212. null,
  213. filesize(__DIR__.'/Fixtures/test.gif'),
  214. $error
  215. );
  216. $this->assertFalse($file->isValid());
  217. }
  218. public function uploadedFileErrorProvider()
  219. {
  220. return [
  221. [UPLOAD_ERR_INI_SIZE],
  222. [UPLOAD_ERR_FORM_SIZE],
  223. [UPLOAD_ERR_PARTIAL],
  224. [UPLOAD_ERR_NO_TMP_DIR],
  225. [UPLOAD_ERR_EXTENSION],
  226. ];
  227. }
  228. public function testIsInvalidIfNotHttpUpload()
  229. {
  230. $file = new UploadedFile(
  231. __DIR__.'/Fixtures/test.gif',
  232. 'original.gif',
  233. null,
  234. filesize(__DIR__.'/Fixtures/test.gif'),
  235. UPLOAD_ERR_OK
  236. );
  237. $this->assertFalse($file->isValid());
  238. }
  239. public function testGetMaxFilesize()
  240. {
  241. $size = UploadedFile::getMaxFilesize();
  242. $this->assertIsInt($size);
  243. $this->assertGreaterThan(0, $size);
  244. if (0 === (int) ini_get('post_max_size') && 0 === (int) ini_get('upload_max_filesize')) {
  245. $this->assertSame(PHP_INT_MAX, $size);
  246. } else {
  247. $this->assertLessThan(PHP_INT_MAX, $size);
  248. }
  249. }
  250. }