AttachmentTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Entity;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\GSFile;
  23. use App\Entity\Note;
  24. use App\Util\GNUsocialTestCase;
  25. use App\Util\TemporaryFile;
  26. use Component\Attachment\Entity\AttachmentToNote;
  27. use Jchook\AssertThrows\AssertThrows;
  28. use SplFileInfo;
  29. use Symfony\Component\HttpFoundation\File\File;
  30. class AttachmentTest extends GNUsocialTestCase
  31. {
  32. use AssertThrows;
  33. public function testAttachmentLifecycle()
  34. {
  35. static::bootKernel();
  36. // Setup first attachment
  37. $file = new TemporaryFile();
  38. $attachment = GSFile::storeFileAsAttachment($file, check_is_supported_mimetype: false);
  39. $path = $attachment->getPath();
  40. $hash = $attachment->getFilehash();
  41. static::assertFileExists($attachment->getPath());
  42. static::assertSame(1, $attachment->getLives());
  43. static::assertFileExists($path);
  44. // Delete the backed storage of the attachment
  45. static::assertTrue($attachment->deleteStorage());
  46. static::assertFileDoesNotExist($path);
  47. static::assertNull($attachment->getPath());
  48. DB::persist($attachment);
  49. DB::flush();
  50. // Setup the second attachment, re-adding the backed store
  51. $file = new TemporaryFile();
  52. $repeated_attachment = GSFile::storeFileAsAttachment($file, check_is_supported_mimetype: false);
  53. $path = $attachment->getPath();
  54. static::assertSame(2, $repeated_attachment->getLives());
  55. static::assertFileExists($path);
  56. // Garbage collect the attachment
  57. $attachment->kill();
  58. static::assertFileExists($path);
  59. static::assertSame(1, $repeated_attachment->getLives());
  60. // Garbage collect the second attachment, which should delete everything
  61. $repeated_attachment->kill();
  62. static::assertSame(0, $repeated_attachment->getLives());
  63. static::assertFileDoesNotExist($path);
  64. static::assertSame([], DB::findBy('attachment', ['filehash' => $hash]));
  65. }
  66. public function testSanitizeAndStoreFileAsAttachment()
  67. {
  68. $test = function (string $method) {
  69. $temp_file = new TemporaryFile();
  70. $temp_file->write(file_get_contents(INSTALLDIR . '/tests/sample-uploads/gnu-logo.png'));
  71. $hash = null;
  72. Event::handle('HashFile', [$temp_file->getPathname(), &$hash]);
  73. $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
  74. $attachment->{$method}();
  75. DB::flush();
  76. $file = new File($temp_file->getRealPath());
  77. GSFile::storeFileAsAttachment($file);
  78. static::assertNotNull($attachment->getFilename());
  79. static::assertFileExists($attachment->getPath());
  80. };
  81. $test('deleteStorage');
  82. $test('kill');
  83. }
  84. public function testGetBestTitle()
  85. {
  86. $attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
  87. $filename = $attachment->getFilename();
  88. static::assertSame($attachment->getFilename(), $attachment->getBestTitle());
  89. $attachment->setFilename(null);
  90. static::assertSame('Untitled attachment', $attachment->getBestTitle());
  91. $attachment->setFilename($filename);
  92. $actor = DB::findOneBy('actor', ['nickname' => 'taken_user']);
  93. DB::persist($note = Note::create(['actor_id' => $actor->getId(), 'content' => 'attachment: some content', 'content_type' => 'text/plain', 'is_local' => true]));
  94. DB::persist(AttachmentToNote::create(['attachment_id' => $attachment->getId(), 'note_id' => $note->getId(), 'title' => 'A title']));
  95. DB::flush();
  96. static::assertSame('A title', $attachment->getBestTitle($note));
  97. }
  98. public function testGetUrl()
  99. {
  100. static::bootKernel();
  101. $attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
  102. $id = $attachment->getId();
  103. static::assertSame("/attachment/{$id}/view", $attachment->getUrl());
  104. }
  105. public function testMimetype()
  106. {
  107. static::bootKernel();
  108. $file = new SplFileInfo(INSTALLDIR . '/tests/sample-uploads/image.jpg');
  109. $hash = null;
  110. Event::handle('HashFile', [$file->getPathname(), &$hash]);
  111. $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
  112. static::assertSame('image', $attachment->getMimetypeMajor());
  113. static::assertSame('jpeg', $attachment->getMimetypeMinor());
  114. $mimetype = $attachment->getMimetype();
  115. $attachment->setMimetype(null);
  116. static::assertNull($attachment->getMimetypeMajor());
  117. static::assertNull($attachment->getMimetypeMinor());
  118. $attachment->setMimetype($mimetype);
  119. }
  120. }