AttachmentTest.php 5.2 KB

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