UploadedFileTest.php 5.7 KB

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