Skip to content
Draft
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
444 changes: 0 additions & 444 deletions Modules/Sources/WordPressKit/SharingServiceRemote.swift

Large diffs are not rendered by default.

78 changes: 14 additions & 64 deletions Sources/WordPressData/Swift/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,87 +99,37 @@ public class Post: AbstractPost {

// MARK: - PublicizeConnections

@objc public func publicizeConnectionDisabledForKeyringID(_ keyringID: NSNumber) -> Bool {
let isKeyringEntryDisabled = disabledPublicizeConnections?[keyringID]?[Constants.publicizeValueKey] == Constants.publicizeDisabledValue

// try to check in case there's an entry for the PublicizeConnection that's keyed by the connectionID.
guard let connections = blog.connections,
let connection = connections.first(where: { $0.keyringConnectionID == keyringID }),
let existingValue = disabledPublicizeConnections?[connection.connectionID]?[Constants.publicizeValueKey] else {
// fall back to keyringID if there is no such entry with the connectionID.
return isKeyringEntryDisabled
}

let isConnectionEntryDisabled = existingValue == Constants.publicizeDisabledValue
return isConnectionEntryDisabled || isKeyringEntryDisabled
}

public func enablePublicizeConnectionWithKeyringID(_ keyringID: NSNumber) {
// if there's another entry keyed by connectionID references to the same connection,
// we need to make sure that the values are kept in sync.
if let connections = blog.connections,
let connection = connections.first(where: { $0.keyringConnectionID == keyringID }),
let _ = disabledPublicizeConnections?[connection.connectionID] {
enablePublicizeConnection(keyedBy: connection.connectionID)
}

enablePublicizeConnection(keyedBy: keyringID)
}

public func disablePublicizeConnectionWithKeyringID(_ keyringID: NSNumber) {
// if there's another entry keyed by connectionID references to the same connection,
// we need to make sure that the values are kept in sync.
if let connections = blog.connections,
let connectionID = connections.first(where: { $0.keyringConnectionID == keyringID })?.connectionID,
let _ = disabledPublicizeConnections?[connectionID] {
disablePublicizeConnection(keyedBy: connectionID)

// additionally, if the keyring entry doesn't exist, there's no need create both formats.
// we can just update the dictionary's key from connectionID to keyringID instead.
if disabledPublicizeConnections?[keyringID] == nil,
let updatedEntry = disabledPublicizeConnections?[connectionID] {
disabledPublicizeConnections?.removeValue(forKey: connectionID)
disabledPublicizeConnections?[keyringID] = updatedEntry
return
}
}

disablePublicizeConnection(keyedBy: keyringID)
@objc public func publicizeConnectionDisabled(forConnectionID connectionID: NSNumber) -> Bool {
disabledPublicizeConnections?[connectionID]?[Constants.publicizeValueKey] == Constants.publicizeDisabledValue
}

/// Marks the Publicize connection with the given id as enabled.
///
/// - Parameter id: The dictionary key for `disabledPublicizeConnections`.
private func enablePublicizeConnection(keyedBy id: NSNumber) {
guard var connection = disabledPublicizeConnections?[id] else {
@objc public func enablePublicizeConnection(forConnectionID connectionID: NSNumber) {
guard var entry = disabledPublicizeConnections?[connectionID] else {
return
}

// if the auto-sharing settings is not yet synced to remote,
// we can just remove the entry since all connections are enabled by default.
guard let _ = connection[Constants.publicizeIdKey] else {
_ = disabledPublicizeConnections?.removeValue(forKey: id)
// If the entry hasn't been synced to remote yet,
// remove it since all connections are enabled by default.
guard let _ = entry[Constants.publicizeIdKey] else {
_ = disabledPublicizeConnections?.removeValue(forKey: connectionID)
return
}

connection[Constants.publicizeValueKey] = Constants.publicizeEnabledValue
disabledPublicizeConnections?[id] = connection
entry[Constants.publicizeValueKey] = Constants.publicizeEnabledValue
disabledPublicizeConnections?[connectionID] = entry
}

/// Marks the Publicize connection with the given id as disabled.
///
/// - Parameter id: The dictionary key for `disabledPublicizeConnections`.
private func disablePublicizeConnection(keyedBy id: NSNumber) {
if let _ = disabledPublicizeConnections?[id] {
disabledPublicizeConnections?[id]?[Constants.publicizeValueKey] = Constants.publicizeDisabledValue
@objc public func disablePublicizeConnection(forConnectionID connectionID: NSNumber) {
if disabledPublicizeConnections?[connectionID] != nil {
disabledPublicizeConnections?[connectionID]?[Constants.publicizeValueKey] = Constants.publicizeDisabledValue
return
}

if disabledPublicizeConnections == nil {
disabledPublicizeConnections = [NSNumber: [String: String]]()
}

disabledPublicizeConnections?[id] = [Constants.publicizeValueKey: Constants.publicizeDisabledValue]
disabledPublicizeConnections?[connectionID] = [Constants.publicizeValueKey: Constants.publicizeDisabledValue]
}

// MARK: - Comments
Expand Down
186 changes: 30 additions & 156 deletions Tests/KeystoneTests/Tests/Models/Post+JetpackSocialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,245 +27,119 @@ class Post_JetpackSocialTests: CoreDataTestCase {

// MARK: - Checking for PublicizeConnection state

func testCheckPublicizeConnectionHavingOnlyKeyringIDEntry() {
func testCheckPublicizeConnectionDisabled() {
// Given
let keyringID = NSNumber(value: 100)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .disabled]
])

// When
let result = post.publicizeConnectionDisabledForKeyringID(keyringID)

// Then
XCTAssertTrue(result)
}

func testCheckPublicizeConnectionHavingOnlyConnectionIDEntry() {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost(disabledConnections: [
connectionID: [.valueKey: .disabled]
])

// When
let result = post.publicizeConnectionDisabledForKeyringID(keyringID)
let result = post.publicizeConnectionDisabled(forConnectionID: connectionID)

// Then
XCTAssertTrue(result)
}

func testCheckPublicizeConnectionHavingDifferentKeyringAndConnectionEntries() {
func testCheckPublicizeConnectionNotDisabled() {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .enabled],
connectionID: [.valueKey: .disabled]
])
let post2 = makePost(disabledConnections: [
keyringID: [.valueKey: .disabled],
connectionID: [.valueKey: .enabled]
])

// When
let result1 = post.publicizeConnectionDisabledForKeyringID(keyringID)
let result2 = post2.publicizeConnectionDisabledForKeyringID(keyringID)
let result = post.publicizeConnectionDisabled(forConnectionID: connectionID)

// Then
// if either one of the value is true, then the method should return true. See: pctCYC-XT-p2#comment-1000
XCTAssertTrue(result1)
XCTAssertTrue(result2)
XCTAssertFalse(result)
}

// MARK: - Disabling connections

func testDisableConnectionWithoutAnyEntries() throws {
func testCheckPublicizeConnectionWithNoEntry() {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost()

// When
post.disablePublicizeConnectionWithKeyringID(keyringID)
let result = post.publicizeConnectionDisabled(forConnectionID: connectionID)

// Then
let entry = try XCTUnwrap(post.disabledPublicizeConnections?[keyringID])
XCTAssertEqual(entry[.valueKey], .disabled)
XCTAssertFalse(result)
}

func testDisableConnectionWithPriorKeyringEntry() throws {
// Given
let keyringID = NSNumber(value: 100)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .enabled]
])

// When
post.disablePublicizeConnectionWithKeyringID(keyringID)

// Then
let entry = try XCTUnwrap(post.disabledPublicizeConnections?[keyringID])
XCTAssertEqual(entry[.valueKey], .disabled)
}
// MARK: - Disabling connections

func testDisableConnectionWithPriorKeyringAndConnectionEntries() throws {
func testDisableConnectionWithoutAnyEntries() throws {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .enabled],
connectionID: [.valueKey: .enabled]
])
let post = makePost()

// When
post.disablePublicizeConnectionWithKeyringID(keyringID)
post.disablePublicizeConnection(forConnectionID: connectionID)

// Then
// both entries' values should be updated.
let keyringEntry = try XCTUnwrap(post.disabledPublicizeConnections?[keyringID])
XCTAssertEqual(keyringEntry[.valueKey], .disabled)

let connectionEntry = try XCTUnwrap(post.disabledPublicizeConnections?[connectionID])
XCTAssertEqual(connectionEntry[.valueKey], .disabled)
let entry = try XCTUnwrap(post.disabledPublicizeConnections?[connectionID])
XCTAssertEqual(entry[.valueKey], .disabled)
}

