From 0bfcde4463efa6be757cff6c50c965e311b54d86 Mon Sep 17 00:00:00 2001 From: Prestasafe Date: Thu, 12 Nov 2026 09:11:22 +0100 Subject: [PATCH 1/2] Fix clearing file uploads without restoring defaults --- .dockerignore | 1 + _dev/src/components/form/FileUpload.vue | 2 +- classes/prettyblocks/core/FieldCore.php | 2 - docker/release.Dockerfile | 4 +- tests/field-fileupload-test.php | 23 +++++++++ tests/fileupload-clear.test.mjs | 62 +++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 tests/fileupload-clear.test.mjs diff --git a/.dockerignore b/.dockerignore index e3c038d8..2204464d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -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 diff --git a/_dev/src/components/form/FileUpload.vue b/_dev/src/components/form/FileUpload.vue index 7a101511..ef99754c 100644 --- a/_dev/src/components/form/FileUpload.vue +++ b/_dev/src/components/form/FileUpload.vue @@ -80,7 +80,7 @@ const removeImg = () => { HttpClient.post(props.uploadUrl, params) .then((data) => { - updateValue(props.default); + updateValue({ url: "" }); }) .catch((error) => console.error(error)); }; diff --git a/classes/prettyblocks/core/FieldCore.php b/classes/prettyblocks/core/FieldCore.php index f5bf5c44..e02d7ab9 100644 --- a/classes/prettyblocks/core/FieldCore.php +++ b/classes/prettyblocks/core/FieldCore.php @@ -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; diff --git a/docker/release.Dockerfile b/docker/release.Dockerfile index 0bf45861..3f71c4fa 100644 --- a/docker/release.Dockerfile +++ b/docker/release.Dockerfile @@ -12,9 +12,9 @@ RUN --mount=type=cache,target=/usr/local/share/.cache/yarn \ COPY _dev ./_dev COPY tests/block-selection.test.mjs tests/datepicker-compat.test.mjs \ - tests/preview-url.test.mjs tests/vite-compat.test.mjs ./tests/ + tests/fileupload-clear.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 \ - tests/preview-url.test.mjs tests/vite-compat.test.mjs \ + tests/fileupload-clear.test.mjs tests/preview-url.test.mjs tests/vite-compat.test.mjs \ && cd _dev && corepack yarn build diff --git a/tests/field-fileupload-test.php b/tests/field-fileupload-test.php index 30048598..96ae7d34 100644 --- a/tests/field-fileupload-test.php +++ b/tests/field-fileupload-test.php @@ -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'); diff --git a/tests/fileupload-clear.test.mjs b/tests/fileupload-clear.test.mjs new file mode 100644 index 00000000..568ddcfa --- /dev/null +++ b/tests/fileupload-clear.test.mjs @@ -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, []); +}); From 0934a79bbcd9f3e75b9d6245770ddf415b13897f Mon Sep 17 00:00:00 2001 From: Prestasafe Date: Thu, 12 Nov 2026 09:13:17 +0100 Subject: [PATCH 2/2] Keep upload regression builder step independent --- docker/release.Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docker/release.Dockerfile b/docker/release.Dockerfile index 3f71c4fa..7d97e050 100644 --- a/docker/release.Dockerfile +++ b/docker/release.Dockerfile @@ -4,17 +4,18 @@ 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/fileupload-clear.test.mjs tests/preview-url.test.mjs tests/vite-compat.test.mjs ./tests/ + tests/preview-url.test.mjs tests/vite-compat.test.mjs ./tests/ RUN node --test 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 \ + tests/preview-url.test.mjs tests/vite-compat.test.mjs \ && cd _dev && corepack yarn build