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

Commit 1d1a48a

Browse files
authored
Merge pull request #502 from wordpress-mobile/feature/18375-fix_prompt_date
Blogging Prompts: fix prompt date
2 parents d4bfabc + 463b4f0 commit 1d1a48a

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

WordPressKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pod::Spec.new do |s|
44
s.name = 'WordPressKit'
5-
s.version = '4.52.0-beta.1'
5+
s.version = '4.52.0-beta.2'
66

77
s.summary = 'WordPressKit offers a clean and simple WordPress.com and WordPress.org API.'
88
s.description = <<-DESC

WordPressKit/RemoteBloggingPrompt.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ extension RemoteBloggingPrompt: Decodable {
2525
case answeredUserAvatarURLs = "answered_users_sample"
2626
}
2727

28+
/// Used to format the fetched object's date string to a date.
29+
private static var dateFormatter: DateFormatter = {
30+
let formatter = DateFormatter()
31+
formatter.locale = .init(identifier: "en_US_POSIX")
32+
formatter.timeZone = .init(secondsFromGMT: 0)
33+
formatter.dateFormat = "yyyy-MM-dd"
34+
return formatter
35+
}()
36+
2837
public init(from decoder: Decoder) throws {
2938
let container = try decoder.container(keyedBy: CodingKeys.self)
3039

@@ -34,7 +43,7 @@ extension RemoteBloggingPrompt: Decodable {
3443
self.content = try container.decode(String.self, forKey: .content)
3544
self.attribution = try container.decode(String.self, forKey: .attribution)
3645
self.answered = try container.decode(Bool.self, forKey: .answered)
37-
self.date = try container.decode(Date.self, forKey: .date)
46+
self.date = Self.dateFormatter.date(from: try container.decode(String.self, forKey: .date)) ?? Date()
3847
self.answeredUsersCount = try container.decode(Int.self, forKey: .answeredUsersCount)
3948

4049
let userAvatars = try container.decode([UserAvatar].self, forKey: .answeredUserAvatarURLs)

WordPressKitTests/BloggingPromptsServiceRemoteTests.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import XCTest
55
class BloggingPromptsServiceRemoteTests: RemoteTestCase, RESTTestable {
66

77
private let siteID = NSNumber(value: 1)
8-
private let dateFormatter = JSONDecoder.DateDecodingStrategy.DateFormat.noTime.formatter
8+
private let dateFormatter: DateFormatter = {
9+
let formatter = DateFormatter()
10+
formatter.locale = .init(identifier: "en_US_POSIX")
11+
formatter.dateFormat = "yyyy-MM-dd"
12+
return formatter
13+
}()
914

1015
var mockApi: WordPressComRestApi!
1116
var service: BloggingPromptsServiceRemote!
@@ -29,8 +34,6 @@ class BloggingPromptsServiceRemoteTests: RemoteTestCase, RESTTestable {
2934
// MARK: Tests
3035

3136
func test_fetchPrompts_returnsRemotePrompts() {
32-
let formatter = JSONDecoder.DateDecodingStrategy.DateFormat.noTime.formatter
33-
let expectedDate = formatter.date(from: "2022-01-01") // using value from the first prompt in blogging-prompts-success.json.
3437
let expectedAvatarURLString = "https://0.gravatar.com/avatar/example?s=96&d=identicon&r=G"
3538
stubRemoteResponse(.bloggingPromptsEndpoint, filename: .mockFileName, contentType: .ApplicationJSON)
3639

@@ -49,7 +52,12 @@ class BloggingPromptsServiceRemoteTests: RemoteTestCase, RESTTestable {
4952
XCTAssertEqual(firstPrompt.title, "Prompt number 1")
5053
XCTAssertEqual(firstPrompt.content, "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->")
5154
XCTAssertEqual(firstPrompt.attribution, "dayone")
52-
XCTAssertEqual(firstPrompt.date, expectedDate)
55+
56+
let firstDateComponents = firstPrompt.date.dateAndTimeComponents()
57+
XCTAssertEqual(firstDateComponents.year!, 2022)
58+
XCTAssertEqual(firstDateComponents.month!, 5)
59+
XCTAssertEqual(firstDateComponents.day!, 3)
60+
5361
XCTAssertFalse(firstPrompt.answered)
5462
XCTAssertEqual(firstPrompt.answeredUsersCount, 0)
5563

@@ -58,6 +66,11 @@ class BloggingPromptsServiceRemoteTests: RemoteTestCase, RESTTestable {
5866
XCTAssertEqual(secondPrompt.answeredUserAvatarURLs.count, 1)
5967
XCTAssertTrue(secondPrompt.attribution.isEmpty)
6068

69+
let secondDateComponents = secondPrompt.date.dateAndTimeComponents()
70+
XCTAssertEqual(secondDateComponents.year!, 2021)
71+
XCTAssertEqual(secondDateComponents.month!, 9)
72+
XCTAssertEqual(secondDateComponents.day!, 12)
73+
6174
let avatarURL = secondPrompt.answeredUserAvatarURLs.first!
6275
XCTAssertEqual(avatarURL.absoluteString, expectedAvatarURLString)
6376

WordPressKitTests/Mock Data/blogging-prompts-success.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"title": "Prompt number 1",
77
"content": "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->",
88
"attribution": "dayone",
9-
"date": "2022-01-01",
9+
"date": "2022-05-03",
1010
"answered": false,
1111
"answered_users_count": 0,
1212
"answered_users_sample": []
@@ -17,7 +17,7 @@
1717
"title": "Prompt number 10",
1818
"content": "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Tell us about a time when you felt out of place.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->",
1919
"attribution": "",
20-
"date": "2022-01-02",
20+
"date": "2021-09-12",
2121
"answered": false,
2222
"answered_users_count": 1,
2323
"answered_users_sample": [

0 commit comments

Comments
 (0)