MediaFileTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  3. print "This script must be run from the command line\n";
  4. exit();
  5. }
  6. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  7. define('GNUSOCIAL', true);
  8. define('STATUSNET', true); // compatibility
  9. require_once INSTALLDIR . '/lib/common.php';
  10. class MediaFileTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function setup()
  13. {
  14. $this->old_attachments_supported = common_config('attachments', 'supported');
  15. $GLOBALS['config']['attachments']['supported'] = true;
  16. }
  17. public function tearDown()
  18. {
  19. $GLOBALS['config']['attachments']['supported'] = $this->old_attachments_supported;
  20. }
  21. /**
  22. * @dataProvider fileTypeCases
  23. *
  24. */
  25. public function testMimeType($filename, $expectedType)
  26. {
  27. if (!file_exists($filename)) {
  28. throw new Exception("Test file $filename missing");
  29. }
  30. $type = MediaFile::getUploadedMimeType($filename, basename($filename));
  31. $this->assertEquals($expectedType, $type);
  32. }
  33. /**
  34. * @dataProvider fileTypeCases
  35. *
  36. */
  37. public function testUploadedMimeType($filename, $expectedType)
  38. {
  39. if (!file_exists($filename)) {
  40. throw new Exception("WTF? $filename test file missing");
  41. }
  42. $tmp = tmpfile();
  43. fwrite($tmp, file_get_contents($filename));
  44. $tmp_metadata = stream_get_meta_data($tmp);
  45. $type = MediaFile::getUploadedMimeType($tmp_metadata['uri'], basename($filename));
  46. $this->assertEquals($expectedType, $type);
  47. }
  48. static public function fileTypeCases()
  49. {
  50. $base = dirname(__FILE__);
  51. $dir = "$base/sample-uploads";
  52. $files = array(
  53. "image.png" => "image/png",
  54. "image.gif" => "image/gif",
  55. "image.jpg" => "image/jpeg",
  56. "image.jpeg" => "image/jpeg",
  57. "office.pdf" => "application/pdf",
  58. "wordproc.odt" => "application/vnd.oasis.opendocument.text",
  59. "wordproc.ott" => "application/vnd.oasis.opendocument.text-template",
  60. "wordproc.doc" => "application/msword",
  61. "wordproc.docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  62. "wordproc.rtf" => "text/rtf",
  63. "spreadsheet.ods" => "application/vnd.oasis.opendocument.spreadsheet",
  64. "spreadsheet.ots" => "application/vnd.oasis.opendocument.spreadsheet-template",
  65. "spreadsheet.xls" => "application/vnd.ms-office", //"application/vnd.ms-excel",
  66. "spreadsheet.xlt" => "application/vnd.ms-office", //"application/vnd.ms-excel",
  67. "spreadsheet.xlsx" => "application/octet-stream", //"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  68. "presentation.odp" => "application/vnd.oasis.opendocument.presentation",
  69. "presentation.otp" => "application/vnd.oasis.opendocument.presentation-template",
  70. "presentation.ppt" => "application/vnd.ms-powerpoint",
  71. "presentation.pptx" => 'application/zip', //"application/vnd.openxmlformats-officedocument.presentationml.presentation",
  72. );
  73. $dataset = array();
  74. foreach ($files as $file => $type) {
  75. $dataset[] = array("$dir/$file", $type);
  76. }
  77. return $dataset;
  78. }
  79. }