From 721cddff5d736da117ad21acd0ae8cfee81b0170 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Mon, 26 Sep 2022 16:28:19 +0800 Subject: [PATCH] fix: post excerptare generated using only published content that is in use (#2457) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.0 #### What this PR does / why we need it: 文章自动摘要只从正在使用的已发布版本的内容中取得 #### Which issue(s) this PR fixes: Fixes #2452 #### Special notes for your reviewer: /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../extension/reconciler/PostReconciler.java | 2 +- .../reconciler/PostReconcilerTest.java | 40 ++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/main/java/run/halo/app/core/extension/reconciler/PostReconciler.java b/src/main/java/run/halo/app/core/extension/reconciler/PostReconciler.java index 521744bb8..2059be1eb 100644 --- a/src/main/java/run/halo/app/core/extension/reconciler/PostReconciler.java +++ b/src/main/java/run/halo/app/core/extension/reconciler/PostReconciler.java @@ -109,7 +109,7 @@ public class PostReconciler implements Reconciler { spec.setExcerpt(excerpt); } if (excerpt.getAutoGenerate()) { - contentService.getContent(spec.getHeadSnapshot()) + contentService.getContent(spec.getReleaseSnapshot()) .subscribe(content -> { String contentRevised = content.content(); status.setExcerpt(getExcerpt(contentRevised)); diff --git a/src/test/java/run/halo/app/core/extension/reconciler/PostReconcilerTest.java b/src/test/java/run/halo/app/core/extension/reconciler/PostReconcilerTest.java index db9eba39b..b43242307 100644 --- a/src/test/java/run/halo/app/core/extension/reconciler/PostReconcilerTest.java +++ b/src/test/java/run/halo/app/core/extension/reconciler/PostReconcilerTest.java @@ -7,6 +7,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.time.Instant; import java.util.List; import java.util.Optional; import java.util.Set; @@ -54,10 +55,8 @@ class PostReconcilerTest { post.getSpec().setHeadSnapshot("post-A-head-snapshot"); when(client.fetch(eq(Post.class), eq(name))) .thenReturn(Optional.of(post)); - when(contentService.getContent(eq(post.getSpec().getHeadSnapshot()))) - .thenReturn(Mono.just( - new ContentWrapper(post.getSpec().getHeadSnapshot(), "hello world", - "

hello world

", "markdown"))); + when(contentService.getContent(eq(post.getSpec().getReleaseSnapshot()))) + .thenReturn(Mono.empty()); Snapshot snapshotV1 = TestPost.snapshotV1(); Snapshot snapshotV2 = TestPost.snapshotV2(); @@ -77,8 +76,39 @@ class PostReconcilerTest { verify(postPermalinkPolicy, times(0)).onPermalinkUpdate(any()); Post value = captor.getValue(); - assertThat(value.getStatus().getExcerpt()).isEqualTo("hello world"); + assertThat(value.getStatus().getExcerpt()).isNull(); assertThat(value.getStatus().getContributors()).isEqualTo(List.of("guqing", "zhangsan")); } + @Test + void reconcileExcerpt() { + // https://github.com/halo-dev/halo/issues/2452 + String name = "post-A"; + Post post = TestPost.postV1(); + post.getSpec().setPublished(true); + post.getSpec().setHeadSnapshot("post-A-head-snapshot"); + post.getSpec().setReleaseSnapshot("post-fake-released-snapshot"); + when(client.fetch(eq(Post.class), eq(name))) + .thenReturn(Optional.of(post)); + when(contentService.getContent(eq(post.getSpec().getReleaseSnapshot()))) + .thenReturn(Mono.just( + new ContentWrapper(post.getSpec().getHeadSnapshot(), "hello world", + "

hello world

", "markdown"))); + + Snapshot snapshotV1 = TestPost.snapshotV1(); + Snapshot snapshotV2 = TestPost.snapshotV2(); + snapshotV2.getSpec().setPublishTime(Instant.now()); + snapshotV1.getSpec().setContributors(Set.of("guqing")); + snapshotV2.getSpec().setContributors(Set.of("guqing", "zhangsan")); + when(contentService.listSnapshots(any())) + .thenReturn(Flux.just(snapshotV1, snapshotV2)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Post.class); + postReconciler.reconcile(new Reconciler.Request(name)); + + verify(client, times(3)).update(captor.capture()); + Post value = captor.getValue(); + assertThat(value.getStatus().getExcerpt()).isEqualTo("hello world"); + } + } \ No newline at end of file