From 707cd0841658a34e5aa8f1a0a340bc76049499b0 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Wed, 11 Dec 2024 14:30:10 +1300 Subject: [PATCH 1/2] Add an unit test to reproduce an issue with querying blog by ID --- WordPress/WordPressTest/BlogBuilder.swift | 11 +++++++---- WordPress/WordPressTest/BlogTests.swift | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/WordPress/WordPressTest/BlogBuilder.swift b/WordPress/WordPressTest/BlogBuilder.swift index c80fb4893454..824c920d02ea 100644 --- a/WordPress/WordPressTest/BlogBuilder.swift +++ b/WordPress/WordPressTest/BlogBuilder.swift @@ -11,15 +11,18 @@ final class BlogBuilder { private let context: NSManagedObjectContext - init(_ context: NSManagedObjectContext) { + init(_ context: NSManagedObjectContext, dotComID: NSNumber? = NSNumber(value: arc4random_uniform(999_999))) { self.context = context blog = NSEntityDescription.insertNewObject(forEntityName: Blog.entityName(), into: context) as! Blog + if let dotComID { + blog.dotComID = dotComID + } + // Non-null properties in Core Data - blog.dotComID = NSNumber(value: arc4random_uniform(999_999)) - blog.url = "https://\(blog.dotComID!).example.com" - blog.xmlrpc = "https://\(blog.dotComID!).example.com/xmlrpc.php" + blog.url = "https://\(blog.dotComID?.stringValue ?? "wwww").example.com" + blog.xmlrpc = "\(blog.url!)/xmlrpc.php" } func with(atomic: Bool) -> Self { diff --git a/WordPress/WordPressTest/BlogTests.swift b/WordPress/WordPressTest/BlogTests.swift index f723dad45f59..413c0002527e 100644 --- a/WordPress/WordPressTest/BlogTests.swift +++ b/WordPress/WordPressTest/BlogTests.swift @@ -289,4 +289,20 @@ final class BlogTests: CoreDataTestCase { XCTAssertEqual(try blog.wordPressClientParsedUrl().url(), "http://example.com/") } + + func testDotComIdShouldBeJetpackSiteID() throws { + let blog = BlogBuilder(mainContext, dotComID: nil) + .set(blogOption: "jetpack_client_id", value: "123") + .build() + XCTAssertEqual(blog.jetpack?.siteID?.int64Value, 123) + + try XCTAssertNil(Blog.lookup(withID: 123, in: mainContext)) + try mainContext.save() + + try XCTAssertNotNil(Blog.lookup(withID: 123, in: mainContext)) + + contextManager.performAndSave { context in + try? XCTAssertNotNil(Blog.lookup(withID: 123, in: context)) + } + } } From 934da1cd2cd2f26f865294a3d9dd598f0ae65e5c Mon Sep 17 00:00:00 2001 From: Tony Li Date: Wed, 11 Dec 2024 14:43:53 +1300 Subject: [PATCH 2/2] Update Blog.dotComID value if needed before saving --- WordPress/Classes/Models/Blog/Blog.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/WordPress/Classes/Models/Blog/Blog.m b/WordPress/Classes/Models/Blog/Blog.m index 68ffd1c95745..af2ca3151e57 100644 --- a/WordPress/Classes/Models/Blog/Blog.m +++ b/WordPress/Classes/Models/Blog/Blog.m @@ -97,6 +97,17 @@ @implementation Blog #pragma mark - NSManagedObject subclass methods +- (void)willSave { + [super willSave]; + + // The `dotComID` getter has a speicial code to _update_ `blogID` value. + // This is a weird patch to make sure `blogID` is set to a correct value. + // + // It's important that calling `[self dotComID]` repeatedly only updates + // `Blog` instance once, which is the case at the moment. + [self dotComID]; +} + - (void)prepareForDeletion { [super prepareForDeletion];