GSFileTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Tests\Core;
  19. use App\Core\GSFile;
  20. use App\Util\GNUsocialTestCase;
  21. use App\Util\TemporaryFile;
  22. class GSFileTest extends GNUsocialTestCase
  23. {
  24. public function testSanitizeAndStoreFileAsAttachment()
  25. {
  26. static::bootKernel();
  27. $file = new TemporaryFile();
  28. $file->write('foo');
  29. $attachment = GSFile::sanitizeAndStoreFileAsAttachment($file);
  30. static::assertSame('text/plain', $attachment->getMimetype());
  31. static::assertSame(3, $attachment->getSize());
  32. static::assertNull($attachment->getWidth());
  33. static::assertNull($attachment->getHeight());
  34. static::assertTrue(file_exists($attachment->getPath()));
  35. static::assertSame(1, $attachment->getLives());
  36. }
  37. public function testEnsureFilenameWithProperExtension()
  38. {
  39. static::assertSame('image.jpeg', GSFile::ensureFilenameWithProperExtension('image.jpeg', 'image/jpeg'));
  40. static::assertSame('image.jpg', GSFile::ensureFilenameWithProperExtension('image.jpg', 'image/jpeg'));
  41. static::assertSame('image.jpeg.png', GSFile::ensureFilenameWithProperExtension('image.jpeg', 'image/png'));
  42. static::assertSame('image.png', GSFile::ensureFilenameWithProperExtension('image', 'image/png'));
  43. static::assertSame('image.gif', GSFile::ensureFilenameWithProperExtension('image', 'image/gif'));
  44. static::assertSame('image.jpg.png', GSFile::ensureFilenameWithProperExtension('image.jpg', 'image/gif', ext: 'png', force: true));
  45. static::assertNull(GSFile::ensureFilenameWithProperExtension('image.jpg', 'image/gif', ext: null, force: true));
  46. }
  47. public function testMimetype()
  48. {
  49. static::assertSame('image', GSFile::mimetypeMajor('image/png'));
  50. static::assertSame('image', GSFile::mimetypeMajor('IMAGE/PNG'));
  51. static::assertSame('image', GSFile::mimetypeMajor('image/jpeg'));
  52. static::assertSame('text', GSFile::mimetypeMajor('text/html'));
  53. static::assertSame('text', GSFile::mimetypeMajor('text/html; charset=utf-8'));
  54. static::assertSame('png', GSFile::mimetypeMinor('image/png'));
  55. static::assertSame('png', GSFile::mimetypeMinor('IMAGE/PNG'));
  56. static::assertSame('jpeg', GSFile::mimetypeMinor('image/jpeg'));
  57. static::assertSame('html', GSFile::mimetypeMinor('text/html'));
  58. static::assertSame('html', GSFile::mimetypeMinor('text/html; charset=utf-8'));
  59. static::assertSame('text/html', GSFile::mimetypeBare('text/html'));
  60. static::assertSame('text/html', GSFile::mimetypeBare('text/html; charset=utf-8'));
  61. }
  62. }