Skip to content
Permalink
Browse files

Fix/improve PSR-7 uploaded file validation support.

  • Loading branch information...
ndm2
ndm2 committed Jul 27, 2018
1 parent c15a2e9 commit bbf07b425c19cf8a32a33f7f751db85e80313d4b
Showing with 37 additions and 9 deletions.
  1. +7 −9 src/Validation/Validation.php
  2. +30 −0 tests/TestCase/Validation/ValidationTest.php
@@ -775,13 +775,15 @@ public static function equalTo($check, $comparedTo)
/**
* Checks that value has a valid file extension.
*
* @param string|array $check Value to check
* @param string|array|\Psr\Http\Message\UploadedFileInterface $check Value to check
* @param array $extensions file extensions to allow. By default extensions are 'gif', 'jpeg', 'png', 'jpg'
* @return bool Success
*/
public static function extension($check, $extensions = ['gif', 'jpeg', 'png', 'jpg'])
{
if (is_array($check)) {
if ($check instanceof UploadedFileInterface) {
return static::extension($check->getClientFilename(), $extensions);
} elseif (is_array($check)) {
$check = isset($check['name']) ? $check['name'] : array_shift($check);
return static::extension($check, $extensions);
@@ -1258,7 +1260,7 @@ public static function uploadError($check, $allowNoFile = false)
* - `optional` - Whether or not this file is optional. Defaults to false.
* If true a missing file will pass the validator regardless of other constraints.
*
* @param array $file The uploaded file data from PHP.
* @param array|\Psr\Http\Message\UploadedFileInterface $file The uploaded file data from PHP.
* @param array $options An array of options for the validation.
* @return bool
*/
@@ -1310,7 +1312,7 @@ public static function uploadedFile($file, array $options = [])
/**
* Validates the size of an uploaded image.
*
* @param array $file The uploaded file data from PHP.
* @param array|\Psr\Http\Message\UploadedFileInterface $file The uploaded file data from PHP.
* @param array $options Options to validate width and height.
* @return bool
*/
@@ -1320,11 +1322,7 @@ public static function imageSize($file, $options)
throw new InvalidArgumentException('Invalid image size validation parameters! Missing `width` and / or `height`.');
}
if ($file instanceof UploadedFileInterface) {
$file = $file->getStream()->getContents();
} elseif (is_array($file) && isset($file['tmp_name'])) {
$file = $file['tmp_name'];
}
$file = static::getFilename($file);
list($width, $height) = getimagesize($file);
@@ -2336,6 +2336,20 @@ public function testExtension()
$this->assertFalse(Validation::extension(['extension.pdf', 'extension.JPG']));
}
/**
* Test extension with a PSR7 object
*/
public function testExtensionPsr7()
{
$file = WWW_ROOT . 'test_theme' . DS . 'img' . DS . 'test.jpg';
$upload = new UploadedFile($file, 5308, UPLOAD_ERR_OK, 'extension.jpeg', 'image/jpeg');
$this->assertTrue(Validation::extension($upload));
$upload = new UploadedFile($file, 163, UPLOAD_ERR_OK, 'no_php_extension', 'text/plain');
$this->assertFalse(Validation::extension($upload));
}
/**
* testMoney method
*
@@ -3117,6 +3131,22 @@ public function testImageSize()
]));
}
/**
* Test imageSize with a PSR7 object
*
* @return void
*/
public function testImageSizePsr7()
{
$image = WWW_ROOT . 'test_theme' . DS . 'img' . DS . 'test.jpg';
$upload = new UploadedFile($image, 5308, UPLOAD_ERR_OK, 'test.jpg', 'image/jpeg');
$this->assertTrue(Validation::imageSize($upload, [
'width' => [Validation::COMPARE_GREATER, 100],
'height' => [Validation::COMPARE_GREATER, 100],
]));
}
/**
* Test imageHeight
*

0 comments on commit bbf07b4

Please sign in to comment.
You can’t perform that action at this time.