Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 8 additions & 0 deletions Networking/Networking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@
DE42F96B296BC23800D514C2 /* customer-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96A296BC23800D514C2 /* customer-without-data.json */; };
DE42F96D296BC67E00D514C2 /* wc-analytics-customers-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96C296BC67E00D514C2 /* wc-analytics-customers-without-data.json */; };
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96E296BC9A700D514C2 /* countries-without-data.json */; };
DE4F2A4229751EAE00B0701C /* setting-coupon-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE4F2A4129751EAE00B0701C /* setting-coupon-without-data.json */; };
DE50295928C5BD0200551736 /* JetpackUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295828C5BD0200551736 /* JetpackUser.swift */; };
DE50295B28C5F99700551736 /* DotcomUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295A28C5F99700551736 /* DotcomUser.swift */; };
DE50295D28C6068B00551736 /* JetpackUserMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295C28C6068B00551736 /* JetpackUserMapper.swift */; };
Expand Down Expand Up @@ -734,6 +735,7 @@
DEC51AF92769A212009F3DF4 /* SystemStatus+Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51AF82769A212009F3DF4 /* SystemStatus+Settings.swift */; };
DEC51AFB2769C66B009F3DF4 /* SystemStatusMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51AFA2769C66B009F3DF4 /* SystemStatusMapperTests.swift */; };
DEC51B02276AFB35009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51B01276AFB34009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift */; };
DEEDA4442975003800F845AB /* settings-general-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEEDA4432975003800F845AB /* settings-general-without-data.json */; };
DEF13C5029629EEA0024A02B /* user-complete-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */; };
DEF13C562965689F0024A02B /* LeaderboardListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */; };
DEF13C5A296571150024A02B /* leaderboards-year-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C59296571150024A02B /* leaderboards-year-without-data.json */; };
Expand Down Expand Up @@ -1517,6 +1519,7 @@
DE42F96A296BC23800D514C2 /* customer-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "customer-without-data.json"; sourceTree = "<group>"; };
DE42F96C296BC67E00D514C2 /* wc-analytics-customers-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "wc-analytics-customers-without-data.json"; sourceTree = "<group>"; };
DE42F96E296BC9A700D514C2 /* countries-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "countries-without-data.json"; sourceTree = "<group>"; };
DE4F2A4129751EAE00B0701C /* setting-coupon-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "setting-coupon-without-data.json"; sourceTree = "<group>"; };
DE50295828C5BD0200551736 /* JetpackUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUser.swift; sourceTree = "<group>"; };
DE50295A28C5F99700551736 /* DotcomUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DotcomUser.swift; sourceTree = "<group>"; };
DE50295C28C6068B00551736 /* JetpackUserMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUserMapper.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1552,6 +1555,7 @@
DEC51AF82769A212009F3DF4 /* SystemStatus+Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SystemStatus+Settings.swift"; sourceTree = "<group>"; };
DEC51AFA2769C66B009F3DF4 /* SystemStatusMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemStatusMapperTests.swift; sourceTree = "<group>"; };
DEC51B01276AFB34009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SystemStatus+DropinMustUsePlugin.swift"; sourceTree = "<group>"; };
DEEDA4432975003800F845AB /* settings-general-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "settings-general-without-data.json"; sourceTree = "<group>"; };
DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "user-complete-without-data.json"; sourceTree = "<group>"; };
DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeaderboardListMapperTests.swift; sourceTree = "<group>"; };
DEF13C59296571150024A02B /* leaderboards-year-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "leaderboards-year-without-data.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2141,6 +2145,8 @@
B559EBA820A0B5B100836CD4 /* Responses */ = {
isa = PBXGroup;
children = (
DE4F2A4129751EAE00B0701C /* setting-coupon-without-data.json */,
DEEDA4432975003800F845AB /* settings-general-without-data.json */,
DEA6B1C8296D0E8A005AA5E9 /* systemStatus-without-data.json */,
DEA6B1C7296D0E8A005AA5E9 /* systemStatusWithPluginsOnly-without-data.json */,
DEA6B1C5296C13FB005AA5E9 /* payment-gateway-list-without-data.json */,
Expand Down Expand Up @@ -3148,11 +3154,13 @@
02616F902921336C0095BC00 /* site-creation-success.json in Resources */,
CC0786632678F79500BA9AC1 /* shipping-label-purchase-success.json in Resources */,
7497376A2141F2BE0008C490 /* top-performers-week-alt.json in Resources */,
DEEDA4442975003800F845AB /* settings-general-without-data.json in Resources */,
D865CE61278CA1AE002C8520 /* stripe-payment-intent-processing.json in Resources */,
743E84F222172D0A00FAC9D7 /* shipment_tracking_plugin_not_active.json in Resources */,
68CB801628D8A39700E169F8 /* customer.json in Resources */,
EEA658402966C05D00112DF0 /* product-search-sku-without-data.json in Resources */,
DE42F96D296BC67E00D514C2 /* wc-analytics-customers-without-data.json in Resources */,
DE4F2A4229751EAE00B0701C /* setting-coupon-without-data.json in Resources */,
03EB99982907F4AA00F06A39 /* just-in-time-message-list-multiple.json in Resources */,
EE80A25029556FBD003591E4 /* coupon-reports-without-data.json in Resources */,
451A97DE260B59870059D135 /* shipping-label-packages-success.json in Resources */,
Expand Down
6 changes: 5 additions & 1 deletion Networking/Networking/Mapper/SiteSettingMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ struct SiteSettingMapper: Mapper {
.settingGroupKey: settingsGroup.rawValue
]

return try decoder.decode(SiteSettingEnvelope.self, from: response).setting
do {
return try decoder.decode(SiteSettingEnvelope.self, from: response).setting
} catch {
return try decoder.decode(SiteSetting.self, from: response)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion Networking/Networking/Mapper/SiteSettingsMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ struct SiteSettingsMapper: Mapper {
.settingGroupKey: settingsGroup.rawValue
]

return try decoder.decode(SiteSettingsEnvelope.self, from: response).settings
do {
return try decoder.decode(SiteSettingsEnvelope.self, from: response).settings
} catch {
return try decoder.decode([SiteSetting].self, from: response)
}
}
}

Expand Down
28 changes: 24 additions & 4 deletions Networking/Networking/Remote/SiteSettingsRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public class SiteSettingsRemote: Remote {
///
public func loadGeneralSettings(for siteID: Int64, completion: @escaping ([SiteSetting]?, Error?) -> Void) {
let path = Constants.siteSettingsPath + Constants.generalSettingsGroup
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: nil)
let request = JetpackRequest(wooApiVersion: .mark3,
method: .get,
siteID: siteID,
path: path,
parameters: nil,
availableAsRESTRequest: true)
let mapper = SiteSettingsMapper(siteID: siteID, settingsGroup: SiteSettingGroup.general)

enqueue(request, mapper: mapper, completion: completion)
Expand All @@ -27,7 +32,12 @@ public class SiteSettingsRemote: Remote {
///
public func loadProductSettings(for siteID: Int64, completion: @escaping ([SiteSetting]?, Error?) -> Void) {
let path = Constants.siteSettingsPath + Constants.productSettingsGroup
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: nil)
let request = JetpackRequest(wooApiVersion: .mark3,
method: .get,
siteID: siteID,
path: path,
parameters: nil,
availableAsRESTRequest: true)
let mapper = SiteSettingsMapper(siteID: siteID, settingsGroup: SiteSettingGroup.product)

enqueue(request, mapper: mapper, completion: completion)
Expand All @@ -43,7 +53,12 @@ public class SiteSettingsRemote: Remote {
///
public func loadSetting(for siteID: Int64, settingGroup: SiteSettingGroup, settingID: String, completion: @escaping (Result<SiteSetting, Error>) -> Void) {
let path = Constants.siteSettingsPath + settingGroup.rawValue + "/" + settingID
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: nil)
let request = JetpackRequest(wooApiVersion: .mark3,
method: .get,
siteID: siteID,
path: path,
parameters: nil,
availableAsRESTRequest: true)
let mapper = SiteSettingMapper(siteID: siteID, settingsGroup: SiteSettingGroup.general)

enqueue(request, mapper: mapper, completion: completion)
Expand All @@ -65,7 +80,12 @@ public class SiteSettingsRemote: Remote {
completion: @escaping (Result<SiteSetting, Error>) -> Void) {
let parameters: [String: Any] = [Constants.valueParameter: value]
let path = Constants.siteSettingsPath + settingGroup.rawValue + "/" + settingID
let request = JetpackRequest(wooApiVersion: .mark3, method: .put, siteID: siteID, path: path, parameters: parameters)
let request = JetpackRequest(wooApiVersion: .mark3,
method: .put,
siteID: siteID,
path: path,
parameters: parameters,
availableAsRESTRequest: true)
let mapper = SiteSettingMapper(siteID: siteID, settingsGroup: SiteSettingGroup.general)

enqueue(request, mapper: mapper, completion: completion)
Expand Down
19 changes: 19 additions & 0 deletions Networking/NetworkingTests/Mapper/SiteSettingMapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ final class SiteSettingMapperTests: XCTestCase {
XCTAssertEqual(setting.label, "Enable coupons")
XCTAssertEqual(setting.value, "yes")
}

/// Verifies the SiteSetting fields are parsed correctly when response has no data envelope.
///
func test_SiteSetting_fields_are_properly_parsed_when_response_has_no_data_envelope() throws {
let setting = try XCTUnwrap(mapLoadCouponSettingResponseWithoutDataEnvelope())
XCTAssertEqual(setting.siteID, dummySiteID)
XCTAssertEqual(setting.settingID, "woocommerce_enable_coupons")
XCTAssertEqual(setting.settingDescription, "Enable the use of coupon codes")
XCTAssertEqual(setting.label, "Enable coupons")
XCTAssertEqual(setting.value, "yes")
}

func test_SiteSetting_value_field_is_properly_parsed_when_value_field_is_not_string() throws {
let setting = try XCTUnwrap(loadMultiselectValueSettingResponse())
XCTAssertEqual(setting.settingID, "woocommerce_all_except_countries")
Expand All @@ -41,6 +53,13 @@ private extension SiteSettingMapperTests {
func mapLoadCouponSettingResponse() -> SiteSetting? {
return mapSetting(from: "setting-coupon")
}

/// Returns the SiteSettingMapper output upon receiving `setting-coupon-without-data`
///
func mapLoadCouponSettingResponseWithoutDataEnvelope() -> SiteSetting? {
return mapSetting(from: "setting-coupon-without-data")
}

/// Returns the SiteSettingMapper output upon receiving `setting-all-except-countries`
///
func loadMultiselectValueSettingResponse() -> SiteSetting? {
Expand Down
44 changes: 41 additions & 3 deletions Networking/NetworkingTests/Mapper/SiteSettingsMapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@ class SiteSettingsMapperTests: XCTestCase {
XCTAssertEqual(decimalSetting.value, "2")
}

/// Verifies the SiteSetting fields are parsed correctly when response has no data envelope.
///
func test_SiteSetting_fields_are_properly_parsed_when_response_has_no_data_envelope() {
let settings = mapLoadGeneralSiteSettingsResponseWithoutDataEnvelope()
XCTAssertEqual(settings.count, 20)

let firstSetting = settings[0]
XCTAssertNotNil(firstSetting)
XCTAssertEqual(firstSetting.siteID, dummySiteID)
XCTAssertEqual(firstSetting.settingID, "woocommerce_store_address")
XCTAssertEqual(firstSetting.settingDescription, "The street address for your business location.")
XCTAssertEqual(firstSetting.label, "Address line 1")
XCTAssertEqual(firstSetting.value, "60 29th Street #343")

let currencySetting = settings[14]
XCTAssertNotNil(currencySetting)
XCTAssertEqual(currencySetting.siteID, dummySiteID)
XCTAssertEqual(currencySetting.settingID, "woocommerce_currency")
XCTAssertEqual(currencySetting.settingDescription,
"This controls what currency prices are listed at in the catalog and which currency gateways will take payments in.")
XCTAssertEqual(currencySetting.label, "Currency")
XCTAssertEqual(currencySetting.value, "USD")

let decimalSetting = settings[18]
XCTAssertNotNil(decimalSetting)
XCTAssertEqual(decimalSetting.siteID, dummySiteID)
XCTAssertEqual(decimalSetting.settingID, "woocommerce_price_num_decimals")
XCTAssertEqual(decimalSetting.settingDescription, "This sets the number of decimal points shown in displayed prices.")
XCTAssertEqual(decimalSetting.label, "Number of decimals")
XCTAssertEqual(decimalSetting.value, "2")
}

/// Verifies that a SiteSetting in a broken state gets default values
///
func test_SiteSettings_are_properly_parsed_when_nulls_received() {
Expand All @@ -63,7 +95,7 @@ class SiteSettingsMapperTests: XCTestCase {
///
private extension SiteSettingsMapperTests {

/// Returns the OrderNotesMapper output upon receiving `filename` (Data Encoded)
/// Returns the [SiteSetting] output upon receiving `filename` (Data Encoded)
///
func mapGeneralSettings(from filename: String) -> [SiteSetting] {
guard let response = Loader.contentsOf(filename) else {
Expand All @@ -73,13 +105,19 @@ private extension SiteSettingsMapperTests {
return try! SiteSettingsMapper(siteID: dummySiteID, settingsGroup: SiteSettingGroup.general).map(response: response)
}

/// Returns the OrderNotesMapper output upon receiving `settings-general`
/// Returns the [SiteSetting] output upon receiving `settings-general`
///
func mapLoadGeneralSiteSettingsResponse() -> [SiteSetting] {
return mapGeneralSettings(from: "settings-general")
}

/// Returns the OrderNotesMapper output upon receiving `broken-settings-general`
/// Returns the [SiteSetting] output upon receiving `settings-general-without-data`
///
func mapLoadGeneralSiteSettingsResponseWithoutDataEnvelope() -> [SiteSetting] {
return mapGeneralSettings(from: "settings-general-without-data")
}

/// Returns the [SiteSetting] output upon receiving `broken-settings-general`
///
func mapLoadBrokenGeneralSiteSettingsResponse() -> [SiteSetting] {
return mapGeneralSettings(from: "broken-settings-general")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": "woocommerce_enable_coupons",
"label": "Enable coupons",
"description": "Enable the use of coupon codes",
"type": "checkbox",
"default": "yes",
"tip": "Coupons can be applied from the cart and checkout pages.",
"value": "yes",
"_links": {
"self": [
{
"href": "https://jamosova3.mystagingwebsite.com/wp-json/wc/v2/settings/general/woocommerce_enable_coupons"
}
],
"collection": [
{
"href": "https://jamosova3.mystagingwebsite.com/wp-json/wc/v2/settings/general"
}
]
}
}
Loading