summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2010-05-10 16:18:29 -0700
committerBrion Vibber <brion@pobox.com>2010-06-28 08:59:47 -0700
commit9c7b66984c46668f314f93337d28c62854b6d134 (patch)
tree75c9c75503803f8d863a7bd04f33c2f139a37849 /tests
parent41d81b996fdd8276cc04e750297a12f852a97bf4 (diff)
Enhanced upload file type detection. If given an original filename, we'll attempt to detect type from the extension if we were unable to make a definitive match from content. Generic octet-stream, zip, and MS Office type are explicitly singled out for re-checks, which fixes OpenOffice and MS Office documents to come up with the proper types when misdetected.
File extensions can also be added to the upload type whitelist; they'll be normalized to types for the actual comparison, so only known extensions will work.
Diffstat (limited to 'tests')
-rw-r--r--tests/MediaFileTest.php73
-rw-r--r--tests/sample-uploads/image.gifbin0 -> 35 bytes
-rw-r--r--tests/sample-uploads/image.jpegbin0 -> 306 bytes
-rw-r--r--tests/sample-uploads/image.jpgbin0 -> 306 bytes
-rw-r--r--tests/sample-uploads/image.pngbin0 -> 159 bytes
5 files changed, 46 insertions, 27 deletions
diff --git a/tests/MediaFileTest.php b/tests/MediaFileTest.php
index 6fe995621..a76a4f45e 100644
--- a/tests/MediaFileTest.php
+++ b/tests/MediaFileTest.php
@@ -34,43 +34,62 @@ class MediaFileTest extends PHPUnit_Framework_TestCase
if (!file_exists($filename)) {
throw new Exception("WTF? $filename test file missing");
}
- $this->assertEquals($expectedType, MediaFile::getUploadedFileType($filename));
+
+ $type = MediaFile::getUploadedFileType($filename, basename($filename));
+ $this->assertEquals($expectedType, $type);
+ }
+
+ /**
+ * @dataProvider fileTypeCases
+ *
+ */
+ public function testUploadedFileType($filename, $expectedType)
+ {
+ if (!file_exists($filename)) {
+ throw new Exception("WTF? $filename test file missing");
+ }
+ $tmp = tmpfile();
+ fwrite($tmp, file_get_contents($filename));
+
+ $type = MediaFile::getUploadedFileType($tmp, basename($filename));
+ $this->assertEquals($expectedType, $type);
}
static public function fileTypeCases()
{
$base = dirname(__FILE__);
$dir = "$base/sample-uploads";
- return array(
- array("$dir/office.pdf", "application/pdf"),
+ $files = array(
+ "image.png" => "image/png",
+ "image.gif" => "image/gif",
+ "image.jpg" => "image/jpeg",
+ "image.jpeg" => "image/jpeg",
+
+ "office.pdf" => "application/pdf",
- array("$dir/wordproc.odt", "application/vnd.oasis.opendocument.text"),
- array("$dir/wordproc.ott", "application/vnd.oasis.opendocument.text-template"),
- array("$dir/wordproc.doc", "application/msword"),
- array("$dir/wordproc.docx",
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
- array("$dir/wordproc.rtf", "text/rtf"),
+ "wordproc.odt" => "application/vnd.oasis.opendocument.text",
+ "wordproc.ott" => "application/vnd.oasis.opendocument.text-template",
+ "wordproc.doc" => "application/msword",
+ "wordproc.docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ "wordproc.rtf" => "text/rtf",
- array("$dir/spreadsheet.ods",
- "application/vnd.oasis.opendocument.spreadsheet"),
- array("$dir/spreadsheet.ots",
- "application/vnd.oasis.opendocument.spreadsheet-template"),
- array("$dir/spreadsheet.xls", "application/vnd.ms-excel"),
- array("$dir/spreadsheet.xlt", "application/vnd.ms-excel"),
- array("$dir/spreadsheet.xlsx",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
+ "spreadsheet.ods" => "application/vnd.oasis.opendocument.spreadsheet",
+ "spreadsheet.ots" => "application/vnd.oasis.opendocument.spreadsheet-template",
+ "spreadsheet.xls" => "application/vnd.ms-excel",
+ "spreadsheet.xlt" => "application/vnd.ms-excel",
+ "spreadsheet.xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
- array("$dir/presentation.odp",
- "application/vnd.oasis-opendocument.presentation"),
- array("$dir/presentation.otp",
- "application/vnd.oasis-opendocument.presentation-template"),
- array("$dir/presentation.ppt",
- "application/vnd.ms-powerpoint"),
- array("$dir/presentation.pot",
- "application/vnd.ms-powerpoint"),
- array("$dir/presentation.pptx",
- "application/vnd.openxmlformats-officedocument.presentationml.presentation"),
+ "presentation.odp" => "application/vnd.oasis.opendocument.presentation",
+ "presentation.otp" => "application/vnd.oasis.opendocument.presentation-template",
+ "presentation.ppt" => "application/vnd.ms-powerpoint",
+ "presentation.pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
);
+
+ $dataset = array();
+ foreach ($files as $file => $type) {
+ $dataset[] = array("$dir/$file", $type);
+ }
+ return $dataset;
}
}
diff --git a/tests/sample-uploads/image.gif b/tests/sample-uploads/image.gif
new file mode 100644
index 000000000..b636f4b8d
--- /dev/null
+++ b/tests/sample-uploads/image.gif
Binary files differ
diff --git a/tests/sample-uploads/image.jpeg b/tests/sample-uploads/image.jpeg
new file mode 100644
index 000000000..21fcb5aef
--- /dev/null
+++ b/tests/sample-uploads/image.jpeg
Binary files differ
diff --git a/tests/sample-uploads/image.jpg b/tests/sample-uploads/image.jpg
new file mode 100644
index 000000000..21fcb5aef
--- /dev/null
+++ b/tests/sample-uploads/image.jpg
Binary files differ
diff --git a/tests/sample-uploads/image.png b/tests/sample-uploads/image.png
new file mode 100644
index 000000000..60cbcfd17
--- /dev/null
+++ b/tests/sample-uploads/image.png
Binary files differ