[TESTS][Attachment][AttachmentThumbnail][GSFile] Reorganize tests and raise test coverage to 100%
This commit is contained in:
parent
4cd3924cc1
commit
043e179c23
@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
namespace App\Tests\Core;
|
namespace App\Tests\Core;
|
||||||
|
|
||||||
use App\Core\DB\DB;
|
|
||||||
use App\Core\GSFile;
|
use App\Core\GSFile;
|
||||||
use App\Util\GNUsocialTestCase;
|
use App\Util\GNUsocialTestCase;
|
||||||
use App\Util\TemporaryFile;
|
use App\Util\TemporaryFile;
|
||||||
@ -30,38 +29,16 @@ class GSFileTest extends GNUsocialTestCase
|
|||||||
{
|
{
|
||||||
public function testSanitizeAndStoreFileAsAttachment()
|
public function testSanitizeAndStoreFileAsAttachment()
|
||||||
{
|
{
|
||||||
|
static::bootKernel();
|
||||||
$file = new TemporaryFile();
|
$file = new TemporaryFile();
|
||||||
|
$file->write('foo');
|
||||||
$attachment = GSFile::sanitizeAndStoreFileAsAttachment($file);
|
$attachment = GSFile::sanitizeAndStoreFileAsAttachment($file);
|
||||||
static::assertSame('application/x-empty', $attachment->getMimetype());
|
static::assertSame('text/plain', $attachment->getMimetype());
|
||||||
static::assertSame(0, $attachment->getSize());
|
static::assertSame(3, $attachment->getSize());
|
||||||
static::assertNull($attachment->getWidth());
|
static::assertNull($attachment->getWidth());
|
||||||
static::assertNull($attachment->getHeight());
|
static::assertNull($attachment->getHeight());
|
||||||
$hash = $attachment->getFilehash();
|
static::assertTrue(file_exists($attachment->getPath()));
|
||||||
$path = $attachment->getPath();
|
|
||||||
static::assertTrue(file_exists($path));
|
|
||||||
static::assertSame(1, $attachment->getLives());
|
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()
|
public function testEnsureFilenameWithProperExtension()
|
||||||
|
95
tests/Entity/AttachmentTest.php
Normal file
95
tests/Entity/AttachmentTest.php
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ 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/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
namespace App\Tests\Entity;
|
||||||
|
|
||||||
|
use App\Core\DB\DB;
|
||||||
|
use App\Core\GSFile;
|
||||||
|
use App\Entity\AttachmentToNote;
|
||||||
|
use App\Entity\Note;
|
||||||
|
use App\Util\GNUsocialTestCase;
|
||||||
|
use App\Util\TemporaryFile;
|
||||||
|
use Jchook\AssertThrows\AssertThrows;
|
||||||
|
|
||||||
|
class AttachmentTest extends GNUsocialTestCase
|
||||||
|
{
|
||||||
|
use AssertThrows;
|
||||||
|
|
||||||
|
public function testAttachmentLifecycle()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
|
||||||
|
// Setup first attachment
|
||||||
|
$file = new TemporaryFile();
|
||||||
|
$attachment = GSFile::sanitizeAndStoreFileAsAttachment($file);
|
||||||
|
$path = $attachment->getPath();
|
||||||
|
$hash = $attachment->getFilehash();
|
||||||
|
static::assertTrue(file_exists($attachment->getPath()));
|
||||||
|
static::assertSame(1, $attachment->getLives());
|
||||||
|
static::assertTrue(file_exists($path));
|
||||||
|
|
||||||
|
// Delete the backed storage of the attachment
|
||||||
|
static::assertTrue($attachment->deleteStorage());
|
||||||
|
static::assertFalse(file_exists($path));
|
||||||
|
static::assertNull($attachment->getPath());
|
||||||
|
DB::flush($attachment);
|
||||||
|
|
||||||
|
// Setup the second attachment, re-adding the backed store
|
||||||
|
$file = new TemporaryFile();
|
||||||
|
$repeated_attachment = GSFile::sanitizeAndStoreFileAsAttachment($file);
|
||||||
|
$path = $attachment->getPath();
|
||||||
|
static::assertSame(2, $repeated_attachment->getLives());
|
||||||
|
static::assertTrue(file_exists($path));
|
||||||
|
|
||||||
|
// Garbage collect the attachment
|
||||||
|
$attachment->kill();
|
||||||
|
static::assertTrue(file_exists($path));
|
||||||
|
static::assertSame(1, $repeated_attachment->getLives());
|
||||||
|
|
||||||
|
// Garbage collect the second attachment, which should delete everything
|
||||||
|
$repeated_attachment->kill();
|
||||||
|
static::assertSame(0, $repeated_attachment->getLives());
|
||||||
|
static::assertFalse(file_exists($path));
|
||||||
|
static::assertSame([], DB::findBy('attachment', ['filehash' => $hash]));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
$actor = DB::findOneBy('gsactor', ['nickname' => 'taken_user']);
|
||||||
|
DB::persist($note = Note::create(['gsactor_id' => $actor->getId(), 'content' => 'some content']));
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
$attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
|
||||||
|
$id = $attachment->getId();
|
||||||
|
static::assertSame("/attachment/{$id}/view", $attachment->getUrl());
|
||||||
|
}
|
||||||
|
}
|
99
tests/Entity/AttachmentThumbnailTest.php
Normal file
99
tests/Entity/AttachmentThumbnailTest.php
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ 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/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
namespace App\Tests\Entity;
|
||||||
|
|
||||||
|
use App\Core\DB\DB;
|
||||||
|
use App\Core\Event;
|
||||||
|
use App\Entity\AttachmentThumbnail;
|
||||||
|
use App\Util\Exception\NotStoredLocallyException;
|
||||||
|
use App\Util\GNUsocialTestCase;
|
||||||
|
use Functional as F;
|
||||||
|
use Jchook\AssertThrows\AssertThrows;
|
||||||
|
|
||||||
|
class AttachmentThumbnailTest extends GNUsocialTestCase
|
||||||
|
{
|
||||||
|
use AssertThrows;
|
||||||
|
|
||||||
|
public function testAttachmentThumbnailLifecycle()
|
||||||
|
{
|
||||||
|
parent::bootKernel();
|
||||||
|
|
||||||
|
// Data fixture already loaded this file, but we need to get it's hash to find it
|
||||||
|
$file = new \SplFileInfo(INSTALLDIR . '/tests/sample-uploads/attachment-lifecycle-target.jpg');
|
||||||
|
Event::handle('HashFile', [$file->getPathname(), &$hash]);
|
||||||
|
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
|
||||||
|
|
||||||
|
$thumbs = [
|
||||||
|
AttachmentThumbnail::getOrCreate($attachment, width: 1, height: 1, crop: false),
|
||||||
|
AttachmentThumbnail::getOrCreate($attachment, width: 2, height: 2, crop: false),
|
||||||
|
AttachmentThumbnail::getOrCreate($attachment, width: 3, height: 3, crop: false),
|
||||||
|
$thumb = AttachmentThumbnail::getOrCreate($attachment, width: 4, height: 4, crop: false),
|
||||||
|
];
|
||||||
|
|
||||||
|
static::assertSame($attachment, $thumb->getAttachment());
|
||||||
|
$thumb->setAttachment(null);
|
||||||
|
static::assertSame($attachment, $thumb->getAttachment());
|
||||||
|
|
||||||
|
$sort = fn ($l, $r) => [$l->getWidth(), $l->getHeight()] <=> [$r->getWidth(), $r->getHeight()];
|
||||||
|
$at_thumbs = F\sort($attachment->getThumbnails(), $sort);
|
||||||
|
static::assertSame($thumbs, $at_thumbs);
|
||||||
|
array_pop($thumbs);
|
||||||
|
$thumb->delete(flush: true);
|
||||||
|
$at_thumbs = F\sort($attachment->getThumbnails(), $sort);
|
||||||
|
static::assertSame($thumbs, $at_thumbs);
|
||||||
|
|
||||||
|
$attachment->deleteStorage();
|
||||||
|
|
||||||
|
// This was deleted earlier, an the backed storage as well, so we can't generate another thumbnail
|
||||||
|
static::assertThrows(NotStoredLocallyException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, width: 4, height: 4, crop: false));
|
||||||
|
|
||||||
|
$attachment->kill();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPredictScalingValues()
|
||||||
|
{
|
||||||
|
// Test without cropping
|
||||||
|
static::assertSame([100, 50], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 100, requested_height: 100, crop: false));
|
||||||
|
static::assertSame([200, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 200, requested_height: 200, crop: false));
|
||||||
|
static::assertSame([300, 150], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 300, requested_height: 300, crop: false));
|
||||||
|
static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 400, requested_height: 400, crop: false));
|
||||||
|
static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 600, requested_height: 600, crop: false));
|
||||||
|
|
||||||
|
// Test with cropping
|
||||||
|
static::assertSame([100, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 100, requested_height: 100, crop: true));
|
||||||
|
static::assertSame([200, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 200, requested_height: 200, crop: true));
|
||||||
|
static::assertSame([300, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 300, requested_height: 300, crop: true));
|
||||||
|
static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 400, requested_height: 400, crop: true));
|
||||||
|
static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 600, requested_height: 600, crop: true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetHTMLAttributes()
|
||||||
|
{
|
||||||
|
parent::bootKernel();
|
||||||
|
$attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
|
||||||
|
$w = $attachment->getWidth();
|
||||||
|
$h = $attachment->getHeight();
|
||||||
|
$thumb = AttachmentThumbnail::getOrCreate($attachment, width: $w, height: $h, crop: false);
|
||||||
|
$id = $attachment->getId();
|
||||||
|
$url = "/attachment/{$id}/thumbnail?w={$w}&h={$h}";
|
||||||
|
static::assertSame($url, $thumb->getUrl());
|
||||||
|
static::assertSame(['height' => $h, 'width' => $w, 'src' => $url], $thumb->getHTMLAttributes());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user