From dd3311938ac827f1fa8560c8840a9a394f5896c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= <8087692+remidej@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:28:29 +0200 Subject: [PATCH] fix: prevent duplicate history versions on publish (#20415) * fix: prevent duplicate history versions on publish * fix: build failing * chore: fix e2e tests --- .../server/src/history/services/lifecycles.ts | 13 +++++++++++ .../e2e/tests/content-manager/history.spec.ts | 22 ++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/core/content-manager/server/src/history/services/lifecycles.ts b/packages/core/content-manager/server/src/history/services/lifecycles.ts index e10d0c4368..c67ae54d22 100644 --- a/packages/core/content-manager/server/src/history/services/lifecycles.ts +++ b/packages/core/content-manager/server/src/history/services/lifecycles.ts @@ -50,6 +50,19 @@ const createLifecyclesService = ({ strapi }: { strapi: Core.Strapi }) => { return next(); } + /** + * When a document is published, the draft version of the document is also updated. + * It creates confusion for users because they see two history versions each publish action. + * To avoid this, we silence the update action during a publish request, + * so that they only see the published version of the document in the history. + */ + if ( + context.action === 'update' && + strapi.requestContext.get()?.request.url.endsWith('/actions/publish') + ) { + return next(); + } + const contentTypeUid = context.contentType.uid; // Ignore content types not created by the user if (!contentTypeUid.startsWith('api::')) { diff --git a/tests/e2e/tests/content-manager/history.spec.ts b/tests/e2e/tests/content-manager/history.spec.ts index d673ce00a5..db8dcc26e7 100644 --- a/tests/e2e/tests/content-manager/history.spec.ts +++ b/tests/e2e/tests/content-manager/history.spec.ts @@ -148,15 +148,12 @@ describeOnCondition(edition === 'EE')('History', () => { // Go to the history page await goToHistoryPage(page); await page.waitForURL(ARTICLE_HISTORY_URL); - // Publish also creates a new draft so we expect the count to increase by 2 - await expect(versionCards).toHaveCount(4); + await expect(versionCards).toHaveCount(3); // Assert the current version is the most recent published version await expect(titleInput).toHaveValue('Being from Kansas City'); // The current version is the most recent draft - await expect(currentVersion.getByText('Draft')).toBeVisible(); + await expect(currentVersion.getByText('Published')).toBeVisible(); await expect(titleInput).toHaveValue('Being from Kansas City'); - // The second in the list is the published version - await expect(previousVersion.getByText('Published')).toBeVisible(); previousVersion.click(); await expect(titleInput).toHaveValue('Being from Kansas City'); @@ -172,7 +169,7 @@ describeOnCondition(edition === 'EE')('History', () => { // Go to the history page await goToHistoryPage(page); await page.waitForURL(ARTICLE_HISTORY_URL); - await expect(versionCards).toHaveCount(5); + await expect(versionCards).toHaveCount(4); // Assert the current version is the modified version await expect(currentVersion.getByText('Modified')).toBeVisible(); await expect(titleInput).toHaveValue('Being from Kansas City, Missouri'); @@ -364,14 +361,13 @@ describeOnCondition(edition === 'EE')('History', () => { // Go to the history page await goToHistoryPage(page); await page.waitForURL('**/content-manager/single-types/api::homepage.homepage/history**'); - // Publish also creates a new draft so we expect the count to increase by 2 - await expect(versionCards).toHaveCount(4); - // The current version is the most recent draft - await expect(currentVersion.getByText('Draft')).toBeVisible(); + await expect(versionCards).toHaveCount(3); + // The current version is the most recent published + await expect(currentVersion.getByText('Published')).toBeVisible(); await expect(titleInput).toHaveValue('Welcome to AFC Richmond'); - // The second in the list is the published version + // The second in the list is the draft version await previousVersion.click(); - await expect(previousVersion.getByText('Published')).toBeVisible(); + await expect(previousVersion.getByText('Draft')).toBeVisible(); await expect(titleInput).toHaveValue('Welcome to AFC Richmond'); // Go back to the entry @@ -385,7 +381,7 @@ describeOnCondition(edition === 'EE')('History', () => { // Go to the history page await goToHistoryPage(page); await page.waitForURL(HISTORY_URL); - await expect(versionCards).toHaveCount(5); + await expect(versionCards).toHaveCount(4); // Assert the current version is the most recent published version await expect(titleInput).toHaveValue('Welcome to AFC Richmond!'); await expect(currentVersion.getByText('Modified')).toBeVisible();