From 202fd7ebbd4afa7f49d06cd8c869eb0d95e2a344 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Thu, 6 Jun 2024 17:16:16 +0100 Subject: [PATCH] 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 --- .../app/components/modal-post-history.js | 5 +- .../acceptance/editor/post-revisions-test.js | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/ghost/admin/app/components/modal-post-history.js b/ghost/admin/app/components/modal-post-history.js index 489b860a7b..05aba6646e 100644 --- a/ghost/admin/app/components/modal-post-history.js +++ b/ghost/admin/app/components/modal-post-history.js @@ -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'), diff --git a/ghost/admin/tests/acceptance/editor/post-revisions-test.js b/ghost/admin/tests/acceptance/editor/post-revisions-test.js index fc2ce08864..b8606480d4 100644 --- a/ghost/admin/tests/acceptance/editor/post-revisions-test.js +++ b/ghost/admin/tests/acceptance/editor/post-revisions-test.js @@ -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'); + }); });