Skip to content

Commit 55d769b

Browse files
committed
Add loadSystemPlugin(siteID:fileNameWithoutExtension:) storage extension for POS use case to fix scenario when plugin folder path is altered by plugins or hosts.
1 parent 57650fa commit 55d769b

File tree

3 files changed

+156
-2
lines changed

3 files changed

+156
-2
lines changed

Modules/Sources/Storage/Tools/StorageType+Extensions.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,23 @@ public extension StorageType {
855855
return firstObject(ofType: SystemPlugin.self, matching: predicate)
856856
}
857857

858+
/// Returns a system plugin with a specified `siteID` and `fileNameWithoutExtension`.
859+
///
860+
/// - Parameters:
861+
/// - siteID: The site ID to filter by.
862+
/// - fileNameWithoutExtension: The plugin file name to match without extension (e.g., "woocommerce", "woocommerce-payments").
863+
/// - active: Optional active state filter. If provided, only plugins with matching active state are returned. If nil, active state is ignored.
864+
/// - Returns: The matching system plugin, or nil if not found.
865+
func loadSystemPlugin(siteID: Int64, fileNameWithoutExtension: String, active: Bool? = nil) -> SystemPlugin? {
866+
let pathPattern = "*/\(fileNameWithoutExtension).*"
867+
let predicate = if let active {
868+
NSPredicate(format: "siteID == %lld AND plugin LIKE %@ AND active == %@", siteID, pathPattern, NSNumber(value: active))
869+
} else {
870+
NSPredicate(format: "siteID == %lld AND plugin LIKE %@", siteID, pathPattern)
871+
}
872+
return firstObject(ofType: SystemPlugin.self, matching: predicate)
873+
}
874+
858875
/// Returns stored system plugins for a provided `siteID` matching the given plugin `paths`.
859876
///
860877
func loadSystemPlugins(siteID: Int64, matchingPaths paths: [String]) -> [SystemPlugin] {

Modules/Sources/Yosemite/PointOfSale/Eligibility/POSSystemStatusService.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public final class POSSystemStatusService: POSSystemStatusServiceProtocol {
5454
})
5555

