Skip to content

Commit

Permalink
Merge branch '2.1' into 2.2
Browse files Browse the repository at this point in the history
* 2.1:
  sub-requests are now created with the same class as their parent
  [FrameworkBundle] removed BC break
  [FrameworkBundle] changed temp kernel name in cache:clear
  [DoctrineBridge] Avoids blob values to be logged by doctrine
  [Security] use current request attributes to generate redirect url?
  [Validator] fix showing wrong max file size for upload errors
  [TwigBridge] removed double var initialization (refs #7344)
  [2.1][TwigBridge] Fixes Issue #7342 in TwigBridge
  [FrameworkBundle] fixed cahe:clear command's warmup
  [TwigBridge] now enter/leave scope on Twig_Node_Module
  [TwigBridge] fixed fixed scope & trans_default_domain node visitor
  [TwigBridge] fixed non probant tests & added new one
  [BrowserKit] added ability to ignored malformed set-cookie header
  [Translation] removed wriong 'use'
  [Translation] added xliff loader/dumper with resname support
  [TwigBridge] fixes

Conflicts:
	src/Symfony/Bundle/FrameworkBundle/HttpKernel.php
	src/Symfony/Component/Security/Http/HttpUtils.php
	src/Symfony/Component/Translation/Loader/XliffFileLoader.php
	src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php
  • Loading branch information
fabpot committed Mar 15, 2013
2 parents 8678b28 + 7aaa46e commit d1ed1c5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
27 changes: 20 additions & 7 deletions Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ public function validate($value, Constraint $constraint)
if ($value instanceof UploadedFile && !$value->isValid()) {
switch ($value->getError()) {
case UPLOAD_ERR_INI_SIZE:
$maxSize = UploadedFile::getMaxFilesize();
$maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
if ($constraint->maxSize) {
if (ctype_digit((string) $constraint->maxSize)) {
$maxSize = (int) $constraint->maxSize;
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1024;
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1048576;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
}
$maxSize = min(UploadedFile::getMaxFilesize(), $maxSize);
} else {
$maxSize = UploadedFile::getMaxFilesize();
}

$this->context->addViolation($constraint->uploadIniSizeErrorMessage, array(
'{{ limit }}' => $maxSize,
'{{ suffix }}' => 'bytes',
Expand Down Expand Up @@ -97,15 +110,15 @@ public function validate($value, Constraint $constraint)
if ($constraint->maxSize) {
if (ctype_digit((string) $constraint->maxSize)) {
$size = filesize($path);
$limit = $constraint->maxSize;
$limit = (int) $constraint->maxSize;
$suffix = 'bytes';
} elseif (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000, 2);
$limit = $matches[1];
$limit = (int) $constraint->maxSize;
$suffix = 'kB';
} elseif (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000000, 2);
$limit = $matches[1];
$limit = (int) $constraint->maxSize;
$suffix = 'MB';
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
Expand Down
17 changes: 16 additions & 1 deletion Tests/Constraints/FileValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,13 @@ public function testInvalidWildcardMimeType()
/**
* @dataProvider uploadedFileErrorProvider
*/
public function testUploadedFileError($error, $message, array $params = array())
public function testUploadedFileError($error, $message, array $params = array(), $maxSize = null)
{
$file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error);

$constraint = new File(array(
$message => 'myMessage',
'maxSize' => $maxSize
));

$this->context->expects($this->once())
Expand All @@ -316,10 +317,24 @@ public function uploadedFileErrorProvider()
);

if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
// when no maxSize is specified on constraint, it should use the ini value
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
'{{ limit }}' => UploadedFile::getMaxFilesize(),
'{{ suffix }}' => 'bytes',
));

// it should use the smaller limitation (maxSize option in this case)
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
'{{ limit }}' => 1,
'{{ suffix }}' => 'bytes',
), '1');

// it correctly parses the maxSize option and not only uses simple string comparison
// 1000M should be bigger than the ini value
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
'{{ limit }}' => UploadedFile::getMaxFilesize(),
'{{ suffix }}' => 'bytes',
), '1000M');
}

return $tests;
Expand Down

0 comments on commit d1ed1c5

Please sign in to comment.