Merge pull request #671 from burzum/2.2-validation

2.2 validation methods
This commit is contained in:
José Lorenzo Rodríguez 2012-05-27 18:09:03 -07:00
commit 18c3fa79c9
2 changed files with 60 additions and 0 deletions

View file

@ -2166,4 +2166,30 @@ class ValidationTest extends CakeTestCase {
$this->assertFalse(Validation::datetime('31 11 2006 1:00pm', 'dmy'));
}
/**
* testMimeType method
*
* @return void
*/
public function testMimeType() {
$file = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
$this->assertTrue(Validation::mimeType($file, array('image/gif')));
$this->assertTrue(Validation::mimeType(array('tmp_name' => $file), array('image/gif')));
$this->assertFalse(Validation::mimeType($file, array('image/png')));
$this->assertFalse(Validation::mimeType(array('tmp_name' => $file), array('image/png')));
}
/**
* testMimeType method
*
* @return void
*/
public function testUploadError() {
$this->assertTrue(Validation::uploadError(0));
$this->assertTrue(Validation::uploadError(array('error' => 0)));
$this->assertFalse(Validation::uploadError(2));
$this->assertFalse(Validation::uploadError(array('error' => 2)));
}
}

View file

@ -18,6 +18,7 @@
*/
App::uses('Multibyte', 'I18n');
App::uses('File', 'Utility');
// Load multibyte if the extension is missing.
if (!function_exists('mb_strlen')) {
class_exists('Multibyte');
@ -858,6 +859,39 @@ class Validation {
return ($sum % 10 == 0);
}
/**
* Checks the mime type of a file
*
* @param string|array $check
* @param array $mimeTypes to check for
* @return boolean Success
*/
public static function mimeType($check, $mimeTypes = array()) {
if (is_array($check) && isset($check['tmp_name'])) {
$check = $check['tmp_name'];
}
$File = new File($check);
$mime = $File->mime();
return in_array($mime, $mimeTypes);
}
/**
* Checking for upload errors
*
* @param string|array $check
* @retrun boolean
* @see http://www.php.net/manual/en/features.file-upload.errors.php
*/
public static function uploadError($check) {
if (is_array($check) && isset($check['error'])) {
$check = $check['error'];
}
return $check === UPLOAD_ERR_OK;
}
/**
* Lazily populate the IP address patterns used for validations
*