From 5237364a215adc1a8578e105dbead1b8b495cea7 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Mon, 16 Aug 2021 17:11:28 +0100 Subject: [PATCH] [TESTS] Raise test coverage for GSFile to 100% --- tests/Core/GSFileTest.php | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/Core/GSFileTest.php b/tests/Core/GSFileTest.php index bb6827ce43..15706393ec 100644 --- a/tests/Core/GSFileTest.php +++ b/tests/Core/GSFileTest.php @@ -21,11 +21,60 @@ namespace App\Tests\Core; +use App\Core\DB\DB; use App\Core\GSFile; use App\Util\GNUsocialTestCase; +use App\Util\TemporaryFile; class GSFileTest extends GNUsocialTestCase { + public function testSanitizeAndStoreFileAsAttachment() + { + $file = new TemporaryFile(); + $attachment = GSFile::sanitizeAndStoreFileAsAttachment($file); + static::assertSame('application/x-empty', $attachment->getMimetype()); + static::assertSame(0, $attachment->getSize()); + static::assertNull($attachment->getWidth()); + static::assertNull($attachment->getHeight()); + $hash = $attachment->getFilehash(); + $path = $attachment->getPath(); + static::assertTrue(file_exists($path)); + static::assertSame(1, $attachment->getLives()); + DB::flush($attachment); + + static::assertTrue($attachment->deleteStorage()); + static::assertFalse(file_exists($path)); + static::assertNull($attachment->getPath()); + + $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 testEnsureFilenameWithProperExtension() + { + static::assertSame('image.jpeg', GSFile::ensureFilenameWithProperExtension('image.jpeg', 'image/jpeg')); + static::assertSame('image.jpg', GSFile::ensureFilenameWithProperExtension('image.jpg', 'image/jpeg')); + static::assertSame('image.jpeg.png', GSFile::ensureFilenameWithProperExtension('image.jpeg', 'image/png')); + static::assertSame('image.png', GSFile::ensureFilenameWithProperExtension('image', 'image/png')); + static::assertSame('image.gif', GSFile::ensureFilenameWithProperExtension('image', 'image/gif')); + static::assertSame('image.jpg.png', GSFile::ensureFilenameWithProperExtension('image.jpg', 'image/gif', ext: 'png', force: true)); + static::assertNull(GSFile::ensureFilenameWithProperExtension('image.jpg', 'image/gif', ext: null, force: true)); + } + public function testMimetype() { static::assertSame('image', GSFile::mimetypeMajor('image/png'));