MediaFileTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. namespace Tests\Unit;
  17. if (!defined('INSTALLDIR')) {
  18. define('INSTALLDIR', dirname(dirname(__DIR__)));
  19. }
  20. if (!defined('PUBLICDIR')) {
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. }
  23. if (!defined('GNUSOCIAL')) {
  24. define('GNUSOCIAL', true);
  25. }
  26. if (!defined('STATUSNET')) { // Compatibility
  27. define('STATUSNET', true);
  28. }
  29. use ClientException;
  30. use Exception;
  31. use MediaFile;
  32. use PHPUnit\Framework\TestCase;
  33. use ServerException;
  34. require_once INSTALLDIR . '/lib/common.php';
  35. final class MediaFileTest extends TestCase
  36. {
  37. public function setup(): void
  38. {
  39. $this->old_attachments_supported = common_config('attachments', 'supported');
  40. $GLOBALS['config']['attachments']['supported'] = true;
  41. }
  42. public function tearDown(): void
  43. {
  44. $GLOBALS['config']['attachments']['supported'] = $this->old_attachments_supported;
  45. }
  46. /**
  47. * @dataProvider fileTypeCases
  48. * @param $filename
  49. * @param $expectedType
  50. * @throws ClientException
  51. * @throws ServerException
  52. */
  53. public function testMimeType($filename, $expectedType)
  54. {
  55. if (!file_exists($filename)) {
  56. throw new Exception("Test file $filename missing");
  57. }
  58. $type = MediaFile::getUploadedMimeType($filename, basename($filename));
  59. $this->assertEquals($expectedType, $type);
  60. }
  61. /**
  62. * @dataProvider fileTypeCases
  63. * @param $filename
  64. * @param $expectedType
  65. * @throws ClientException
  66. * @throws ServerException
  67. */
  68. public function testUploadedMimeType($filename, $expectedType)
  69. {
  70. if (!file_exists($filename)) {
  71. throw new Exception("WTF? $filename test file missing");
  72. }
  73. $tmp = tmpfile();
  74. fwrite($tmp, file_get_contents($filename));
  75. $tmp_metadata = stream_get_meta_data($tmp);
  76. $type = MediaFile::getUploadedMimeType($tmp_metadata['uri'], basename($filename));
  77. $this->assertEquals($expectedType, $type);
  78. }
  79. static public function fileTypeCases()
  80. {
  81. $base = dirname(__FILE__);
  82. $dir = "$base/sample-uploads";
  83. $files = array(
  84. "image.png" => "image/png",
  85. "image.gif" => "image/gif",
  86. "image.jpg" => "image/jpeg",
  87. "image.jpeg" => "image/jpeg",
  88. "office.pdf" => "application/pdf",
  89. "wordproc.odt" => "application/vnd.oasis.opendocument.text",
  90. "wordproc.ott" => "application/vnd.oasis.opendocument.text-template",
  91. "wordproc.doc" => "application/msword",
  92. "wordproc.docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  93. "wordproc.rtf" => "text/rtf",
  94. "spreadsheet.ods" => "application/vnd.oasis.opendocument.spreadsheet",
  95. "spreadsheet.ots" => "application/vnd.oasis.opendocument.spreadsheet-template",
  96. "spreadsheet.xls" => "application/vnd.ms-excel",
  97. "spreadsheet.xlt" => "application/vnd.ms-excel",
  98. "spreadsheet.xlsx" =>"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  99. "presentation.odp" => "application/vnd.oasis.opendocument.presentation",
  100. "presentation.otp" => "application/vnd.oasis.opendocument.presentation-template",
  101. "presentation.ppt" => "application/vnd.ms-powerpoint",
  102. "presentation.pptx" => 'application/zip', //"application/vnd.openxmlformats-officedocument.presentationml.presentation",
  103. );
  104. $dataset = array();
  105. foreach ($files as $file => $type) {
  106. $dataset[] = array("$dir/$file", $type);
  107. }
  108. return $dataset;
  109. }
  110. }