UploadedFileTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\Tests\Component\HttpFoundation\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 testErrorIsOkByDefault()
  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(UPLOAD_ERR_OK, $file->getError());
  64. }
  65. /**
  66. * @expectedException Symfony\Component\HttpFoundation\File\Exception\FileException
  67. */
  68. public function testMoveLocalFileIsNotAllowed()
  69. {
  70. $file = new UploadedFile(
  71. __DIR__.'/Fixtures/test.gif',
  72. 'original.gif',
  73. 'image/gif',
  74. filesize(__DIR__.'/Fixtures/test.gif'),
  75. UPLOAD_ERR_OK
  76. );
  77. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  78. }
  79. public function testMoveLocalFileIsAllowedInTestMode()
  80. {
  81. $path = __DIR__.'/Fixtures/test.copy.gif';
  82. $targetDir = __DIR__.'/Fixtures/directory';
  83. $targetPath = $targetDir.'/test.copy.gif';
  84. @unlink($path);
  85. @unlink($targetPath);
  86. copy(__DIR__.'/Fixtures/test.gif', $path);
  87. $file = new UploadedFile(
  88. $path,
  89. 'original.gif',
  90. 'image/gif',
  91. filesize($path),
  92. UPLOAD_ERR_OK,
  93. true
  94. );
  95. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  96. $this->assertTrue(file_exists($targetPath));
  97. $this->assertFalse(file_exists($path));
  98. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  99. @unlink($targetPath);
  100. }
  101. /**
  102. * @dataProvider getClientFilenameFixtures
  103. */
  104. public function testGetClientOriginalNameSanitizeFilename($filename, $sanitizedFilename)
  105. {
  106. $file = new UploadedFile(
  107. __DIR__.'/Fixtures/test.gif',
  108. $filename,
  109. 'image/gif',
  110. filesize(__DIR__.'/Fixtures/test.gif'),
  111. null
  112. );
  113. $this->assertEquals($sanitizedFilename, $file->getClientOriginalName());
  114. }
  115. public function getClientFilenameFixtures()
  116. {
  117. return array(
  118. array('original.gif', 'original.gif'),
  119. array('..\\..\\original.gif', 'original.gif'),
  120. array('../../original.gif', 'original.gif'),
  121. array('файлfile.gif', 'файлfile.gif'),
  122. array('..\\..\\файлfile.gif', 'файлfile.gif'),
  123. array('../../файлfile.gif', 'файлfile.gif'),
  124. );
  125. }
  126. public function testGetSize()
  127. {
  128. $file = new UploadedFile(
  129. __DIR__.'/Fixtures/test.gif',
  130. 'original.gif',
  131. 'image/gif',
  132. filesize(__DIR__.'/Fixtures/test.gif'),
  133. null
  134. );
  135. $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  136. $file = new UploadedFile(
  137. __DIR__.'/Fixtures/test',
  138. 'original.gif',
  139. 'image/gif'
  140. );
  141. $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
  142. }
  143. public function testGetExtension()
  144. {
  145. $file = new UploadedFile(
  146. __DIR__.'/Fixtures/test.gif',
  147. 'original.gif',
  148. null
  149. );
  150. $this->assertEquals('gif', $file->getExtension());
  151. }
  152. public function testIsValid()
  153. {
  154. $file = new UploadedFile(
  155. __DIR__.'/Fixtures/test.gif',
  156. 'original.gif',
  157. null,
  158. filesize(__DIR__.'/Fixtures/test.gif'),
  159. UPLOAD_ERR_OK
  160. );
  161. $this->assertTrue($file->isValid());
  162. }
  163. /**
  164. * @dataProvider uploadedFileErrorProvider
  165. */
  166. public function testIsInvalidOnUploadError($error)
  167. {
  168. $file = new UploadedFile(
  169. __DIR__.'/Fixtures/test.gif',
  170. 'original.gif',
  171. null,
  172. filesize(__DIR__.'/Fixtures/test.gif'),
  173. $error
  174. );
  175. $this->assertFalse($file->isValid());
  176. }
  177. public function uploadedFileErrorProvider()
  178. {
  179. return array(
  180. array(UPLOAD_ERR_INI_SIZE),
  181. array(UPLOAD_ERR_FORM_SIZE),
  182. array(UPLOAD_ERR_PARTIAL),
  183. array(UPLOAD_ERR_NO_TMP_DIR),
  184. array(UPLOAD_ERR_EXTENSION),
  185. );
  186. }
  187. }