Skip to content

Commit

Permalink
PRO-6591: parked fields overrides (#4732)
Browse files Browse the repository at this point in the history
  • Loading branch information
myovchev authored Sep 20, 2024
1 parent 0e978d8 commit 04cd8f5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* The `@apostrophecms/page` module APIs no longer allow a page to become a child of itself. Thanks to [Maarten Marx](https://github.com/Pixelguymm) for reporting the issue.
* Uploaded SVGs now permit `<use>` 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 parked fields are not modified for parked pages when not configured in `_defaults`.

## 4.7.0 (2024-09-05)

Expand Down
15 changes: 15 additions & 0 deletions modules/@apostrophecms/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,21 @@ module.exports = {
},
handlers(self) {
return {
'@apostrophecms/page-type:beforeSave': {
handleParkedFieldsOverride(req, doc) {
if (!doc.parkedId) {
return;
}
const parked = self.parked.find(p => p.parkedId === doc.parkedId);
if (!parked) {
return;
}
const parkedFields = Object.keys(parked).filter(field => field !== '_defaults');
for (const parkedField of parkedFields) {
doc[parkedField] = parked[parkedField];
}
}
},
beforeSend: {
async addLevelAttributeToBody(req) {
// Add level as a data attribute on the body tag
Expand Down
2 changes: 1 addition & 1 deletion test/content-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions test/events2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
]
}
Expand Down
75 changes: 75 additions & 0 deletions test/parked-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,81 @@ describe('Parked Pages', function() {
});
await validate(apos6, [ '/', '/archive', '/default1', '/default2', '/default3', '/default3/child1' ]);
});

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({
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) {
Expand Down

0 comments on commit 04cd8f5

Please sign in to comment.