diff --git a/tests/Core/FormTest.php b/tests/Core/FormTest.php index 938a6bf2dd..06279958a6 100644 --- a/tests/Core/FormTest.php +++ b/tests/Core/FormTest.php @@ -24,8 +24,10 @@ namespace App\Tests\Core; use App\Core\DB\DB; use App\Core\Form; use App\Entity\GSActor; +use App\Util\Exception\ServerException; use App\Util\Form\ArrayTransformer; use App\Util\GNUsocialTestCase; +use Jchook\AssertThrows\AssertThrows; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Form as SymfForm; @@ -33,13 +35,15 @@ use Symfony\Component\HttpFoundation\Request; class FormTest extends GNUsocialTestCase { + use AssertThrows; + public function testCreate() { parent::bootKernel(); $form = Form::create($form_array = [ ['content', TextareaType::class, ['label' => ' ', 'data' => '', 'attr' => ['placeholder' => 'placeholder']]], ['array_trans', TextareaType::class, ['data' => ['foo', 'bar'], 'transformer' => ArrayTransformer::class]], - ['testpost', SubmitType::class, ['label' => 'Post']], + ['testpost', SubmitType::class, ['label' => 'Post']], ]); static::assertSame(get_class($form), 'Symfony\\Component\\Form\\Form'); foreach ($form as $name => $f) { @@ -74,6 +78,15 @@ class FormTest extends GNUsocialTestCase static::assertTrue(Form::isRequired($form_array, 'content')); } + /** + * Using 'save' or 'submit' as a form name is not allowed, becuase then they're likely to + * collide with other forms in the same page + */ + public function testDisallowedGenericFormName() + { + static::assertThrows(ServerException::class, fn () => Form::create([['save', SubmitType::class, []]])); + } + /** * Test creating a form with default values pulled from an existing object. Can be used in conjunction with `Form::hanlde` to update said object */ diff --git a/tests/Entity/AttachmentTest.php b/tests/Entity/AttachmentTest.php index 8d38bc38cf..beb58bc094 100644 --- a/tests/Entity/AttachmentTest.php +++ b/tests/Entity/AttachmentTest.php @@ -20,12 +20,14 @@ 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 { @@ -69,6 +71,26 @@ class AttachmentTest extends GNUsocialTestCase 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')); + 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]; @@ -92,4 +114,20 @@ class AttachmentTest extends GNUsocialTestCase $id = $attachment->getId(); static::assertSame("/attachment/{$id}/view", $attachment->getUrl()); } + + public function testMimetype() + { + $file = new \SplFileInfo(INSTALLDIR . '/tests/sample-uploads/image.jpg'); + 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); + } } diff --git a/tests/Entity/AttachmentThumbnailTest.php b/tests/Entity/AttachmentThumbnailTest.php index ab2c624f69..eb03fe7d11 100644 --- a/tests/Entity/AttachmentThumbnailTest.php +++ b/tests/Entity/AttachmentThumbnailTest.php @@ -22,6 +22,7 @@ namespace App\Tests\Entity; use App\Core\DB\DB; use App\Core\Event; use App\Entity\AttachmentThumbnail; +use App\Util\Exception\ClientException; use App\Util\Exception\NotStoredLocallyException; use App\Util\GNUsocialTestCase; use Functional as F; @@ -67,6 +68,17 @@ class AttachmentThumbnailTest extends GNUsocialTestCase $attachment->kill(); } + public function testInvalidThumbnail() + { + parent::bootKernel(); + + $file = new \SplFileInfo(INSTALLDIR . '/tests/sample-uploads/spreadsheet.ods'); + Event::handle('HashFile', [$file->getPathname(), &$hash]); + $attachment = DB::findOneBy('attachment', ['filehash' => $hash]); + + static::assertThrows(ClientException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, width: 1, height: 1, crop: false)); + } + public function testPredictScalingValues() { // Test without cropping diff --git a/tests/sample-uploads/gnu-logo.png b/tests/sample-uploads/gnu-logo.png new file mode 100644 index 0000000000..5d79e748ef Binary files /dev/null and b/tests/sample-uploads/gnu-logo.png differ