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
17 changes: 11 additions & 6 deletions Modules/Sources/Yosemite/Tools/Plugins/PluginsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import protocol Storage.StorageManagerType
/// A service for system plugins.
public protocol PluginsServiceProtocol {
/// Waits for a specific plugin to be available in storage.
func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin
/// - Parameters:
/// - siteID: The site ID to search for the plugin.
/// - pluginPath: The plugin's file path (e.g., "woocommerce/woocommerce.php" for WooCommerce).
/// - isActive: Whether the plugin is active or not.
/// - Returns: The SystemPlugin when found in storage.
func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin
}

public class PluginsService: PluginsServiceProtocol {
Expand All @@ -16,20 +21,20 @@ public class PluginsService: PluginsServiceProtocol {
}

@MainActor
public func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin {
let predicate = \StorageSystemPlugin.siteID == siteID && \StorageSystemPlugin.name == pluginName && \StorageSystemPlugin.active == isActive
let nameDescriptor = NSSortDescriptor(keyPath: \StorageSystemPlugin.name, ascending: true)
public func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin {
let predicate = \StorageSystemPlugin.siteID == siteID && \StorageSystemPlugin.plugin == pluginPath && \StorageSystemPlugin.active == isActive
let pluginDescriptor = NSSortDescriptor(keyPath: \StorageSystemPlugin.plugin, ascending: true)
let resultsController = ResultsController<StorageSystemPlugin>(storageManager: storageManager,
matching: predicate,
fetchLimit: 1,
sortedBy: [nameDescriptor])
sortedBy: [pluginDescriptor])
do {
try resultsController.performFetch()
if let plugin = resultsController.fetchedObjects.first {
return plugin
}
} catch {
DDLogError("Error loading plugin \(pluginName) for site \(siteID) initially: \(error.localizedDescription)")
DDLogError("Error loading plugin \(pluginPath) for site \(siteID) initially: \(error.localizedDescription)")
}

return await withCheckedContinuation { continuation in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ struct PluginsServiceTests {
storageManager.insertWCPlugin(siteID: siteID, isActive: true, version: "1.0.0")

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

// Then
#expect(result.siteID == siteID)
#expect(result.name == PluginConstants.pluginName)
#expect(result.plugin == PluginConstants.plugin)
#expect(result.active == true)
#expect(result.version == "1.0.0")
}
Expand All @@ -33,14 +33,15 @@ struct PluginsServiceTests {
await storageManager.reset()

// When
async let plugin = sut.waitForPluginInStorage(siteID: siteID, pluginName: PluginConstants.pluginName, isActive: true)
async let plugin = sut.waitForPluginInStorage(siteID: siteID, pluginPath: PluginConstants.plugin, isActive: true)
#expect(storageManager.viewStorage.loadSystemPlugins(siteID: siteID).count == 0)
storageManager.insertWCPlugin(siteID: siteID, isActive: true, version: "2.0.0")
#expect(storageManager.viewStorage.loadSystemPlugins(siteID: siteID).count == 1)

// Then
let result = await plugin
#expect(result.siteID == siteID)
#expect(result.plugin == PluginConstants.plugin)
#expect(result.name == PluginConstants.pluginName)
#expect(result.active == true)
#expect(result.version == "2.0.0")
Expand All @@ -50,7 +51,11 @@ struct PluginsServiceTests {
private extension MockStorageManager {
func insertWCPlugin(siteID: Int64, isActive: Bool, version: String? = nil) {
performAndSave({ storage in
let plugin = SystemPlugin.fake().copy(siteID: siteID, name: PluginConstants.pluginName, version: version, active: isActive)
let plugin = SystemPlugin.fake().copy(siteID: siteID,
plugin: PluginConstants.plugin,
name: PluginConstants.pluginName,
version: version,
active: isActive)
let newPlugin = storage.insertNewObject(ofType: StorageSystemPlugin.self)
newPlugin.update(with: plugin)
}, completion: nil, on: .main)
Expand All @@ -67,5 +72,6 @@ private extension MockStorageManager {

// MARK: - Constants
private enum PluginConstants {
static let plugin = "example-plugin/example-plugin.php"
static let pluginName = "Example Plugin"
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private extension LegacyPOSTabEligibilityChecker {

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

@MainActor
Expand Down Expand Up @@ -254,7 +254,7 @@ private extension LegacyPOSTabEligibilityChecker {

private extension LegacyPOSTabEligibilityChecker {
enum Constants {
static let wcPluginName = "WooCommerce"
static let wcPlugin = "woocommerce/woocommerce.php"
static let wcPluginMinimumVersion = "9.6.0-beta"
static let wcPluginMinimumVersionWithFeatureSwitch = "10.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private extension POSTabEligibilityChecker {

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

@MainActor
Expand Down Expand Up @@ -279,7 +279,7 @@ private extension POSTabEligibilityChecker {

private extension POSTabEligibilityChecker {
enum Constants {
static let wcPluginName = "WooCommerce"
static let wcPlugin = "woocommerce/woocommerce.php"
static let wcPluginMinimumVersion = "9.6.0-beta"
static let wcPluginMinimumVersionWithFeatureSwitch = "10.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private extension LegacyPOSTabEligibilityCheckerTests {
func setupWooCommerceVersion(_ version: String = "9.6.0-beta") {
pluginsService.pluginToReturn = .fake().copy(
siteID: siteID,
plugin: "WooCommerce",
plugin: "woocommerce/woocommerce.php",
version: version,
active: true
)
Expand Down Expand Up @@ -426,7 +426,7 @@ private extension LegacyPOSTabEligibilityCheckerTests {
private final class MockPluginsService: PluginsServiceProtocol {
var pluginToReturn: SystemPlugin = .fake()

func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin {
func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin {
pluginToReturn
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ private extension POSTabEligibilityCheckerTests {
func setupWooCommerceVersion(_ version: String = "9.6.0-beta") {
pluginsService.pluginToReturn = .fake().copy(
siteID: siteID,
plugin: "WooCommerce",
plugin: "woocommerce/woocommerce.php",
version: version,
active: true
)
Expand Down Expand Up @@ -648,7 +648,7 @@ private extension POSTabEligibilityCheckerTests {
private final class MockPluginsService: PluginsServiceProtocol {
var pluginToReturn: SystemPlugin = .fake()

func waitForPluginInStorage(siteID: Int64, pluginName: String, isActive: Bool) async -> SystemPlugin {
func waitForPluginInStorage(siteID: Int64, pluginPath: String, isActive: Bool) async -> SystemPlugin {
pluginToReturn
}
}
Expand Down