func testDisableConnectionHavingOnlyConnectionIDEntry() throws {
func testDisableConnectionWithPriorEntry() throws {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost(disabledConnections: [
connectionID: [.valueKey: .enabled]
])

// When
post.disablePublicizeConnectionWithKeyringID(keyringID)
post.disablePublicizeConnection(forConnectionID: connectionID)

// Then
// if the keyring entry doesn't exist, the dictionary key should be updated to the keyringID.
let keyringEntry = try XCTUnwrap(post.disabledPublicizeConnections?[keyringID])
XCTAssertEqual(keyringEntry[.valueKey], .disabled)

// the connection entry should be deleted.
XCTAssertNil(post.disabledPublicizeConnections?[connectionID])
let entry = try XCTUnwrap(post.disabledPublicizeConnections?[connectionID])
XCTAssertEqual(entry[.valueKey], .disabled)
}

// MARK: - Enabling connections

// Note: unlikely case since there must be an entry in the `disabledPublicizeConnections` for the switch to be on.
func testEnableConnectionWithoutAnyEntries() {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost()

// When
post.enablePublicizeConnectionWithKeyringID(keyringID)
post.enablePublicizeConnection(forConnectionID: connectionID)

// Then
// Calling the enable method should do nothing.
XCTAssertNil(post.disabledPublicizeConnections?[keyringID])
}

func testEnableConnectionWithLocalKeyringEntry() {
// Given
let keyringID = NSNumber(value: 100)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .disabled]
])

