Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 54ca822

Browse files
committed
Add test for RemoteBlogSettings encoding to dictionary
1 parent 18069a2 commit 54ca822

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

Sources/WordPressKit/Services/BlogServiceRemoteREST.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ typedef void (^SettingsHandler)(RemoteBlogSettings *settings);
6868

6969
// FIXME: Temporary public for testing. Will remove once the logic moves to Swift
7070
- (RemoteBlogSettings *)remoteBlogSettingFromJSONDictionary:(NSDictionary *)json;
71+
- (NSDictionary *)remoteSettingsToDictionary:(RemoteBlogSettings *)settings;
7172

7273
@end

Tests/WordPressKitTests/Tests/RemoteBlogSettingsTests.swift

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import XCTest
44
final class RemoteBlogSettingsTests: XCTestCase {
55

66
func testInitWithJSON() throws {
7-
let jsonPath = try XCTUnwrap(
8-
Bundle(for: type(of: self))
9-
.path(forResource: "rest-site-settings", ofType: "json")
10-
)
11-
let json = try XCTUnwrap(JSONLoader().loadFile(jsonPath))
7+
let json = try loadJSONSettings()
128
// FIXME: The init logic is currently in BlogServiceRemoteREST. We'll test it from there first, then update the test with the new location.
139
let blogService = BlogServiceRemoteREST(wordPressComRestApi: MockWordPressComRestApi(), siteID: 0)
1410
let settings = try XCTUnwrap(blogService.remoteBlogSetting(fromJSONDictionary: json))
@@ -27,4 +23,38 @@ final class RemoteBlogSettingsTests: XCTestCase {
2723
// Note that here we're obviously testing only one of the possible paths.
2824
XCTAssertEqual(settings.defaultPostFormat, "standard")
2925
}
26+
27+
func testToDictionary() throws {
28+
// Rather than creating an object and checking the resulting NSDictionary,
29+
// let's load one, convert it, then compare the source and converted dictionaries
30+
let json = try loadJSONSettings()
31+
// FIXME: The init logic is currently in BlogServiceRemoteREST. We'll test it from there first, then update the test with the new location.
32+
let blogService = BlogServiceRemoteREST(wordPressComRestApi: MockWordPressComRestApi(), siteID: 0)
33+
let settings = try XCTUnwrap(blogService.remoteBlogSetting(fromJSONDictionary: json))
34+
35+
let dictionary = try XCTUnwrap(blogService.remoteSettings(toDictionary: settings))
36+
37+
// name and tagline have different keys when encoded...
38+
XCTAssertEqual(dictionary["blogname"] as? String, settings.name) // from JSON this is "name"
39+
XCTAssertEqual(dictionary["blogdescription"] as? String, settings.tagline) // from JSON this is "description"
40+
// Flattened settings properties
41+
XCTAssertEqual(dictionary["blog_public"] as? NSNumber, settings.privacy)
42+
XCTAssertEqual(dictionary["lang_id"] as? NSNumber, settings.languageID)
43+
XCTAssertEqual(dictionary["site_icon"] as? NSNumber, settings.iconMediaID)
44+
XCTAssertEqual(dictionary["gmt_offset"] as? NSNumber, settings.gmtOffset)
45+
// And so on...
46+
47+
// defaultPostFormat has custom encoding, so let's test it explicitly.
48+
// Note that here we're obviously testing only one of the possible paths.
49+
XCTAssertEqual(dictionary["default_post_format"] as? String, settings.defaultPostFormat)
50+
}
51+
52+
func loadJSONSettings() throws -> [String: Any] {
53+
let jsonPath = try XCTUnwrap(
54+
Bundle(for: type(of: self))
55+
.path(forResource: "rest-site-settings", ofType: "json")
56+
)
57+
let json = try XCTUnwrap(JSONLoader().loadFile(jsonPath))
58+
return json
59+
}
3060
}

0 commit comments

Comments
 (0)