[COMPONENT][Attachment][TESTS] Fix Entity/AttachmentTest

This commit is contained in:
Diogo Peralta Cordeiro 2022-03-07 17:08:55 +00:00
parent cc4f967186
commit 47f03d4c9f
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 23 additions and 6 deletions

View File

@ -19,15 +19,17 @@ declare(strict_types = 1);
// along with GNU social. If not, see <http://www.gnu.org/licenses/>. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}} // }}}
namespace App\Tests\Entity; namespace Component\Attachment\tests\Entity;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\GSFile; use App\Core\GSFile;
use App\Core\Router\Router;
use App\Entity\Note; use App\Entity\Note;
use App\Util\GNUsocialTestCase; use App\Util\GNUsocialTestCase;
use App\Util\TemporaryFile; use App\Util\TemporaryFile;
use Component\Attachment\Entity\AttachmentToNote; use Component\Attachment\Entity\AttachmentToNote;
use Component\Conversation\Conversation;
use Jchook\AssertThrows\AssertThrows; use Jchook\AssertThrows\AssertThrows;
use SplFileInfo; use SplFileInfo;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
@ -107,6 +109,7 @@ class AttachmentTest extends GNUsocialTestCase
$actor = DB::findOneBy('actor', ['nickname' => 'taken_user']); $actor = DB::findOneBy('actor', ['nickname' => 'taken_user']);
DB::persist($note = Note::create(['actor_id' => $actor->getId(), 'content' => 'attachment: some content', 'content_type' => 'text/plain', 'is_local' => true])); DB::persist($note = Note::create(['actor_id' => $actor->getId(), 'content' => 'attachment: some content', 'content_type' => 'text/plain', 'is_local' => true]));
Conversation::assignLocalConversation($note, null);
DB::persist(AttachmentToNote::create(['attachment_id' => $attachment->getId(), 'note_id' => $note->getId(), 'title' => 'A title'])); DB::persist(AttachmentToNote::create(['attachment_id' => $attachment->getId(), 'note_id' => $note->getId(), 'title' => 'A title']));
DB::flush(); DB::flush();
@ -118,7 +121,7 @@ class AttachmentTest extends GNUsocialTestCase
static::bootKernel(); static::bootKernel();
$attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0]; $attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
$id = $attachment->getId(); $id = $attachment->getId();
static::assertSame("/attachment/{$id}/view", $attachment->getUrl()); static::assertSame("/object/note/42/attachment/{$id}/view", $attachment->getUrl(note: 42, type: Router::ABSOLUTE_PATH));
} }
public function testMimetype() public function testMimetype()

View File

@ -221,13 +221,15 @@ class Posting extends Component
Event::handle('ProcessNoteContent', [$note, $content, $content_type, $process_note_content_extra_args]); Event::handle('ProcessNoteContent', [$note, $content, $content_type, $process_note_content_extra_args]);
} }
// These are note attachments now, and not just attachments, ensure these relations are ensured
if ($processed_attachments !== []) { if ($processed_attachments !== []) {
foreach ($processed_attachments as [$a, $fname]) { foreach ($processed_attachments as [$a, $fname]) {
// Most attachments should already be associated with its author, but maybe it didn't make sense
//for this attachment, or it's simply a repost of an attachment by a different actor
if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $actor->getId()]) === 0) { if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $actor->getId()]) === 0) {
DB::persist(ActorToAttachment::create($args)); DB::persist(ActorToAttachment::create($args));
} }
DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname])); DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => $fname]));
$a->livesIncrementAndGet();
} }
} }

View File

@ -87,11 +87,18 @@ class RepeatNote extends NoteHandlerPlugin
locale: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(), locale: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(),
// If it's a repeat, the reply_to should be to the original, conversation ought to be the same // If it's a repeat, the reply_to should be to the original, conversation ought to be the same
reply_to: $note->getReplyTo(), reply_to: $note->getReplyTo(),
processed_attachments: $note->getAttachmentsWithTitle(), processed_attachments: $processed_attachments = $note->getAttachmentsWithTitle(),
flush_and_notify: false, flush_and_notify: false,
rendered: $note->getRendered(), rendered: $note->getRendered(),
); );
// Increment attachment lives, posting already handled actor_to_attachment relations for us,
// and it obviously handled the attachment_to_note
foreach ($processed_attachments as [$a, $fname]) {
// We usually don't have to increment lives manually when storing an attachment, but we aren't re-storing it
$a->livesIncrementAndGet();
}
DB::persist(RepeatEntity::create([ DB::persist(RepeatEntity::create([
'note_id' => $repeat->getId(), 'note_id' => $repeat->getId(),
'actor_id' => $actor_id, 'actor_id' => $actor_id,

View File

@ -32,6 +32,7 @@ use App\Util\Exception\NoSuchFileException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\Exception\NotStoredLocallyException; use App\Util\Exception\NotStoredLocallyException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Exception\TemporaryFileException;
use App\Util\TemporaryFile; use App\Util\TemporaryFile;
use Component\Attachment\Entity\Attachment; use Component\Attachment\Entity\Attachment;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
@ -55,8 +56,10 @@ class GSFile
{ {
/** /**
* Perform file validation (checks and normalization), store the given file if needed * Perform file validation (checks and normalization), store the given file if needed
* IMPORTANT: A new attachment is stored with 1 live, a known attachment has its lives incremented
* *
* @throws DuplicateFoundException * @throws DuplicateFoundException
* @throws TemporaryFileException
*/ */
public static function storeFileAsAttachment(TemporaryFile|SymfonyFile $file, bool $check_is_supported_mimetype = true): Attachment public static function storeFileAsAttachment(TemporaryFile|SymfonyFile $file, bool $check_is_supported_mimetype = true): Attachment
{ {
@ -65,8 +68,8 @@ class GSFile
try { try {
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]); $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
// Attachment Exists // Attachment Exists
// We had this attachment, but not the file, thus no filename, update meta
if (\is_null($attachment->getFilename())) { if (\is_null($attachment->getFilename())) {
// We had this attachment, but not the file, thus no filename, update meta
$mimetype = $attachment->getMimetype() ?? $file->getMimeType(); $mimetype = $attachment->getMimetype() ?? $file->getMimeType();
$width = $attachment->getWidth(); $width = $attachment->getWidth();
$height = $attachment->getHeight(); $height = $attachment->getHeight();
@ -99,6 +102,8 @@ class GSFile
throw new FileNotAllowedException($mimetype); throw new FileNotAllowedException($mimetype);
} }
} }
// Increment lives
$attachment->livesIncrementAndGet();
} catch (NotFoundException) { } catch (NotFoundException) {
// Create an Attachment // Create an Attachment
// The following properly gets the mimetype with `file` or other // The following properly gets the mimetype with `file` or other
@ -127,7 +132,7 @@ class GSFile
'size' => $file->getSize(), 'size' => $file->getSize(),
'width' => $width, 'width' => $width,
'height' => $height, 'height' => $height,
'lives' => 0, 'lives' => 1,
]); ]);
if (!$check_is_supported_mimetype || self::isMimetypeAllowed($mimetype)) { if (!$check_is_supported_mimetype || self::isMimetypeAllowed($mimetype)) {
$file->move(Common::config('attachments', 'dir'), $hash); $file->move(Common::config('attachments', 'dir'), $hash);