Skip to content

Commit

Permalink
fix: integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud authored and juliusknorr committed Mar 22, 2022
1 parent afbbdf0 commit 4615926
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
10 changes: 4 additions & 6 deletions lib/Db/AssignmentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,16 @@ private function getOrigin(Assignment $assignment) {
*/
public function transferOwnership(string $ownerId, string $newOwnerId) {
$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";
$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();
$params = [
'owner' => $ownerId,
'newOwner' => $newOwnerId,
'type' => Assignment::TYPE_USER
];

$sql = "UPDATE `*PREFIX*{$this->tableName}` SET `participant` = :newOwner WHERE `participant` = :owner AND `type`= :type";
$stmt = $this->db->executeQuery($sql, $params);
$stmt->closeCursor();
Expand Down
23 changes: 22 additions & 1 deletion lib/Db/BoardMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function findAllByUser(string $userId, ?int $limit = null, ?int $offset =

// shared with user
$qb->resetQueryParts();
$qb->select('b.id', 'title', 'owner', 'color', 'archived', 'deleted_at', 'last_modified')
$qb->selectDistinct('b.id', 'title', 'owner', 'color', 'archived', 'deleted_at', 'last_modified')
//->selectAlias('1', 'shared')
->from('deck_boards', 'b')
->innerJoin('b', 'deck_board_acl', 'acl', $qb->expr()->eq('b.id', 'acl.board_id'))
Expand Down Expand Up @@ -490,4 +490,25 @@ public function transferOwnership($ownerId, $newOwnerId) {
$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;
}
}
}
18 changes: 18 additions & 0 deletions lib/Service/BoardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ public function addAcl($boardId, $type, $participant, $edit, $share, $manage) {
$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);
Expand Down Expand Up @@ -681,6 +684,7 @@ public function clone($id, $userId) {
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);
Expand Down Expand Up @@ -723,4 +727,18 @@ public function getBoardUrl($endpoint) {
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]);
}
}
2 changes: 1 addition & 1 deletion tests/integration/database/TransferOwnershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function createBoardWithExampleData() {
*/
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);
Expand Down

0 comments on commit 4615926

Please sign in to comment.