UploadedFileTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 testFileUploadsWithNoMimeType()
  21. {
  22. $file = new UploadedFile(
  23. __DIR__.'/Fixtures/test.gif',
  24. 'original.gif',
  25. null,
  26. filesize(__DIR__.'/Fixtures/test.gif'),
  27. UPLOAD_ERR_OK
  28. );
  29. $this->assertEquals('application/octet-stream', $file->getClientMimeType());
  30. if (extension_loaded('fileinfo')) {
  31. $this->assertEquals('image/gif', $file->getMimeType());
  32. }
  33. }
  34. public function testFileUploadsWithUnknownMimeType()
  35. {
  36. $file = new UploadedFile(
  37. __DIR__.'/Fixtures/.unknownextension',
  38. 'original.gif',
  39. null,
  40. filesize(__DIR__.'/Fixtures/.unknownextension'),
  41. UPLOAD_ERR_OK
  42. );
  43. $this->assertEquals('application/octet-stream', $file->getClientMimeType());
  44. }
  45. public function testErrorIsOkByDefault()
  46. {
  47. $file = new UploadedFile(
  48. __DIR__.'/Fixtures/test.gif',
  49. 'original.gif',
  50. 'image/gif',
  51. filesize(__DIR__.'/Fixtures/test.gif'),
  52. null
  53. );
  54. $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
  55. }
  56. public function testGetClientOriginalName()
  57. {
  58. $file = new UploadedFile(
  59. __DIR__.'/Fixtures/test.gif',
  60. 'original.gif',
  61. 'image/gif',
  62. filesize(__DIR__.'/Fixtures/test.gif'),
  63. null
  64. );
  65. $this->assertEquals('original.gif', $file->getClientOriginalName());
  66. }
  67. /**
  68. * @expectedException Symfony\Component\HttpFoundation\File\Exception\FileException
  69. */
  70. public function testMoveLocalFileIsNotAllowed()
  71. {
  72. $file = new UploadedFile(
  73. __DIR__.'/Fixtures/test.gif',
  74. 'original.gif',
  75. 'image/gif',
  76. filesize(__DIR__.'/Fixtures/test.gif'),
  77. UPLOAD_ERR_OK
  78. );
  79. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  80. }
  81. public function testMoveLocalFileIsAllowedInTestMode()
  82. {
  83. $path = __DIR__.'/Fixtures/test.copy.gif';
  84. $targetDir = __DIR__.'/Fixtures/directory';
  85. $targetPath = $targetDir.'/test.copy.gif';
  86. @unlink($path);
  87. @unlink($targetPath);
  88. copy(__DIR__.'/Fixtures/test.gif', $path);
  89. $file = new UploadedFile(
  90. $path,
  91. 'original.gif',
  92. 'image/gif',
  93. filesize($path),
  94. UPLOAD_ERR_OK,
  95. true
  96. );
  97. $movedFile = $file->move(__DIR__.'/Fixtures/directory');
  98. $this->assertTrue(file_exists($targetPath));
  99. $this->assertFalse(file_exists($path));
  100. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  101. @unlink($targetPath);
  102. }
  103. public function testGetClientOriginalNameSanitizeFilename()
  104. {
  105. $file = new UploadedFile(
  106. __DIR__.'/Fixtures/test.gif',
  107. '../../original.gif',
  108. 'image/gif',
  109. filesize(__DIR__.'/Fixtures/test.gif'),
  110. null
  111. );
  112. $this->assertEquals('original.gif', $file->getClientOriginalName());
  113. }
  114. }