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.
|
|
|
|
*/
|
|
|
|
|
2011-05-05 17:37:03 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator\Constraints;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\File;
|
|
|
|
use Symfony\Component\Validator\Constraints\FileValidator;
|
2011-06-14 09:47:04 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File as FileObject;
|
2011-07-05 19:05:01 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
protected $context;
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $validator;
|
|
|
|
protected $path;
|
|
|
|
protected $file;
|
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function setUp()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->validator = new FileValidator();
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->validator->initialize($this->context);
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FileValidatorTest';
|
|
|
|
$this->file = fopen($this->path, 'w');
|
|
|
|
}
|
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function tearDown()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
fclose($this->file);
|
2011-06-14 09:44:38 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = null;
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->validator = null;
|
|
|
|
$this->path = null;
|
|
|
|
$this->file = null;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid(null, new File()));
|
|
|
|
}
|
|
|
|
|
2010-12-10 09:47:43 +00:00
|
|
|
public function testEmptyStringIsValid()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-12-10 09:47:43 +00:00
|
|
|
$this->assertTrue($this->validator->isValid('', new File()));
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testExpectsStringCompatibleTypeOrFile()
|
|
|
|
{
|
|
|
|
$this->validator->isValid(new \stdClass(), new File());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidFile()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid($this->path, new File()));
|
|
|
|
}
|
|
|
|
|
2011-07-05 19:05:01 +01:00
|
|
|
public function testValidUploadedfile()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2011-07-05 19:05:01 +01:00
|
|
|
$file = new UploadedFile($this->path, 'originalName');
|
|
|
|
$this->assertTrue($this->validator->isValid($file, new File()));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testTooLargeBytes()
|
|
|
|
{
|
|
|
|
fwrite($this->file, str_repeat('0', 11));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
2011-06-14 09:47:04 +01:00
|
|
|
'maxSize' => 10,
|
|
|
|
'maxSizeMessage' => 'myMessage',
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ limit }}' => '10 bytes',
|
|
|
|
'{{ size }}' => '11 bytes',
|
|
|
|
'{{ file }}' => $this->path,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($this->getFile($this->path), $constraint));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooLargeKiloBytes()
|
|
|
|
{
|
|
|
|
fwrite($this->file, str_repeat('0', 1400));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
2011-06-14 09:47:04 +01:00
|
|
|
'maxSize' => '1k',
|
|
|
|
'maxSizeMessage' => 'myMessage',
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ limit }}' => '1 kB',
|
|
|
|
'{{ size }}' => '1.4 kB',
|
|
|
|
'{{ file }}' => $this->path,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($this->getFile($this->path), $constraint));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooLargeMegaBytes()
|
|
|
|
{
|
|
|
|
fwrite($this->file, str_repeat('0', 1400000));
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
2011-06-14 09:47:04 +01:00
|
|
|
'maxSize' => '1M',
|
|
|
|
'maxSizeMessage' => 'myMessage',
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ limit }}' => '1 MB',
|
|
|
|
'{{ size }}' => '1.4 MB',
|
|
|
|
'{{ file }}' => $this->path,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($this->getFile($this->path), $constraint));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testInvalidMaxSize()
|
|
|
|
{
|
|
|
|
$constraint = new File(array(
|
|
|
|
'maxSize' => '1abc',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->isValid($this->path, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidMimeType()
|
|
|
|
{
|
2011-06-14 09:47:04 +01:00
|
|
|
$file = $this
|
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPathname')
|
|
|
|
->will($this->returnValue($this->path))
|
|
|
|
;
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getMimeType')
|
|
|
|
->will($this->returnValue('image/jpg'))
|
|
|
|
;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$constraint = new File(array(
|
|
|
|
'mimeTypes' => array('image/png', 'image/jpg'),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($file, $constraint));
|
|
|
|
}
|
|
|
|
|
2011-09-01 15:10:16 +01:00
|
|
|
public function testValidWildcardMimeType()
|
|
|
|
{
|
|
|
|
$file = $this
|
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPathname')
|
|
|
|
->will($this->returnValue($this->path))
|
|
|
|
;
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getMimeType')
|
|
|
|
->will($this->returnValue('image/jpg'))
|
|
|
|
;
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2011-09-01 15:10:16 +01:00
|
|
|
$constraint = new File(array(
|
|
|
|
'mimeTypes' => array('image/*'),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($file, $constraint));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testInvalidMimeType()
|
|
|
|
{
|
2011-06-14 09:47:04 +01:00
|
|
|
$file = $this
|
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPathname')
|
|
|
|
->will($this->returnValue($this->path))
|
|
|
|
;
|
|
|
|
$file
|
2011-09-01 15:10:16 +01:00
|
|
|
->expects($this->once())
|
2011-06-14 09:47:04 +01:00
|
|
|
->method('getMimeType')
|
|
|
|
->will($this->returnValue('application/pdf'))
|
|
|
|
;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$constraint = new File(array(
|
|
|
|
'mimeTypes' => array('image/png', 'image/jpg'),
|
|
|
|
'mimeTypesMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ type }}' => '"application/pdf"',
|
|
|
|
'{{ types }}' => '"image/png", "image/jpg"',
|
|
|
|
'{{ file }}' => $this->path,
|
|
|
|
));
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($this->validator->isValid($file, $constraint));
|
|
|
|
}
|
2011-06-14 09:47:04 +01:00
|
|
|
|
2011-09-01 15:10:16 +01:00
|
|
|
public function testInvalidWildcardMimeType()
|
|
|
|
{
|
|
|
|
$file = $this
|
|
|
|
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
;
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPathname')
|
|
|
|
->will($this->returnValue($this->path))
|
|
|
|
;
|
|
|
|
$file
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getMimeType')
|
|
|
|
->will($this->returnValue('application/pdf'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$constraint = new File(array(
|
|
|
|
'mimeTypes' => array('image/*', 'image/jpg'),
|
|
|
|
'mimeTypesMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ type }}' => '"application/pdf"',
|
|
|
|
'{{ types }}' => '"image/*", "image/jpg"',
|
|
|
|
'{{ file }}' => $this->path,
|
|
|
|
));
|
|
|
|
|
2011-09-01 15:10:16 +01:00
|
|
|
$this->assertFalse($this->validator->isValid($file, $constraint));
|
|
|
|
}
|
|
|
|
|
2011-07-05 19:05:01 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider uploadedFileErrorProvider
|
|
|
|
*/
|
2012-02-01 12:53:45 +00:00
|
|
|
public function testUploadedFileError($error, $message, array $params = array())
|
2011-07-05 19:05:01 +01:00
|
|
|
{
|
|
|
|
$file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error);
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$constraint = new File(array(
|
|
|
|
$message => 'myMessage',
|
|
|
|
));
|
2011-07-05 19:05:01 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', $params);
|
2011-07-05 19:05:01 +01:00
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($file, $constraint));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uploadedFileErrorProvider()
|
|
|
|
{
|
|
|
|
return array(
|
2012-02-01 12:53:45 +00:00
|
|
|
array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array('{{ limit }}' => UploadedFile::getMaxFilesize() . ' bytes')),
|
2011-07-05 19:05:01 +01:00
|
|
|
array(UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'),
|
|
|
|
array(UPLOAD_ERR_PARTIAL, 'uploadErrorMessage'),
|
|
|
|
array(UPLOAD_ERR_NO_TMP_DIR, 'uploadErrorMessage'),
|
|
|
|
array(UPLOAD_ERR_EXTENSION, 'uploadErrorMessage'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
abstract protected function getFile($filename);
|
2011-06-15 12:46:46 +01:00
|
|
|
}
|