MediaFileTest.php 3.25 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
9 10
define('GNUSOCIAL', true);
define('STATUSNET', true);  // compatibility
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

require_once INSTALLDIR . '/lib/common.php';

class MediaFileTest extends PHPUnit_Framework_TestCase
{

    public function setup()
    {
        $this->old_attachments_supported = common_config('attachments', 'supported');
        $GLOBALS['config']['attachments']['supported'] = true;
    }

    public function tearDown()
    {
        $GLOBALS['config']['attachments']['supported'] = $this->old_attachments_supported;
    }

    /**
     * @dataProvider fileTypeCases
     *
     */
32
    public function testMimeType($filename, $expectedType)
33 34 35 36
    {
        if (!file_exists($filename)) {
            throw new Exception("WTF? $filename test file missing");
        }
37

38
        $type = MediaFile::getUploadedMimeType($filename, basename($filename));
39 40 41 42 43 44 45
        $this->assertEquals($expectedType, $type);
    }

    /**
     * @dataProvider fileTypeCases
     *
     */
46
    public function testUploadedMimeType($filename, $expectedType)
47 48 49 50 51 52 53
    {
        if (!file_exists($filename)) {
            throw new Exception("WTF? $filename test file missing");
        }
        $tmp = tmpfile();
        fwrite($tmp, file_get_contents($filename));

54
        $tmp_metadata = stream_get_meta_data($tmp);
55
        $type = MediaFile::getUploadedMimeType($tmp_metadata['uri'], basename($filename));
56
        $this->assertEquals($expectedType, $type);
57 58 59 60 61 62
    }

    static public function fileTypeCases()
    {
        $base = dirname(__FILE__);
        $dir = "$base/sample-uploads";
63 64 65 66 67 68 69
        $files = array(
            "image.png" => "image/png",
            "image.gif" => "image/gif",
            "image.jpg" => "image/jpeg",
            "image.jpeg" => "image/jpeg",
        
            "office.pdf" => "application/pdf",
70
            
71 72 73 74 75
            "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",
76
            
77 78 79 80 81
            "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",
82
            
83 84 85 86
            "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",
87
        );
88 89 90 91 92 93

        $dataset = array();
        foreach ($files as $file => $type) {
            $dataset[] = array("$dir/$file", $type);
        }
        return $dataset;
94 95 96 97
    }

}