Skip to content
This repository
Browse code

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

  • Loading branch information...
commit e3a6e9e169644d93fd02877e545a99ca1963b59e 1 parent 1c0f97e
Florian Krämer authored
26  lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -2166,4 +2166,30 @@ public function testDatetime() {
2166 2166
 		$this->assertFalse(Validation::datetime('31 11 2006 1:00pm', 'dmy'));
2167 2167
 	}
2168 2168
 
  2169
+/**
  2170
+ * testMimeType method
  2171
+ *
  2172
+ * @return void
  2173
+ */
  2174
+	public function testMimeType() {
  2175
+		$file = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
  2176
+		$this->assertTrue(Validation::mimeType($file, array('image/gif')));
  2177
+		$this->assertTrue(Validation::mimeType(array('tmp_name' => $file), array('image/gif')));
  2178
+
  2179
+		$this->assertFalse(Validation::mimeType($file, array('image/png')));
  2180
+		$this->assertFalse(Validation::mimeType(array('tmp_name' => $file), array('image/png')));
  2181
+	}
  2182
+
  2183
+/**
  2184
+ * testMimeType method
  2185
+ *
  2186
+ * @return void
  2187
+ */
  2188
+	public function testUploadError() {
  2189
+		$this->assertTrue(Validation::uploadError(0));
  2190
+		$this->assertTrue(Validation::uploadError(array('error' => 0)));
  2191
+
  2192
+		$this->assertFalse(Validation::uploadError(2));
  2193
+		$this->assertFalse(Validation::uploadError(array('error' => 2)));
  2194
+	}
2169 2195
 }
34  lib/Cake/Utility/Validation.php
@@ -18,6 +18,7 @@
18 18
  */
19 19
 
20 20
 App::uses('Multibyte', 'I18n');
  21
+App::uses('File', 'Utility');
21 22
 // Load multibyte if the extension is missing.
22 23
 if (!function_exists('mb_strlen')) {
23 24
 	class_exists('Multibyte');
@@ -859,6 +860,39 @@ public static function luhn($check, $deep = false) {
859 860
 	}
860 861
 
861 862
 /**
  863
+ * Checks the mime type of a file
  864
+ *
  865
+ * @param string|array $check
  866
+ * @param array $mimeTypes to check for
  867
+ * @return boolean Success
  868
+ */
  869
+	public static function mimeType($check, $mimeTypes = array()) {
  870
+		if (is_array($check) && isset($check['tmp_name'])) {
  871
+			$check = $check['tmp_name'];
  872
+		}
  873
+
  874
+		$File = new File($check);
  875
+		$info = $File->info();
  876
+
  877
+		return in_array($info['mime'], $mimeTypes);
  878
+	}
  879
+
  880
+/**
  881
+ * Checking for upload errors
  882
+ *
  883
+ * @param string|array $check
  884
+ * @retrun boolean
  885
+ * @see http://www.php.net/manual/en/features.file-upload.errors.php
  886
+ */
  887
+	public static function uploadError($check) {
  888
+		if (is_array($check) && isset($check['error'])) {
  889
+			$check = $check['error'];
  890
+		}
  891
+
  892
+		return $check === UPLOAD_ERR_OK;
  893
+	}
  894
+
  895
+/**
862 896
  * Lazily populate the IP address patterns used for validations
863 897
  *
864 898
  * @return void

0 notes on commit e3a6e9e

ADmad

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

Florian Krämer

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

ADmad

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

ceeram

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?

José Lorenzo Rodríguez
Please sign in to comment.
Something went wrong with that request. Please try again.