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