Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post / Page creation: Don't send default Post.authorID to the API #19717

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions WordPress/Classes/Services/PostService.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion WordPress/Classes/Services/PostService.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
43 changes: 43 additions & 0 deletions WordPress/WordPressTest/Services/PostServiceWPComTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down