Skip to content

Commit

Permalink
Merge pull request #19 from claucambra/bugfix/locking-userinfo
Browse files Browse the repository at this point in the history
Refactor locking in item userinfo to presence or lack thereof of locked key
  • Loading branch information
claucambra authored Aug 1, 2024
2 parents 23ffd30 + 8e0a82f commit a248c67
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
10 changes: 6 additions & 4 deletions Sources/NextcloudFileProviderKit/Item/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ public class Item: NSObject, NSFileProviderItem {
}

public var userInfo: [AnyHashable : Any]? {
[
var userInfoDict = [AnyHashable : Any]()
if metadata.lock {
// Can be used to display lock/unlock context menu entries for FPUIActions
"isLockable": !self.contentType.conforms(to: .directory) && !metadata.lock,
"isUnlockable": !self.contentType.conforms(to: .directory) && metadata.lock,
]
// Note that only files, not folders, should be lockable/unlockable
userInfoDict["locked"] = metadata.lock
}
return userInfoDict
}

@available(macOS 13.0, *)
Expand Down
51 changes: 14 additions & 37 deletions Tests/NextcloudFileProviderKitTests/ItemPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,35 +183,6 @@ final class ItemPropertyTests: XCTestCase {
XCTAssertTrue(item.contentType.conforms(to: .bundle))
}

func testItemUserInfoLockingPropsFolder() {
let metadata = ItemMetadata()
metadata.ocId = "test-id"
metadata.etag = "test-etag"
metadata.account = Self.account.ncKitAccount
metadata.name = "test"
metadata.fileName = "test"
metadata.fileNameView = "test"
metadata.serverUrl = Self.account.davFilesUrl
metadata.urlBase = Self.account.serverUrl
metadata.userId = Self.account.username
metadata.user = Self.account.username
metadata.directory = true
metadata.contentType = "httpd/unix-directory"
metadata.lockTime = .init()
metadata.lockTimeOut = .init()

let item = Item(
metadata: metadata,
parentItemIdentifier: .rootContainer,
remoteInterface: MockRemoteInterface(account: Self.account)
)

XCTAssertNotNil(item.userInfo?["isLockable"])
XCTAssertNotNil(item.userInfo?["isUnlockable"])
XCTAssertFalse(item.userInfo?["isLockable"] as? Bool ?? true) // directories not lockable
XCTAssertFalse(item.userInfo?["isUnlockable"] as? Bool ?? true)
}

func testItemUserInfoLockingPropsFileLocked() {
let metadata = ItemMetadata()
metadata.ocId = "test-id"
Expand All @@ -238,10 +209,13 @@ final class ItemPropertyTests: XCTestCase {
remoteInterface: MockRemoteInterface(account: Self.account)
)

XCTAssertNotNil(item.userInfo?["isLockable"])
XCTAssertNotNil(item.userInfo?["isUnlockable"])
XCTAssertFalse(item.userInfo?["isLockable"] as? Bool ?? true)
XCTAssertTrue(item.userInfo?["isUnlockable"] as? Bool ?? false) // locked file, can unlock
XCTAssertNotNil(item.userInfo?["locked"])

let fileproviderItems = ["fileproviderItems": [item]]
let lockPredicate = NSPredicate(
format: "SUBQUERY ( fileproviderItems, $fileproviderItem, $fileproviderItem.userInfo.locked != nil ).@count > 0"
)
XCTAssertTrue(lockPredicate.evaluate(with: fileproviderItems))
}

func testItemUserInfoLockingPropsFileUnlocked() {
Expand All @@ -265,9 +239,12 @@ final class ItemPropertyTests: XCTestCase {
remoteInterface: MockRemoteInterface(account: Self.account)
)

XCTAssertNotNil(item.userInfo?["isLockable"])
XCTAssertNotNil(item.userInfo?["isUnlockable"])
XCTAssertTrue(item.userInfo?["isLockable"] as? Bool ?? false) // unlocked file, can lock
XCTAssertFalse(item.userInfo?["isUnlockable"] as? Bool ?? true)
XCTAssertNil(item.userInfo?["locked"])

let fileproviderItems = ["fileproviderItems": [item]]
let lockPredicate = NSPredicate(
format: "SUBQUERY ( fileproviderItems, $fileproviderItem, $fileproviderItem.userInfo.locked == nil ).@count > 0"
)
XCTAssertTrue(lockPredicate.evaluate(with: fileproviderItems))
}
}

0 comments on commit a248c67

Please sign in to comment.