Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/OzuCms/Form/OzuEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class OzuEditorField extends OzuField

private int $maxFileSize = 5;

private ?array $allowedExtensions = null;

private ?string $cropRatio = null;

public function setWithoutParagraphs(): self
Expand Down Expand Up @@ -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;
Expand All @@ -70,14 +72,28 @@ 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;

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');
}

// formatting 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';
Expand All @@ -89,6 +105,7 @@ public function toArray(): array
'withoutParagraphs' => $this->withoutParagraphs,
'hideToolbar' => $this->hideToolbar,
'toolbar' => collect($this->toolbar)->map(fn ($item) => $item->value)->toArray(),
...(!empty($this->allowedExtensions) ? ['allowedExtensions' => $this->allowedExtensions] : []),
'height' => $this->height,
'maxHeight' => $this->maxHeight,
'maxFileSize' => $this->maxFileSize,
Expand Down
1 change: 1 addition & 0 deletions src/OzuCms/Form/OzuEditorToolbarButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
84 changes: 84 additions & 0 deletions tests/Unit/OzuEditorFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

use Code16\OzuClient\Exceptions\OzuClientException;
use Code16\OzuClient\OzuCms\Form\OzuEditorToolbarButton;
use Code16\OzuClient\OzuCms\Form\OzuField;

it('throws when setting allowed extensions if File upload is not enabled', function () {
$field = OzuField::makeEditor('content');

// Default toolbar does not include File
expect(fn () => $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())
->not->toHaveKey('allowedExtensions');

$field->setAllowedExtensions(['.jpg', '.pdf']);

expect($field->toArray())
->toHaveKey('allowedExtensions')
->and($field->toArray()['allowedExtensions'])
->toBe([
'.jpg', '.pdf',
]);
});

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');
});