Skip to content

Commit

Permalink
WIP: Use CoreDataStack in SiteManagementService
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Jan 26, 2023
1 parent 4f9dc21 commit d2f1d2a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
28 changes: 18 additions & 10 deletions WordPress/Classes/Services/SiteManagementService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ extension NSNotification.Name {

/// SiteManagementService handles operations for managing a WordPress.com site.
///
open class SiteManagementService: LocalCoreDataService {
open class SiteManagementService: NSObject {

private let coreDataStack: CoreDataStack

init(coreDataStack: CoreDataStack) {
self.coreDataStack = coreDataStack
}

/// Deletes the specified WordPress.com site.
///
/// - Parameters:
Expand All @@ -33,15 +40,16 @@ open class SiteManagementService: LocalCoreDataService {
}
remote.deleteSite(blog.dotComID!,
success: {
self.managedObjectContext.perform {
let blogService = BlogService(managedObjectContext: self.managedObjectContext)
blogService.remove(blog)

ContextManager.sharedInstance().save(self.managedObjectContext, completion: {
NotificationCenter.default.post(name: .WPSiteDeleted, object: nil)
success?()
}, on: .main)
}
self.coreDataStack.performAndSave({ context in
guard let blogInContext = try? context.existingObject(with: blog.objectID) as? Blog else {
return
}
let blogService = BlogService(managedObjectContext: context)
blogService.remove(blogInContext)
}, completion: {
NotificationCenter.default.post(name: .WPSiteDeleted, object: nil)
success?()
}, on: .main)
},
failure: { error in
failure?(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private extension QuickStartTourGuide {
}

private func grantCongratulationsAward(for blog: Blog) {
let service = SiteManagementService(managedObjectContext: ContextManager.sharedInstance().mainContext)
let service = SiteManagementService(coreDataStack: ContextManager.sharedInstance())
service.markQuickStartChecklistAsComplete(for: blog)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ open class DeleteSiteViewController: UITableViewController {

let trackedBlog = blog
WPAppAnalytics.track(.siteSettingsDeleteSiteRequested, with: trackedBlog)
let service = SiteManagementService(managedObjectContext: ContextManager.sharedInstance().mainContext)
let service = SiteManagementService(coreDataStack: ContextManager.sharedInstance())
service.deleteSiteForBlog(blog,
success: { [weak self] in
WPAppAnalytics.track(.siteSettingsDeleteSiteResponseOK, with: trackedBlog)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public extension SiteSettingsViewController {

let trackedBlog = blog
WPAppAnalytics.track(.siteSettingsExportSiteRequested, with: trackedBlog)
let service = SiteManagementService(managedObjectContext: ContextManager.sharedInstance().mainContext)
let service = SiteManagementService(coreDataStack: ContextManager.sharedInstance())
service.exportContentForBlog(blog,
success: {
WPAppAnalytics.track(.siteSettingsExportSiteResponseOK, with: trackedBlog)
Expand Down Expand Up @@ -79,7 +79,7 @@ public extension SiteSettingsViewController {
SVProgressHUD.show(withStatus: status)

WPAppAnalytics.track(.siteSettingsDeleteSitePurchasesRequested, with: blog)
let service = SiteManagementService(managedObjectContext: ContextManager.sharedInstance().mainContext)
let service = SiteManagementService(coreDataStack: ContextManager.sharedInstance())
service.getActivePurchasesForBlog(blog,
success: { [weak self] purchases in
SVProgressHUD.dismiss()
Expand Down
20 changes: 12 additions & 8 deletions WordPress/WordPressTest/SiteManagementServiceTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation
import XCTest
import Nimble

@testable import WordPress

class SiteManagementServiceTests: CoreDataTestCase {
Expand Down Expand Up @@ -56,7 +58,7 @@ class SiteManagementServiceTests: CoreDataTestCase {
override func setUp() {
super.setUp()

siteManagementService = SiteManagementServiceTester(managedObjectContext: contextManager.mainContext)
siteManagementService = SiteManagementServiceTester(coreDataStack: contextManager)
mockRemoteService = siteManagementService.mockRemoteService
}

Expand Down Expand Up @@ -97,26 +99,28 @@ class SiteManagementServiceTests: CoreDataTestCase {

func testDeleteSiteRemovesExistingBlogOnSuccess() {
let context = contextManager.mainContext
let blog =

let blog = insertBlog(context)

insertBlog(context)
let blogObjectID = blog.objectID

XCTAssertFalse(blogObjectID.isTemporaryID, "Should be a permanent object")

let expect = expectation(
description: "Remove Blog success expectation")
let expectation = expectation(description: "Remove Blog success expectation")
mockRemoteService.reset()
siteManagementService.deleteSiteForBlog(blog,
success: {
expect.fulfill()
expectation.fulfill()
}, failure: nil)
mockRemoteService.successBlockPassedIn?()
waitForExpectations(timeout: 2, handler: nil)

context.refreshAllObjects()
context.processPendingChanges()
let shouldBeRemoved = try? context.existingObject(with: blogObjectID)
XCTAssertFalse(shouldBeRemoved != nil, "Blog was not removed")
XCTAssertEqual(shouldBeRemoved?.isDeleted, true)
XCTAssertEqual(shouldBeRemoved?.isFault, true)
XCTAssertEqual(context.countObjects(ofType: Blog.self), 0)
expect(try? context.existingObject(with: blogObjectID)).toEventually(beNil())
}

func testDeleteSiteCallsFailureBlock() {
Expand Down

0 comments on commit d2f1d2a

Please sign in to comment.