Skip to content

Commit

Permalink
Adding uploadError() and mimeType() to the Validation class
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed May 27, 2012
1 parent 1c0f97e commit e3a6e9e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -2166,4 +2166,30 @@ public function testDatetime() {
$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 comment has been minimized.

Copy link
@ADmad

ADmad Jun 2, 2012

Member

@burzum This test has been failing since quite a while on PHP 5.2 http://travis-ci.org/#!/cakephp/cakephp/jobs/1504158

This comment has been minimized.

Copy link
@burzum

burzum Jun 4, 2012

Contributor

I haven't had yet time to look at this in 5.2 environment. But I think I know what's wrong.

This comment has been minimized.

Copy link
@ADmad

ADmad Jun 4, 2012

Member

No worries, I just wanted to bring it to your attention in case you were unaware.

This comment has been minimized.

Copy link
@ceeram

ceeram Jun 14, 2012

Contributor

I came up with this fix: 74a0bd9
All now passes again on 5.2 in travis: http://travis-ci.org/#!/ceeram/cakephp/builds/1622157
Is this ok by you to merge in?

This comment has been minimized.

Copy link
@markstory

markstory Jun 15, 2012

Member

I think that's a good fix @ceeram

This comment has been minimized.

Copy link
@lorenzo

lorenzo via email Jun 15, 2012

Member
$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)));
}
}
34 changes: 34 additions & 0 deletions lib/Cake/Utility/Validation.php
Expand Up @@ -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');
Expand Down Expand Up @@ -858,6 +859,39 @@ public static function luhn($check, $deep = false) {
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);
$info = $File->info();

return in_array($info['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
*
Expand Down

0 comments on commit e3a6e9e

Please sign in to comment.