Skip to content

Commit 938e983

Browse files
authored
Only lookup local post when quering with "wp_jp_foreign_id" (#24586)
* Add an unit test to reproduce the linear issue JETPACK-537 The user's site have multiple posts with the same "wp_jp_foreign_id". https://linear.app/a8c/issue/JETPACK-537 * Only lookup local post when quering with "wp_jp_foreign_id"
1 parent 1af4ec8 commit 938e983

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Sources/WordPressData/Objective-C/PostHelper.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ + (NSArray *)mergePosts:(NSArray <RemotePost *> *)remotePosts
182182
if (post == nil) {
183183
NSUUID *foreignID = remotePost.foreignID;
184184
if (foreignID != nil) {
185-
post = [blog lookupPostWithForeignID:foreignID inContext:context];
185+
post = [blog lookupLocalPostWithForeignID:foreignID inContext:context];
186186
}
187187
}
188188
if (!post) {

Sources/WordPressData/Swift/Blog+Post.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ extension Blog {
3535
///
3636
/// - Parameter foreignID: The foreign ID associated with the post; used to deduplicate new posts.
3737
/// - Returns: The `AbstractPost` associated with the given foreign ID.
38-
@objc(lookupPostWithForeignID:inContext:)
39-
public func lookupPost(withForeignID foreignID: UUID, in context: NSManagedObjectContext) -> AbstractPost? {
38+
@objc(lookupLocalPostWithForeignID:inContext:)
39+
public func lookupLocalPost(withForeignID foreignID: UUID, in context: NSManagedObjectContext) -> AbstractPost? {
4040
let request = NSFetchRequest<AbstractPost>(entityName: NSStringFromClass(AbstractPost.self))
41-
request.predicate = NSPredicate(format: "blog = %@ AND original = NULL AND \(#keyPath(AbstractPost.foreignID)) == %@", self, foreignID as NSUUID)
41+
request.predicate = NSPredicate(format: "blog = %@ AND original = NULL AND (postID = NULL OR postID <= 0) AND \(#keyPath(AbstractPost.foreignID)) == %@", self, foreignID as NSUUID)
4242
request.fetchLimit = 1
4343
return (try? context.fetch(request))?.first
4444
}

Tests/KeystoneTests/Tests/Services/PostRepositoryTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,30 @@ class PostRepositoryTests: CoreDataTestCase {
217217
}
218218
}
219219

220+
func testFetchPostsWithTheSameForeignID() async throws {
221+
let posts = try (1...10).map {
222+
let post = try XCTUnwrap(RemotePost(siteID: 1, status: "publish", title: "Post: Test", content: "This is a test post"))
223+
post.postID = NSNumber(value: $0)
224+
post.type = "post"
225+
226+
// Assign the same foreign id to a few posts
227+
if $0 <= 3 {
228+
post.metadata = [[
229+
"id": 1234,
230+
"key": PostHelper.foreignIDKey,
231+
"value": "892D484C-9972-47DE-8103-03A7FDE4EFCC"
232+
]]
233+
}
234+
235+
return post
236+
}
237+
238+
remoteMock.remotePostsToReturnOnSyncPostsOfType = [posts]
239+
240+
let _ = try await repository.paginate(type: Post.self, statuses: [.publish], offset: 0, number: 100, in: blogID)
241+
let numberOfPosts = try await contextManager.performQuery { $0.countObjects(ofType: Post.self) }
242+
XCTAssertEqual(numberOfPosts, 10)
243+
}
220244
}
221245

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

0 commit comments

Comments
 (0)