GSFileTest.php 3.4 KB

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