Skip to content

Commit 602cd36

Browse files
authored
Merge pull request #8638 from woocommerce/feat/8637-site-settings-migration
REST API: Migrate site settings endpoints
2 parents 52f3aab + 419896c commit 602cd36

File tree

9 files changed

+595
-15
lines changed

9 files changed

+595
-15
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@
704704
DE42F96B296BC23800D514C2 /* customer-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96A296BC23800D514C2 /* customer-without-data.json */; };
705705
DE42F96D296BC67E00D514C2 /* wc-analytics-customers-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96C296BC67E00D514C2 /* wc-analytics-customers-without-data.json */; };
706706
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96E296BC9A700D514C2 /* countries-without-data.json */; };
707+
DE4F2A4229751EAE00B0701C /* setting-coupon-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE4F2A4129751EAE00B0701C /* setting-coupon-without-data.json */; };
707708
DE50295928C5BD0200551736 /* JetpackUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295828C5BD0200551736 /* JetpackUser.swift */; };
708709
DE50295B28C5F99700551736 /* DotcomUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295A28C5F99700551736 /* DotcomUser.swift */; };
709710
DE50295D28C6068B00551736 /* JetpackUserMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295C28C6068B00551736 /* JetpackUserMapper.swift */; };
@@ -739,6 +740,7 @@
739740
DEC51AF92769A212009F3DF4 /* SystemStatus+Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51AF82769A212009F3DF4 /* SystemStatus+Settings.swift */; };
740741
DEC51AFB2769C66B009F3DF4 /* SystemStatusMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51AFA2769C66B009F3DF4 /* SystemStatusMapperTests.swift */; };
741742
DEC51B02276AFB35009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51B01276AFB34009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift */; };
743+
DEEDA4442975003800F845AB /* settings-general-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEEDA4432975003800F845AB /* settings-general-without-data.json */; };
742744
DEF13C5029629EEA0024A02B /* user-complete-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */; };
743745
DEF13C562965689F0024A02B /* LeaderboardListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */; };
744746
DEF13C5A296571150024A02B /* leaderboards-year-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C59296571150024A02B /* leaderboards-year-without-data.json */; };
@@ -1527,6 +1529,7 @@
15271529
DE42F96A296BC23800D514C2 /* customer-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "customer-without-data.json"; sourceTree = "<group>"; };
15281530
DE42F96C296BC67E00D514C2 /* wc-analytics-customers-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "wc-analytics-customers-without-data.json"; sourceTree = "<group>"; };
15291531
DE42F96E296BC9A700D514C2 /* countries-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "countries-without-data.json"; sourceTree = "<group>"; };
1532+
DE4F2A4129751EAE00B0701C /* setting-coupon-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "setting-coupon-without-data.json"; sourceTree = "<group>"; };
15301533
DE50295828C5BD0200551736 /* JetpackUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUser.swift; sourceTree = "<group>"; };
15311534
DE50295A28C5F99700551736 /* DotcomUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DotcomUser.swift; sourceTree = "<group>"; };
15321535
DE50295C28C6068B00551736 /* JetpackUserMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUserMapper.swift; sourceTree = "<group>"; };
@@ -1562,6 +1565,7 @@
15621565
DEC51AF82769A212009F3DF4 /* SystemStatus+Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SystemStatus+Settings.swift"; sourceTree = "<group>"; };
15631566
DEC51AFA2769C66B009F3DF4 /* SystemStatusMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemStatusMapperTests.swift; sourceTree = "<group>"; };
15641567
DEC51B01276AFB34009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SystemStatus+DropinMustUsePlugin.swift"; sourceTree = "<group>"; };
1568+
DEEDA4432975003800F845AB /* settings-general-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "settings-general-without-data.json"; sourceTree = "<group>"; };
15651569
DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "user-complete-without-data.json"; sourceTree = "<group>"; };
15661570
DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeaderboardListMapperTests.swift; sourceTree = "<group>"; };
15671571
DEF13C59296571150024A02B /* leaderboards-year-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "leaderboards-year-without-data.json"; sourceTree = "<group>"; };
@@ -2151,6 +2155,8 @@
21512155
B559EBA820A0B5B100836CD4 /* Responses */ = {
21522156
isa = PBXGroup;
21532157
children = (
2158+
DE4F2A4129751EAE00B0701C /* setting-coupon-without-data.json */,
2159+
DEEDA4432975003800F845AB /* settings-general-without-data.json */,
21542160
DEA6B1C8296D0E8A005AA5E9 /* systemStatus-without-data.json */,
21552161
DEA6B1C7296D0E8A005AA5E9 /* systemStatusWithPluginsOnly-without-data.json */,
21562162
DEA6B1C5296C13FB005AA5E9 /* payment-gateway-list-without-data.json */,
@@ -3167,11 +3173,13 @@
31673173
02616F902921336C0095BC00 /* site-creation-success.json in Resources */,
31683174
CC0786632678F79500BA9AC1 /* shipping-label-purchase-success.json in Resources */,
31693175
7497376A2141F2BE0008C490 /* top-performers-week-alt.json in Resources */,
3176+
DEEDA4442975003800F845AB /* settings-general-without-data.json in Resources */,
31703177
D865CE61278CA1AE002C8520 /* stripe-payment-intent-processing.json in Resources */,
31713178
743E84F222172D0A00FAC9D7 /* shipment_tracking_plugin_not_active.json in Resources */,
31723179
68CB801628D8A39700E169F8 /* customer.json in Resources */,
31733180
EEA658402966C05D00112DF0 /* product-search-sku-without-data.json in Resources */,
31743181
DE42F96D296BC67E00D514C2 /* wc-analytics-customers-without-data.json in Resources */,
3182+
DE4F2A4229751EAE00B0701C /* setting-coupon-without-data.json in Resources */,
31753183
03EB99982907F4AA00F06A39 /* just-in-time-message-list-multiple.json in Resources */,
31763184
EE80A25029556FBD003591E4 /* coupon-reports-without-data.json in Resources */,
31773185
451A97DE260B59870059D135 /* shipping-label-packages-success.json in Resources */,

Networking/Networking/Mapper/SiteSettingMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ struct SiteSettingMapper: Mapper {
2525
.settingGroupKey: settingsGroup.rawValue
2626
]
2727

28-
return try decoder.decode(SiteSettingEnvelope.self, from: response).setting
28+
do {
29+
return try decoder.decode(SiteSettingEnvelope.self, from: response).setting
30+
} catch {
31+
return try decoder.decode(SiteSetting.self, from: response)
32+
}
2933
}
3034
}
3135

