Permalink
Browse files

Adding uploadError() and mimeType() to the Validation class

  • Loading branch information...
1 parent 1c0f97e commit e3a6e9e169644d93fd02877e545a99ca1963b59e @burzum burzum committed May 27, 2012
Showing with 60 additions and 0 deletions.
  1. +26 −0 lib/Cake/Test/Case/Utility/ValidationTest.php
  2. +34 −0 lib/Cake/Utility/Validation.php
View
26 lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -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')));
@ADmad
ADmad Jun 2, 2012

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

@burzum
burzum Jun 4, 2012

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

@ADmad
ADmad Jun 4, 2012

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

@ceeram
ceeram Jun 14, 2012

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?

@markstory
markstory Jun 15, 2012

I think that's a good fix @ceeram

@lorenzo
lorenzo Jun 15, 2012
+ $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
34 lib/Cake/Utility/Validation.php
@@ -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');
@@ -859,6 +860,39 @@ public static function luhn($check, $deep = false) {
}
/**
+ * 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
*
* @return void

0 comments on commit e3a6e9e

Please sign in to comment.