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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests/*
!tests/frontend-compat.test.mjs
!tests/block-selection.test.mjs
!tests/datepicker-compat.test.mjs
!tests/fileupload-clear.test.mjs
!tests/preview-url.test.mjs
!tests/vite-compat.test.mjs
docs
Expand Down
2 changes: 1 addition & 1 deletion _dev/src/components/form/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const removeImg = () => {

HttpClient.post(props.uploadUrl, params)
.then((data) => {
updateValue(props.default);
updateValue({ url: "" });
})
.catch((error) => console.error(error));
};
Expand Down
2 changes: 0 additions & 2 deletions classes/prettyblocks/core/FieldCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,8 +1030,6 @@ public function secureFileUploadEntry($array)
}
} elseif (isset($array['url']) && $array['url'] !== '') {
$url = $array['url'];
} elseif ($this->force_default_value && isset($this->default['url'])) {
$url = $this->default['url'];
}
$secure['url'] = $url;

Expand Down
3 changes: 2 additions & 1 deletion docker/release.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ FROM node:20-alpine AS assets

WORKDIR /src
COPY _dev/package.json _dev/package-lock.json _dev/yarn.lock ./_dev/
COPY tests/frontend-compat.test.mjs ./tests/
COPY tests/frontend-compat.test.mjs tests/fileupload-clear.test.mjs ./tests/
RUN node --test tests/frontend-compat.test.mjs
RUN --mount=type=cache,target=/usr/local/share/.cache/yarn \
cd _dev \
&& corepack yarn install --frozen-lockfile --non-interactive

COPY _dev ./_dev
RUN node --test tests/fileupload-clear.test.mjs
COPY tests/block-selection.test.mjs tests/datepicker-compat.test.mjs \
tests/preview-url.test.mjs tests/vite-compat.test.mjs ./tests/
RUN node --test tests/block-selection.test.mjs tests/datepicker-compat.test.mjs \
Expand Down
23 changes: 23 additions & 0 deletions tests/field-fileupload-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,29 @@ function assertLocalImage($url, $expectedSize, $message)
try {
$imageSize = strlen($png);

$optional = new PrestaSafe\PrettyBlocks\Core\FieldCore([
'type' => 'fileupload',
'force_default_value' => true,
'default' => ['url' => '/placeholder.png'],
]);
assertSameValue('/placeholder.png', $optional->formatFieldFileupload()['url'], 'new field still uses its default');
assertSameValue('/placeholder.png', $optional->secureFileUploadEntry([])['url'], 'absent URL still uses its default');
assertSameValue('/placeholder.png', $optional->secureFileUploadEntry(['url' => null])['url'], 'null URL retains the legacy default');
$optional->new_value = ['url' => ''];
$cleared = $optional->formatFieldFileupload();
assertSameValue('', $cleared['url'], 'explicit empty new value must not restore the default');
foreach (['filename', 'extension', 'mediatype'] as $key) {
assertSameValue('', $cleared[$key], 'cleared field has no ' . $key);
}
$optional->new_value = null;
$optional->value = json_decode(json_encode($cleared), true);
assertSameValue('', $optional->formatFieldFileupload()['url'], 'saved empty value stays empty after reload');
assertSameValue('', $optional->formatFieldFileuploadForFront()['url'], 'front end must not restore the default');
$optional->new_value = ['url' => 'uploads/pixel.png'];
assertSameValue('uploads/pixel.png', $optional->formatFieldFileupload()['url'], 'upload after clearing still works');
$optional->force_default_value = false;
assertSameValue('', $optional->secureFileUploadEntry([])['url'], 'missing URL without forced default stays empty');

assertZeroMetadata('', 'empty URL');
assertZeroMetadata('', 'missing value', false);
assertZeroMetadata(42, 'non-string URL');
Expand Down
62 changes: 62 additions & 0 deletions tests/fileupload-clear.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import test from 'node:test';
import { runInNewContext } from 'node:vm';

const source = readFileSync(new URL('../_dev/src/components/form/FileUpload.vue', import.meta.url), 'utf8');
const update = source.match(/const updateValue = \(event\) => \{[\s\S]*?\n\};/)?.[0];
const remove = source.match(/const removeImg = \(\) => \{[\s\S]*?\n\};/)?.[0];
assert.ok(update && remove, 'The real file upload actions must be testable.');

function fixture(defaultValue, response = Promise.resolve({ success: true })) {
const props = {
default: defaultValue,
modelValue: { url: '/uploads/old.png', filename: 'old.png', mediatype: 'image' },
path: '$/modules/prettyblocks/views/images',
uploadUrl: '/upload',
};
const events = [];
const requests = [];
const removeImg = runInNewContext(update + '\n' + remove + '\nremoveImg', {
props,
security_app: { ajax_token: 'test-token' },
emit: (...args) => events.push(args),
HttpClient: { post(url, params) { requests.push({ url, params }); return response; } },
console: { error() {} },
});
return { removeImg, props, events, requests };
}

const settle = () => new Promise(resolve => setImmediate(resolve));

for (const defaultValue of [{ url: '/placeholder.png' }, { url: '' }, undefined]) {
test(`removing an uploaded file emits an explicit empty URL, not default ${JSON.stringify(defaultValue)}`, async () => {
const f = fixture(defaultValue);
f.removeImg();
assert.equal(f.events.length, 0, 'wait for the removal request');
await settle();
assert.deepEqual(JSON.parse(JSON.stringify(f.events)), [
['update:modelValue', { url: '' }], ['saveParent'],
]);
assert.equal(f.requests[0].url, '/upload');
assert.equal(f.requests[0].params.action, 'removeImage');
assert.equal(f.requests[0].params.state, f.props.modelValue);
assert.equal(f.requests[0].params.ajax_token, 'test-token');
assert.equal(f.props.modelValue.url, '/uploads/old.png', 'do not mutate the input prop');
});
}

test('a missing file on disk can still be cleared from the field', async () => {
const f = fixture({ url: '/placeholder.png' }, Promise.resolve({ success: false, message: 'Image not found' }));
f.removeImg();
await settle();
assert.equal(f.events[0][1].url, '');
assert.equal(f.events[1][0], 'saveParent');
});

test('a failed removal request does not emit a replacement value or save', async () => {
const f = fixture({ url: '/placeholder.png' }, Promise.reject(new Error('Network unavailable')));
f.removeImg();
await settle();
assert.deepEqual(f.events, []);
});