Add LockManagerTest

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
This commit is contained in:
Marcel Klehr 2022-11-18 15:12:58 +01:00
parent 067121a14e
commit 6843125163
2 changed files with 59 additions and 5 deletions

View File

@ -7,9 +7,9 @@
namespace OCA\Bookmarks\Service;
use DateTime;
use OCA\Bookmarks\Db\FolderMapper;
use OCA\Bookmarks\Db\Types;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IDBConnection;
class LockManager {
@ -23,10 +23,12 @@ class LockManager {
* @var FolderMapper
*/
private $folderMapper;
private ITimeFactory $timeFactory;
public function __construct(IDBConnection $db, FolderMapper $folderMapper) {
public function __construct(IDBConnection $db, FolderMapper $folderMapper, ITimeFactory $timeFactory) {
$this->db = $db;
$this->folderMapper = $folderMapper;
$this->timeFactory = $timeFactory;
}
/**
@ -35,7 +37,7 @@ class LockManager {
*/
public function setLock(string $userId, bool $locked): void {
$this->folderMapper->findRootFolder($userId);
$value = $locked ? new DateTime() : new DateTime('@0'); // now or begin of UNIX time
$value = $locked ? $this->timeFactory->getDateTime() : $this->timeFactory->getDateTime('@0'); // now or begin of UNIX time
$qb = $this->db->getQueryBuilder();
$qb->update('bookmarks_root_folders')
->set('locked_time', $qb->createNamedParameter($value, Types::DATETIME))
@ -59,10 +61,10 @@ class LockManager {
return false;
}
try {
$dateTime = new DateTime($lockedAt);
$dateTime = $this->timeFactory->getDateTime($lockedAt);
} catch (\Exception $e) {
return false;
}
return time() - $dateTime->getTimestamp() < self::TIMEOUT;
return $this->timeFactory->getDateTime()->getTimestamp() - $dateTime->getTimestamp() < self::TIMEOUT;
}
}

52
tests/LockManagerTest.php Normal file
View File

@ -0,0 +1,52 @@
<?php
/*
* Copyright (c) 2022. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Tests;
use OC;
use OCA\Bookmarks\Db\FolderMapper;
use OCA\Bookmarks\Service\LockManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IDBConnection;
class LockManagerTest extends TestCase {
public string $user;
public ITimeFactory $timeFactory;
public LockManager $lockManager;
public FolderMapper $folderMapper;
protected function setUp(): void {
parent::setUp();
$this->user = 'test';
$this->folderMapper = OC::$server->get(FolderMapper::class);
$this->folderMapper->findRootFolder($this->user);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->lockManager = new LockManager(OC::$server->get(IDBConnection::class), $this->folderMapper, $this->timeFactory);
$this->lockManager->setLock($this->user, false);
}
public function testLockUnlock(): void {
$this->timeFactory->expects($this->atLeastOnce())->method('getDateTime')->willReturnCallback(fn ($arg) => new \DateTime($arg));
$this->assertFalse($this->lockManager->getLock($this->user), 'should not be locked');
$this->lockManager->setLock($this->user, true);
$this->assertTrue($this->lockManager->getLock($this->user), 'should be locked');
$this->lockManager->setLock($this->user, false);
$this->assertFalse($this->lockManager->getLock($this->user), 'should not be locked');
}
public function testLockTimeout() {
$this->assertFalse($this->lockManager->getLock($this->user), 'should not be locked');
$startTime = new \DateTime();
$startTime = $startTime->sub(new \DateInterval('PT31M'));
$stub = $this->timeFactory->expects($this->atLeastOnce())->method('getDateTime');
$stub->willReturnCallback(fn ($arg) => $startTime);
$this->lockManager->setLock($this->user, true);
$this->assertTrue($this->lockManager->getLock($this->user), 'should be locked');
$stub->willReturnCallback(fn ($arg) => new \DateTime($arg));
$this->assertFalse($this->lockManager->getLock($this->user), 'lock should have timed out');
}
}