summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-07-20 19:02:38 +0000
committerDries Buytaert <dries@buytaert.net>2009-07-20 19:02:38 +0000
commit444ae51f1745f9fcee0664bd3bf2fb13aea5fbae (patch)
tree5a4bbe263b8e77aff3af5c4d3a9a91112b33a1fb
parentd24d954479320116bc01f332c28a8eaab28c3ccd (diff)
downloadbrdo-444ae51f1745f9fcee0664bd3bf2fb13aea5fbae.tar.gz
brdo-444ae51f1745f9fcee0664bd3bf2fb13aea5fbae.tar.bz2
- Patch #520664 by rfay, quicksketch: file_get_mimetype() should not be case-sensitive. Comes with more and better tests.
-rw-r--r--includes/file.inc8
-rw-r--r--includes/file.mimetypes.inc15
-rw-r--r--modules/simpletest/tests/file.test49
3 files changed, 65 insertions, 7 deletions
diff --git a/includes/file.inc b/includes/file.inc
index ead32264c..5478e8748 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -1544,10 +1544,14 @@ function file_upload_max_size() {
* An optional map of extensions to their mimetypes, in the form:
* - 'mimetypes': a list of mimetypes, keyed by an identifier,
* - 'extensions': the mapping itself, an associative array in which
- * the key is the extension and the value is the mimetype identifier.
+ * the key is the extension (lowercase) and the value is the mimetype identifier.
+ * If $mapping is omitted, the drupal variable mime_extension_mapping is checked
+ * for a value and if that fails then file_default_mimetype_mapping() is called
*
* @return
* The internet media type registered for the extension or application/octet-stream for unknown extensions.
+ * @see
+ * file_default_mimetype_mapping()
*/
function file_get_mimetype($filename, $mapping = NULL) {
if (!isset($mapping)) {
@@ -1571,7 +1575,7 @@ function file_get_mimetype($filename, $mapping = NULL) {
// - image.jpeg, and
// - awesome.image.jpeg
while ($additional_part = array_pop($file_parts)) {
- $extension = $additional_part . ($extension ? '.' . $extension : '');
+ $extension = strtolower($additional_part . ($extension ? '.' . $extension : ''));
if (isset($mapping['extensions'][$extension])) {
return $mapping['mimetypes'][$mapping['extensions'][$extension]];
}
diff --git a/includes/file.mimetypes.inc b/includes/file.mimetypes.inc
index 501e1a4cf..f0586f2fb 100644
--- a/includes/file.mimetypes.inc
+++ b/includes/file.mimetypes.inc
@@ -1,7 +1,18 @@
<?php
+// $Id$
+
+/**
+ * @file
+ * Provides mimetype mappings.
+ */
+
/**
* Default MIME extension mapping.
+ *
+ * @return
+ * Array of mimetypes correlated to the extensions that relate to them.
+ * @see file_get_mimetype()
*/
function file_default_mimetype_mapping() {
return array(
@@ -355,6 +366,8 @@ function file_default_mimetype_mapping() {
344 => 'x-epoc/x-sisx-app',
345 => 'x-world/x-vrml',
),
+
+ // Extensions added to this list MUST be lower-case.
'extensions' => array(
'ez' => 0,
'atom' => 1,
@@ -484,7 +497,7 @@ function file_default_mimetype_mapping() {
'pfb' => 113,
'pcf' => 113,
'gsf' => 113,
- 'pcf.Z' => 113,
+ 'pcf.z' => 113,
'mm' => 114,
'spl' => 115,
'gnumeric' => 116,
diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test
index 6c1dcdd42..54dfa0d61 100644
--- a/modules/simpletest/tests/file.test
+++ b/modules/simpletest/tests/file.test
@@ -30,7 +30,7 @@ function file_test_file_scan_callback($filepath = NULL) {
$files = &drupal_static(__FUNCTION__, array());
if (isset($filepath)) {
$files[] = $filepath;
- }
+ }
else {
return $files;
}
@@ -2016,23 +2016,64 @@ class FileMimeTypeTest extends DrupalWebTestCase {
}
/**
- * Test basic extraction of mimetypes from the filename.
+ * Test mapping of mimetypes from filenames.
*/
public function testFileMimeTypeDetection() {
$test_case = array(
'test.jar' => 'application/java-archive',
'test.jpeg' => 'image/jpeg',
+ 'test.JPEG' => 'image/jpeg',
'test.jpg' => 'image/jpeg',
'test.jar.jpg' => 'image/jpeg',
'test.jpg.jar' => 'application/java-archive',
'test.pcf.Z' => 'application/x-font',
- 'pcf.Z' => 'application/octet-stream',
+ 'pcf.z' => 'application/octet-stream',
+ 'jar' => 'application/octet-stream',
+ 'some.junk' => 'application/octet-stream',
+ );
+
+ // Test using default mappings (not using 'mime_extension_mapping').
+ variable_del('mime_extension_mapping');
+ foreach ($test_case as $input => $expected) {
+ $output = file_get_mimetype($input);
+ $this->assertIdentical($output, $expected, t('Mimetype (using default mappings) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
+ }
+
+ // Now test using mappings from the mime_extension_mapping variable.
+ $mapping = array(
+ 'mimetypes' => array(
+ 0 => 'application/java-archive',
+ 1 => 'image/jpeg',
+ ),
+ 'extensions' => array(
+ 'jar' => 0,
+ 'jpg' => 1,
+ )
+ );
+
+ $test_case = array(
+ 'test.jar' => 'application/java-archive',
+ 'test.jpeg' => 'application/octet-stream',
+ 'test.jpg' => 'image/jpeg',
+ 'test.jar.jpg' => 'image/jpeg',
+ 'test.jpg.jar' => 'application/java-archive',
+ 'test.pcf.z' => 'application/octet-stream',
+ 'pcf.z' => 'application/octet-stream',
'jar' => 'application/octet-stream',
+ 'some.junk' => 'application/octet-stream',
);
+ variable_set('mime_extension_mapping', $mapping);
foreach ($test_case as $input => $expected) {
$output = file_get_mimetype($input);
- $this->assertIdentical($output, $expected, t('Mimetype for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
+ $this->assertIdentical($output, $expected, t('Mimetype (using mappings from variable) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
+ }
+
+ // Now test the same when passing in the map.
+ variable_del('mime_extension_mapping');
+ foreach ($test_case as $input => $expected) {
+ $output = file_get_mimetype($input, $mapping);
+ $this->assertIdentical($output, $expected, t('Mimetype (using passed-in mappings) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
}
}
}