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'); + }); });