Skip to content

Commit 63de2d8

Browse files
Allow to upload files in editors (#24)
* Allow to upload files in editors * Update src/OzuCms/Form/OzuEditorField.php * Allow all extensions by default when allowedExtensions isnt set
1 parent 3038c87 commit 63de2d8

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

src/OzuCms/Form/OzuEditorField.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class OzuEditorField extends OzuField
2424

2525
private int $maxFileSize = 5;
2626

27+
private ?array $allowedExtensions = null;
28+
2729
private ?string $cropRatio = null;
2830

2931
public function setWithoutParagraphs(): self
@@ -58,8 +60,8 @@ public function setHeight(int $height, ?int $maxHeight = null): self
5860

5961
public function setMaxFileSize(int $maxFileSize): self
6062
{
61-
if (!in_array(OzuEditorToolbarButton::Image, $this->toolbar)) {
62-
throw new OzuClientException('You should allow Image Uploads by adding OzuEditorToolbarButton::Image in toolbar configuration before setting max file size');
63+
if (!in_array(OzuEditorToolbarButton::Image, $this->toolbar) && !in_array(OzuEditorToolbarButton::File, $this->toolbar)) {
64+
throw new OzuClientException('You should allow Uploads by adding OzuEditorToolbarButton::Image or OzuEditorToolbarButton::File in toolbar configuration before setting max file size');
6365
}
6466

6567
$this->maxFileSize = $maxFileSize;
@@ -70,14 +72,28 @@ public function setMaxFileSize(int $maxFileSize): self
7072
public function setCropRatio(string $cropRatio): self
7173
{
7274
if (!in_array(OzuEditorToolbarButton::Image, $this->toolbar)) {
73-
throw new OzuClientException('You should allow Image Uploads by adding OzuEditorToolbarButton::Image in toolbar configuration before setting image crop ratio');
75+
throw new OzuClientException('You should allow image uploads by adding OzuEditorToolbarButton::Image in toolbar configuration before setting image crop ratio');
7476
}
7577

7678
$this->cropRatio = $cropRatio;
7779

7880
return $this;
7981
}
8082

83+
public function setAllowedExtensions(array $extensions): self
84+
{
85+
if (!in_array(OzuEditorToolbarButton::File, $this->toolbar)) {
86+
throw new OzuClientException('You should allow uploads by adding OzuEditorToolbarButton::File in toolbar configuration before setting the allowed extensions for uploads');
87+
}
88+
89+
// formatting extensions to be compatible with the editor
90+
$this->allowedExtensions = collect($extensions)
91+
->map(fn ($filter) => str($filter)->trim()->start('.')->value())
92+
->all();
93+
94+
return $this;
95+
}
96+
8197
public function type(): string
8298
{
8399
return 'editor';
@@ -89,6 +105,7 @@ public function toArray(): array
89105
'withoutParagraphs' => $this->withoutParagraphs,
90106
'hideToolbar' => $this->hideToolbar,
91107
'toolbar' => collect($this->toolbar)->map(fn ($item) => $item->value)->toArray(),
108+
...(!empty($this->allowedExtensions) ? ['allowedExtensions' => $this->allowedExtensions] : []),
92109
'height' => $this->height,
93110
'maxHeight' => $this->maxHeight,
94111
'maxFileSize' => $this->maxFileSize,

src/OzuCms/Form/OzuEditorToolbarButton.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum OzuEditorToolbarButton: string
1515
case Heading2 = 'heading-2';
1616
case Iframe = 'iframe';
1717
case Image = 'upload-image';
18+
case File = 'upload';
1819
case Video = 'video';
1920
case Quote = 'blockquote';
2021
}

tests/Unit/OzuEditorFieldTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
use Code16\OzuClient\Exceptions\OzuClientException;
4+
use Code16\OzuClient\OzuCms\Form\OzuEditorToolbarButton;
5+
use Code16\OzuClient\OzuCms\Form\OzuField;
6+
7+
it('throws when setting allowed extensions if File upload is not enabled', function () {
8+
$field = OzuField::makeEditor('content');
9+
10+
// Default toolbar does not include File
11+
expect(fn () => $field->setAllowedExtensions(['jpg', 'pdf']))
12+
->toThrow(OzuClientException::class);
13+
});
14+
15+
it('normalizes allowed extensions when File upload is enabled', function () {
16+
$field = OzuField::makeEditor('content')
17+
->setToolbar([
18+
OzuEditorToolbarButton::Bold,
19+
OzuEditorToolbarButton::File, // enable uploads
20+
]);
21+
22+
$field->setAllowedExtensions([' jpg ', 'PDF', '.zip']);
23+
24+
expect($field->toArray()['allowedExtensions'])
25+
->toBe(['.jpg', '.PDF', '.zip']);
26+
});
27+
28+
it('includes allowedExtensions in payload only when File upload is enabled', function () {
29+
$field = OzuField::makeEditor('content');
30+
31+
// By default, no File button -> no allowedExtensions key
32+
expect($field->toArray())
33+
->not->toHaveKey('allowedExtensions');
34+
35+
// Enable File button -> allowedExtensions present with defaults
36+
$field->setToolbar([
37+
OzuEditorToolbarButton::Bold,
38+
OzuEditorToolbarButton::File,
39+
]);
40+
41+
expect($field->toArray())
42+
->not->toHaveKey('allowedExtensions');
43+
44+
$field->setAllowedExtensions(['.jpg', '.pdf']);
45+
46+
expect($field->toArray())
47+
->toHaveKey('allowedExtensions')
48+
->and($field->toArray()['allowedExtensions'])
49+
->toBe([
50+
'.jpg', '.pdf',
51+
]);
52+
});
53+
54+
it('enforces max file size can only be set when Image or File upload is enabled', function () {
55+
$field = OzuField::makeEditor('content');
56+
57+
// Neither Image nor File -> should throw
58+
expect(fn () => $field->setMaxFileSize(10))
59+
->toThrow(OzuClientException::class);
60+
61+
// With File -> should work
62+
$fieldWithFile = OzuField::makeEditor('content')
63+
->setToolbar([OzuEditorToolbarButton::File]);
64+
$fieldWithFile->setMaxFileSize(12);
65+
expect($fieldWithFile->toArray()['maxFileSize'])->toBe(12);
66+
67+
// With Image -> should also work
68+
$fieldWithImage = OzuField::makeEditor('content')
69+
->setToolbar([OzuEditorToolbarButton::Image]);
70+
$fieldWithImage->setMaxFileSize(8);
71+
expect($fieldWithImage->toArray()['maxFileSize'])->toBe(8);
72+
});
73+
74+
it('enforces crop ratio can only be set when Image upload is enabled', function () {
75+
$field = OzuField::makeEditor('content');
76+
expect(fn () => $field->setCropRatio('16/9'))
77+
->toThrow(OzuClientException::class);
78+
79+
$fieldWithImage = OzuField::makeEditor('content')
80+
->setToolbar([OzuEditorToolbarButton::Image])
81+
->setCropRatio('4/3');
82+
83+
expect($fieldWithImage->toArray()['cropRatio'])->toBe('4/3');
84+
});

0 commit comments

Comments
 (0)