From fe11780ad5557aa107f53c97fa3b547e081b391d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 31 Mar 2026 17:32:24 +0200 Subject: [PATCH] Add method to check lock activity and corresponding tests - Added `isActiveAt` method in `StoredObjectLock` to check if a lock is active at a specific time. - Implemented `isLockedAt` method in `StoredObject` to determine if an object is locked at a given time. - Included unit tests for `isActiveAt` method in `StoredObjectLockTest` to validate various scenarios. --- .../Entity/StoredObject.php | 19 +++++ .../Entity/StoredObjectLock.php | 11 +++ .../Tests/Entity/StoredObjectLockTest.php | 76 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectLockTest.php diff --git a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php index 956cffbd2..fb045111c 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php @@ -131,6 +131,25 @@ class StoredObject implements Document, TrackCreationInterface $this->locks->removeElement($lock); } + public function isLockedAt(\DateTimeImmutable $at): bool + { + foreach ($this->locks as $lock) { + if ($lock->isActiveAt($at)) { + return true; + } + } + + return false; + } + + /** + * @return Collection + */ + public function getLocks(): Collection + { + return $this->locks; + } + public function addGenerationTrial(): self { ++$this->generationTrialsCounter; diff --git a/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php b/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php index 40fe9b284..2f7a3fcf7 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php @@ -118,4 +118,15 @@ class StoredObjectLock { return $this->expireAt; } + + /** + * Return true if the lock must be considered as active. + * + * A StoredObjectLock is active if there isn't any expiration date, or + * if the expiration date and time is before the given time. + */ + public function isActiveAt(\DateTimeImmutable $at): bool + { + return null === $this->getExpireAt() || $at < $this->getExpireAt(); + } } diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectLockTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectLockTest.php new file mode 100644 index 000000000..a45bcb1c7 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectLockTest.php @@ -0,0 +1,76 @@ +isActiveAt($now)); + self::assertTrue($lock->isActiveAt($now->modify('+1 year'))); + } + + public function testIsActiveAtWithFutureExpiration(): void + { + $storedObject = new StoredObject(); + $now = new \DateTimeImmutable(); + $expireAt = $now->modify('+1 hour'); + $lock = new StoredObjectLock( + $storedObject, + StoredObjectLockMethodEnum::WOPI, + $now, + 'token', + $expireAt + ); + + self::assertTrue($lock->isActiveAt($now)); + self::assertTrue($lock->isActiveAt($now->modify('+30 minutes'))); + } + + public function testIsActiveAtWithPastExpiration(): void + { + $storedObject = new StoredObject(); + $now = new \DateTimeImmutable(); + $expireAt = $now->modify('-1 hour'); + $lock = new StoredObjectLock( + $storedObject, + StoredObjectLockMethodEnum::WOPI, + $now->modify('-2 hours'), + 'token', + $expireAt + ); + + self::assertFalse($lock->isActiveAt($now)); + self::assertFalse($lock->isActiveAt($expireAt)); + self::assertFalse($lock->isActiveAt($expireAt->modify('+1 second'))); + } +}