123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace Tests\Unit;
- if (!defined('INSTALLDIR')) {
- define('INSTALLDIR', dirname(dirname(__DIR__)));
- }
- if (!defined('PUBLICDIR')) {
- define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
- }
- if (!defined('GNUSOCIAL')) {
- define('GNUSOCIAL', true);
- }
- if (!defined('STATUSNET')) {
- define('STATUSNET', true);
- }
- use ClientException;
- use Exception;
- use MediaFile;
- use PHPUnit\Framework\TestCase;
- use ServerException;
- require_once INSTALLDIR . '/lib/common.php';
- final class MediaFileTest extends TestCase
- {
- public function setup(): void
- {
- $this->old_attachments_supported = common_config('attachments', 'supported');
- $GLOBALS['config']['attachments']['supported'] = true;
- }
- public function tearDown(): void
- {
- $GLOBALS['config']['attachments']['supported'] = $this->old_attachments_supported;
- }
-
- public function testMimeType($filename, $expectedType)
- {
- if (!file_exists($filename)) {
- throw new Exception("Test file $filename missing");
- }
- $type = MediaFile::getUploadedMimeType($filename, basename($filename));
- $this->assertEquals($expectedType, $type);
- }
-
- public function testUploadedMimeType($filename, $expectedType)
- {
- if (!file_exists($filename)) {
- throw new Exception("WTF? $filename test file missing");
- }
- $tmp = tmpfile();
- fwrite($tmp, file_get_contents($filename));
- $tmp_metadata = stream_get_meta_data($tmp);
- $type = MediaFile::getUploadedMimeType($tmp_metadata['uri'], basename($filename));
- $this->assertEquals($expectedType, $type);
- }
- static public function fileTypeCases()
- {
- $base = dirname(__FILE__);
- $dir = "$base/sample-uploads";
- $files = array(
- "image.png" => "image/png",
- "image.gif" => "image/gif",
- "image.jpg" => "image/jpeg",
- "image.jpeg" => "image/jpeg",
- "office.pdf" => "application/pdf",
- "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",
- "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",
- "presentation.odp" => "application/vnd.oasis.opendocument.presentation",
- "presentation.otp" => "application/vnd.oasis.opendocument.presentation-template",
- "presentation.ppt" => "application/vnd.ms-powerpoint",
- "presentation.pptx" => 'application/zip',
- );
- $dataset = array();
- foreach ($files as $file => $type) {
- $dataset[] = array("$dir/$file", $type);
- }
- return $dataset;
- }
- }
|