2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-09-09 13:40:40 +01:00
|
|
|
namespace Symfony\Tests\Component\HttpFoundation\File;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-09-09 13:40:40 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class UploadedFileTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-03-17 23:07:53 +00:00
|
|
|
protected function setUp()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!ini_get('file_uploads')) {
|
2011-03-17 23:07:53 +00:00
|
|
|
$this->markTestSkipped('file_uploads is disabled in php.ini');
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-06-14 09:40:42 +01:00
|
|
|
public function testConstructWhenFileNotExists()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
|
|
|
|
|
|
|
|
new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/not_here',
|
|
|
|
'original.gif',
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-02-06 04:20:21 +00:00
|
|
|
public function testFileUploadsWithNoMimeType()
|
|
|
|
{
|
2011-03-17 23:07:53 +00:00
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
null,
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
UPLOAD_ERR_OK
|
|
|
|
);
|
2011-02-06 04:20:21 +00:00
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
$this->assertEquals('application/octet-stream', $file->getClientMimeType());
|
|
|
|
|
2011-03-29 00:11:10 +01:00
|
|
|
if (extension_loaded('fileinfo')) {
|
|
|
|
$this->assertEquals('image/gif', $file->getMimeType());
|
|
|
|
}
|
2011-02-06 04:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileUploadsWithUnknownMimeType()
|
|
|
|
{
|
2011-03-17 23:07:53 +00:00
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/.unknownextension',
|
|
|
|
'original.gif',
|
|
|
|
null,
|
|
|
|
filesize(__DIR__.'/Fixtures/.unknownextension'),
|
|
|
|
UPLOAD_ERR_OK
|
|
|
|
);
|
2011-02-06 04:20:21 +00:00
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
$this->assertEquals('application/octet-stream', $file->getClientMimeType());
|
2011-02-06 04:20:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testErrorIsOkByDefault()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2011-03-17 23:07:53 +00:00
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
'image/gif',
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
null
|
|
|
|
);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-17 23:07:53 +00:00
|
|
|
$this->assertEquals(UPLOAD_ERR_OK, $file->getError());
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2011-03-17 23:07:53 +00:00
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
public function testGetClientOriginalName()
|
2011-02-06 04:20:21 +00:00
|
|
|
{
|
2011-03-17 23:07:53 +00:00
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
'image/gif',
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
null
|
|
|
|
);
|
2011-02-06 04:20:21 +00:00
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
$this->assertEquals('original.gif', $file->getClientOriginalName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\HttpFoundation\File\Exception\FileException
|
|
|
|
*/
|
|
|
|
public function testMoveLocalFileIsNotAllowed()
|
|
|
|
{
|
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
'image/gif',
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
UPLOAD_ERR_OK
|
|
|
|
);
|
|
|
|
|
|
|
|
$movedFile = $file->move(__DIR__.'/Fixtures/directory');
|
2011-02-06 04:20:21 +00:00
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
public function testMoveLocalFileIsAllowedInTestMode()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/Fixtures/test.copy.gif';
|
|
|
|
$targetDir = __DIR__.'/Fixtures/directory';
|
|
|
|
$targetPath = $targetDir.'/test.copy.gif';
|
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
|
|
|
|
|
$file = new UploadedFile(
|
|
|
|
$path,
|
|
|
|
'original.gif',
|
|
|
|
'image/gif',
|
|
|
|
filesize($path),
|
|
|
|
UPLOAD_ERR_OK,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$movedFile = $file->move(__DIR__.'/Fixtures/directory');
|
|
|
|
|
|
|
|
$this->assertTrue(file_exists($targetPath));
|
|
|
|
$this->assertFalse(file_exists($path));
|
|
|
|
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
|
|
|
|
|
|
|
@unlink($targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testGetClientOriginalNameSanitizeFilename()
|
2011-04-29 18:26:59 +01:00
|
|
|
{
|
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'../../original.gif',
|
|
|
|
'image/gif',
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
$this->assertEquals('original.gif', $file->getClientOriginalName());
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|
2011-06-14 09:40:42 +01:00
|
|
|
|
|
|
|
public function testGetSize()
|
|
|
|
{
|
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
'image/gif',
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
|
|
|
|
|
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test',
|
|
|
|
'original.gif',
|
|
|
|
'image/gif'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetExtension()
|
|
|
|
{
|
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('gif', $file->getExtension());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsValid()
|
|
|
|
{
|
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
null,
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
UPLOAD_ERR_OK
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($file->isValid());
|
|
|
|
}
|
2011-07-05 19:05:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider uploadedFileErrorProvider
|
|
|
|
*/
|
|
|
|
public function testIsInvalidOnUploadError($error)
|
|
|
|
{
|
|
|
|
$file = new UploadedFile(
|
|
|
|
__DIR__.'/Fixtures/test.gif',
|
|
|
|
'original.gif',
|
|
|
|
null,
|
|
|
|
filesize(__DIR__.'/Fixtures/test.gif'),
|
|
|
|
$error
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($file->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function uploadedFileErrorProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(UPLOAD_ERR_INI_SIZE),
|
|
|
|
array(UPLOAD_ERR_FORM_SIZE),
|
|
|
|
array(UPLOAD_ERR_PARTIAL),
|
|
|
|
array(UPLOAD_ERR_NO_TMP_DIR),
|
|
|
|
array(UPLOAD_ERR_EXTENSION),
|
|
|
|
);
|
|
|
|
}
|
2011-06-15 12:46:46 +01:00
|
|
|
}
|