UploadedFileTest.php 7.1 KB

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