2021-09-06 02:31:27 +02:00
|
|
|
<?php
|
|
|
|
|
2021-09-19 12:38:57 +02:00
|
|
|
namespace Aweos\Resizer\Tests;
|
2021-09-06 02:31:27 +02:00
|
|
|
|
2021-10-30 21:13:55 +02:00
|
|
|
use Aweos\Resizer\Exceptions\ResizerException;
|
2021-09-06 02:31:27 +02:00
|
|
|
use Aweos\Resizer\Models\Setting;
|
|
|
|
use Event;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
2021-12-27 02:18:56 +01:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2021-12-17 01:29:15 +01:00
|
|
|
use MediaLibrary;
|
2021-09-06 02:31:27 +02:00
|
|
|
use Storage;
|
|
|
|
|
|
|
|
class ResizerTest extends TestCase
|
|
|
|
{
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Storage::fake('local');
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media = MediaLibrary::instance();
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDontCopyOriginalFileWhenSettingsAreNotSet(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', []);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
2022-02-16 01:48:07 +01:00
|
|
|
$this->media->put('/pages/test.jpg', $file->get());
|
2021-10-30 21:13:55 +02:00
|
|
|
|
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
|
|
|
|
|
|
|
$this->assertFileCount(0, '');
|
|
|
|
}
|
|
|
|
|
2021-12-27 02:18:56 +01:00
|
|
|
public function testDeleteOldCacheWhenUploadingFile(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
2022-02-16 01:48:07 +01:00
|
|
|
$this->media->put('/pages/test.jpg', $file->get());
|
2021-12-27 02:18:56 +01:00
|
|
|
Cache::tags(['resizer', 'resizer.pages/test.jpg'])->put('resizer.original.pages/test.jpg', 'AAA');
|
|
|
|
|
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
|
|
|
|
|
|
|
$this->assertFileCount(0, '');
|
|
|
|
$this->assertFalse(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->has('resizer.original.pages/test.jpg'));
|
|
|
|
}
|
|
|
|
|
2021-10-30 21:13:55 +02:00
|
|
|
public function testThrowExceptionWhenOriginalFileNotFound(): void
|
|
|
|
{
|
|
|
|
$this->expectException(ResizerException::class);
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
|
|
|
|
|
|
|
$this->assertFileCount(0, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCopyOriginalFileWithoutSizesWhenNoSizesAreSet(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(1, 'pages');
|
|
|
|
$this->assertDoesntHaveFile('pages/test.jpg');
|
2021-09-06 02:31:27 +02:00
|
|
|
$this->assertHasFile('pages/test-500x600.jpg');
|
|
|
|
}
|
|
|
|
|
2021-09-13 01:18:40 +02:00
|
|
|
public function testCopyOriginalFileWithoutSizesWhenFilenameDoesntBeginWithASlash(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/test.jpg', $file->get());
|
2021-09-13 01:18:40 +02:00
|
|
|
Event::fire('media.file.upload', [null, 'pages/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(1, 'pages');
|
2021-09-13 01:18:40 +02:00
|
|
|
$this->assertHasFile('pages/test-500x600.jpg');
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:14:39 +01:00
|
|
|
public function testSluggifyFilename(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
|
|
|
$this->media->put('/pages/test ö space.jpg', $file->get());
|
|
|
|
Event::fire('media.file.upload', [null, 'pages/test ö space.jpg', null]);
|
|
|
|
Storage::assertExists('media/pages/test-oe-space.jpg');
|
|
|
|
|
|
|
|
$this->assertFileCount(1, 'pages');
|
|
|
|
$this->assertHasFile('pages/test-oe-space-500x600.jpg');
|
|
|
|
}
|
|
|
|
|
2021-09-06 02:31:27 +02:00
|
|
|
public function testCopyTwoDirectoriesDeepButNotAnotherDirectory(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages-neu/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages-neu/test.jpg', null]);
|
|
|
|
|
|
|
|
$this->assertFileCount(0, 'pages');
|
|
|
|
$this->assertFileCount(0, 'pages-neu');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCopySubdirectoriesOfSelectedPath(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', []);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/neu/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(1, 'pages/neu');
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateSizeIfSizeIsSmallerWithSameAspectRatio(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['250']);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/neu/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(2, 'pages/neu');
|
2021-09-06 02:31:27 +02:00
|
|
|
$this->assertHasFile('pages/neu/test-250x300.jpg');
|
|
|
|
$this->assertHasFile('pages/neu/test-500x600.jpg');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateSizeIfSizeIsSmallerWithDifferentAspectRatio(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['250']);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 100);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/neu/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
|
|
|
|
|
|
|
$this->assertHasFile('pages/neu/test-250x50.jpg');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDontGenerateSizeIfOriginalFileIsSmaller(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['250']);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 250, 1000);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/neu/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(1, 'pages/neu');
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDontGenerateSizeIfImageWouldBeLarger(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['250']);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 249, 1000);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/neu/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(1, 'pages/neu');
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateBreakpointImage(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '5x8']]);
|
|
|
|
Setting::set('breakpoints', ['100']);
|
|
|
|
|
|
|
|
$file = UploadedFile::fake()->image('test.jpg', 500, 400);
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/neu/test.jpg', $file->get());
|
2021-09-06 02:31:27 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(4, 'pages/neu');
|
2021-09-06 02:31:27 +02:00
|
|
|
$this->assertHasFile('pages/neu/test-500x400.jpg');
|
|
|
|
$this->assertHasFile('pages/neu/test-250x400.jpg');
|
|
|
|
$this->assertHasFile('pages/neu/test-100x160.jpg');
|
|
|
|
$this->assertHasFile('pages/neu/test-100x80.jpg');
|
|
|
|
}
|
|
|
|
|
2021-09-14 02:13:29 +02:00
|
|
|
public function testDontGenerateSvgImageVesions(): void
|
2021-09-06 13:03:38 +02:00
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['100']);
|
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
|
2021-09-06 13:03:38 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.svg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(0, 'pages');
|
2021-09-14 02:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneratePngImage(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['100']);
|
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
|
2021-09-14 02:13:29 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.png', null]);
|
|
|
|
|
|
|
|
$this->assertHasFile('pages/test-100x100.png');
|
2021-09-17 14:28:41 +02:00
|
|
|
$this->assertHasFile('pages/test-500x500.png');
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(2, 'pages');
|
2021-09-16 01:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDontUpsizeAJpgImage(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['100', '600']);
|
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
|
2021-09-16 01:11:00 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
|
|
|
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(2, 'pages');
|
2021-09-17 13:29:16 +02:00
|
|
|
$this->assertHasFile('pages/test-100x100.jpg');
|
|
|
|
$this->assertHasFile('pages/test-500x500.jpg');
|
2021-09-16 01:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDontUpsizeAPngImage(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
|
|
|
Setting::set('sizes', []);
|
|
|
|
Setting::set('breakpoints', ['100', '600']);
|
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get());
|
2021-09-16 01:11:00 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.png', null]);
|
|
|
|
|
2021-09-17 13:29:16 +02:00
|
|
|
$this->assertHasFile('pages/test-100x200.png');
|
2021-09-17 09:52:06 +02:00
|
|
|
$this->assertFileCount(2, 'pages');
|
2021-09-06 13:03:38 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 14:22:19 +02:00
|
|
|
public function testGeneratePdfImages(): void
|
|
|
|
{
|
|
|
|
Setting::set('folders', [['folder' => '/pages']]);
|
2021-09-17 14:28:41 +02:00
|
|
|
Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x1']]);
|
|
|
|
Setting::set('breakpoints', [200]);
|
2021-09-17 14:22:19 +02:00
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
$this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
|
2021-09-17 14:22:19 +02:00
|
|
|
Event::fire('media.file.upload', [null, '/pages/test.pdf', null]);
|
|
|
|
|
2021-09-17 14:28:41 +02:00
|
|
|
$this->assertHasFile('pages/test-200x259.pdf.jpg');
|
|
|
|
$this->assertHasFile('pages/test-200x200.pdf.jpg');
|
2021-10-31 22:03:34 +01:00
|
|
|
$this->assertHasFile('pages/test-1275x1275.pdf.jpg');
|
|
|
|
$this->assertHasFile('pages/test-1275x1650.pdf.jpg');
|
2021-09-17 14:28:41 +02:00
|
|
|
$this->assertFileCount(4, 'pages');
|
2021-09-17 14:22:19 +02:00
|
|
|
}
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|