// When
post.enablePublicizeConnectionWithKeyringID(keyringID)

// Then
// if the entry hasn't been synced yet, the entry will be deleted since all connections are enabled by default.
XCTAssertNil(post.disabledPublicizeConnections?[keyringID])
}

func testEnableConnectionWithSyncedKeyringEntry() throws {
// Given
let keyringID = NSNumber(value: 100)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .disabled, .idKey: "24"] // having an id means the entry exists on backend.
])

// When
post.enablePublicizeConnectionWithKeyringID(keyringID)

// Then
let keyringEntry = try XCTUnwrap(post.disabledPublicizeConnections?[keyringID])
XCTAssertEqual(keyringEntry[.valueKey], .enabled)
}

// both keyring entry and connection entry are synced
func testEnableConnectionWithPriorSyncedKeyringAndConnectionEntries() throws {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .disabled, .idKey: "24"], // having an id means the entry exists on backend.
connectionID: [.valueKey: .disabled, .idKey: "26"]
])

// When
post.enablePublicizeConnectionWithKeyringID(keyringID)

// Then
// both entries should be updated.
let keyringEntry = try XCTUnwrap(post.disabledPublicizeConnections?[keyringID])
XCTAssertEqual(keyringEntry[.valueKey], .enabled)

let connectionEntry = try XCTUnwrap(post.disabledPublicizeConnections?[connectionID])
XCTAssertEqual(connectionEntry[.valueKey], .enabled)
XCTAssertNil(post.disabledPublicizeConnections?[connectionID])
}

// the keyring entry is local, but the connection entry is synced.
func testEnableConnectionWithPriorLocalKeyringAndSyncedConnectionEntries() throws {
func testEnableConnectionWithLocalEntry() {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let post = makePost(disabledConnections: [
keyringID: [.valueKey: .disabled],
connectionID: [.valueKey: .disabled, .idKey: "26"]
connectionID: [.valueKey: .disabled]
])

// When
post.enablePublicizeConnectionWithKeyringID(keyringID)
post.enablePublicizeConnection(forConnectionID: connectionID)

// Then
// the local entry should be removed.
XCTAssertNil(post.disabledPublicizeConnections?[keyringID])

// but the synced entry should be updated.
let entry = try XCTUnwrap(post.disabledPublicizeConnections?[connectionID])
XCTAssertEqual(entry[.valueKey], .enabled)
// If the entry hasn't been synced yet, it will be removed since all connections are enabled by default.
XCTAssertNil(post.disabledPublicizeConnections?[connectionID])
}

func testEnableConnectionHavingOnlyConnectionEntry() throws {
func testEnableConnectionWithSyncedEntry() throws {
// Given
let keyringID = NSNumber(value: 100)
let connectionID = NSNumber(value: 200)
let keyringID2 = NSNumber(value: 101)
let connectionID2 = NSNumber(value: 201)
let post = makePost(disabledConnections: [
connectionID: [.valueKey: .disabled, .idKey: "26"],
connectionID2: [.valueKey: .disabled]
connectionID: [.valueKey: .disabled, .idKey: "24"] // having an id means the entry exists on backend.
])

// When
post.enablePublicizeConnectionWithKeyringID(keyringID)
post.enablePublicizeConnectionWithKeyringID(keyringID2)
post.enablePublicizeConnection(forConnectionID: connectionID)

// Then
// there shouldn't be any entries keyed by the keyringIDs.
XCTAssertNil(post.disabledPublicizeConnections?[keyringID])
XCTAssertNil(post.disabledPublicizeConnections?[keyringID2])

// the entry with connectionID should be updated.
let entry = try XCTUnwrap(post.disabledPublicizeConnections?[connectionID])
XCTAssertEqual(entry[.valueKey], .enabled)

// and if the entry isn't synced, it should be deleted.
XCTAssertNil(post.disabledPublicizeConnections?[connectionID2])
}

// MARK: - Helpers
Expand Down
Loading