|
| 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