Skip to content

Only lookup local post when quering with "wp_jp_foreign_id" #24586

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

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Sources/WordPressData/Objective-C/PostHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ + (NSArray *)mergePosts:(NSArray <RemotePost *> *)remotePosts
if (post == nil) {
NSUUID *foreignID = remotePost.foreignID;
if (foreignID != nil) {
post = [blog lookupPostWithForeignID:foreignID inContext:context];
post = [blog lookupLocalPostWithForeignID:foreignID inContext:context];
}
}
if (!post) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/WordPressData/Swift/Blog+Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ extension Blog {
///
/// - Parameter foreignID: The foreign ID associated with the post; used to deduplicate new posts.
/// - Returns: The `AbstractPost` associated with the given foreign ID.
@objc(lookupPostWithForeignID:inContext:)
public func lookupPost(withForeignID foreignID: UUID, in context: NSManagedObjectContext) -> AbstractPost? {
@objc(lookupLocalPostWithForeignID:inContext:)
public func lookupLocalPost(withForeignID foreignID: UUID, in context: NSManagedObjectContext) -> AbstractPost? {
let request = NSFetchRequest<AbstractPost>(entityName: NSStringFromClass(AbstractPost.self))
request.predicate = NSPredicate(format: "blog = %@ AND original = NULL AND \(#keyPath(AbstractPost.foreignID)) == %@", self, foreignID as NSUUID)
request.predicate = NSPredicate(format: "blog = %@ AND original = NULL AND (postID = NULL OR postID <= 0) AND \(#keyPath(AbstractPost.foreignID)) == %@", self, foreignID as NSUUID)
request.fetchLimit = 1
return (try? context.fetch(request))?.first
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/KeystoneTests/Tests/Services/PostRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ class PostRepositoryTests: CoreDataTestCase {
}
}

func testFetchPostsWithTheSameForeignID() async throws {
let posts = try (1...10).map {
let post = try XCTUnwrap(RemotePost(siteID: 1, status: "publish", title: "Post: Test", content: "This is a test post"))
post.postID = NSNumber(value: $0)
post.type = "post"

// Assign the same foreign id to a few posts
if $0 <= 3 {
post.metadata = [[
"id": 1234,
"key": PostHelper.foreignIDKey,
"value": "892D484C-9972-47DE-8103-03A7FDE4EFCC"
]]
}

return post
}

remoteMock.remotePostsToReturnOnSyncPostsOfType = [posts]

let _ = try await repository.paginate(type: Post.self, statuses: [.publish], offset: 0, number: 100, in: blogID)
let numberOfPosts = try await contextManager.performQuery { $0.countObjects(ofType: Post.self) }
XCTAssertEqual(numberOfPosts, 10)
}
}

// These mock classes are copied from PostServiceWPComTests. We can't simply remove the `private` in the original class
Expand Down