/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site · PHP · 232 lines · 181 code · 37 blank · 14 comment · 2 complexity · e9df96b0d9d397a8f6480e2606494c0e MD5 · raw file

  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 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. public function testGetClientOriginalExtension()
  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('gif', $file->getClientOriginalExtension());
  86. }
  87. /**
  88. * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
  89. */
  90. public function testMoveLocalFileIsNotAllowed()
  91. {
  92. $file = new UploadedFile(
  93. __DIR__.'/Fixtures/test.gif',
  94. 'original.gif',
  95. 'image/gif',
  96. filesize(__DIR__.'/Fixtures/test.gif'),
  97. UPLOAD_ERR_OK
  98. );
  99. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  100. }
  101. public function testMoveLocalFileIsAllowedInTestMode()
  102. {
  103. $path = __DIR__.'/Fixtures/test.copy.gif';
  104. $targetDir = __DIR__.'/Fixtures/directory';
  105. $targetPath = $targetDir.'/test.copy.gif';
  106. @unlink($path);
  107. @unlink($targetPath);
  108. copy(__DIR__.'/Fixtures/test.gif', $path);
  109. $file = new UploadedFile(
  110. $path,
  111. 'original.gif',
  112. 'image/gif',
  113. filesize($path),
  114. UPLOAD_ERR_OK,
  115. true
  116. );
  117. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  118. $this->assertTrue(file_exists($targetPath));
  119. $this->assertFalse(file_exists($path));
  120. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  121. @unlink($targetPath);
  122. }
  123. public function testGetClientOriginalNameSanitizeFilename()
  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('original.gif', $file->getClientOriginalName());
  133. }
  134. public function testGetSize()
  135. {
  136. $file = new UploadedFile(
  137. __DIR__.'/Fixtures/test.gif',
  138. 'original.gif',
  139. 'image/gif',
  140. filesize(__DIR__.'/Fixtures/test.gif'),
  141. null
  142. );
  143. $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
  144. $file = new UploadedFile(
  145. __DIR__.'/Fixtures/test',
  146. 'original.gif',
  147. 'image/gif'
  148. );
  149. $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
  150. }
  151. public function testGetExtension()
  152. {
  153. $file = new UploadedFile(
  154. __DIR__.'/Fixtures/test.gif',
  155. 'original.gif',
  156. null
  157. );
  158. $this->assertEquals('gif', $file->getExtension());
  159. }
  160. public function testIsValid()
  161. {
  162. $file = new UploadedFile(
  163. __DIR__.'/Fixtures/test.gif',
  164. 'original.gif',
  165. null,
  166. filesize(__DIR__.'/Fixtures/test.gif'),
  167. UPLOAD_ERR_OK
  168. );
  169. $this->assertTrue($file->isValid());
  170. }
  171. /**
  172. * @dataProvider uploadedFileErrorProvider
  173. */
  174. public function testIsInvalidOnUploadError($error)
  175. {
  176. $file = new UploadedFile(
  177. __DIR__.'/Fixtures/test.gif',
  178. 'original.gif',
  179. null,
  180. filesize(__DIR__.'/Fixtures/test.gif'),
  181. $error
  182. );
  183. $this->assertFalse($file->isValid());
  184. }
  185. public function uploadedFileErrorProvider()
  186. {
  187. return array(
  188. array(UPLOAD_ERR_INI_SIZE),
  189. array(UPLOAD_ERR_FORM_SIZE),
  190. array(UPLOAD_ERR_PARTIAL),
  191. array(UPLOAD_ERR_NO_TMP_DIR),
  192. array(UPLOAD_ERR_EXTENSION),
  193. );
  194. }
  195. }