MediaFileTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/util/common.php';
  35. final class MediaFileTest extends TestCase
  36. {
  37. protected function setup(): void
  38. {
  39. $this->old_attachments_supported = common_config('attachments', 'supported');
  40. $GLOBALS['config']['attachments']['supported'] = true;
  41. }
  42. protected function tearDown(): void
  43. {
  44. $GLOBALS['config']['attachments']['supported'] = $this->old_attachments_supported;
  45. }
  46. /**
  47. * @dataProvider fileTypeCases
  48. *
  49. * @param $filename
  50. * @param $expectedType
  51. *
  52. * @throws ClientException
  53. * @throws ServerException
  54. */
  55. public function testMimeType($filename, $expectedType)
  56. {
  57. if (!file_exists($filename)) {
  58. throw new Exception("Test file {$filename} missing");
  59. }
  60. $type = MediaFile::getUploadedMimeType($filename, basename($filename));
  61. static::assertSame($expectedType, $type);
  62. }
  63. /**
  64. * @dataProvider fileTypeCases
  65. *
  66. * @param $filename
  67. * @param $expectedType
  68. *
  69. * @throws ClientException
  70. * @throws ServerException
  71. */
  72. public function testUploadedMimeType($filename, $expectedType)
  73. {
  74. if (!file_exists($filename)) {
  75. throw new Exception("WTF? {$filename} test file missing");
  76. }
  77. $tmp = tmpfile();
  78. fwrite($tmp, file_get_contents($filename));
  79. $tmp_metadata = stream_get_meta_data($tmp);
  80. $type = MediaFile::getUploadedMimeType($tmp_metadata['uri'], basename($filename));
  81. static::assertSame($expectedType, $type);
  82. }
  83. public static function fileTypeCases()
  84. {
  85. $base = __DIR__;
  86. $dir = "{$base}/sample-uploads";
  87. $files = [
  88. 'image.png' => 'image/png',
  89. 'image.gif' => 'image/gif',
  90. 'image.jpg' => 'image/jpeg',
  91. 'image.jpeg' => 'image/jpeg',
  92. 'office.pdf' => 'application/pdf',
  93. 'wordproc.odt' => 'application/vnd.oasis.opendocument.text',
  94. 'wordproc.ott' => 'application/vnd.oasis.opendocument.text-template',
  95. 'wordproc.doc' => 'application/msword',
  96. 'wordproc.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  97. 'wordproc.rtf' => 'text/rtf',
  98. 'spreadsheet.ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  99. 'spreadsheet.ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
  100. 'spreadsheet.xls' => 'application/vnd.ms-excel',
  101. 'spreadsheet.xlt' => 'application/vnd.ms-excel',
  102. 'spreadsheet.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  103. 'presentation.odp' => 'application/vnd.oasis.opendocument.presentation',
  104. 'presentation.otp' => 'application/vnd.oasis.opendocument.presentation-template',
  105. 'presentation.ppt' => 'application/vnd.ms-powerpoint',
  106. 'presentation.pptx' => 'application/zip', //"application/vnd.openxmlformats-officedocument.presentationml.presentation",
  107. ];
  108. $dataset = [];
  109. foreach ($files as $file => $type) {
  110. $dataset[] = ["{$dir}/{$file}", $type];
  111. }
  112. return $dataset;
  113. }
  114. }