Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor locking in item userinfo to presence or lack thereof of locked key #19

Merged
merged 1 commit into from
Aug 1, 2024
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
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))
}
}
Loading