Merge pull request #7577 into 2.8

Backport Validation::uploadedFile() to 2.x.
This commit is contained in:
mark_story 2015-10-20 21:09:20 -04:00
commit a99766c897
3 changed files with 170 additions and 2 deletions

View file

@ -400,7 +400,7 @@ class ModelTaskTest extends CakeTestCase {
public function testInteractiveDoValidationWithSkipping() {
$this->Task->expects($this->any())
->method('in')
->will($this->onConsecutiveCalls('34', '24', 'n', '10', 's'));
->will($this->onConsecutiveCalls('35', '24', 'n', '10', 's'));
$this->Task->interactive = true;
$Model = $this->getMock('Model');
$Model->primaryKey = 'id';

View file

@ -2454,6 +2454,12 @@ class ValidationTest extends CakeTestCase {
$this->assertFalse(Validation::uploadError(2));
$this->assertFalse(Validation::uploadError(array('error' => 2)));
$this->assertFalse(Validation::uploadError(array('error' => '2')));
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_NO_FILE));
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_FORM_SIZE, true));
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_INI_SIZE, true));
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_NO_TMP_DIR, true));
$this->assertTrue(Validation::uploadError(UPLOAD_ERR_NO_FILE, true));
}
/**
@ -2474,4 +2480,110 @@ class ValidationTest extends CakeTestCase {
$this->assertFalse(Validation::fileSize(array('tmp_name' => $image), '>', '1KB'));
}
/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileErrorCode() {
$this->assertFalse(Validation::uploadedFile('derp'));
$invalid = array(
'name' => 'testing'
);
$this->assertFalse(Validation::uploadedFile($invalid));
$file = array(
'name' => 'cake.power.gif',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_OK,
'type' => 'image/gif',
'size' => 201
);
$this->assertTrue(Validation::uploadedFile($file));
$file['error'] = UPLOAD_ERR_NO_FILE;
$this->assertFalse(Validation::uploadedFile($file), 'Error upload should fail.');
}
/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileMimeType() {
$file = array(
'name' => 'cake.power.gif',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_OK,
'type' => 'text/plain',
'size' => 201
);
$options = array(
'types' => array('text/plain')
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'Incorrect mimetype.');
$options = array(
'types' => array('image/gif', 'image/png')
);
$this->assertTrue(Validation::uploadedFile($file, $options));
}
/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileSize() {
$file = array(
'name' => 'cake.power.gif',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_OK,
'type' => 'text/plain',
'size' => 201
);
$options = array(
'minSize' => 500
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too small');
$options = array(
'maxSize' => 100
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too big');
$options = array(
'minSize' => 100,
);
$this->assertTrue(Validation::uploadedFile($file, $options));
$options = array(
'maxSize' => 500,
);
$this->assertTrue(Validation::uploadedFile($file, $options));
$options = array(
'minSize' => 100,
'maxSize' => 500
);
$this->assertTrue(Validation::uploadedFile($file, $options));
}
/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileNoFile() {
$file = array(
'name' => '',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_NO_FILE,
'type' => '',
'size' => 0
);
$options = array(
'optional' => true,
'minSize' => 500,
'types' => array('image/gif', 'image/png')
);
$this->assertTrue(Validation::uploadedFile($file, $options), 'No file should be ok.');
$options = array(
'optional' => false
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'File is required.');
}
}

View file

@ -1022,17 +1022,73 @@ class Validation {
* Checking for upload errors
*
* @param string|array $check Value to check.
* @param bool $allowNoFile Set to true to allow UPLOAD_ERR_NO_FILE as a pass.
* @return bool
* @see http://www.php.net/manual/en/features.file-upload.errors.php
*/
public static function uploadError($check) {
public static function uploadError($check, $allowNoFile = false) {
if (is_array($check) && isset($check['error'])) {
$check = $check['error'];
}
if ($allowNoFile) {
return in_array((int)$check, array(UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE), true);
}
return (int)$check === UPLOAD_ERR_OK;
}
/**
* Validate an uploaded file.
*
* Helps join `uploadError`, `fileSize` and `mimeType` into
* one higher level validation method.
*
* ### Options
*
* - `types` - A list of valid mime types. If empty all types
* will be accepted. The `type` will not be looked at, instead
* the file type will be checked with ext/finfo.
* - `minSize` - The minimum file size. Defaults to not checking.
* - `maxSize` - The maximum file size. Defaults to not checking.
* - `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 $options An array of options for the validation.
* @return bool
*/
public static function uploadedFile($file, $options = array()) {
$options += array(
'minSize' => null,
'maxSize' => null,
'types' => null,
'optional' => false,
);
if (!is_array($file)) {
return false;
}
$keys = array('name', 'tmp_name', 'error', 'type', 'size');
if (array_keys($file) != $keys) {
return false;
}
if (!static::uploadError($file, $options['optional'])) {
return false;
}
if ($options['optional'] && (int) $file['error'] === UPLOAD_ERR_NO_FILE) {
return true;
}
if (isset($options['minSize']) && !static::fileSize($file, '>=', $options['minSize'])) {
return false;
}
if (isset($options['maxSize']) && !static::fileSize($file, '<=', $options['maxSize'])) {
return false;
}
if (isset($options['types']) && !static::mimeType($file, $options['types'])) {
return false;
}
return true;
}
/**
* Lazily populate the IP address patterns used for validations
*