-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments: add REST endpoint to fetch a single comment #486
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7341efc
Add method to fetch a single comment with ID.
ScoutHarris c0754b3
Add tests for fetching a single comment.
ScoutHarris 2bfea2b
Bump pod version.
ScoutHarris 26b975b
Fix whitespace.
ScoutHarris 11c2944
Give the error param a name.
ScoutHarris 3835103
Fix whitespace.
ScoutHarris c177bc8
Merge branch 'trunk' into feature/17790-fetch_single_comment
ScoutHarris a580849
Bump pod version.
ScoutHarris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,28 @@ - (NSString *)parameterForCommentStatus:(NSNumber *)status | |
} | ||
} | ||
|
||
- (void)getCommentWithID:(NSNumber *)commentID | ||
success:(void (^)(RemoteComment *comment))success | ||
failure:(void (^)(NSError *))failure | ||
{ | ||
NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@", self.siteID, commentID]; | ||
NSString *requestUrl = [self pathForEndpoint:path | ||
withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1]; | ||
|
||
[self.wordPressComRestApi GET:requestUrl | ||
parameters:nil | ||
success:^(id responseObject, NSHTTPURLResponse *httpResponse) { | ||
RemoteComment *comment = [self remoteCommentFromJSONDictionary:responseObject]; | ||
if (success) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a little more concise as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in ObjC. 😄 |
||
success(comment); | ||
} | ||
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) { | ||
if (failure) { | ||
failure(error); | ||
} | ||
}]; | ||
} | ||
|
||
- (void)createComment:(RemoteComment *)comment | ||
success:(void (^)(RemoteComment *comment))success | ||
failure:(void (^)(NSError *))failure | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,19 @@ import XCTest | |
|
||
final class CommentServiceRemoteRESTTests: RemoteTestCase, RESTTestable { | ||
private let fetchCommentsSuccessFilename = "site-comments-success.json" | ||
private let fetchCommentSuccessFilename = "site-comment-success.json" | ||
private let siteId = 0 | ||
private let commentId = 1 | ||
private var remote: CommentServiceRemoteREST! | ||
|
||
private var siteCommentsEndpoint: String { | ||
return "sites/\(siteId)/comments" | ||
} | ||
|
||
private var siteCommentEndpoint: String { | ||
return "sites/\(siteId)/comments/\(commentId)" | ||
} | ||
|
||
override func setUp() { | ||
super.setUp() | ||
remote = CommentServiceRemoteREST(wordPressComRestApi: getRestApi(), siteID: NSNumber(value: siteId)) | ||
|
@@ -64,4 +70,44 @@ final class CommentServiceRemoteRESTTests: RemoteTestCase, RESTTestable { | |
|
||
waitForExpectations(timeout: timeout, handler: nil) | ||
} | ||
|
||
func testGetSingleCommentSucceeds() { | ||
let expect = expectation(description: "Fetching a single site comment should succeed") | ||
|
||
stubRemoteResponse(siteCommentEndpoint, | ||
filename: fetchCommentSuccessFilename, | ||
contentType: .ApplicationJSON) | ||
|
||
remote.getCommentWithID(NSNumber(value: commentId), | ||
success: { comment in | ||
|
||
guard let comment = comment else { | ||
XCTFail("Failed to retrieve mock site comment") | ||
return | ||
} | ||
|
||
XCTAssertEqual(comment.authorID, NSNumber(value: 12345)) | ||
XCTAssertEqual(comment.author, "Comment Author") | ||
XCTAssertEqual(comment.authorEmail, "[email protected]") | ||
XCTAssertEqual(comment.authorUrl, "author URL") | ||
XCTAssertEqual(comment.authorIP, "000.0.00.000") | ||
XCTAssertEqual(comment.date, NSDate(wordPressComJSONString: "2021-08-04T07:58:49+00:00") as Date) | ||
XCTAssertEqual(comment.link, "comment URL") | ||
XCTAssertEqual(comment.parentID, nil) | ||
XCTAssertEqual(comment.postID, NSNumber(value: 1)) | ||
XCTAssertEqual(comment.postTitle, "Post title") | ||
XCTAssertEqual(comment.status, "approve") | ||
XCTAssertEqual(comment.type, "comment") | ||
XCTAssertEqual(comment.isLiked, false) | ||
XCTAssertEqual(comment.likeCount, NSNumber(value: 0)) | ||
XCTAssertEqual(comment.canModerate, true) | ||
XCTAssertEqual(comment.content, "I am comment content") | ||
XCTAssertEqual(comment.rawContent, "I am comment raw content") | ||
expect.fulfill() | ||
}, failure: { _ in | ||
XCTFail("This callback shouldn't get called") | ||
}) | ||
|
||
waitForExpectations(timeout: timeout, handler: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import wpxmlrpc | |
class CommentServiceRemoteXMLRPCTests: RemoteTestCase, XMLRPCTestable { | ||
private var remote: Any? | ||
private let getSiteCommentsSuccessMockFilename = "xmlrpc-site-comments-success.xml" | ||
private let getSiteCommentSuccessMockFilename = "xmlrpc-site-comment-success.xml" | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
@@ -59,4 +60,41 @@ class CommentServiceRemoteXMLRPCTests: RemoteTestCase, XMLRPCTestable { | |
} | ||
} | ||
|
||
func testGetSingleCommentSucceeds() { | ||
let expect = expectation(description: "Fetching a single site comment should succeed") | ||
|
||
stubRemoteResponse(XMLRPCTestableConstants.xmlRpcUrl, | ||
filename: getSiteCommentSuccessMockFilename, | ||
contentType: .XML) | ||
|
||
if let remoteInstance = remote as? CommentServiceRemote { | ||
|
||
remoteInstance.getCommentWithID(NSNumber(value: 1), | ||
success: { comment in | ||
|
||
guard let comment = comment else { | ||
XCTFail("Failed to retrieve mock site comment") | ||
return | ||
} | ||
|
||
XCTAssertEqual(comment.author, "Comment Author") | ||
XCTAssertEqual(comment.authorEmail, "[email protected]") | ||
XCTAssertEqual(comment.authorUrl, "author URL") | ||
XCTAssertEqual(comment.authorIP, "000.0.00.000") | ||
XCTAssertEqual(comment.link, "comment URL") | ||
XCTAssertEqual(comment.parentID, NSNumber(value: 1)) | ||
XCTAssertEqual(comment.postID, NSNumber(value: 2)) | ||
XCTAssertEqual(comment.postTitle, "Post title") | ||
XCTAssertEqual(comment.status, "approve") | ||
XCTAssertEqual(comment.type, "comment") | ||
XCTAssertEqual(comment.content, "I am comment content") | ||
XCTAssertEqual(comment.rawContent, nil) | ||
expect.fulfill() | ||
}, failure: { _ in | ||
XCTFail("This callback shouldn't get called") | ||
}) | ||
|
||
waitForExpectations(timeout: timeout, handler: nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"ID": 1, | ||
"post": { | ||
"ID": 1, | ||
"title": "Post title", | ||
"type": "post", | ||
"link": "post URL" | ||
}, | ||
"author": { | ||
"ID": 12345, | ||
"login": "", | ||
"email": "[email protected]", | ||
"name": "Comment Author", | ||
"first_name": "", | ||
"last_name": "", | ||
"nice_name": "", | ||
"URL": "author URL", | ||
"avatar_URL": "avatar URL", | ||
"profile_URL": "profile URL", | ||
"ip_address": "000.0.00.000" | ||
}, | ||
"date": "2021-08-04T07:58:49+00:00", | ||
"URL": "comment URL", | ||
"short_URL": "short URL", | ||
"content": "I am comment content", | ||
"raw_content": "I am comment raw content", | ||
"status": "approved", | ||
"parent": false, | ||
"type": "comment", | ||
"like_count": 0, | ||
"i_like": false, | ||
"meta": { | ||
"links": { | ||
} | ||
}, | ||
"can_moderate": true, | ||
"i_replied": false | ||
} |
25 changes: 25 additions & 0 deletions
25
WordPressKitTests/Mock Data/xmlrpc-site-comment-success.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<methodResponse> | ||
<params> | ||
<param> | ||
<value> | ||
<struct> | ||
<member><name>date_created_gmt</name><value><dateTime.iso8601>20210804T21:01:08</dateTime.iso8601></value></member> | ||
<member><name>user_id</name><value><string>1</string></value></member> | ||
<member><name>comment_id</name><value><string>1</string></value></member> | ||
<member><name>parent</name><value><string>1</string></value></member> | ||
<member><name>status</name><value><string>approve</string></value></member> | ||
<member><name>content</name><value><string>I am comment content</string></value></member> | ||
<member><name>link</name><value><string>comment URL</string></value></member> | ||
<member><name>post_id</name><value><string>2</string></value></member> | ||
<member><name>post_title</name><value><string>Post title</string></value></member> | ||
<member><name>author</name><value><string>Comment Author</string></value></member> | ||
<member><name>author_url</name><value><string>author URL</string></value></member> | ||
<member><name>author_email</name><value><string>[email protected]</string></value></member> | ||
<member><name>author_ip</name><value><string>000.0.00.000</string></value></member> | ||
<member><name>type</name><value><string>comment</string></value></member> | ||
</struct> | ||
</value> | ||
</param> | ||
</params> | ||
</methodResponse> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error could have a name here to match other declarations like the one above (same for the method implementation in the .m)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woops. Fixed!