Skip to content

Commit 8c09e5f

Browse files
authored
Refactor PluginsService to use plugin file path for better stability (#15871)
2 parents dba69c0 + c6488a1 commit 8c09e5f

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

Modules/Sources/Yosemite/Tools/Plugins/PluginsService.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import protocol Storage.StorageManagerType
55
/// A service for system plugins.
66
public protocol PluginsServiceProtocol {
77
/// Waits for a specific plugin to be available in storage.
8-
func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin
8+
/// - Parameters:
9+
/// - siteID: The site ID to search for the plugin.
10+
/// - pluginPath: The plugin's file path (e.g., "woocommerce/woocommerce.php" for WooCommerce).
11+
/// - isActive: Whether the plugin is active or not.
12+
/// - Returns: The SystemPlugin when found in storage.
13+
func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin
914
}
1015

1116
public class PluginsService: PluginsServiceProtocol {
@@ -16,20 +21,20 @@ public class PluginsService: PluginsServiceProtocol {
1621
}
1722

1823
@MainActor
19-
public func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin {
20-
let predicate = \StorageSystemPlugin.siteID == siteID && \StorageSystemPlugin.name == pluginName && \StorageSystemPlugin.active == isActive
21-
let nameDescriptor = NSSortDescriptor(keyPath: \StorageSystemPlugin.name, ascending: true)
24+
public func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin {
25+
let predicate = \StorageSystemPlugin.siteID == siteID && \StorageSystemPlugin.plugin == pluginPath && \StorageSystemPlugin.active == isActive
26+
let pluginDescriptor = NSSortDescriptor(keyPath: \StorageSystemPlugin.plugin, ascending: true)
2227
let resultsController = ResultsController<StorageSystemPlugin>(storageManager: storageManager,
2328
matching: predicate,
2429
fetchLimit: 1,
25-
sortedBy: [nameDescriptor])
30+
sortedBy: [pluginDescriptor])
2631
do {
2732
try resultsController.performFetch()
2833
if let plugin = resultsController.fetchedObjects.first {
2934
return plugin
3035
}
3136
} catch {
32-
DDLogError("Error loading plugin \(pluginName) for site \(siteID) initially: \(error.localizedDescription)")
37+
DDLogError("Error loading plugin \(pluginPath) for site \(siteID) initially: \(error.localizedDescription)")
3338
}
3439

3540
return await withCheckedContinuation { continuation in

Modules/Tests/YosemiteTests/Tools/Plugins/PluginsServiceTests.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ struct PluginsServiceTests {
1818
storageManager.insertWCPlugin(siteID: siteID, isActive: true, version: "1.0.0")
1919

2020
// When
21-
let result = await sut.waitForPluginInStorage(siteID: siteID, pluginName: PluginConstants.pluginName, isActive: true)
21+
let result = await sut.waitForPluginInStorage(siteID: siteID, pluginPath: PluginConstants.plugin, isActive: true)
2222

2323
// Then
2424
#expect(result.siteID == siteID)
25-
#expect(result.name == PluginConstants.pluginName)
25+
#expect(result.plugin == PluginConstants.plugin)
2626
#expect(result.active == true)
2727
#expect(result.version == "1.0.0")
2828
}
@@ -33,14 +33,15 @@ struct PluginsServiceTests {
3333
await storageManager.reset()
3434

3535
// When
36-
async let plugin = sut.waitForPluginInStorage(siteID: siteID, pluginName: PluginConstants.pluginName, isActive: true)
36+
async let plugin = sut.waitForPluginInStorage(siteID: siteID, pluginPath: PluginConstants.plugin, isActive: true)
3737
#expect(storageManager.viewStorage.loadSystemPlugins(siteID: siteID).count == 0)
3838
storageManager.insertWCPlugin(siteID: siteID, isActive: true, version: "2.0.0")
3939
#expect(storageManager.viewStorage.loadSystemPlugins(siteID: siteID).count == 1)
4040

4141
// Then
4242
let result = await plugin
4343
#expect(result.siteID == siteID)
44+
#expect(result.plugin == PluginConstants.plugin)
4445
#expect(result.name == PluginConstants.pluginName)
4546
#expect(result.active == true)
4647
#expect(result.version == "2.0.0")
@@ -50,7 +51,11 @@ struct PluginsServiceTests {
5051
private extension MockStorageManager {
5152
func insertWCPlugin(siteID: Int64, isActive: Bool, version: String? = nil) {
5253
performAndSave({ storage in
53-
let plugin = SystemPlugin.fake().copy(siteID: siteID, name: PluginConstants.pluginName, version: version, active: isActive)
54+
let plugin = SystemPlugin.fake().copy(siteID: siteID,
55+
plugin: PluginConstants.plugin,
56+
name: PluginConstants.pluginName,
57+
version: version,
58+
active: isActive)
5459
let newPlugin = storage.insertNewObject(ofType: StorageSystemPlugin.self)
5560
newPlugin.update(with: plugin)
5661
}, completion: nil, on: .main)
@@ -67,5 +72,6 @@ private extension MockStorageManager {
6772

6873
// MARK: - Constants
6974
private enum PluginConstants {
75+
static let plugin = "example-plugin/example-plugin.php"
7076
static let pluginName = "Example Plugin"
7177
}

WooCommerce/Classes/ViewRelated/Dashboard/Settings/POS/LegacyPOSTabEligibilityChecker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private extension LegacyPOSTabEligibilityChecker {
154154

155155
@MainActor
156156
func fetchWooCommercePlugin(siteID: Int64) async -> SystemPlugin {
157-
await pluginsService.waitForPluginInStorage(siteID: siteID, pluginName: Constants.wcPluginName, isActive: true)
157+
await pluginsService.waitForPluginInStorage(siteID: siteID, pluginPath: Constants.wcPlugin, isActive: true)
158158
}
159159

160160
@MainActor
@@ -254,7 +254,7 @@ private extension LegacyPOSTabEligibilityChecker {
254254

255255
private extension LegacyPOSTabEligibilityChecker {
256256
enum Constants {
257-
static let wcPluginName = "WooCommerce"
257+
static let wcPlugin = "woocommerce/woocommerce.php"
258258
static let wcPluginMinimumVersion = "9.6.0-beta"
259259
static let wcPluginMinimumVersionWithFeatureSwitch = "10.0.0"
260260
}

WooCommerce/Classes/ViewRelated/Dashboard/Settings/POS/POSTabEligibilityChecker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private extension POSTabEligibilityChecker {
140140

141141
@MainActor
142142
func fetchWooCommercePlugin(siteID: Int64) async -> SystemPlugin {
143-
await pluginsService.waitForPluginInStorage(siteID: siteID, pluginName: Constants.wcPluginName, isActive: true)
143+
await pluginsService.waitForPluginInStorage(siteID: siteID, pluginPath: Constants.wcPlugin, isActive: true)
144144
}
145145

146146
@MainActor
@@ -279,7 +279,7 @@ private extension POSTabEligibilityChecker {
279279

280280
private extension POSTabEligibilityChecker {
281281
enum Constants {
282-
static let wcPluginName = "WooCommerce"
282+
static let wcPlugin = "woocommerce/woocommerce.php"
283283
static let wcPluginMinimumVersion = "9.6.0-beta"
284284
static let wcPluginMinimumVersionWithFeatureSwitch = "10.0.0"
285285
}

WooCommerce/WooCommerceTests/ViewRelated/Settings/POS/LegacyPOSTabEligibilityCheckerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ private extension LegacyPOSTabEligibilityCheckerTests {
365365
func setupWooCommerceVersion(_ version: String = "9.6.0-beta") {
366366
pluginsService.pluginToReturn = .fake().copy(
367367
siteID: siteID,
368-
plugin: "WooCommerce",
368+
plugin: "woocommerce/woocommerce.php",
369369
version: version,
370370
active: true
371371
)
@@ -426,7 +426,7 @@ private extension LegacyPOSTabEligibilityCheckerTests {
426426
private final class MockPluginsService: PluginsServiceProtocol {
427427
var pluginToReturn: SystemPlugin = .fake()
428428

429-
func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin {
429+
func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin {
430430
pluginToReturn
431431
}
432432
}

WooCommerce/WooCommerceTests/ViewRelated/Settings/POS/POSTabEligibilityCheckerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ private extension POSTabEligibilityCheckerTests {
587587
func setupWooCommerceVersion(_ version: String = "9.6.0-beta") {
588588
pluginsService.pluginToReturn = .fake().copy(
589589
siteID: siteID,
590-
plugin: "WooCommerce",
590+
plugin: "woocommerce/woocommerce.php",
591591
version: version,
592592
active: true
593593
)
@@ -648,7 +648,7 @@ private extension POSTabEligibilityCheckerTests {
648648
private final class MockPluginsService: PluginsServiceProtocol {
649649
var pluginToReturn: SystemPlugin = .fake()
650650

651-
func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin {
651+
func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin {
652652
pluginToReturn
653653
}
654654
}

0 commit comments

Comments
 (0)