Skip to content

Commit 6b97022

Browse files
authored
fix: Pass null post ID for local drafts in GutenbergKit (#22580)
Local draft posts have remotePostId defaulting to 0, which was passed directly to GutenbergKit's setPostId(). GBK expects null for unsaved posts. Now check isLocalDraft and pass null instead.
1 parent e221a2a commit 6b97022

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

WordPress/src/main/java/org/wordpress/android/ui/posts/GutenbergKitSettingsBuilder.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ object GutenbergKitSettingsBuilder {
6363
).apply {
6464
setTitle(post?.title ?: "")
6565
setContent(post?.content ?: "")
66-
setPostId(post?.remotePostId?.toInt())
66+
setPostId(
67+
if (post?.isLocalDraft == true) null
68+
else post?.remotePostId?.toInt()
69+
)
6770
setPostStatus(post?.status ?: "draft")
6871
setAuthHeader(authHeader)
6972
setSiteApiNamespace(siteApiNamespace)

WordPress/src/test/java/org/wordpress/android/ui/posts/GutenbergKitSettingsBuilderTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.assertj.core.api.Assertions.assertThat
44
import org.junit.Test
55
import org.junit.runner.RunWith
66
import org.mockito.junit.MockitoJUnitRunner
7+
import org.wordpress.android.fluxc.model.PostModel
78
import org.wordpress.android.fluxc.model.SiteModel
89

910
@RunWith(MockitoJUnitRunner::class)
@@ -431,6 +432,29 @@ class GutenbergKitSettingsBuilderTest {
431432
assertThat(config.postId).isNull()
432433
}
433434

435+
@Test
436+
fun `local draft post results in null post ID`() {
437+
val site = SiteModel().apply {
438+
url = "https://example.wordpress.com"
439+
siteId = 123L
440+
setIsWPCom(true)
441+
setIsJetpackConnected(false)
442+
origin = SiteModel.ORIGIN_WPCOM_REST
443+
}
444+
val post = PostModel().apply {
445+
setIsLocalDraft(true)
446+
setRemotePostId(99L)
447+
}
448+
val config = GutenbergKitSettingsBuilder
449+
.buildPostConfiguration(
450+
site = site,
451+
post = post,
452+
accessToken = "test_token"
453+
)
454+
455+
assertThat(config.postId).isNull()
456+
}
457+
434458
@Test
435459
fun `null post status defaults to draft`() {
436460
val config = buildWPComConfig()

0 commit comments

Comments
 (0)