From 6a231476e7eadced67b6bdb1af2987e14671c616 Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Thu, 4 Dec 2025 15:33:46 +0100 Subject: [PATCH 1/3] Allow to upload files in editors --- src/OzuCms/Form/OzuEditorField.php | 23 ++++++- src/OzuCms/Form/OzuEditorToolbarButton.php | 1 + tests/Unit/OzuEditorFieldTest.php | 79 ++++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/OzuEditorFieldTest.php diff --git a/src/OzuCms/Form/OzuEditorField.php b/src/OzuCms/Form/OzuEditorField.php index ed93f1d..68555c0 100644 --- a/src/OzuCms/Form/OzuEditorField.php +++ b/src/OzuCms/Form/OzuEditorField.php @@ -24,6 +24,8 @@ class OzuEditorField extends OzuField private int $maxFileSize = 5; + private array $allowedExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp']; + private ?string $cropRatio = null; public function setWithoutParagraphs(): self @@ -58,8 +60,8 @@ public function setHeight(int $height, ?int $maxHeight = null): self public function setMaxFileSize(int $maxFileSize): self { - if (!in_array(OzuEditorToolbarButton::Image, $this->toolbar)) { - throw new OzuClientException('You should allow Image Uploads by adding OzuEditorToolbarButton::Image in toolbar configuration before setting max file size'); + if (!in_array(OzuEditorToolbarButton::Image, $this->toolbar) && !in_array(OzuEditorToolbarButton::File, $this->toolbar)) { + throw new OzuClientException('You should allow Uploads by adding OzuEditorToolbarButton::Image or OzuEditorToolbarButton::File in toolbar configuration before setting max file size'); } $this->maxFileSize = $maxFileSize; @@ -70,7 +72,7 @@ public function setMaxFileSize(int $maxFileSize): self public function setCropRatio(string $cropRatio): self { if (!in_array(OzuEditorToolbarButton::Image, $this->toolbar)) { - throw new OzuClientException('You should allow Image Uploads by adding OzuEditorToolbarButton::Image in toolbar configuration before setting image crop ratio'); + throw new OzuClientException('You should allow image uploads by adding OzuEditorToolbarButton::Image in toolbar configuration before setting image crop ratio'); } $this->cropRatio = $cropRatio; @@ -78,6 +80,20 @@ public function setCropRatio(string $cropRatio): self return $this; } + public function setAllowedExtensions(array $extensions): self + { + if (!in_array(OzuEditorToolbarButton::File, $this->toolbar)) { + throw new OzuClientException('You should allow uploads by adding OzuEditorToolbarButton::File in toolbar configuration before setting the allowed extensions for uploads'); + } + + // formating extensions to be compatible with the editor + $this->allowedExtensions = collect($extensions) + ->map(fn ($filter) => str($filter)->trim()->start('.')->value()) + ->all(); + + return $this; + } + public function type(): string { return 'editor'; @@ -89,6 +105,7 @@ public function toArray(): array 'withoutParagraphs' => $this->withoutParagraphs, 'hideToolbar' => $this->hideToolbar, 'toolbar' => collect($this->toolbar)->map(fn ($item) => $item->value)->toArray(), + ...(in_array(OzuEditorToolbarButton::File, $this->toolbar) ? ['allowedExtensions' => $this->allowedExtensions] : []), 'height' => $this->height, 'maxHeight' => $this->maxHeight, 'maxFileSize' => $this->maxFileSize, diff --git a/src/OzuCms/Form/OzuEditorToolbarButton.php b/src/OzuCms/Form/OzuEditorToolbarButton.php index 8389855..c98e997 100644 --- a/src/OzuCms/Form/OzuEditorToolbarButton.php +++ b/src/OzuCms/Form/OzuEditorToolbarButton.php @@ -15,6 +15,7 @@ enum OzuEditorToolbarButton: string case Heading2 = 'heading-2'; case Iframe = 'iframe'; case Image = 'upload-image'; + case File = 'upload'; case Video = 'video'; case Quote = 'blockquote'; } diff --git a/tests/Unit/OzuEditorFieldTest.php b/tests/Unit/OzuEditorFieldTest.php new file mode 100644 index 0000000..682c86a --- /dev/null +++ b/tests/Unit/OzuEditorFieldTest.php @@ -0,0 +1,79 @@ + $field->setAllowedExtensions(['jpg', 'pdf'])) + ->toThrow(OzuClientException::class); +}); + +it('normalizes allowed extensions when File upload is enabled', function () { + $field = OzuField::makeEditor('content') + ->setToolbar([ + OzuEditorToolbarButton::Bold, + OzuEditorToolbarButton::File, // enable uploads + ]); + + $field->setAllowedExtensions([' jpg ', 'PDF', '.zip']); + + expect($field->toArray()['allowedExtensions']) + ->toBe(['.jpg', '.PDF', '.zip']); +}); + +it('includes allowedExtensions in payload only when File upload is enabled', function () { + $field = OzuField::makeEditor('content'); + + // By default, no File button -> no allowedExtensions key + expect($field->toArray()) + ->not->toHaveKey('allowedExtensions'); + + // Enable File button -> allowedExtensions present with defaults + $field->setToolbar([ + OzuEditorToolbarButton::Bold, + OzuEditorToolbarButton::File, + ]); + + expect($field->toArray()) + ->toHaveKey('allowedExtensions') + ->and($field->toArray()['allowedExtensions']) + ->toBe([ + '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp', + ]); +}); + +it('enforces max file size can only be set when Image or File upload is enabled', function () { + $field = OzuField::makeEditor('content'); + + // Neither Image nor File -> should throw + expect(fn () => $field->setMaxFileSize(10)) + ->toThrow(OzuClientException::class); + + // With File -> should work + $fieldWithFile = OzuField::makeEditor('content') + ->setToolbar([OzuEditorToolbarButton::File]); + $fieldWithFile->setMaxFileSize(12); + expect($fieldWithFile->toArray()['maxFileSize'])->toBe(12); + + // With Image -> should also work + $fieldWithImage = OzuField::makeEditor('content') + ->setToolbar([OzuEditorToolbarButton::Image]); + $fieldWithImage->setMaxFileSize(8); + expect($fieldWithImage->toArray()['maxFileSize'])->toBe(8); +}); + +it('enforces crop ratio can only be set when Image upload is enabled', function () { + $field = OzuField::makeEditor('content'); + expect(fn () => $field->setCropRatio('16/9')) + ->toThrow(OzuClientException::class); + + $fieldWithImage = OzuField::makeEditor('content') + ->setToolbar([OzuEditorToolbarButton::Image]) + ->setCropRatio('4/3'); + + expect($fieldWithImage->toArray()['cropRatio'])->toBe('4/3'); +}); From ceed2983c6dfd4df5926a1b268ef0024b83fcb64 Mon Sep 17 00:00:00 2001 From: Lucien Date: Thu, 4 Dec 2025 15:38:54 +0100 Subject: [PATCH 2/3] Update src/OzuCms/Form/OzuEditorField.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/OzuCms/Form/OzuEditorField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OzuCms/Form/OzuEditorField.php b/src/OzuCms/Form/OzuEditorField.php index 68555c0..4514372 100644 --- a/src/OzuCms/Form/OzuEditorField.php +++ b/src/OzuCms/Form/OzuEditorField.php @@ -86,7 +86,7 @@ public function setAllowedExtensions(array $extensions): self throw new OzuClientException('You should allow uploads by adding OzuEditorToolbarButton::File in toolbar configuration before setting the allowed extensions for uploads'); } - // formating extensions to be compatible with the editor + // formatting extensions to be compatible with the editor $this->allowedExtensions = collect($extensions) ->map(fn ($filter) => str($filter)->trim()->start('.')->value()) ->all(); From 3d9f54b5a56a39415a3d69eb124fc16aef5c5faa Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Thu, 4 Dec 2025 15:54:00 +0100 Subject: [PATCH 3/3] Allow all extensions by default when allowedExtensions isnt set --- src/OzuCms/Form/OzuEditorField.php | 4 ++-- tests/Unit/OzuEditorFieldTest.php | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/OzuCms/Form/OzuEditorField.php b/src/OzuCms/Form/OzuEditorField.php index 68555c0..2b6042a 100644 --- a/src/OzuCms/Form/OzuEditorField.php +++ b/src/OzuCms/Form/OzuEditorField.php @@ -24,7 +24,7 @@ class OzuEditorField extends OzuField private int $maxFileSize = 5; - private array $allowedExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp']; + private ?array $allowedExtensions = null; private ?string $cropRatio = null; @@ -105,7 +105,7 @@ public function toArray(): array 'withoutParagraphs' => $this->withoutParagraphs, 'hideToolbar' => $this->hideToolbar, 'toolbar' => collect($this->toolbar)->map(fn ($item) => $item->value)->toArray(), - ...(in_array(OzuEditorToolbarButton::File, $this->toolbar) ? ['allowedExtensions' => $this->allowedExtensions] : []), + ...(!empty($this->allowedExtensions) ? ['allowedExtensions' => $this->allowedExtensions] : []), 'height' => $this->height, 'maxHeight' => $this->maxHeight, 'maxFileSize' => $this->maxFileSize, diff --git a/tests/Unit/OzuEditorFieldTest.php b/tests/Unit/OzuEditorFieldTest.php index 682c86a..0b5fe8f 100644 --- a/tests/Unit/OzuEditorFieldTest.php +++ b/tests/Unit/OzuEditorFieldTest.php @@ -38,11 +38,16 @@ OzuEditorToolbarButton::File, ]); + expect($field->toArray()) + ->not->toHaveKey('allowedExtensions'); + + $field->setAllowedExtensions(['.jpg', '.pdf']); + expect($field->toArray()) ->toHaveKey('allowedExtensions') ->and($field->toArray()['allowedExtensions']) ->toBe([ - '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp', + '.jpg', '.pdf', ]); });