Skip to content

Commit 29a6121

Browse files
committed
Duplicate test infra in WordPressData in order to run BlogTests
1 parent 964f835 commit 29a6121

File tree

6 files changed

+869
-0
lines changed

6 files changed

+869
-0
lines changed
+310
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
import XCTest
2+
@testable import WordPressData
3+
4+
final class BlogTests: CoreDataTestCase {
5+
6+
// MARK: - Atomic Tests
7+
func testIsAtomic() {
8+
let blog = BlogBuilder(mainContext)
9+
.with(atomic: true)
10+
.build()
11+
12+
XCTAssertTrue(blog.isAtomic())
13+
}
14+
15+
func testIsNotAtomic() {
16+
let blog = BlogBuilder(mainContext)
17+
.with(atomic: false)
18+
.build()
19+
20+
XCTAssertFalse(blog.isAtomic())
21+
}
22+
23+
// MARK: - Blog Lookup
24+
func testThatLookupByBlogIDWorks() throws {
25+
let blog = BlogBuilder(mainContext).build()
26+
XCTAssertNotNil(blog.dotComID)
27+
XCTAssertNotNil(Blog.lookup(withID: blog.dotComID!, in: mainContext))
28+
}
29+
30+
func testThatLookupByBlogIDFailsForInvalidBlogID() throws {
31+
XCTAssertNil(Blog.lookup(withID: NSNumber(integerLiteral: 1), in: mainContext))
32+
}
33+
34+
func testThatLookupByBlogIDWorksForIntegerBlogID() throws {
35+
let blog = BlogBuilder(mainContext).build()
36+
XCTAssertNotNil(blog.dotComID)
37+
XCTAssertNotNil(try Blog.lookup(withID: blog.dotComID!.intValue, in: mainContext))
38+
}
39+
40+
func testThatLookupByBlogIDFailsForInvalidIntegerBlogID() throws {
41+
XCTAssertNil(try Blog.lookup(withID: 1, in: mainContext))
42+
}
43+
44+
func testThatLookupBlogIDWorksForInt64BlogID() throws {
45+
let blog = BlogBuilder(mainContext).build()
46+
XCTAssertNotNil(blog.dotComID)
47+
XCTAssertNotNil(try Blog.lookup(withID: blog.dotComID!.int64Value, in: mainContext))
48+
}
49+
50+
func testThatLookupByBlogIDFailsForInvalidInt64BlogID() throws {
51+
XCTAssertNil(try Blog.lookup(withID: Int64(1), in: mainContext))
52+
}
53+
54+
// MARK: - Post lookup
55+
func testThatLookupPostWorks() {
56+
let context = contextManager.newDerivedContext()
57+
let blog = BlogBuilder(context)
58+
.set(blogOption: "foo", value: "bar")
59+
.build()
60+
let post = PostBuilder(context, blog: blog).build()
61+
post.postID = NSNumber(value: Int64.max)
62+
contextManager.saveContextAndWait(context)
63+
64+
XCTAssertIdentical(blog.lookupPost(withID: post.postID!, in: mainContext)?.managedObjectContext, mainContext)
65+
XCTAssertIdentical(blog.lookupPost(withID: post.postID!, in: context)?.managedObjectContext, context)
66+
}
67+
68+
// MARK: - Plugin Management
69+
func testThatPluginManagementIsDisabledForSimpleSites() {
70+
let blog = BlogBuilder(mainContext)
71+
.with(atomic: true)
72+
.build()
73+
74+
XCTAssertFalse(blog.supports(.pluginManagement))
75+
}
76+
77+
func testThatPluginManagementIsEnabledForBusinessPlans() {
78+
let blog = BlogBuilder(mainContext)
79+
.with(isHostedAtWPCom: true)
80+
.with(planID: 1008) // Business plan
81+
.with(isAdmin: true)
82+
.build()
83+
84+
XCTAssertTrue(blog.supports(.pluginManagement))
85+
}
86+
87+
func testThatPluginManagementIsDisabledForPrivateSites() {
88+
let blog = BlogBuilder(mainContext)
89+
.with(isHostedAtWPCom: true)
90+
.with(planID: 1008) // Business plan
91+
.with(isAdmin: true)
92+
.with(siteVisibility: .private)
93+
.build()
94+
95+
XCTAssertTrue(blog.supports(.pluginManagement))
96+
}
97+
98+
// FIXME: Crashes because WPAccount fixture sets username and triggers BuildSettings access
99+
// func testThatPluginManagementIsEnabledForJetpack() {
100+
// let blog = BlogBuilder(mainContext)
101+
// .withAnAccount()
102+
// .withJetpack(version: "5.6", username: "test_user", email: "[email protected]")
103+
// .with(isHostedAtWPCom: false)
104+
// .with(isAdmin: true)
105+
// .build()
106+
//
107+
// XCTAssertTrue(blog.supports(.pluginManagement))
108+
// }
109+
110+
func testThatPluginManagementIsDisabledForWordPress54AndBelow() {
111+
let blog = BlogBuilder(mainContext)
112+
.with(wordPressVersion: "5.4")
113+
.with(username: "test_username")
114+
.with(password: "test_password")
115+
.with(isAdmin: true)
116+
.build()
117+
118+
XCTAssertFalse(blog.supports(.pluginManagement))
119+
}
120+
121+
func testThatPluginManagementIsEnabledForWordPress55AndAbove() {
122+
let blog = BlogBuilder(mainContext)
123+
.with(wordPressVersion: "5.5")
124+
.with(username: "test_username")
125+
.with(password: "test_password")
126+
.with(isAdmin: true)
127+
.build()
128+
129+
XCTExpectFailure("Fails because it gets a nil WordPressOrgRestApi instance", strict: true)
130+
XCTAssertTrue(blog.supports(.pluginManagement))
131+
}
132+
133+
func testThatPluginManagementIsDisabledForNonAdmins() {
134+
let blog = BlogBuilder(mainContext)
135+
.with(wordPressVersion: "5.5")
136+
.with(username: "test_username")
137+
.with(password: "test_password")
138+
.with(isAdmin: false)
139+
.build()
140+
141+
XCTAssertFalse(blog.supports(.pluginManagement))
142+
}
143+
144+
func testStatsActiveForSitesHostedAtWPCom() {
145+
let blog = BlogBuilder(mainContext)
146+
.isHostedAtWPcom()
147+
.with(modules: [""])
148+
.build()
149+
150+
XCTAssertTrue(blog.isStatsActive())
151+
}
152+
153+
func testStatsActiveForSitesNotHotedAtWPCom() {
154+
let blog = BlogBuilder(mainContext)
155+
.isNotHostedAtWPcom()
156+
.with(modules: ["stats"])
157+
.build()
158+
159+
XCTAssertTrue(blog.isStatsActive())
160+
}
161+
162+
func testStatsNotActiveForSitesNotHotedAtWPCom() {
163+
let blog = BlogBuilder(mainContext)
164+
.isNotHostedAtWPcom()
165+
.with(modules: [""])
166+
.build()
167+
168+
XCTAssertFalse(blog.isStatsActive())
169+
}
170+
171+
// MARK: - Blog.version string conversion testing
172+
func testTheVersionIsAStringWhenGivenANumber() {
173+
let blog = BlogBuilder(mainContext)
174+
.set(blogOption: "software_version", value: 13.37)
175+
.build()
176+
177+
XCTAssertTrue((blog.version as Any) is String)
178+
XCTAssertEqual(blog.version, "13.37")
179+
}
180+
181+
func testTheVersionIsAStringWhenGivenAString() {
182+
let blog = BlogBuilder(mainContext)
183+
.set(blogOption: "software_version", value: "5.5")
184+
.build()
185+
186+
XCTAssertTrue((blog.version as Any) is String)
187+
XCTAssertEqual(blog.version, "5.5")
188+
}
189+
190+
func testTheVersionDefaultsToAnEmptyStringWhenTheValueIsNotConvertible() {
191+
let blog = BlogBuilder(mainContext)
192+
.set(blogOption: "software_version", value: NSObject())
193+
.build()
194+
195+
XCTAssertTrue((blog.version as Any) is String)
196+
XCTAssertEqual(blog.version, "")
197+
}
198+
199+
// FIXME: Crashes because WPAccount fixture sets username and triggers BuildSettings access
200+
// func testRemoveDuplicates() async throws {
201+
// // Create an account with duplicated blogs
202+
// let xmlrpc = "https://xmlrpc.test.wordpress.com"
203+
// let account = try await contextManager.performAndSave { context in
204+
// let account = WPAccount.fixture(context: context)
205+
// account.blogs = Set(
206+
// (1...10).map { _ in
207+
// let blog = BlogBuilder(context).build()
208+
// blog.xmlrpc = xmlrpc
209+
// return blog
210+
// }
211+
// )
212+
// return account
213+
// }
214+
// try XCTAssertEqual(mainContext.count(for: Blog.fetchRequest()), 10)
215+
//
216+
// try await contextManager.performAndSave { context in
217+
// let accountInContext = try XCTUnwrap(context.existingObject(with: account.objectID) as? WPAccount)
218+
// let blog = Blog.lookup(xmlrpc: xmlrpc, andRemoveDuplicateBlogsOf: accountInContext, in: context)
219+
// XCTAssertNotNil(blog)
220+
// }
221+
//
222+
// try XCTAssertEqual(mainContext.count(for: Blog.fetchRequest()), 1)
223+
// }
224+
225+
// MARK: - Blog Feature Domains
226+
227+
func testBlogSupportsDomainsHostedAtWPcom() {
228+
let blog = BlogBuilder(mainContext)
229+
.isHostedAtWPcom()
230+
.with(atomic: false)
231+
.with(isAdmin: true)
232+
.build()
233+
234+
let result = blog.supports(.domains)
235+
236+
XCTAssertTrue(result, "Domains should be supported for WPcom hosted blogs")
237+
}
238+
239+
func testBlogSupportsDomainsAtomic() {
240+
let blog = BlogBuilder(mainContext)
241+
.isNotHostedAtWPcom()
242+
.with(atomic: true)
243+
.with(isAdmin: true)
244+
.build()
245+
246+
let result = blog.supports(.domains)
247+
248+
XCTAssertTrue(result, "Domains should be supported for Atomic blogs")
249+
}
250+
251+
func testShouldNotSupportDomainsNotAdmin() {
252+
let blog = BlogBuilder(mainContext)
253+
.isHostedAtWPcom()
254+
.with(atomic: false)
255+
.with(isAdmin: false)
256+
.build()
257+
258+
let result = blog.supports(.domains)
259+
260+
XCTAssertFalse(result, "Domains should not be supported for non-admin users")
261+
}
262+
263+
func testShouldNotSupportDomainsForP2s() {
264+
let blog = BlogBuilder(mainContext)
265+
.isHostedAtWPcom()
266+
.with(atomic: false)
267+
.with(isAdmin: true)
268+
.with(isWPForTeamsSite: true)
269+
.build()
270+
271+
let result = blog.supports(.domains)
272+
273+
XCTAssertFalse(result, "Domains should not be supported when the site is P2 site")
274+
}
275+
276+
// Blog URL Parsing Tests
277+
func testBlogUrlShouldBeParseableForBlogWithSimpleUrl() throws {
278+
let blog = BlogBuilder(mainContext)
279+
.isHostedAtWPcom()
280+
.with(url: "http://example.com")
281+
.build()
282+
283+
XCTAssertEqual(try blog.wordPressClientParsedUrl().url(), "http://example.com/")
284+
}
285+
286+
func testBlogUrlShouldBeParseableForBlogWithMappedDomain() throws {
287+
let blog = BlogBuilder(mainContext)
288+
.with(url: "http://example.com")
289+
.withMappedDomain(mappedDomainUrl: "http://example2.com")
290+
.build()
291+
292+
XCTAssertEqual(try blog.wordPressClientParsedUrl().url(), "http://example.com/")
293+
}
294+
295+
func testDotComIdShouldBeJetpackSiteID() throws {
296+
let blog = BlogBuilder(mainContext, dotComID: nil)
297+
.set(blogOption: "jetpack_client_id", value: "123")
298+
.build()
299+
XCTAssertEqual(blog.jetpack?.siteID?.int64Value, 123)
300+
301+
try XCTAssertNil(Blog.lookup(withID: 123, in: mainContext))
302+
try mainContext.save()
303+
304+
try XCTAssertNotNil(Blog.lookup(withID: 123, in: mainContext))
305+
306+
contextManager.performAndSave { context in
307+
try? XCTAssertNotNil(Blog.lookup(withID: 123, in: context))
308+
}
309+
}
310+
}

0 commit comments

Comments
 (0)