Skip to content

Commit 98442cb

Browse files
authored
Fix an issue with RemoteBlog failing to parse non-standard capabilities (#25474)
* Fix an issue with RemoteBlog failing to parse non-standard capabilities * Revert me-site-success change * Update unit tests
1 parent 68ff59f commit 98442cb

5 files changed

Lines changed: 23 additions & 5 deletions

File tree

Modules/Sources/WordPressKitModels/RemoteBlog.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import Foundation
5252
public var options: NSDictionary
5353

5454
/// Blog's capabilities: Indicate which actions are allowed / not allowed, for the current user.
55-
public var capabilities: [String: Bool]
55+
public var capabilities: [String: Any]
5656

5757
/// Blog's total disk quota space.
5858
public var quotaSpaceAllowed: NSNumber?
@@ -74,7 +74,7 @@ import Foundation
7474
self.jetpack = json.number(forKey: "jetpack")?.boolValue ?? false
7575
self.jetpackConnection = json.number(forKey: "jetpack_connection")?.boolValue ?? false
7676
self.icon = json.string(forKeyPath: "icon.img")
77-
self.capabilities = json.object(forKey: "capabilities") as? [String: Bool] ?? [:]
77+
self.capabilities = json.object(forKey: "capabilities") as? [String: Any] ?? [:]
7878
self.isAdmin = json.number(forKeyPath: "capabilities.manage_options")?.boolValue ?? false
7979
self.visible = json.number(forKey: "visible")?.boolValue ?? false
8080
self.options = RemoteBlogOptionsHelper.mapOptions(fromResponse: json)

Tests/KeystoneTests/Tests/Services/JetpackCapabilitiesServiceTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class JetpackCapabilitiesServiceTests: CoreDataTestCase {
2424
let service = JetpackCapabilitiesService(coreDataStack: contextManager, capabilitiesServiceRemote: remoteMock)
2525

2626
service.sync(blogs: [RemoteBlog.mock()], success: { blogs in
27-
XCTAssertTrue(blogs.first!.capabilities["backup"]!)
28-
XCTAssertTrue(blogs.first!.capabilities["scan"]!)
27+
XCTAssertTrue(blogs.first!.capabilities["backup"]! as! Bool)
28+
XCTAssertTrue(blogs.first!.capabilities["scan"]! as! Bool)
2929
expect.fulfill()
3030
})
3131

Tests/WordPressKitTests/WordPressKitTests/Mock Data/me-sites-success.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"URL": "https:\/\/test1.wordpress.com",
88
"user_can_manage": false,
99
"capabilities": {
10+
"vc_access_rules_post_types": "custom",
1011
"edit_pages": true,
1112
"edit_posts": true,
1213
"edit_others_posts": true,

Tests/WordPressKitTests/WordPressKitTests/Tests/AccountServiceRemoteRESTTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable {
143143
stubRemoteResponse(meSitesEndpoint, filename: getBlogsSuccessMockFilename, contentType: .ApplicationJSON)
144144
remote.getBlogsWithSuccess({ blogs in
145145
XCTAssertEqual(blogs?.count, 3, "There should be 3 blogs here")
146+
147+
guard let blog = blogs?.first as? RemoteBlog else {
148+
XCTFail("Blogs should not be empty")
149+
expect.fulfill()
150+
return
151+
}
152+
153+
XCTAssertEqual(blog.blogID, NSNumber(value: 55555551))
154+
XCTAssertEqual(blog.name, "Public Test Blog")
155+
XCTAssertEqual(blog.url, "https://test1.wordpress.com")
156+
157+
// Check some capabilities
158+
XCTAssertTrue(blog.capabilities["edit_pages"] as? Bool ?? false)
159+
XCTAssertTrue(blog.capabilities["edit_posts"] as? Bool ?? false)
160+
XCTAssertTrue(blog.capabilities["publish_posts"] as? Bool ?? false)
161+
XCTAssertEqual(blog.capabilities["vc_access_rules_post_types"] as? String, "custom", "Non-boolean capabilities are preserved and parsed correctly")
162+
146163
expect.fulfill()
147164
}, failure: { _ in
148165
XCTFail("This callback shouldn't get called")

WordPress/WordPressShareExtension/Sources/Extensions/RemoteBlog+Capabilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension RemoteBlog {
2525
/// Returns true if a given capability is enabled. False otherwise
2626
///
2727
public func isUserCapableOf(_ capability: Capability) -> Bool {
28-
return capabilities[capability.rawValue, default: false]
28+
return capabilities[capability.rawValue] as? Bool ?? false
2929
}
3030

3131
/// Returns true if the current user is allowed to publish to the Blog

0 commit comments

Comments
 (0)