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]; 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)) + } + } }