Added fallback to current excerpt when revision excerpt is null

no issue

- following on from the removal of the `post_revisions.custom_excerpt` column population it was possible in some circumstances to lose your excerpt when restoring an old version
- this change means when no custom_excerpt exists on a revision we both preview and restore the current custom_excerpt to avoid any data loss
This commit is contained in:
Kevin Ansfield 2024-06-06 17:16:16 +01:00
parent c8fc93e989
commit 202fd7ebbd
2 changed files with 52 additions and 1 deletions

View File

@ -51,7 +51,10 @@ export default class ModalPostHistory extends Component {
latest: index === 0,
createdAt: revision.get('createdAt'),
title: revision.get('title'),
custom_excerpt: revision.get('customExcerpt'),
// custom_excerpt is a new field that was added to the post-revision model
// that may not have been populated for older revisions. To cover that case
// we revert to the current post's customExcerpt to avoid losing data when restoring.
custom_excerpt: revision.get('customExcerpt') ?? this.post.customExcerpt,
feature_image: revision.get('featureImage'),
feature_image_alt: revision.get('featureImageAlt'),
feature_image_caption: revision.get('featureImageCaption'),

View File

@ -142,4 +142,52 @@ describe('Acceptance: Post revisions', function () {
// post has been saved with correct data
expect(post.attrs.customExcerpt).to.equal('Old excerpt');
});
it('reverts to current post excerpt if revision excerpt is missing (with editorExcerpt feature flag)', async function () {
enableLabsFlag(this.server, 'editorExcerpt');
const post = this.server.create('post', {
title: 'Current Title',
customExcerpt: 'Current excerpt',
status: 'draft'
});
this.server.create('post-revision', {
post,
title: post.title,
postStatus: 'draft',
author: post.authors.models[0],
createdAt: moment(post.updatedAt).subtract(1, 'hour'),
reason: 'explicit_save'
});
this.server.create('post-revision', {
post,
title: 'Old Title',
customExcerpt: null,
postStatus: 'draft',
author: post.authors.models[0],
createdAt: moment(post.updatedAt).subtract(1, 'day'),
reason: 'initial_revision'
});
await visit(`/editor/post/${post.id}`);
// open post history menu
await click('[data-test-psm-trigger]');
await click('[data-test-toggle="post-history"]');
// latest excerpt is set to current
expect(find('[data-test-post-history-preview-excerpt]')).to.exist;
expect(find('[data-test-post-history-preview-excerpt]')).to.have.trimmed.text('Current excerpt');
// previous post can be previewed and excerpt is set to current
await click('[data-test-revision-item="1"] [data-test-button="preview-revision"]');
expect(find('[data-test-post-history-preview-excerpt]')).to.have.trimmed.text('Current excerpt');
// restore saves current excerpt
await click('[data-test-revision-item="1"] [data-test-button="restore-revision"]');
await click('[data-test-modal="restore-revision"] [data-test-button="restore"]');
// post has been saved with correct data
expect(post.attrs.customExcerpt).to.equal('Current excerpt');
});
});