diff --git a/src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php b/src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php index 7ea2d59cdf..0aee4cd978 100644 --- a/src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php +++ b/src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php @@ -50,17 +50,10 @@ class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface return null; } - if (!$finfo = new \finfo(FILEINFO_MIME)) { + if (!$finfo = new \finfo(FILEINFO_MIME_TYPE)) { return null; } - $type = $finfo->file($path); - - // remove charset (added as of PHP 5.3) - if (false !== $pos = strpos($type, ';')) { - $type = substr($type, 0, $pos); - } - - return $type; + return $finfo->file($path); } } \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/Fixtures/.unknownextension b/tests/Symfony/Tests/Component/HttpFoundation/File/Fixtures/.unknownextension new file mode 100644 index 0000000000..4d1ae35ba2 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/Fixtures/.unknownextension @@ -0,0 +1 @@ +f \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php new file mode 100644 index 0000000000..a56bc9ac3c --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpFoundation\File; + +use Symfony\Component\HttpFoundation\File\File; +use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; +use Symfony\Component\HttpFoundation\File\MimeType\ContentTypeMimeTypeGuesser; +use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser; +use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; +use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException; + +class MimeTypeTest extends \PHPUnit_Framework_TestCase +{ + protected $path; + + public function testGuessImageWithoutExtension() + { + $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test')); + } + + public function testGuessImageWithContentTypeMimeTypeGuesser() + { + $guesser = MimeTypeGuesser::getInstance(); + $guesser->register(new ContentTypeMimeTypeGuesser()); + $this->assertEquals('image/gif', $guesser->guess(__DIR__.'/../Fixtures/test')); + } + + public function testGuessImageWithFileBinaryMimeTypeGuesser() + { + $guesser = MimeTypeGuesser::getInstance(); + $guesser->register(new FileBinaryMimeTypeGuesser()); + $this->assertEquals('image/gif', $guesser->guess(__DIR__.'/../Fixtures/test')); + } + + public function testGuessImageWithKnownExtension() + { + $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif')); + } + + public function testGuessFileWithUnknownExtension() + { + $this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension')); + } + + public function testGuessWithIncorrectPath() + { + $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException'); + MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here'); + } + + public function testGuessWithNonReadablePath() + { + $path = __DIR__.'/../Fixtures/to_delete'; + touch($path); + chmod($path, 0333); + + $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException'); + MimeTypeGuesser::getInstance()->guess($path); + + + } + public static function tearDownAfterClass() + { + $path = __DIR__.'/../Fixtures/to_delete'; + chmod($path, 0666); + @unlink($path); + } +} \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php index 5e5cb5d696..fe2fa432a4 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile; class UploadedFileTest extends \PHPUnit_Framework_TestCase { + public function testFileUploadsMustBeEnabled() { // we can't change this setting without modifying php.ini :( @@ -31,6 +32,42 @@ class UploadedFileTest extends \PHPUnit_Framework_TestCase } } + public function testFileUploadsWithNoMimeType() + { + // we can't change this setting without modifying php.ini :( + if (ini_get('file_uploads')) { + + $file = new UploadedFile( + __DIR__.'/Fixtures/test.gif', + 'original.gif', + null, + filesize(__DIR__.'/Fixtures/test.gif'), + UPLOAD_ERR_OK + ); + + $this->assertAttributeEquals('application/octet-stream', 'mimeType', $file); + $this->assertEquals('image/gif', $file->getMimeType()); + } + } + + public function testFileUploadsWithUnknownMimeType() + { + // we can't change this setting without modifying php.ini :( + if (ini_get('file_uploads')) { + + $file = new UploadedFile( + __DIR__.'/Fixtures/.unknownextension', + 'original.gif', + null, + filesize(__DIR__.'/Fixtures/.unknownextension'), + UPLOAD_ERR_OK + ); + + $this->assertAttributeEquals('application/octet-stream', 'mimeType', $file); + $this->assertEquals('application/octet-stream', $file->getMimeType()); + } + } + public function testErrorIsOkByDefault() { // we can't change this setting without modifying php.ini :( @@ -46,4 +83,19 @@ class UploadedFileTest extends \PHPUnit_Framework_TestCase $this->assertEquals(UPLOAD_ERR_OK, $file->getError()); } } + public function testGetOriginalName() + { + // we can't change this setting without modifying php.ini :( + if (ini_get('file_uploads')) { + $file = new UploadedFile( + __DIR__.'/Fixtures/test.gif', + 'original.gif', + 'image/gif', + filesize(__DIR__.'/Fixtures/test.gif'), + null + ); + + $this->assertEquals('original.gif', $file->getOriginalName()); + } + } } \ No newline at end of file