Skip to content

Commit

Permalink
throw exception in Validation::mimeType() if PHP has nothing to provi…
Browse files Browse the repository at this point in the history
…de the mimetype
  • Loading branch information
ceeram committed Jun 14, 2012
1 parent 5413143 commit 74a0bd9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -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'));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/Cake/Utility/Validation.php
Expand Up @@ -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'])) {
Expand All @@ -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);
}

Expand Down

0 comments on commit 74a0bd9

Please sign in to comment.