[TESTS] Fix broken tests and expand tests around Attachments

This commit is contained in:
Hugo Sales 2021-08-19 19:14:37 +01:00
parent f1bd4db495
commit 56e5d5c4a0
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 64 additions and 1 deletions

View File

@ -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,6 +35,8 @@ use Symfony\Component\HttpFoundation\Request;
class FormTest extends GNUsocialTestCase
{
use AssertThrows;
public function testCreate()
{
parent::bootKernel();
@ -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
*/

View File

@ -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);
}
}

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 388 KiB