123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace App\Tests\Entity;
- use App\Core\DB\DB;
- use App\Core\Event;
- use App\Core\GSFile;
- use App\Entity\AttachmentToNote;
- use App\Entity\Note;
- use App\Util\GNUsocialTestCase;
- use App\Util\TemporaryFile;
- use Jchook\AssertThrows\AssertThrows;
- use Symfony\Component\HttpFoundation\File\File;
- class AttachmentTest extends GNUsocialTestCase
- {
- use AssertThrows;
- public function testAttachmentLifecycle()
- {
- static::bootKernel();
-
- $file = new TemporaryFile();
- $attachment = GSFile::sanitizeAndStoreFileAsAttachment($file);
- $path = $attachment->getPath();
- $hash = $attachment->getFilehash();
- static::assertTrue(file_exists($attachment->getPath()));
- static::assertSame(1, $attachment->getLives());
- static::assertTrue(file_exists($path));
-
- static::assertTrue($attachment->deleteStorage());
- static::assertFalse(file_exists($path));
- static::assertNull($attachment->getPath());
- DB::persist($attachment);
- DB::flush();
-
- $file = new TemporaryFile();
- $repeated_attachment = GSFile::sanitizeAndStoreFileAsAttachment($file);
- $path = $attachment->getPath();
- static::assertSame(2, $repeated_attachment->getLives());
- static::assertTrue(file_exists($path));
-
- $attachment->kill();
- static::assertTrue(file_exists($path));
- static::assertSame(1, $repeated_attachment->getLives());
-
- $repeated_attachment->kill();
- static::assertSame(0, $repeated_attachment->getLives());
- static::assertFalse(file_exists($path));
- static::assertSame([], DB::findBy('attachment', ['filehash' => $hash]));
- }
- public function testSanitizeAndStoreFileAsAttachment()
- {
- $test = function (string $method) {
- $temp_file = new TemporaryFile();
- $temp_file->write(file_get_contents(INSTALLDIR . '/tests/sample-uploads/gnu-logo.png'));
- $hash = null;
- Event::handle('HashFile', [$temp_file->getPathname(), &$hash]);
- $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
- $attachment->{$method}();
- DB::flush();
- $file = new File($temp_file->getRealPath());
- GSFile::sanitizeAndStoreFileAsAttachment($file);
- static::assertNotNull($attachment->getFilename());
- static::assertTrue(file_exists($attachment->getPath()));
- };
- $test('deleteStorage');
- $test('kill');
- }
- public function testGetBestTitle()
- {
- $attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
- $filename = $attachment->getFilename();
- static::assertSame($attachment->getFilename(), $attachment->getBestTitle());
- $attachment->setFilename(null);
- static::assertSame('Untitled attachment', $attachment->getBestTitle());
- $attachment->setFilename($filename);
- $actor = DB::findOneBy('gsactor', ['nickname' => 'taken_user']);
- DB::persist($note = Note::create(['gsactor_id' => $actor->getId(), 'content' => 'some content']));
- DB::persist(AttachmentToNote::create(['attachment_id' => $attachment->getId(), 'note_id' => $note->getId(), 'title' => 'A title']));
- DB::flush();
- static::assertSame('A title', $attachment->getBestTitle($note));
- }
- public function testGetUrl()
- {
- $attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
- $id = $attachment->getId();
- static::assertSame("/attachment/{$id}/view", $attachment->getUrl());
- }
- public function testMimetype()
- {
- $file = new \SplFileInfo(INSTALLDIR . '/tests/sample-uploads/image.jpg');
- $hash = null;
- Event::handle('HashFile', [$file->getPathname(), &$hash]);
- $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
- static::assertSame('image', $attachment->getMimetypeMajor());
- static::assertSame('jpeg', $attachment->getMimetypeMinor());
- $mimetype = $attachment->getMimetype();
- $attachment->setMimetype(null);
- static::assertNull($attachment->getMimetypeMajor());
- static::assertNull($attachment->getMimetypeMinor());
- $attachment->setMimetype($mimetype);
- }
- }
|