UploadedFileTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 testErrorIsOkByDefault()
  78. {
  79. $file = new UploadedFile(
  80. __DIR__.'/Fixtures/test.gif',
  81. 'original.gif',
  82. 'image/gif',
  83. filesize(__DIR__.'/Fixtures/test.gif'),
  84. null
  85. );
  86. $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
  87. }
  88. public function testGetClientOriginalName()
  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('original.gif', $file->getClientOriginalName());
  98. }
  99. public function testGetClientOriginalExtension()
  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('gif', $file->getClientOriginalExtension());
  109. }
  110. /**
  111. * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
  112. */
  113. public function testMoveLocalFileIsNotAllowed()
  114. {
  115. $file = new UploadedFile(
  116. __DIR__.'/Fixtures/test.gif',
  117. 'original.gif',
  118. 'image/gif',
  119. filesize(__DIR__.'/Fixtures/test.gif'),
  120. UPLOAD_ERR_OK
  121. );
  122. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  123. }
  124. public function testMoveLocalFileIsAllowedInTestMode()
  125. {
  126. $path = __DIR__.'/Fixtures/test.copy.gif';
  127. $targetDir = __DIR__.'/Fixtures/directory';
  128. $targetPath = $targetDir.'/test.copy.gif';
  129. @unlink($path);
  130. @unlink($targetPath);
  131. copy(__DIR__.'/Fixtures/test.gif', $path);
  132. $file = new UploadedFile(
  133. $path,
  134. 'original.gif',
  135. 'image/gif',
  136. filesize($path),
  137. UPLOAD_ERR_OK,
  138. true
  139. );
  140. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  141. $this->assertFileExists($targetPath);
  142. $this->assertFileNotExists($path);
  143. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  144. @unlink($targetPath);
  145. }
  146. public function testGetClientOriginalNameSanitizeFilename()
  147. {
  148. $file = new UploadedFile(
  149. __DIR__.'/Fixtures/test.gif',
  150. '../../original.gif',
  151. 'image/gif',
  152. filesize(__DIR__.'/Fixtures/test.gif'),
  153. null
  154. );
  155. $this->assertEquals('original.gif', $file->getClientOriginalName());
  156. }
  157. public function testGetSize()
  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(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  167. $file = new UploadedFile(
  168. __DIR__.'/Fixtures/test',
  169. 'original.gif',
  170. 'image/gif'
  171. );
  172. $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
  173. }
  174. public function testGetExtension()
  175. {
  176. $file = new UploadedFile(
  177. __DIR__.'/Fixtures/test.gif',
  178. 'original.gif',
  179. null
  180. );
  181. $this->assertEquals('gif', $file->getExtension());
  182. }
  183. public function testIsValid()
  184. {
  185. $file = new UploadedFile(
  186. __DIR__.'/Fixtures/test.gif',
  187. 'original.gif',
  188. null,
  189. filesize(__DIR__.'/Fixtures/test.gif'),
  190. UPLOAD_ERR_OK,
  191. true
  192. );
  193. $this->assertTrue($file->isValid());
  194. }
  195. /**
  196. * @dataProvider uploadedFileErrorProvider
  197. */
  198. public function testIsInvalidOnUploadError($error)
  199. {
  200. $file = new UploadedFile(
  201. __DIR__.'/Fixtures/test.gif',
  202. 'original.gif',
  203. null,
  204. filesize(__DIR__.'/Fixtures/test.gif'),
  205. $error
  206. );
  207. $this->assertFalse($file->isValid());
  208. }
  209. public function uploadedFileErrorProvider()
  210. {
  211. return array(
  212. array(UPLOAD_ERR_INI_SIZE),
  213. array(UPLOAD_ERR_FORM_SIZE),
  214. array(UPLOAD_ERR_PARTIAL),
  215. array(UPLOAD_ERR_NO_TMP_DIR),
  216. array(UPLOAD_ERR_EXTENSION),
  217. );
  218. }
  219. public function testIsInvalidIfNotHttpUpload()
  220. {
  221. $file = new UploadedFile(
  222. __DIR__.'/Fixtures/test.gif',
  223. 'original.gif',
  224. null,
  225. filesize(__DIR__.'/Fixtures/test.gif'),
  226. UPLOAD_ERR_OK
  227. );
  228. $this->assertFalse($file->isValid());
  229. }
  230. }