Dashboard sipadu mbip
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UploadedFileTest.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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->expectException('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 testCaseSensitiveMimeType()
  82. {
  83. $file = new UploadedFile(
  84. __DIR__.'/Fixtures/case-sensitive-mime-type.xlsm',
  85. 'test.xlsm',
  86. 'application/vnd.ms-excel.sheet.macroEnabled.12',
  87. null
  88. );
  89. $this->assertEquals('xlsm', $file->guessClientExtension());
  90. }
  91. public function testErrorIsOkByDefault()
  92. {
  93. $file = new UploadedFile(
  94. __DIR__.'/Fixtures/test.gif',
  95. 'original.gif',
  96. 'image/gif',
  97. null
  98. );
  99. $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
  100. }
  101. public function testGetClientOriginalName()
  102. {
  103. $file = new UploadedFile(
  104. __DIR__.'/Fixtures/test.gif',
  105. 'original.gif',
  106. 'image/gif',
  107. null
  108. );
  109. $this->assertEquals('original.gif', $file->getClientOriginalName());
  110. }
  111. public function testGetClientOriginalExtension()
  112. {
  113. $file = new UploadedFile(
  114. __DIR__.'/Fixtures/test.gif',
  115. 'original.gif',
  116. 'image/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. UPLOAD_ERR_OK
  131. );
  132. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  133. }
  134. public function failedUploadedFile()
  135. {
  136. 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) {
  137. yield [new UploadedFile(
  138. __DIR__.'/Fixtures/test.gif',
  139. 'original.gif',
  140. 'image/gif',
  141. $error
  142. )];
  143. }
  144. }
  145. /**
  146. * @dataProvider failedUploadedFile
  147. */
  148. public function testMoveFailed(UploadedFile $file)
  149. {
  150. switch ($file->getError()) {
  151. case UPLOAD_ERR_INI_SIZE:
  152. $exceptionClass = IniSizeFileException::class;
  153. break;
  154. case UPLOAD_ERR_FORM_SIZE:
  155. $exceptionClass = FormSizeFileException::class;
  156. break;
  157. case UPLOAD_ERR_PARTIAL:
  158. $exceptionClass = PartialFileException::class;
  159. break;
  160. case UPLOAD_ERR_NO_FILE:
  161. $exceptionClass = NoFileException::class;
  162. break;
  163. case UPLOAD_ERR_CANT_WRITE:
  164. $exceptionClass = CannotWriteFileException::class;
  165. break;
  166. case UPLOAD_ERR_NO_TMP_DIR:
  167. $exceptionClass = NoTmpDirFileException::class;
  168. break;
  169. case UPLOAD_ERR_EXTENSION:
  170. $exceptionClass = ExtensionFileException::class;
  171. break;
  172. default:
  173. $exceptionClass = FileException::class;
  174. }
  175. $this->expectException($exceptionClass);
  176. $file->move(__DIR__.'/Fixtures/directory');
  177. }
  178. public function testMoveLocalFileIsAllowedInTestMode()
  179. {
  180. $path = __DIR__.'/Fixtures/test.copy.gif';
  181. $targetDir = __DIR__.'/Fixtures/directory';
  182. $targetPath = $targetDir.'/test.copy.gif';
  183. @unlink($path);
  184. @unlink($targetPath);
  185. copy(__DIR__.'/Fixtures/test.gif', $path);
  186. $file = new UploadedFile(
  187. $path,
  188. 'original.gif',
  189. 'image/gif',
  190. UPLOAD_ERR_OK,
  191. true
  192. );
  193. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  194. $this->assertFileExists($targetPath);
  195. $this->assertFileNotExists($path);
  196. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  197. @unlink($targetPath);
  198. }
  199. public function testGetClientOriginalNameSanitizeFilename()
  200. {
  201. $file = new UploadedFile(
  202. __DIR__.'/Fixtures/test.gif',
  203. '../../original.gif',
  204. 'image/gif'
  205. );
  206. $this->assertEquals('original.gif', $file->getClientOriginalName());
  207. }
  208. public function testGetSize()
  209. {
  210. $file = new UploadedFile(
  211. __DIR__.'/Fixtures/test.gif',
  212. 'original.gif',
  213. 'image/gif'
  214. );
  215. $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  216. $file = new UploadedFile(
  217. __DIR__.'/Fixtures/test',
  218. 'original.gif',
  219. 'image/gif'
  220. );
  221. $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
  222. }
  223. /**
  224. * @group legacy
  225. * @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1.
  226. */
  227. public function testConstructDeprecatedSize()
  228. {
  229. $file = new UploadedFile(
  230. __DIR__.'/Fixtures/test.gif',
  231. 'original.gif',
  232. 'image/gif',
  233. filesize(__DIR__.'/Fixtures/test.gif'),
  234. UPLOAD_ERR_OK,
  235. false
  236. );
  237. $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  238. }
  239. /**
  240. * @group legacy
  241. * @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1.
  242. */
  243. public function testConstructDeprecatedSizeWhenPassingOnlyThe4Needed()
  244. {
  245. $file = new UploadedFile(
  246. __DIR__.'/Fixtures/test.gif',
  247. 'original.gif',
  248. 'image/gif',
  249. filesize(__DIR__.'/Fixtures/test.gif')
  250. );
  251. $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  252. }
  253. public function testGetExtension()
  254. {
  255. $file = new UploadedFile(
  256. __DIR__.'/Fixtures/test.gif',
  257. 'original.gif'
  258. );
  259. $this->assertEquals('gif', $file->getExtension());
  260. }
  261. public function testIsValid()
  262. {
  263. $file = new UploadedFile(
  264. __DIR__.'/Fixtures/test.gif',
  265. 'original.gif',
  266. null,
  267. UPLOAD_ERR_OK,
  268. true
  269. );
  270. $this->assertTrue($file->isValid());
  271. }
  272. /**
  273. * @dataProvider uploadedFileErrorProvider
  274. */
  275. public function testIsInvalidOnUploadError($error)
  276. {
  277. $file = new UploadedFile(
  278. __DIR__.'/Fixtures/test.gif',
  279. 'original.gif',
  280. null,
  281. $error
  282. );
  283. $this->assertFalse($file->isValid());
  284. }
  285. public function uploadedFileErrorProvider()
  286. {
  287. return [
  288. [UPLOAD_ERR_INI_SIZE],
  289. [UPLOAD_ERR_FORM_SIZE],
  290. [UPLOAD_ERR_PARTIAL],
  291. [UPLOAD_ERR_NO_TMP_DIR],
  292. [UPLOAD_ERR_EXTENSION],
  293. ];
  294. }
  295. public function testIsInvalidIfNotHttpUpload()
  296. {
  297. $file = new UploadedFile(
  298. __DIR__.'/Fixtures/test.gif',
  299. 'original.gif',
  300. null,
  301. UPLOAD_ERR_OK
  302. );
  303. $this->assertFalse($file->isValid());
  304. }
  305. }