Networking/Networking/Mapper/SiteSettingsMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ struct SiteSettingsMapper: Mapper {
2626
.settingGroupKey: settingsGroup.rawValue
2727
]
2828

29-
return try decoder.decode(SiteSettingsEnvelope.self, from: response).settings
29+
do {
30+
return try decoder.decode(SiteSettingsEnvelope.self, from: response).settings
31+
} catch {
32+
return try decoder.decode([SiteSetting].self, from: response)
33+
}
3034
}
3135
}
3236

Networking/Networking/Remote/SiteSettingsRemote.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ public class SiteSettingsRemote: Remote {
1313
///
1414
public func loadGeneralSettings(for siteID: Int64, completion: @escaping ([SiteSetting]?, Error?) -> Void) {
1515
let path = Constants.siteSettingsPath + Constants.generalSettingsGroup
16-
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: nil)
16+
let request = JetpackRequest(wooApiVersion: .mark3,
17+
method: .get,
18+
siteID: siteID,
19+
path: path,
20+
parameters: nil,
21+
availableAsRESTRequest: true)
1722
let mapper = SiteSettingsMapper(siteID: siteID, settingsGroup: SiteSettingGroup.general)
1823

1924
enqueue(request, mapper: mapper, completion: completion)
@@ -27,7 +32,12 @@ public class SiteSettingsRemote: Remote {
2732
///
2833
public func loadProductSettings(for siteID: Int64, completion: @escaping ([SiteSetting]?, Error?) -> Void) {
2934
let path = Constants.siteSettingsPath + Constants.productSettingsGroup
30-
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: nil)
35+
let request = JetpackRequest(wooApiVersion: .mark3,
36+
method: .get,
37+
siteID: siteID,
38+
path: path,
39+
parameters: nil,
40+
availableAsRESTRequest: true)
3141
let mapper = SiteSettingsMapper(siteID: siteID, settingsGroup: SiteSettingGroup.product)
3242

3343
enqueue(request, mapper: mapper, completion: completion)
@@ -43,7 +53,12 @@ public class SiteSettingsRemote: Remote {
4353
///
4454
public func loadSetting(for siteID: Int64, settingGroup: SiteSettingGroup, settingID: String, completion: @escaping (Result<SiteSetting, Error>) -> Void) {
4555
let path = Constants.siteSettingsPath + settingGroup.rawValue + "/" + settingID
46-
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: path, parameters: nil)
56+
let request = JetpackRequest(wooApiVersion: .mark3,
57+
method: .get,
58+
siteID: siteID,
59+
path: path,
60+
parameters: nil,
61+
availableAsRESTRequest: true)
4762
let mapper = SiteSettingMapper(siteID: siteID, settingsGroup: SiteSettingGroup.general)
4863

4964
enqueue(request, mapper: mapper, completion: completion)
@@ -65,7 +80,12 @@ public class SiteSettingsRemote: Remote {
6580
completion: @escaping (Result<SiteSetting, Error>) -> Void) {
6681
let parameters: [String: Any] = [Constants.valueParameter: value]
6782
let path = Constants.siteSettingsPath + settingGroup.rawValue + "/" + settingID
68-
let request = JetpackRequest(wooApiVersion: .mark3, method: .put, siteID: siteID, path: path, parameters: parameters)
83+
let request = JetpackRequest(wooApiVersion: .mark3,
84+
method: .put,
85+
siteID: siteID,
86+
path: path,
87+
parameters: parameters,
88+
availableAsRESTRequest: true)
6989
let mapper = SiteSettingMapper(siteID: siteID, settingsGroup: SiteSettingGroup.general)
7090

7191
enqueue(request, mapper: mapper, completion: completion)

Networking/NetworkingTests/Mapper/SiteSettingMapperTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ final class SiteSettingMapperTests: XCTestCase {
1717
XCTAssertEqual(setting.label, "Enable coupons")
1818
XCTAssertEqual(setting.value, "yes")
1919
}
20+
21+
/// Verifies the SiteSetting fields are parsed correctly when response has no data envelope.
22+
///
23+
func test_SiteSetting_fields_are_properly_parsed_when_response_has_no_data_envelope() throws {
24+
let setting = try XCTUnwrap(mapLoadCouponSettingResponseWithoutDataEnvelope())
25+
XCTAssertEqual(setting.siteID, dummySiteID)
26+
XCTAssertEqual(setting.settingID, "woocommerce_enable_coupons")
27+
XCTAssertEqual(setting.settingDescription, "Enable the use of coupon codes")
28+
XCTAssertEqual(setting.label, "Enable coupons")
29+
XCTAssertEqual(setting.value, "yes")
30+
}
31+
2032
func test_SiteSetting_value_field_is_properly_parsed_when_value_field_is_not_string() throws {
2133
let setting = try XCTUnwrap(loadMultiselectValueSettingResponse())
2234
XCTAssertEqual(setting.settingID, "woocommerce_all_except_countries")
@@ -41,6 +53,13 @@ private extension SiteSettingMapperTests {
4153
func mapLoadCouponSettingResponse() -> SiteSetting? {
4254
return mapSetting(from: "setting-coupon")
4355
}
56+
57+
/// Returns the SiteSettingMapper output upon receiving `setting-coupon-without-data`
58+
///
59+
func mapLoadCouponSettingResponseWithoutDataEnvelope() -> SiteSetting? {
60+
return mapSetting(from: "setting-coupon-without-data")
61+
}
62+
4463
/// Returns the SiteSettingMapper output upon receiving `setting-all-except-countries`
4564
///
4665
func loadMultiselectValueSettingResponse() -> SiteSetting? {

Networking/NetworkingTests/Mapper/SiteSettingsMapperTests.swift

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@ class SiteSettingsMapperTests: XCTestCase {
4242
XCTAssertEqual(decimalSetting.value, "2")
4343
}
4444

45+
/// Verifies the SiteSetting fields are parsed correctly when response has no data envelope.
46+
///
47+
func test_SiteSetting_fields_are_properly_parsed_when_response_has_no_data_envelope() {
48+
let settings = mapLoadGeneralSiteSettingsResponseWithoutDataEnvelope()
49+
XCTAssertEqual(settings.count, 20)
50+
51+
let firstSetting = settings[0]
52+
XCTAssertNotNil(firstSetting)
53+
XCTAssertEqual(firstSetting.siteID, dummySiteID)
54+
XCTAssertEqual(firstSetting.settingID, "woocommerce_store_address")
55+
XCTAssertEqual(firstSetting.settingDescription, "The street address for your business location.")
56+
XCTAssertEqual(firstSetting.label, "Address line 1")
57+
XCTAssertEqual(firstSetting.value, "60 29th Street #343")
58+
59+
let currencySetting = settings[14]
60+
XCTAssertNotNil(currencySetting)
61+
XCTAssertEqual(currencySetting.siteID, dummySiteID)
62+
XCTAssertEqual(currencySetting.settingID, "woocommerce_currency")
63+
XCTAssertEqual(currencySetting.settingDescription,
64+
"This controls what currency prices are listed at in the catalog and which currency gateways will take payments in.")
65+
XCTAssertEqual(currencySetting.label, "Currency")
66+
XCTAssertEqual(currencySetting.value, "USD")
67+
68+
let decimalSetting = settings[18]
69+
XCTAssertNotNil(decimalSetting)
70+
XCTAssertEqual(decimalSetting.siteID, dummySiteID)
71+
XCTAssertEqual(decimalSetting.settingID, "woocommerce_price_num_decimals")
72+
XCTAssertEqual(decimalSetting.settingDescription, "This sets the number of decimal points shown in displayed prices.")
73+
XCTAssertEqual(decimalSetting.label, "Number of decimals")
74+
XCTAssertEqual(decimalSetting.value, "2")
75+
}
76+
4577
/// Verifies that a SiteSetting in a broken state gets default values
4678
///
4779
func test_SiteSettings_are_properly_parsed_when_nulls_received() {
@@ -63,7 +95,7 @@ class SiteSettingsMapperTests: XCTestCase {
6395
///
6496
private extension SiteSettingsMapperTests {
6597

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

76-
/// Returns the OrderNotesMapper output upon receiving `settings-general`
108+
/// Returns the [SiteSetting] output upon receiving `settings-general`
77109
///
78110
func mapLoadGeneralSiteSettingsResponse() -> [SiteSetting] {
79111
return mapGeneralSettings(from: "settings-general")
80112
}
81113

82-
/// Returns the OrderNotesMapper output upon receiving `broken-settings-general`
114+
/// Returns the [SiteSetting] output upon receiving `settings-general-without-data`
115+
///
116+
func mapLoadGeneralSiteSettingsResponseWithoutDataEnvelope() -> [SiteSetting] {
117+
return mapGeneralSettings(from: "settings-general-without-data")
118+
}
119+
120+
/// Returns the [SiteSetting] output upon receiving `broken-settings-general`
83121
///
84122
func mapLoadBrokenGeneralSiteSettingsResponse() -> [SiteSetting] {
85123
return mapGeneralSettings(from: "broken-settings-general")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"id": "woocommerce_enable_coupons",
3+
"label": "Enable coupons",
4+
"description": "Enable the use of coupon codes",
5+
"type": "checkbox",
6+
"default": "yes",
7+
"tip": "Coupons can be applied from the cart and checkout pages.",
8+
"value": "yes",
9+
"_links": {
10+
"self": [
11+
{
12+
"href": "https://jamosova3.mystagingwebsite.com/wp-json/wc/v2/settings/general/woocommerce_enable_coupons"
13+
}
14+
],
15+
"collection": [
16+
{
17+
"href": "https://jamosova3.mystagingwebsite.com/wp-json/wc/v2/settings/general"
18+
}
19+
]
20+
}
21+
}

0 commit comments

Comments
 (0)