Permalink
Browse files

throw exception in Validation::mimeType() if PHP has nothing to provi…

…de the mimetype
  • Loading branch information...
1 parent 5413143 commit 74a0bd98c28991664fc0a3b0e8b34dc4f9dbf009 @ceeram ceeram committed Jun 14, 2012
Showing with 24 additions and 5 deletions.
  1. +20 −5 lib/Cake/Test/Case/Utility/ValidationTest.php
  2. +4 −0 lib/Cake/Utility/Validation.php
@@ -2174,12 +2174,27 @@ public function testDatetime() {
* @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')));
+ $image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
+ $File = new File($image, false);
+ $this->skipIf(!$File->mime(), 'Cannot determine mimeType');
+ $this->assertTrue(Validation::mimeType($image, array('image/gif')));
+ $this->assertTrue(Validation::mimeType(array('tmp_name' => $image), array('image/gif')));
- $this->assertFalse(Validation::mimeType($file, array('image/png')));
- $this->assertFalse(Validation::mimeType(array('tmp_name' => $file), array('image/png')));
+ $this->assertFalse(Validation::mimeType($image, array('image/png')));
+ $this->assertFalse(Validation::mimeType(array('tmp_name' => $image), array('image/png')));
+ }
+
+/**
+ * testMimeTypeFalse method
+ *
+ * @expectedException CakeException
+ * @return void
+ */
+ public function testMimeTypeFalse() {
+ $image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
+ $File = new File($image, false);
+ $this->skipIf($File->mime(), 'mimeType can be determined, no Exception will be thrown');
+ Validation::mimeType($image, array('image/gif'));
}
/**
@@ -865,6 +865,7 @@ public static function luhn($check, $deep = false) {
* @param string|array $check
* @param array $mimeTypes to check for
* @return boolean Success
+ * @throws CakeException when mime type can not be determined.
*/
public static function mimeType($check, $mimeTypes = array()) {
if (is_array($check) && isset($check['tmp_name'])) {
@@ -874,6 +875,9 @@ public static function mimeType($check, $mimeTypes = array()) {
$File = new File($check);
$mime = $File->mime();
+ if ($mime === false) {
+ throw new CakeException(__d('cake_dev', 'Can not determine the mimetype.'));
+ }
return in_array($mime, $mimeTypes);
}

0 comments on commit 74a0bd9

Please sign in to comment.