From c7c9302109d7b85a8d68ab14cd5179bf9334963b Mon Sep 17 00:00:00 2001 From: Luka Trovic Date: Tue, 8 Mar 2022 10:27:06 +0100 Subject: [PATCH] fix: integration tests Signed-off-by: Luka Trovic --- lib/Db/AssignmentMapper.php | 14 ++++++------- lib/Db/BoardMapper.php | 21 +++++++++++++++++++ lib/Service/BoardService.php | 18 ++++++++++++++++ .../database/TransferOwnershipTest.php | 2 +- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/lib/Db/AssignmentMapper.php b/lib/Db/AssignmentMapper.php index 836add105..222e556cc 100644 --- a/lib/Db/AssignmentMapper.php +++ b/lib/Db/AssignmentMapper.php @@ -154,19 +154,17 @@ class AssignmentMapper extends QBMapper implements IPermissionMapper { * @return void */ public function transferOwnership(string $ownerId, string $newOwnerId) { - $params = [ - 'newOwner' => $newOwnerId, - 'type' => Assignment::TYPE_USER - ]; - $qb = $this->db->getQueryBuilder(); - $sql = "DELETE FROM `*PREFIX*{$this->tableName}` WHERE `participant` = :newOwner AND `type`= :type"; - $stmt = $this->db->executeQuery($sql, $params); - $stmt->closeCursor(); $params = [ 'owner' => $ownerId, 'newOwner' => $newOwnerId, 'type' => Assignment::TYPE_USER ]; + $qb = $this->db->getQueryBuilder(); + $sql = "DELETE FROM `*PREFIX*{$this->tableName}` WHERE `participant` = :newOwner AND `type`= :type AND id IN + (SELECT id FROM `*PREFIX*{$this->tableName}` WHERE `participant` = :owner)"; + $stmt = $this->db->executeQuery($sql, $params); + $stmt->closeCursor(); + $sql = "UPDATE `*PREFIX*{$this->tableName}` SET `participant` = :newOwner WHERE `participant` = :owner AND `type`= :type"; $stmt = $this->db->executeQuery($sql, $params); $stmt->closeCursor(); diff --git a/lib/Db/BoardMapper.php b/lib/Db/BoardMapper.php index ee28a27c3..c0a8e2391 100644 --- a/lib/Db/BoardMapper.php +++ b/lib/Db/BoardMapper.php @@ -318,4 +318,25 @@ class BoardMapper extends DeckMapper implements IPermissionMapper { $stmt = $this->db->executeQuery($sql, $params); $stmt->closeCursor(); } + + /** + * Reset Cache for a + * given board or a given user + * + * @param int|null $boardId + * @param int|null $userId + */ + public function flushCache(?int $boardId = null, ?string $userId = null) + { + if ($boardId) { + unset($this->boardCache[$boardId]); + } else { + $this->boardCache = null; + } + if ($userId) { + unset($this->userBoardCache[$userId]); + } else { + $this->userBoardCache = null; + } + } } diff --git a/lib/Service/BoardService.php b/lib/Service/BoardService.php index 4636ee69c..6d2c0d38a 100644 --- a/lib/Service/BoardService.php +++ b/lib/Service/BoardService.php @@ -525,6 +525,9 @@ class BoardService { $this->boardMapper->mapAcl($newAcl); $this->changeHelper->boardChanged($boardId); + $board = $this->find($boardId); + $this->clearBoardFromCache($board); + // TODO: use the dispatched event for this try { $resourceProvider = \OC::$server->query(\OCA\Deck\Collaboration\Resources\ResourceProvider::class); @@ -678,6 +681,7 @@ class BoardService { public function transferOwnership(string $owner, string $newOwner): void { $boards = $this->boardMapper->findAllByUser($owner); foreach ($boards as $board) { + $this->clearBoardFromCache($board); $this->aclMapper->transferOwnership($board->getId(), $owner, $newOwner); } $this->boardMapper->transferOwnership($owner, $newOwner); @@ -720,4 +724,18 @@ class BoardService { private function clearBoardsCache() { $this->boardsCache = null; } + + /** + * Clean a given board data + * from the Cache + * + * @param OCA\Deck\Db\Board $board + */ + private function clearBoardFromCache(Board $board) { + $boardId = $board->getId(); + $boardOwnerId = $board->getOwner(); + + $this->boardMapper->flushCache($boardId, $boardOwnerId); + unset($this->boardsCache[$boardId]); + } } diff --git a/tests/integration/database/TransferOwnershipTest.php b/tests/integration/database/TransferOwnershipTest.php index 2b1e30403..2c549e9c0 100644 --- a/tests/integration/database/TransferOwnershipTest.php +++ b/tests/integration/database/TransferOwnershipTest.php @@ -82,7 +82,7 @@ class TransferOwnershipTest extends \Test\TestCase { */ public function testTransferBoardOwnership() { $this->boardService->transferOwnership(self::TEST_USER_1, self::TEST_USER_2); - $this->invokePrivate($this->boardService, 'clearBoardsCache'); + /* $this->invokePrivate($this->boardService, 'clearBoardsCache'); */ $board = $this->boardService->find($this->board->getId()); $boardOwner = $board->getOwner(); $this->assertEquals(self::TEST_USER_2, $boardOwner);