2021-08-19 01:32:20 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-08-19 01:32:20 +09:00
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
2022-03-08 02:08:55 +09:00
|
|
|
namespace Component\Attachment\tests\Entity;
|
2021-08-19 01:32:20 +09:00
|
|
|
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2021-08-20 03:14:37 +09:00
|
|
|
use App\Core\Event;
|
2021-08-19 01:32:20 +09:00
|
|
|
use App\Core\GSFile;
|
2022-03-28 00:43:59 +09:00
|
|
|
use App\Core\Router;
|
2021-08-19 01:32:20 +09:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\GNUsocialTestCase;
|
|
|
|
use App\Util\TemporaryFile;
|
2021-12-26 18:48:16 +09:00
|
|
|
use Component\Attachment\Entity\AttachmentToNote;
|
2022-03-08 02:08:55 +09:00
|
|
|
use Component\Conversation\Conversation;
|
2021-08-19 01:32:20 +09:00
|
|
|
use Jchook\AssertThrows\AssertThrows;
|
2021-10-10 17:26:18 +09:00
|
|
|
use SplFileInfo;
|
2021-08-20 03:14:37 +09:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
2021-08-19 01:32:20 +09:00
|
|
|
|
|
|
|
class AttachmentTest extends GNUsocialTestCase
|
|
|
|
{
|
|
|
|
use AssertThrows;
|
|
|
|
|
|
|
|
public function testAttachmentLifecycle()
|
|
|
|
{
|
|
|
|
static::bootKernel();
|
|
|
|
|
|
|
|
// Setup first attachment
|
|
|
|
$file = new TemporaryFile();
|
2021-10-29 02:05:28 +09:00
|
|
|
$attachment = GSFile::storeFileAsAttachment($file, check_is_supported_mimetype: false);
|
2021-08-19 01:32:20 +09:00
|
|
|
$path = $attachment->getPath();
|
|
|
|
$hash = $attachment->getFilehash();
|
2021-10-10 17:26:18 +09:00
|
|
|
static::assertFileExists($attachment->getPath());
|
2021-08-19 01:32:20 +09:00
|
|
|
static::assertSame(1, $attachment->getLives());
|
2021-10-10 17:26:18 +09:00
|
|
|
static::assertFileExists($path);
|
2021-08-19 01:32:20 +09:00
|
|
|
|
|
|
|
// Delete the backed storage of the attachment
|
|
|
|
static::assertTrue($attachment->deleteStorage());
|
2021-10-29 02:05:28 +09:00
|
|
|
static::assertFileDoesNotExist($path);
|
2021-08-19 01:32:20 +09:00
|
|
|
static::assertNull($attachment->getPath());
|
2021-09-07 04:59:36 +09:00
|
|
|
DB::persist($attachment);
|
|
|
|
DB::flush();
|
2021-08-19 01:32:20 +09:00
|
|
|
|
|
|
|
// Setup the second attachment, re-adding the backed store
|
|
|
|
$file = new TemporaryFile();
|
2021-10-29 02:05:28 +09:00
|
|
|
$repeated_attachment = GSFile::storeFileAsAttachment($file, check_is_supported_mimetype: false);
|
2021-08-19 01:32:20 +09:00
|
|
|
$path = $attachment->getPath();
|
|
|
|
static::assertSame(2, $repeated_attachment->getLives());
|
2021-10-10 17:26:18 +09:00
|
|
|
static::assertFileExists($path);
|
2021-08-19 01:32:20 +09:00
|
|
|
|
|
|
|
// Garbage collect the attachment
|
|
|
|
$attachment->kill();
|
2021-10-10 17:26:18 +09:00
|
|
|
static::assertFileExists($path);
|
2021-08-19 01:32:20 +09:00
|
|
|
static::assertSame(1, $repeated_attachment->getLives());
|
|
|
|
|
|
|
|
// Garbage collect the second attachment, which should delete everything
|
|
|
|
$repeated_attachment->kill();
|
|
|
|
static::assertSame(0, $repeated_attachment->getLives());
|
2021-10-29 02:05:28 +09:00
|
|
|
static::assertFileDoesNotExist($path);
|
2021-08-19 01:32:20 +09:00
|
|
|
static::assertSame([], DB::findBy('attachment', ['filehash' => $hash]));
|
|
|
|
}
|
|
|
|
|
2021-08-20 03:14:37 +09:00
|
|
|
public function testSanitizeAndStoreFileAsAttachment()
|
|
|
|
{
|
|
|
|
$test = function (string $method) {
|
|
|
|
$temp_file = new TemporaryFile();
|
|
|
|
$temp_file->write(file_get_contents(INSTALLDIR . '/tests/sample-uploads/gnu-logo.png'));
|
2021-09-07 03:49:03 +09:00
|
|
|
$hash = null;
|
2021-08-20 03:14:37 +09:00
|
|
|
Event::handle('HashFile', [$temp_file->getPathname(), &$hash]);
|
|
|
|
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
|
|
|
|
$attachment->{$method}();
|
|
|
|
DB::flush();
|
|
|
|
|
|
|
|
$file = new File($temp_file->getRealPath());
|
2021-09-22 23:04:45 +09:00
|
|
|
GSFile::storeFileAsAttachment($file);
|
2021-08-20 03:14:37 +09:00
|
|
|
static::assertNotNull($attachment->getFilename());
|
2021-10-10 17:26:18 +09:00
|
|
|
static::assertFileExists($attachment->getPath());
|
2021-08-20 03:14:37 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
$test('deleteStorage');
|
|
|
|
$test('kill');
|
|
|
|
}
|
|
|
|
|
2021-08-19 01:32:20 +09:00
|
|
|
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);
|
|
|
|
|
2022-03-27 23:19:09 +09:00
|
|
|
$actor = DB::findOneBy('actor', ['nickname' => 'taken_user']);
|
2021-11-24 07:34:35 +09:00
|
|
|
DB::persist($note = Note::create(['actor_id' => $actor->getId(), 'content' => 'attachment: some content', 'content_type' => 'text/plain', 'is_local' => true]));
|
2022-03-08 02:08:55 +09:00
|
|
|
Conversation::assignLocalConversation($note, null);
|
2021-08-19 01:32:20 +09:00
|
|
|
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()
|
|
|
|
{
|
2021-10-29 02:05:28 +09:00
|
|
|
static::bootKernel();
|
2021-08-19 01:32:20 +09:00
|
|
|
$attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
|
|
|
|
$id = $attachment->getId();
|
2022-03-08 02:08:55 +09:00
|
|
|
static::assertSame("/object/note/42/attachment/{$id}/view", $attachment->getUrl(note: 42, type: Router::ABSOLUTE_PATH));
|
2021-08-19 01:32:20 +09:00
|
|
|
}
|
2021-08-20 03:14:37 +09:00
|
|
|
|
|
|
|
public function testMimetype()
|
|
|
|
{
|
2021-10-29 02:05:28 +09:00
|
|
|
static::bootKernel();
|
2021-10-10 17:26:18 +09:00
|
|
|
$file = new SplFileInfo(INSTALLDIR . '/tests/sample-uploads/image.jpg');
|
2021-09-07 03:49:03 +09:00
|
|
|
$hash = null;
|
2021-08-20 03:14:37 +09:00
|
|
|
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);
|
|
|
|
}
|
2021-08-19 01:32:20 +09:00
|
|
|
}
|