Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Modules/Sources/WordPressKitObjC/TaxonomyServiceRemoteREST.m
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ - (void)updateTaxonomyWithType:(NSString *)typeIdentifier
{
return [jsonArray wp_map:^id(NSDictionary *jsonCategory) {
return [self remoteCategoryWithJSONDictionary:jsonCategory];
}];
}] ?: @[];
}

- (RemotePostCategory *)remoteCategoryWithJSONDictionary:(NSDictionary *)jsonCategory
Expand All @@ -303,7 +303,7 @@ - (RemotePostCategory *)remoteCategoryWithJSONDictionary:(NSDictionary *)jsonCat
{
return [jsonArray wp_map:^id(NSDictionary *jsonTag) {
return [self remoteTagWithJSONDictionary:jsonTag];
}];
}] ?: @[];
}

- (RemotePostTag *)remoteTagWithJSONDictionary:(NSDictionary *)jsonTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ - (void)testThatSearchCategoriesWithNameWorks
failure:^(NSError * __unused error) {}];
}

/// A response missing the `categories` key must yield an empty array, not `nil`
/// (a `nil` `NSArray` traps when bridged to a non-optional Swift array).
- (void)testThatGetCategoriesWithMissingCategoriesKeyReturnsEmptyArray
{
NSString *url = [self GETtaxonomyURLWithType:@"categories"];

id<WordPressComRESTAPIInterfacing> api = self.service.wordPressComRESTAPI;
NSDictionary *json = @{ @"found": @0 };
NSHTTPURLResponse *response = OCMStrictClassMock([NSHTTPURLResponse class]);
OCMStub([api get:[OCMArg isEqual:url]
parameters:[OCMArg any]
success:([OCMArg invokeBlockWithArgs:json, response, nil])
failure:[OCMArg isNotNil]]);

XCTestExpectation *gotEmptyArray = [self expectationWithDescription:@"categories should be empty"];
id success = ^(NSArray<RemotePostCategory *> * _Nonnull categories) {
XCTAssertNotNil(categories);
XCTAssertEqualObjects(categories, @[]);
[gotEmptyArray fulfill];
};
[self.service getCategoriesWithSuccess:success
failure:^(NSError * __unused error) { XCTFail(@"should not fail"); }];

[self waitForExpectations:@[gotEmptyArray] timeout:0.1];
}

#pragma mark - Tags

- (void)testThatCreateTagWorks
Expand Down Expand Up @@ -315,4 +341,30 @@ - (void)testThatSearchTagsWithNameWorks
failure:^(NSError * __unused error) {}];
}

/// A response missing the `tags` key must yield an empty array, not `nil`
/// (a `nil` `NSArray` traps when bridged to a non-optional Swift array).
- (void)testThatGetTagsWithMissingTagsKeyReturnsEmptyArray
{
NSString *url = [self GETtaxonomyURLWithType:@"tags"];

id<WordPressComRESTAPIInterfacing> api = self.service.wordPressComRESTAPI;
NSDictionary *json = @{ @"found": @0 };
NSHTTPURLResponse *response = OCMStrictClassMock([NSHTTPURLResponse class]);
OCMStub([api get:[OCMArg isEqual:url]
parameters:[OCMArg any]
success:([OCMArg invokeBlockWithArgs:json, response, nil])
failure:[OCMArg isNotNil]]);

XCTestExpectation *gotEmptyArray = [self expectationWithDescription:@"tags should be empty"];
id success = ^(NSArray<RemotePostTag *> * _Nonnull tags) {
XCTAssertNotNil(tags);
XCTAssertEqualObjects(tags, @[]);
[gotEmptyArray fulfill];
};
[self.service getTagsWithSuccess:success
failure:^(NSError * __unused error) { XCTFail(@"should not fail"); }];

[self waitForExpectations:@[gotEmptyArray] timeout:0.1];
}

@end