From 2d6e7f119ec05f11740f8e2cf36ef4706a84d1dc Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Thu, 25 Jul 2024 19:00:57 +0200 Subject: [PATCH 1/6] `FileDefaultPreview` class --- .../Ui/FilePreviews/FileDefaultPreview.php | 49 +++++++++++++++++++ .../FilePreviews/FileDefaultPreviewTest.php | 41 ++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/Panel/Ui/FilePreviews/FileDefaultPreview.php create mode 100644 tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php diff --git a/src/Panel/Ui/FilePreviews/FileDefaultPreview.php b/src/Panel/Ui/FilePreviews/FileDefaultPreview.php new file mode 100644 index 0000000000..c17341af3a --- /dev/null +++ b/src/Panel/Ui/FilePreviews/FileDefaultPreview.php @@ -0,0 +1,49 @@ + + * @link https://getkirby.com + * @copyright Bastian Allgeier + * @license https://getkirby.com/license + * @since 5.0.0 + * @internal + */ +class FileDefaultPreview extends FilePreview +{ + public string $component = 'k-file-default-preview'; + + /** + * Accepts any file as last resort + */ + public static function accepts(File $file): bool + { + return true; + } + + /** + * Icon or image to display as thumbnail + */ + public function image(): array|null + { + return $this->file->panel()->image([ + 'back' => 'transparent', + 'ratio' => '1/1' + ], 'cards'); + } + + public function props(): array + { + return [ + ...parent::props(), + 'image' => $this->image() + ]; + } +} diff --git a/tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php b/tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php new file mode 100644 index 0000000000..3eb194d923 --- /dev/null +++ b/tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php @@ -0,0 +1,41 @@ + 'test']); + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + + $this->assertTrue(FileDefaultPreview::accepts($file)); + } + + /** + * @covers ::image + * @covers ::props + */ + public function testProps() + { + $page = new Page(['slug' => 'test']); + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $component = new FileDefaultPreview($file); + $props = $component->props(); + + $this->assertSame('image', $props['image']['icon']); + $this->assertFalse($props['image']['cover']); + $this->assertIsString($props['image']['src']); + } +} From e09870c5899052196e9df7e434e25bfb70f9b000 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Thu, 25 Jul 2024 19:08:30 +0200 Subject: [PATCH 2/6] `FileImagePreview` class --- src/Cms/Core.php | 5 +- src/Panel/File.php | 2 +- .../Ui/FilePreviews/FileImagePreview.php | 48 ++++++++++++++++ tests/Cms/App/AppPluginsTest.php | 2 +- tests/Cms/CoreTest.php | 2 +- .../Ui/FilePreviews/FileImagePreviewTest.php | 57 +++++++++++++++++++ 6 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 src/Panel/Ui/FilePreviews/FileImagePreview.php create mode 100644 tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php diff --git a/src/Cms/Core.php b/src/Cms/Core.php index 441292d0c0..0292a724f4 100644 --- a/src/Cms/Core.php +++ b/src/Cms/Core.php @@ -10,6 +10,7 @@ use Kirby\Cms\Auth\TotpChallenge; use Kirby\Form\Field\BlocksField; use Kirby\Form\Field\LayoutField; +use Kirby\Panel\Ui\FilePreviews\FileImagePreview; /** * The Core class lists all parts of Kirby @@ -280,7 +281,9 @@ public function fields(): array */ public function filePreviews(): array { - return []; + return [ + FileImagePreview::class + ]; } /** diff --git a/src/Panel/File.php b/src/Panel/File.php index 6b81adb86f..813a5b3a80 100644 --- a/src/Panel/File.php +++ b/src/Panel/File.php @@ -301,7 +301,7 @@ protected function imageSource( public function isFocusable(): bool { // blueprint option - $option = $this->model->blueprint()->focus(); + $option = $this->model->blueprint()->focus(); // fallback to whether the file is viewable // (images should be focusable by default, others not) $option ??= $this->model->isViewable(); diff --git a/src/Panel/Ui/FilePreviews/FileImagePreview.php b/src/Panel/Ui/FilePreviews/FileImagePreview.php new file mode 100644 index 0000000000..6b970bc51d --- /dev/null +++ b/src/Panel/Ui/FilePreviews/FileImagePreview.php @@ -0,0 +1,48 @@ + + * @link https://getkirby.com + * @copyright Bastian Allgeier + * @license https://getkirby.com/license + * @since 5.0.0 + * @internal + */ +class FileImagePreview extends FileDefaultPreview +{ + public string $component = 'k-file-image-preview'; + + public static function accepts(File $file): bool + { + return $file->type() === 'image'; + } + + public function details(): array + { + return [ + ...parent::details(), + [ + 'title' => I18n::translate('dimensions'), + 'text' => $this->file->dimensions() . ' ' . I18n::translate('pixel') + ], + [ + 'title' => I18n::translate('orientation'), + 'text' => I18n::translate('orientation.' . $this->file->dimensions()->orientation()) + ] + ]; + } + + public function props(): array + { + return [ + ...parent::props(), + 'focusable' => $this->file->panel()->isFocusable() + ]; + } +} diff --git a/tests/Cms/App/AppPluginsTest.php b/tests/Cms/App/AppPluginsTest.php index 9ca831b64e..a9e81e422c 100644 --- a/tests/Cms/App/AppPluginsTest.php +++ b/tests/Cms/App/AppPluginsTest.php @@ -416,7 +416,7 @@ public function testFilePreviews() ] ]); - $this->assertCount(1, $app->extensions('filePreviews')); + $this->assertCount(2, $app->extensions('filePreviews')); } public function testKirbyTag() diff --git a/tests/Cms/CoreTest.php b/tests/Cms/CoreTest.php index 2eef2147a4..bd6bad3bbb 100644 --- a/tests/Cms/CoreTest.php +++ b/tests/Cms/CoreTest.php @@ -265,7 +265,7 @@ public function testFields() public function testFilePreviews() { $previews = $this->core->filePreviews(); - $this->assertCount(0, $previews); + $this->assertCount(1, $previews); } /** diff --git a/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php b/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php new file mode 100644 index 0000000000..ac12f8235a --- /dev/null +++ b/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php @@ -0,0 +1,57 @@ + 'test']); + + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $this->assertTrue(FileImagePreview::accepts($file)); + + $file = new File(['filename' => 'test.xls', 'parent' => $page]); + $this->assertFalse(FileImagePreview::accepts($file)); + } + + /** + * @covers ::details + */ + public function testDetails() + { + $page = new Page(['slug' => 'test']); + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $preview = new FileImagePreview($file); + $details = $preview->details(); + + $detail = array_pop($details); + $this->assertSame('Orientation', $detail['title']); + + $detail = array_pop($details); + $this->assertSame('Dimensions', $detail['title']); + } + + /** + * @covers ::props + */ + public function testProps() + { + $page = new Page(['slug' => 'test']); + $file = new File(['filename' => 'test.xls', 'parent' => $page]); + $preview = new FileImagePreview($file); + $props = $preview->props(); + $this->assertFalse($props['focusable']); + } +} From 9777f0898883ade4ebcdb8fa272aef45881f19f4 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Tue, 6 Aug 2024 12:52:51 +0200 Subject: [PATCH 3/6] Fix merge conflicts --- .../Ui/FilePreviews/FileDefaultPreview.php | 49 ------------------- .../Ui/FilePreviews/FileImagePreview.php | 9 +++- tests/Panel/Ui/FilePreviewTest.php | 2 +- .../FilePreviews/FileDefaultPreviewTest.php | 41 ---------------- 4 files changed, 8 insertions(+), 93 deletions(-) delete mode 100644 src/Panel/Ui/FilePreviews/FileDefaultPreview.php delete mode 100644 tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php diff --git a/src/Panel/Ui/FilePreviews/FileDefaultPreview.php b/src/Panel/Ui/FilePreviews/FileDefaultPreview.php deleted file mode 100644 index c17341af3a..0000000000 --- a/src/Panel/Ui/FilePreviews/FileDefaultPreview.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @link https://getkirby.com - * @copyright Bastian Allgeier - * @license https://getkirby.com/license - * @since 5.0.0 - * @internal - */ -class FileDefaultPreview extends FilePreview -{ - public string $component = 'k-file-default-preview'; - - /** - * Accepts any file as last resort - */ - public static function accepts(File $file): bool - { - return true; - } - - /** - * Icon or image to display as thumbnail - */ - public function image(): array|null - { - return $this->file->panel()->image([ - 'back' => 'transparent', - 'ratio' => '1/1' - ], 'cards'); - } - - public function props(): array - { - return [ - ...parent::props(), - 'image' => $this->image() - ]; - } -} diff --git a/src/Panel/Ui/FilePreviews/FileImagePreview.php b/src/Panel/Ui/FilePreviews/FileImagePreview.php index 6b970bc51d..66a8f86201 100644 --- a/src/Panel/Ui/FilePreviews/FileImagePreview.php +++ b/src/Panel/Ui/FilePreviews/FileImagePreview.php @@ -3,6 +3,7 @@ namespace Kirby\Panel\Ui\FilePreviews; use Kirby\Cms\File; +use Kirby\Panel\Ui\FilePreview; use Kirby\Toolkit\I18n; /** @@ -14,9 +15,13 @@ * @since 5.0.0 * @internal */ -class FileImagePreview extends FileDefaultPreview +class FileImagePreview extends FilePreview { - public string $component = 'k-file-image-preview'; + public function __construct( + public File $file, + public string $component = 'k-file-image-preview' + ) { + } public static function accepts(File $file): bool { diff --git a/tests/Panel/Ui/FilePreviewTest.php b/tests/Panel/Ui/FilePreviewTest.php index 6f0d60fe29..c4b4420a20 100644 --- a/tests/Panel/Ui/FilePreviewTest.php +++ b/tests/Panel/Ui/FilePreviewTest.php @@ -70,7 +70,7 @@ public function testDetails() public function testFactory() { $page = new Page(['slug' => 'test']); - $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $file = new File(['filename' => 'test.pdf', 'parent' => $page]); $component = FilePreview::factory($file); $this->assertInstanceOf(DefaultFilePreview::class, $component); diff --git a/tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php b/tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php deleted file mode 100644 index 3eb194d923..0000000000 --- a/tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php +++ /dev/null @@ -1,41 +0,0 @@ - 'test']); - $file = new File(['filename' => 'test.jpg', 'parent' => $page]); - - $this->assertTrue(FileDefaultPreview::accepts($file)); - } - - /** - * @covers ::image - * @covers ::props - */ - public function testProps() - { - $page = new Page(['slug' => 'test']); - $file = new File(['filename' => 'test.jpg', 'parent' => $page]); - $component = new FileDefaultPreview($file); - $props = $component->props(); - - $this->assertSame('image', $props['image']['icon']); - $this->assertFalse($props['image']['cover']); - $this->assertIsString($props['image']['src']); - } -} From 248c54496d928a194d2e13d7740c7652b14ef8c6 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Tue, 13 Aug 2024 12:35:10 +0200 Subject: [PATCH 4/6] Fixes after merge conflicts --- src/Cms/Core.php | 4 +-- ...eImagePreview.php => ImageFilePreview.php} | 4 +-- tests/Panel/Ui/FilePreviewTest.php | 36 ++++++++++++------- ...eviewTest.php => ImageFilePreviewTest.php} | 12 +++---- 4 files changed, 34 insertions(+), 22 deletions(-) rename src/Panel/Ui/FilePreviews/{FileImagePreview.php => ImageFilePreview.php} (91%) rename tests/Panel/Ui/FilePreviews/{FileImagePreviewTest.php => ImageFilePreviewTest.php} (77%) diff --git a/src/Cms/Core.php b/src/Cms/Core.php index 0292a724f4..eb6414c164 100644 --- a/src/Cms/Core.php +++ b/src/Cms/Core.php @@ -10,7 +10,7 @@ use Kirby\Cms\Auth\TotpChallenge; use Kirby\Form\Field\BlocksField; use Kirby\Form\Field\LayoutField; -use Kirby\Panel\Ui\FilePreviews\FileImagePreview; +use Kirby\Panel\Ui\FilePreviews\ImageFilePreview; /** * The Core class lists all parts of Kirby @@ -282,7 +282,7 @@ public function fields(): array public function filePreviews(): array { return [ - FileImagePreview::class + ImageFilePreview::class ]; } diff --git a/src/Panel/Ui/FilePreviews/FileImagePreview.php b/src/Panel/Ui/FilePreviews/ImageFilePreview.php similarity index 91% rename from src/Panel/Ui/FilePreviews/FileImagePreview.php rename to src/Panel/Ui/FilePreviews/ImageFilePreview.php index 66a8f86201..6565dbf0de 100644 --- a/src/Panel/Ui/FilePreviews/FileImagePreview.php +++ b/src/Panel/Ui/FilePreviews/ImageFilePreview.php @@ -15,11 +15,11 @@ * @since 5.0.0 * @internal */ -class FileImagePreview extends FilePreview +class ImageFilePreview extends FilePreview { public function __construct( public File $file, - public string $component = 'k-file-image-preview' + public string $component = 'k-image-file-preview' ) { } diff --git a/tests/Panel/Ui/FilePreviewTest.php b/tests/Panel/Ui/FilePreviewTest.php index c4b4420a20..160da4c3c3 100644 --- a/tests/Panel/Ui/FilePreviewTest.php +++ b/tests/Panel/Ui/FilePreviewTest.php @@ -33,6 +33,18 @@ class InvalidFilePreview */ class FilePreviewTest extends TestCase { + public function setUp(): void + { + $this->app = new App([ + 'roots' => [ + 'index' => '/dev/null' + ], + ]); + + // authenticate for preview URL + $this->app->impersonate('kirby'); + } + /** * @covers ::details */ @@ -72,8 +84,8 @@ public function testFactory() $page = new Page(['slug' => 'test']); $file = new File(['filename' => 'test.pdf', 'parent' => $page]); - $component = FilePreview::factory($file); - $this->assertInstanceOf(DefaultFilePreview::class, $component); + $preview = FilePreview::factory($file); + $this->assertInstanceOf(DefaultFilePreview::class, $preview); } /** @@ -92,13 +104,13 @@ public function testFactoryWithCustomHandler() $page = new Page(['slug' => 'test']); - $file = new File(['filename' => 'test.jpg', 'parent' => $page]); - $component = FilePreview::factory($file); - $this->assertInstanceOf(DefaultFilePreview::class, $component); + $file = new File(['filename' => 'test.foo', 'parent' => $page]); + $preview = FilePreview::factory($file); + $this->assertInstanceOf(DefaultFilePreview::class, $preview); - $file = new File(['filename' => 'test.xls', 'parent' => $page]); - $component = FilePreview::factory($file); - $this->assertInstanceOf(DummyFilePreview::class, $component); + $file = new File(['filename' => 'test.xls', 'parent' => $page]); + $preview = FilePreview::factory($file); + $this->assertInstanceOf(DummyFilePreview::class, $preview); } /** @@ -159,10 +171,10 @@ public function testProps() */ public function testRender() { - $page = new Page(['slug' => 'test']); - $file = new File(['filename' => 'test.jpg', 'parent' => $page]); - $component = new DummyFilePreview($file); + $page = new Page(['slug' => 'test']); + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $preview = new DummyFilePreview($file); - $this->assertSame('k-dummy-file-preview', $component->render()['component']); + $this->assertSame('k-dummy-file-preview', $preview->render()['component']); } } diff --git a/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php b/tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php similarity index 77% rename from tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php rename to tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php index ac12f8235a..be48b5a57d 100644 --- a/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php +++ b/tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php @@ -7,10 +7,10 @@ use Kirby\TestCase; /** - * @coversDefaultClass \Kirby\Panel\Ui\FilePreviews\FileImagePreview + * @coversDefaultClass \Kirby\Panel\Ui\FilePreviews\ImageFilePreview * @covers ::__construct */ -class FileImagePreviewTest extends TestCase +class ImageFilePreviewTest extends TestCase { /** * @covers ::accepts @@ -20,10 +20,10 @@ public function testAccepts() $page = new Page(['slug' => 'test']); $file = new File(['filename' => 'test.jpg', 'parent' => $page]); - $this->assertTrue(FileImagePreview::accepts($file)); + $this->assertTrue(ImageFilePreview::accepts($file)); $file = new File(['filename' => 'test.xls', 'parent' => $page]); - $this->assertFalse(FileImagePreview::accepts($file)); + $this->assertFalse(ImageFilePreview::accepts($file)); } /** @@ -33,7 +33,7 @@ public function testDetails() { $page = new Page(['slug' => 'test']); $file = new File(['filename' => 'test.jpg', 'parent' => $page]); - $preview = new FileImagePreview($file); + $preview = new ImageFilePreview($file); $details = $preview->details(); $detail = array_pop($details); @@ -50,7 +50,7 @@ public function testProps() { $page = new Page(['slug' => 'test']); $file = new File(['filename' => 'test.xls', 'parent' => $page]); - $preview = new FileImagePreview($file); + $preview = new ImageFilePreview($file); $props = $preview->props(); $this->assertFalse($props['focusable']); } From 3f905b2f9d4a36f4f5e4779124c513f47488d9f7 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Tue, 13 Aug 2024 12:44:50 +0200 Subject: [PATCH 5/6] Fix `FilePreview::props()` --- src/Panel/Ui/FilePreview.php | 1 + tests/Panel/Ui/FilePreviewTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Panel/Ui/FilePreview.php b/src/Panel/Ui/FilePreview.php index 80c1af7444..6c1ee88a88 100644 --- a/src/Panel/Ui/FilePreview.php +++ b/src/Panel/Ui/FilePreview.php @@ -97,6 +97,7 @@ public function props(): array { return [ 'details' => $this->details(), + 'image' => $this->image(), 'url' => $this->file->previewUrl() ]; } diff --git a/tests/Panel/Ui/FilePreviewTest.php b/tests/Panel/Ui/FilePreviewTest.php index 160da4c3c3..def68d591a 100644 --- a/tests/Panel/Ui/FilePreviewTest.php +++ b/tests/Panel/Ui/FilePreviewTest.php @@ -163,6 +163,7 @@ public function testProps() $props = $preview->props(); $this->assertIsArray($props['details']); + $this->assertIsArray($props['image']); $this->assertIsString($props['url']); } From 161d2ecd7abd0cb3cd050f66d2c1eb47e5a09ab9 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Tue, 13 Aug 2024 13:06:05 +0200 Subject: [PATCH 6/6] Add unit test for ImageFilePreview factory --- tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php b/tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php index be48b5a57d..e9945ccee8 100644 --- a/tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php +++ b/tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php @@ -4,6 +4,7 @@ use Kirby\Cms\File; use Kirby\Cms\Page; +use Kirby\Panel\Ui\FilePreview; use Kirby\TestCase; /** @@ -43,6 +44,17 @@ public function testDetails() $this->assertSame('Dimensions', $detail['title']); } + /** + * @coversNothing + */ + public function testFactory() + { + $page = new Page(['slug' => 'test']); + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $preview = FilePreview::factory($file); + $this->assertInstanceOf(ImageFilePreview::class, $preview); + } + /** * @covers ::props */