-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCustomPostEditorServiceTests.swift
More file actions
258 lines (219 loc) · 8.74 KB
/
CustomPostEditorServiceTests.swift
File metadata and controls
258 lines (219 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import Testing
import Foundation
import WordPressAPI
import WordPressAPIInternal
@testable import WordPress
@testable import WordPressCore
@testable import WordPressData
@MainActor
struct CustomPostEditorServiceTests {
// MARK: - New Post Tests
@Test("applyLocally updates PostCreateParams for new posts")
func applyLocallyUpdatesCreateParamsForNewPost() throws {
// Given
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let service = try makeService(blog: blog, post: nil)
var settings = service.settings
settings.slug = "custom-slug"
settings.excerpt = "Custom excerpt"
// When
service.applyLocally(settings: settings)
// Then: the settings property should reflect the applied changes
#expect(service.settings.slug == "custom-slug")
#expect(service.settings.excerpt == "Custom excerpt")
#expect(service.inspectPendingSettings() == nil) // new posts don't use pendingSettings
}
// MARK: - Existing Post Tests
@Test("applyLocally stores pendingSettings for existing posts")
func applyLocallyStoresPendingSettingsForExistingPost() throws {
// Given
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let post = makeRemotePost()
let service = try makeService(blog: blog, post: post)
var settings = service.settings
settings.slug = "updated-slug"
// When
service.applyLocally(settings: settings)
// Then
#expect(service.inspectPendingSettings() != nil)
#expect(service.inspectPendingSettings()?.slug == "updated-slug")
}
@Test("settings returns pendingSettings when set")
func settingsReturnsPendingSettingsWhenSet() throws {
// Given
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let post = makeRemotePost()
let service = try makeService(blog: blog, post: post)
var settings = service.settings
settings.slug = "pending-slug"
// When
service.applyLocally(settings: settings)
// Then: the settings property should return the pending settings
#expect(service.settings.slug == "pending-slug")
}
@Test("settings returns original settings when no pendingSettings")
func settingsReturnsOriginalWhenNoPendingSettings() throws {
// Given
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let post = makeRemotePost()
let service = try makeService(blog: blog, post: post)
// Then: pendingSettings should be nil and settings should come from the post
#expect(service.inspectPendingSettings() == nil)
#expect(service.settings.slug == "test-post")
}
// MARK: - hasSettingsChanges Tests
@Test("hasSettingsChanges returns false when no changes made to existing post")
func hasSettingsChangesReturnsFalseForUnmodifiedExistingPost() throws {
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let post = makeRemotePost()
let service = try makeService(blog: blog, post: post)
#expect(service.hasSettingsChanges == false)
}
@Test("hasSettingsChanges returns true after applying different settings to existing post")
func hasSettingsChangesReturnsTrueAfterApplyingSettingsToExistingPost() throws {
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let post = makeRemotePost()
let service = try makeService(blog: blog, post: post)
var settings = service.settings
settings.slug = "changed-slug"
service.applyLocally(settings: settings)
#expect(service.hasSettingsChanges == true)
}
@Test("hasSettingsChanges returns false after reverting settings to original on existing post")
func hasSettingsChangesReturnsFalseAfterRevertingExistingPost() throws {
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let post = makeRemotePost()
let service = try makeService(blog: blog, post: post)
let original = service.settings
var modified = original
modified.slug = "changed-slug"
service.applyLocally(settings: modified)
#expect(service.hasSettingsChanges == true)
// Revert
service.applyLocally(settings: original)
#expect(service.hasSettingsChanges == false)
}
@Test("hasSettingsChanges returns false for unmodified new post")
func hasSettingsChangesReturnsFalseForUnmodifiedNewPost() throws {
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let service = try makeService(blog: blog, post: nil)
#expect(service.hasSettingsChanges == false)
}
@Test("hasSettingsChanges returns true after applying different settings to new post")
func hasSettingsChangesReturnsTrueAfterApplyingSettingsToNewPost() throws {
let context = ContextManager.forTesting().mainContext
let blog = BlogBuilder(context).build()
let service = try makeService(blog: blog, post: nil)
var settings = service.settings
settings.slug = "new-slug"
service.applyLocally(settings: settings)
#expect(service.hasSettingsChanges == true)
}
}
// MARK: - Test Helpers
private func makeService(
blog: Blog,
post: AnyPostWithEditContext?
) throws -> CustomPostEditorService {
let api = try WordPressAPI(
urlSession: .shared,
siteInfo: .selfHosted(
siteUrl: .parse(input: "https://example.com"),
apiRoot: .parse(input: "https://example.com/wp-json")
),
authentication: .none
)
let client = WordPressClient(
api: api,
siteURL: URL(string: "https://example.com")!
)
let wpService = try api.createSelfHostedService(cache: .bootstrap())
return CustomPostEditorService(
blog: blog,
post: post,
details: makePostTypeDetails(),
client: client,
wpService: wpService
)
}
private func makePostTypeDetails() -> PostTypeDetailsWithEditContext {
PostTypeDetailsWithEditContext(
capabilities: [:],
description: "",
hierarchical: false,
viewable: true,
labels: makePostTypeLabels(),
name: "Test Post Type",
slug: "test_post_type",
supports: PostTypeSupportsMap(map: [
.title: .bool(true),
.editor: .bool(true),
]),
hasArchive: .bool(false),
taxonomies: [],
restBase: "test_post_type",
restNamespace: "wp/v2",
visibility: PostTypeVisibility(showInNavMenus: true, showUi: true),
icon: nil
)
}
private func makePostTypeLabels() -> PostTypeLabels {
PostTypeLabels(
name: "", singularName: "", addNew: "", addNewItem: "",
editItem: "", newItem: "", viewItem: "", viewItems: "",
searchItems: "", notFound: "", notFoundInTrash: "",
parentItemColon: nil, allItems: "", archives: "",
attributes: "", insertIntoItem: "", uploadedToThisItem: "",
featuredImage: "", setFeaturedImage: "", removeFeaturedImage: "",
useFeaturedImage: "", filterItemsList: "", filterByDate: "",
itemsListNavigation: "", itemsList: "", itemPublished: "",
itemPublishedPrivately: "", itemRevertedToDraft: "",
itemTrashed: "", itemScheduled: "", itemUpdated: "",
itemLink: "", itemLinkDescription: "", menuName: "",
nameAdminBar: ""
)
}
private func makeRemotePost(
tags: [TermId]? = nil,
categories: [TermId]? = nil
) -> AnyPostWithEditContext {
AnyPostWithEditContext(
id: PostId(1),
date: "2025-01-01T00:00:00",
dateGmt: Date(timeIntervalSince1970: 0),
guid: PostGuidWithEditContext(raw: nil, rendered: ""),
link: "https://example.com",
modified: "2025-01-01T00:00:00",
modifiedGmt: Date(timeIntervalSince1970: 0),
slug: "test-post",
status: .draft,
postType: "post",
password: nil,
permalinkTemplate: nil,
generatedSlug: nil,
title: nil,
content: PostContentWithEditContext(raw: nil, rendered: "", protected: nil, blockVersion: nil),
author: nil,
excerpt: nil,
featuredMedia: nil,
commentStatus: .open,
pingStatus: .open,
format: nil,
meta: nil,
sticky: nil,
template: "",
categories: categories,
tags: tags,
parent: nil,
menuOrder: nil,
additionalFields: nil
)
}