2021-08-19 01:32:20 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-08-19 01:32:20 +09:00
|
|
|
// {{{ 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;
|
2021-08-20 03:14:37 +09:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-08-19 01:32:20 +09:00
|
|
|
use App\Util\Exception\NotStoredLocallyException;
|
|
|
|
use App\Util\GNUsocialTestCase;
|
|
|
|
use Functional as F;
|
|
|
|
use Jchook\AssertThrows\AssertThrows;
|
2021-10-10 17:26:18 +09:00
|
|
|
use SplFileInfo;
|
2021-08-19 01:32:20 +09:00
|
|
|
|
|
|
|
class AttachmentThumbnailTest extends GNUsocialTestCase
|
|
|
|
{
|
|
|
|
use AssertThrows;
|
|
|
|
|
|
|
|
public function testAttachmentThumbnailLifecycle()
|
|
|
|
{
|
|
|
|
parent::bootKernel();
|
|
|
|
|
2021-08-19 09:45:11 +09:00
|
|
|
// Data fixture already loaded this file, but we need to get its hash to find it
|
2021-10-10 17:26:18 +09:00
|
|
|
$file = new SplFileInfo(INSTALLDIR . '/tests/sample-uploads/attachment-lifecycle-target.jpg');
|
2021-09-07 03:49:03 +09:00
|
|
|
$hash = null;
|
2021-08-19 01:32:20 +09:00
|
|
|
Event::handle('HashFile', [$file->getPathname(), &$hash]);
|
|
|
|
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
|
|
|
|
|
|
|
|
$thumbs = [
|
2021-09-28 03:47:25 +09:00
|
|
|
AttachmentThumbnail::getOrCreate($attachment, 'small', crop: false),
|
|
|
|
AttachmentThumbnail::getOrCreate($attachment, 'medium', crop: false),
|
|
|
|
AttachmentThumbnail::getOrCreate($attachment, 'medium', crop: false),
|
|
|
|
$thumb = AttachmentThumbnail::getOrCreate($attachment, 'big', crop: false),
|
2021-08-19 01:32:20 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2021-08-19 09:45:11 +09:00
|
|
|
// This was deleted earlier, and the backed storage as well, so we can't generate another thumbnail
|
2021-09-28 03:47:25 +09:00
|
|
|
static::assertThrows(NotStoredLocallyException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, 'big', crop: false));
|
2021-08-19 01:32:20 +09:00
|
|
|
|
|
|
|
$attachment->kill();
|
|
|
|
}
|
|
|
|
|
2021-08-20 03:14:37 +09:00
|
|
|
public function testInvalidThumbnail()
|
|
|
|
{
|
|
|
|
parent::bootKernel();
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
$file = new SplFileInfo(INSTALLDIR . '/tests/sample-uploads/spreadsheet.ods');
|
2021-09-07 03:49:03 +09:00
|
|
|
$hash = null;
|
2021-08-20 03:14:37 +09:00
|
|
|
Event::handle('HashFile', [$file->getPathname(), &$hash]);
|
|
|
|
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
|
|
|
|
|
2021-09-28 03:47:25 +09:00
|
|
|
static::assertThrows(ClientException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, 'small', crop: false));
|
2021-08-20 03:14:37 +09:00
|
|
|
}
|
|
|
|
|
2021-09-28 03:47:25 +09:00
|
|
|
// public function testPredictScalingValues()
|
|
|
|
// {
|
|
|
|
// // Test without cropping
|
|
|
|
// static::assertSame([100, 50], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'small', crop: false));
|
|
|
|
// static::assertSame([200, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'small', crop: false));
|
|
|
|
// static::assertSame([300, 150], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'medium', crop: false));
|
|
|
|
// static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'medium', crop: false));
|
|
|
|
// static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'big', crop: false));
|
|
|
|
|
|
|
|
// // Test with cropping
|
|
|
|
// static::assertSame([100, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'small', crop: true));
|
|
|
|
// static::assertSame([200, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'small', crop: true));
|
|
|
|
// static::assertSame([300, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'medium', crop: true));
|
|
|
|
// static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'medium', crop: true));
|
|
|
|
// static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_size: 'big', crop: true));
|
|
|
|
// }
|
2021-08-19 01:32:20 +09:00
|
|
|
|
2021-09-14 21:36:30 +09:00
|
|
|
// TODO re-enable test
|
|
|
|
// 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());
|
|
|
|
// }
|
2021-08-19 01:32:20 +09:00
|
|
|
}
|