From db11fdb678e3e7ec738f584ba8e00e899dd2c2df Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Wed, 7 Dec 2022 23:11:19 -0500 Subject: [PATCH 1/3] If a post's authorID is 0 or nil, don't set it. --- WordPress/Classes/Services/PostService.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/WordPress/Classes/Services/PostService.m b/WordPress/Classes/Services/PostService.m index 852295048e83..4fc84e8fa4ac 100644 --- a/WordPress/Classes/Services/PostService.m +++ b/WordPress/Classes/Services/PostService.m @@ -814,7 +814,11 @@ - (RemotePost *)remotePostWithPost:(AbstractPost *)post remotePost.password = post.password; remotePost.type = @"post"; remotePost.authorAvatarURL = post.authorAvatarURL; - remotePost.authorID = post.authorID; + // If a Post's authorID is 0 (the default Core Data value) + // or nil, don't send it to the API. + if (post.authorID.integerValue != 0) { + remotePost.authorID = post.authorID; + } remotePost.excerpt = post.mt_excerpt; remotePost.slug = post.wp_slug; From e905ae561012158fe42f3131b4821b8ec676751f Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Wed, 7 Dec 2022 23:12:26 -0500 Subject: [PATCH 2/3] Test remotePostWithPost and author IDs. --- WordPress/Classes/Services/PostService.h | 7 +++ .../Services/PostServiceWPComTests.swift | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/WordPress/Classes/Services/PostService.h b/WordPress/Classes/Services/PostService.h index 00add862073f..a774abaf52f4 100644 --- a/WordPress/Classes/Services/PostService.h +++ b/WordPress/Classes/Services/PostService.h @@ -160,6 +160,13 @@ forceDraftIfCreating:(BOOL)forceDraftIfCreating success:(nullable void (^)(void))success failure:(void (^)(NSError * _Nullable error))failure; +/** + Creates a RemotePost from an AbstractPost to be used for API calls. + + @param post The AbstractPost used to create the RemotePost + */ +- (RemotePost *)remotePostWithPost:(AbstractPost *)post; + @end NS_ASSUME_NONNULL_END diff --git a/WordPress/WordPressTest/Services/PostServiceWPComTests.swift b/WordPress/WordPressTest/Services/PostServiceWPComTests.swift index 7110474ae54c..f8d858100fe5 100644 --- a/WordPress/WordPressTest/Services/PostServiceWPComTests.swift +++ b/WordPress/WordPressTest/Services/PostServiceWPComTests.swift @@ -293,6 +293,49 @@ class PostServiceWPComTests: CoreDataTestCase { wait(for: [expectation], timeout: timeout) } + /// The default `Post.authorID` value (currently 0 in the Core Data model) should + /// be `nil` for `RemotePost`s. + func testRemotePostAuthorIDNilForDefaultPostAuthorID() { + // Given + let post = PostBuilder(mainContext).build() + try! mainContext.save() + + // When + let remotePost = self.service.remotePost(with: post) + + // Then + XCTAssertNil(remotePost.authorID) + } + + /// `Post.authorID`s set to `nil` should be `nil` for `RemotePost`s. + func testRemotePostAuthorIDNilForNilPostAuthorID() { + // Given + let post = PostBuilder(mainContext).build() + post.authorID = nil + try! mainContext.save() + + // When + let remotePost = self.service.remotePost(with: post) + + // Then + XCTAssertNil(remotePost.authorID) + } + + /// `Post.authorID`s set to a valid value should be reflected in `RemotePost`s. + func testRemotePostAuthorSetForValidPostAuthorID() { + // Given + let expectedAuthorID: NSNumber = 1 + let post = PostBuilder(mainContext).build() + post.authorID = expectedAuthorID + try! mainContext.save() + + // When + let remotePost = self.service.remotePost(with: post) + + // Then + XCTAssertEqual(remotePost.authorID, expectedAuthorID) + } + private func createRemotePost(_ status: BasePost.Status = .draft) -> RemotePost { let remotePost = RemotePost(siteID: 1, status: status.rawValue, From 8a92382ebe7139aab825dae6ad9b5d9d96d6a5b5 Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Wed, 7 Dec 2022 23:36:26 -0500 Subject: [PATCH 3/3] Add release note. --- RELEASE-NOTES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 4680b1ebf1ac..52e2347031b3 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -1,5 +1,6 @@ 21.4 ----- +* [*] Fixed an issue where publishing Posts and Pages could fail under certain conditions. [#19717] * [*] Share extension navigation bar is no longer transparent [#19700] 21.3