5656
// Loads WooCommerce plugin from storage.
57-
guard let wcPlugin = storageManager.viewStorage.loadSystemPlugin(siteID: siteID, path: Constants.wcPluginPath, active: true)?.toReadOnly() else {
57+
guard let wcPlugin = storageManager.viewStorage.loadSystemPlugin(siteID: siteID,
58+
fileNameWithoutExtension: Constants.wcPluginFileNameWithoutExtension,
59+
active: true)?.toReadOnly() else {
5860
return POSPluginAndFeatureInfo(wcPlugin: nil, featureValue: nil)
5961
}
6062

@@ -66,7 +68,7 @@ public final class POSSystemStatusService: POSSystemStatusServiceProtocol {
6668

6769
private extension POSSystemStatusService {
6870
enum Constants {
69-
static let wcPluginPath = "woocommerce/woocommerce.php"
71+
static let wcPluginFileNameWithoutExtension = "woocommerce"
7072
}
7173
}
7274

Modules/Tests/StorageTests/Tools/StorageTypeExtensionsTests.swift

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,141 @@ final class StorageTypeExtensionsTests: XCTestCase {
15381538
XCTAssertNil(foundPlugin)
15391539
}
15401540

1541+
// MARK: - `loadSystemPlugin(siteID:fileNameWithoutExtension:)`
1542+
1543+
func test_loadSystemPlugin_by_fileNameWithoutExtension_returns_matching_plugin_when_two_plugins_in_storage() throws {
1544+
// Given
1545+
let systemPlugin1 = storage.insertNewObject(ofType: SystemPlugin.self)
1546+
systemPlugin1.plugin = "woocommerce-payments/woocommerce-payments.php"
1547+
systemPlugin1.siteID = sampleSiteID
1548+
1549+
let systemPlugin2 = storage.insertNewObject(ofType: SystemPlugin.self)
1550+
systemPlugin2.plugin = "test-plugin/woocommerce-gift-cards.php"
1551+
systemPlugin2.siteID = sampleSiteID
1552+
1553+
// When
1554+
let foundSystemPlugin = try XCTUnwrap(storage.loadSystemPlugin(siteID: sampleSiteID, fileNameWithoutExtension: "woocommerce-gift-cards"))
1555+
1556+
// Then
1557+
XCTAssertEqual(foundSystemPlugin, systemPlugin2)
1558+
}
1559+
1560+
func test_loadSystemPlugin_by_fileNameWithoutExtension_returns_matching_plugin_when_plugin_path_is_in_different_valid_formats() throws {
1561+
let validPluginPaths = [
1562+
"woocommerce/woocommerce.php",
1563+
"woocommerce/woocommerce.swift",
1564+
"woocommerce//woocommerce.php",
1565+
"test-plugin/test-plugin/woocommerce.php",
1566+
"test-plugin/test-plugin/test-plugin/woocommerce.tmp.php",
1567+
"test-plugin/test-plugin/woocommerce.tmp.php",
1568+
"test-plugin/woocommerce.tmp.php",
1569+
"/woocommerce.tmp.php"
1570+
]
1571+
1572+
for pluginPath in validPluginPaths {
1573+
// Given
1574+
let plugin = storage.insertNewObject(ofType: SystemPlugin.self)
1575+
plugin.plugin = pluginPath
1576+
plugin.siteID = sampleSiteID
1577+
1578+
// When
1579+
let foundPlugin = try XCTUnwrap(storage.loadSystemPlugin(siteID: sampleSiteID, fileNameWithoutExtension: "woocommerce"))
1580+
1581+
// Then
1582+
XCTAssertEqual(foundPlugin, plugin)
1583+
1584+
// Cleanup for next iteration
1585+
storage.deleteObject(plugin)
1586+
}
1587+
}
1588+
1589+
func test_loadSystemPlugin_by_fileNameWithoutExtension_returns_nil_when_plugin_path_is_in_different_invalid_formats() throws {
1590+
let invalidPluginPaths = [
1591+
"woocommerce/woocommerce",
1592+
"woocommerce/woocommerce-dev.php",
1593+
"test-plugin/woocommerce-dev.php",
1594+
"test-plugin/test-plugin/woocommerce-dev.php",
1595+
"woocommerce.tmp.php"
1596+
]
1597+
1598+
for pluginPath in invalidPluginPaths {
1599+
// Given
1600+
let plugin = storage.insertNewObject(ofType: SystemPlugin.self)
1601+
plugin.plugin = pluginPath
1602+
plugin.siteID = sampleSiteID
1603+
1604+
// When
1605+
let foundPlugin = storage.loadSystemPlugin(siteID: sampleSiteID, fileNameWithoutExtension: "woocommerce")
1606+
1607+
// Then
1608+
XCTAssertNil(foundPlugin)
1609+
1610+
// Cleanup for next iteration
1611+
storage.deleteObject(plugin)
1612+
}
1613+
}
1614+
1615+
func test_loadSystemPlugin_by_fileNameWithoutExtension_returns_matching_plugin_when_active_state_is_set() throws {
1616+
// Given
1617+
let inactivePlugin = storage.insertNewObject(ofType: SystemPlugin.self)
1618+
inactivePlugin.plugin = "test-plugin/woocommerce.php"
1619+
inactivePlugin.siteID = sampleSiteID
1620+
inactivePlugin.active = false
1621+
1622+
let activePlugin = storage.insertNewObject(ofType: SystemPlugin.self)
1623+
activePlugin.plugin = "woocommerce/woocommerce.php"
1624+
activePlugin.siteID = sampleSiteID
1625+
activePlugin.active = true
1626+
1627+
// When
1628+
let foundActivePlugin = try XCTUnwrap(storage.loadSystemPlugin(siteID: sampleSiteID, fileNameWithoutExtension: "woocommerce", active: true))
1629+
1630+
// Then
1631+
XCTAssertEqual(foundActivePlugin, activePlugin)
1632+
1633+
// When
1634+
let foundInactivePlugin = try XCTUnwrap(storage.loadSystemPlugin(siteID: sampleSiteID, fileNameWithoutExtension: "woocommerce", active: false))
1635+
1636+
// Then
1637+
XCTAssertEqual(foundInactivePlugin, inactivePlugin)
1638+
}
1639+
1640+
func test_loadSystemPlugin_by_fileNameWithoutExtension_returns_matching_plugin_when_active_state_is_nil() throws {
1641+
// Given
1642+
let activePlugin = storage.insertNewObject(ofType: SystemPlugin.self)
1643+
activePlugin.plugin = "test-plugin/woocommerce.php"
1644+
activePlugin.siteID = sampleSiteID
1645+
activePlugin.active = true
1646+
1647+
let inactivePlugin = storage.insertNewObject(ofType: SystemPlugin.self)
1648+
inactivePlugin.plugin = "jetpack/jetpack.php"
1649+
inactivePlugin.siteID = sampleSiteID
1650+
inactivePlugin.active = false
1651+
1652+
// When
1653+
let foundPlugin = storage.loadSystemPlugin(siteID: sampleSiteID, fileNameWithoutExtension: "woocommerce", active: nil)
1654+
1655+
// Then
1656+
XCTAssertNotNil(foundPlugin)
1657+
XCTAssertEqual(foundPlugin, activePlugin)
1658+
}
1659+
1660+
func test_loadSystemPlugin_by_fileNameWithoutExtension_returns_nil_when_active_state_does_not_match() {
1661+
// Given
1662+
let activePlugin = storage.insertNewObject(ofType: SystemPlugin.self)
1663+
activePlugin.plugin = "woocommerce/woocommerce.php"
1664+
activePlugin.siteID = sampleSiteID
1665+
activePlugin.active = true
1666+
1667+
// When
1668+
let foundPlugin = storage.loadSystemPlugin(siteID: sampleSiteID, fileNameWithoutExtension: "woocommerce", active: false)
1669+
1670+
// Then
1671+
XCTAssertNil(foundPlugin)
1672+
}
1673+
1674+
// MARK: - `loadSystemPlugins(siteID:matchingPaths:)`
1675+
15411676
func test_loadSystemPlugins_by_siteID_matching_paths() {
15421677
// Given
15431678
let systemPlugin1 = storage.insertNewObject(ofType: SystemPlugin.self)

0 commit comments

Comments
 (0)