From 9b42c33c7084154f2124fc3310d6be6058f294e2 Mon Sep 17 00:00:00 2001 From: Miro Yovchev <2827783+myovchev@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:41:28 +0300 Subject: [PATCH 1/5] Fix parked pages slug --- CHANGELOG.md | 1 + modules/@apostrophecms/page/index.js | 14 ++++++ test/parked-pages.js | 75 ++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f66524eaf..4ca0f1e119 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * Uploaded SVGs now permit `` tags granted their `xlink:href` property is a local reference and begins with the `#` character. This improves SVG support while mitgating XSS vulnerabilities. * Default properties of object fields present in a widget now populate correctly even if never focused in the editor. +* Ensure `slug` is not modified for parked pages when not configured in `_defaults`. ## 4.7.0 (2024-09-05) diff --git a/modules/@apostrophecms/page/index.js b/modules/@apostrophecms/page/index.js index c26eb59f5a..6b87973100 100644 --- a/modules/@apostrophecms/page/index.js +++ b/modules/@apostrophecms/page/index.js @@ -846,6 +846,20 @@ module.exports = { }, handlers(self) { return { + '@apostrophecms/page-type:beforeSave': { + handleParkedSlugOverride(req, doc) { + if (!doc.parkedId) { + return; + } + const parked = self.parked.find(p => p.parkedId === doc.parkedId); + if (!parked) { + return; + } + if (parked.slug && !parked._defaults?.slug) { + doc.slug = parked.slug; + } + } + }, beforeSend: { async addLevelAttributeToBody(req) { // Add level as a data attribute on the body tag diff --git a/test/parked-pages.js b/test/parked-pages.js index bfddfcccca..2a01979015 100644 --- a/test/parked-pages.js +++ b/test/parked-pages.js @@ -271,6 +271,81 @@ describe('Parked Pages', function() { }); await validate(apos6, [ '/', '/archive', '/default1', '/default2', '/default3', '/default3/child1' ]); }); + + it('slug override on save is possible only when slug is configured as default', async function () { + this.timeout(20000); + await t.destroy(apos6); + apos6 = await t.create({ + root: module, + modules: { + '@apostrophecms/page': { + options: { + park: [ + ...park2, + { + parkedId: 'default3', + type: 'default-page', + title: 'Default 3', + slug: '/default3' + }, + { + parkedId: 'default4', + type: 'default-page', + title: 'Default 4', + _defaults: { + slug: '/default4' + } + } + ] + } + }, + 'default-page': {} + } + }); + const manager = apos6.doc.getManager('default-page'); + const req = apos.task.getReq({ mode: 'draft' }); + + // slug override not possible, slug is NOT configured in defaults + { + const page = await manager.find(req, { + parkedId: 'default3', + aposMode: 'draft' + }).toObject(); + assert.strictEqual(page.slug, '/default3'); + assert.deepStrictEqual(page.parked, [ 'parkedId', 'type', 'title', 'slug' ]); + + page.slug = '/default3-overridden'; + await manager.update(req, page); + const updated = await manager.find(req, { + parkedId: 'default3', + aposMode: 'draft' + }).toObject(); + + assert.strictEqual(updated.slug, '/default3'); + } + + // slug override is possible because slug is configured in defaults + { + const page = await manager.find(req, { + parkedId: 'default4', + aposMode: 'draft' + }).toObject(); + assert.strictEqual(page.slug, '/default4'); + assert.deepStrictEqual(page.parked, [ 'parkedId', 'type', 'title' ]); + + page.slug = '/default4-overridden'; + await manager.update(req, page); + const updated = await manager.find(req, { + parkedId: 'default4', + aposMode: 'draft' + }).toObject(); + + assert.strictEqual(updated.slug, '/default4-overridden'); + } + + await t.destroy(apos6); + apos6 = null; + }); }); async function validate(apos, expected) { From ba222fe54612598a0bab15bfaea216f8f2e6baf1 Mon Sep 17 00:00:00 2001 From: Miro Yovchev <2827783+myovchev@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:54:42 +0300 Subject: [PATCH 2/5] Fix home page slug if needed --- modules/@apostrophecms/page/index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/@apostrophecms/page/index.js b/modules/@apostrophecms/page/index.js index 6b87973100..926c5af50c 100644 --- a/modules/@apostrophecms/page/index.js +++ b/modules/@apostrophecms/page/index.js @@ -242,6 +242,7 @@ module.exports = { self.addDuplicateParkedPagesMigration(); self.apos.migration.add('deduplicateRanks2', self.deduplicateRanks2Migration); self.apos.migration.add('missingLastPublishedAt', self.missingLastPublishedAtMigration); + self.apos.migration.add('ensureHomeSlug', self.ensureHomeSlugMigration); await self.createIndexes(); self.composeFilters(); }, @@ -2982,6 +2983,22 @@ database.`); }); }); }, + ensureHomeSlugMigration() { + return self.apos.migration.eachDoc({ + parkedId: 'home' + }, async doc => { + if (doc.slug === '/') { + return; + } + await self.apos.doc.db.updateOne({ + _id: doc._id + }, { + $set: { + slug: '/' + } + }); + }); + }, async inferLastTargetIdAndPosition(doc) { const parentPath = self.getParentPath(doc); const parentAposDocId = parentPath.split('/').pop(); From 58bc74de9abe5b81a010e692f20e467e44f1c39d Mon Sep 17 00:00:00 2001 From: Miro Yovchev <2827783+myovchev@users.noreply.github.com> Date: Fri, 20 Sep 2024 17:06:10 +0300 Subject: [PATCH 3/5] Remove unnecessary migration --- modules/@apostrophecms/page/index.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/modules/@apostrophecms/page/index.js b/modules/@apostrophecms/page/index.js index 926c5af50c..6b87973100 100644 --- a/modules/@apostrophecms/page/index.js +++ b/modules/@apostrophecms/page/index.js @@ -242,7 +242,6 @@ module.exports = { self.addDuplicateParkedPagesMigration(); self.apos.migration.add('deduplicateRanks2', self.deduplicateRanks2Migration); self.apos.migration.add('missingLastPublishedAt', self.missingLastPublishedAtMigration); - self.apos.migration.add('ensureHomeSlug', self.ensureHomeSlugMigration); await self.createIndexes(); self.composeFilters(); }, @@ -2983,22 +2982,6 @@ database.`); }); }); }, - ensureHomeSlugMigration() { - return self.apos.migration.eachDoc({ - parkedId: 'home' - }, async doc => { - if (doc.slug === '/') { - return; - } - await self.apos.doc.db.updateOne({ - _id: doc._id - }, { - $set: { - slug: '/' - } - }); - }); - }, async inferLastTargetIdAndPosition(doc) { const parentPath = self.getParentPath(doc); const parentAposDocId = parentPath.split('/').pop(); From b0cfed20e00a3dad6af2f80b82238bf9145dfb80 Mon Sep 17 00:00:00 2001 From: Miro Yovchev <2827783+myovchev@users.noreply.github.com> Date: Fri, 20 Sep 2024 17:21:19 +0300 Subject: [PATCH 4/5] Improve the implementation --- CHANGELOG.md | 2 +- modules/@apostrophecms/page/index.js | 7 ++++--- test/parked-pages.js | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ca0f1e119..1167bce455 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ * Uploaded SVGs now permit `` tags granted their `xlink:href` property is a local reference and begins with the `#` character. This improves SVG support while mitgating XSS vulnerabilities. * Default properties of object fields present in a widget now populate correctly even if never focused in the editor. -* Ensure `slug` is not modified for parked pages when not configured in `_defaults`. +* Ensure parked fields are not modified for parked pages when not configured in `_defaults`. ## 4.7.0 (2024-09-05) diff --git a/modules/@apostrophecms/page/index.js b/modules/@apostrophecms/page/index.js index 6b87973100..fe3afa5c9f 100644 --- a/modules/@apostrophecms/page/index.js +++ b/modules/@apostrophecms/page/index.js @@ -847,7 +847,7 @@ module.exports = { handlers(self) { return { '@apostrophecms/page-type:beforeSave': { - handleParkedSlugOverride(req, doc) { + handleParkedFieldsOverride(req, doc) { if (!doc.parkedId) { return; } @@ -855,8 +855,9 @@ module.exports = { if (!parked) { return; } - if (parked.slug && !parked._defaults?.slug) { - doc.slug = parked.slug; + const parkedFields = Object.keys(parked).filter(field => field !== '_defaults'); + for (const parkedField of parkedFields) { + doc[parkedField] = parked[parkedField]; } } }, diff --git a/test/parked-pages.js b/test/parked-pages.js index 2a01979015..2f96d1e344 100644 --- a/test/parked-pages.js +++ b/test/parked-pages.js @@ -272,7 +272,7 @@ describe('Parked Pages', function() { await validate(apos6, [ '/', '/archive', '/default1', '/default2', '/default3', '/default3/child1' ]); }); - it('slug override on save is possible only when slug is configured as default', async function () { + it('field override on save is possible only when it is configured as default', async function () { this.timeout(20000); await t.destroy(apos6); apos6 = await t.create({ From fbe2989d4f38ce757ee0f31106c881db4deb467b Mon Sep 17 00:00:00 2001 From: Miro Yovchev <2827783+myovchev@users.noreply.github.com> Date: Fri, 20 Sep 2024 17:46:36 +0300 Subject: [PATCH 5/5] Fix tests relying on incorrect behavior --- test/content-i18n.js | 2 +- test/events2.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/content-i18n.js b/test/content-i18n.js index f15f9c719a..b1f46c4743 100644 --- a/test/content-i18n.js +++ b/test/content-i18n.js @@ -96,7 +96,7 @@ describe('content-i18n', function() { peoplePageFrCA = await apos.page.find(frCAReq, { parkedId: 'people' }).toObject(); - assert(peoplePageFrCA.title === 'Altered'); + assert(peoplePageFrCA.title === 'People'); }); let home; diff --git a/test/events2.js b/test/events2.js index 0121e7a216..15c9f37a3b 100644 --- a/test/events2.js +++ b/test/events2.js @@ -45,10 +45,12 @@ describe('Promisified Events: @apostrophecms/doc-type:beforeInsert', function() { type: 'default-page', findMeAgain: true, - title: 'Test', slug: '/test', visibility: 'public', - parkedId: 'test' + parkedId: 'test', + _defaults: { + title: 'Test' + } } ] }