From 63ecbcb3b2de6e4be5a074638933e10fffa8a222 Mon Sep 17 00:00:00 2001 From: Luka Trovic Date: Fri, 20 Mar 2026 10:28:10 +0100 Subject: [PATCH] [stable32] fix(cards): correctly copy label and description when cloning cards Signed-off-by: Luka Trovic --- lib/Service/CardService.php | 7 +++-- lib/Service/LabelService.php | 36 ++++++-------------------- tests/unit/Service/CardServiceTest.php | 27 +++++++++++++++++-- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/lib/Service/CardService.php b/lib/Service/CardService.php index 1c84771a8..b8cad204a 100644 --- a/lib/Service/CardService.php +++ b/lib/Service/CardService.php @@ -392,8 +392,11 @@ public function cloneCard(int $id, ?int $targetStackId = null):Card { } $this->assignmentService->assignUser($newCard->getId(), $assignement->getParticipant()); } - $newCard->setDescription($originCard->getDescription()); - $card = $this->enrichCards([$this->cardMapper->update($newCard)]); + + $freshCard = $this->cardMapper->find($newCard->getId()); + $freshCard->setDescription($originCard->getDescription()); + $card = $this->enrichCards([$this->cardMapper->update($freshCard)]); + return $card[0]; } diff --git a/lib/Service/LabelService.php b/lib/Service/LabelService.php index 69f372c42..f5d8183ec 100644 --- a/lib/Service/LabelService.php +++ b/lib/Service/LabelService.php @@ -17,29 +17,13 @@ class LabelService { - /** @var LabelMapper */ - private $labelMapper; - /** @var PermissionService */ - private $permissionService; - /** @var BoardService */ - private $boardService; - /** @var ChangeHelper */ - private $changeHelper; - /** @var LabelServiceValidator */ - private LabelServiceValidator $labelServiceValidator; - public function __construct( - LabelMapper $labelMapper, - PermissionService $permissionService, - BoardService $boardService, - ChangeHelper $changeHelper, - LabelServiceValidator $labelServiceValidator, + private LabelMapper $labelMapper, + private PermissionService $permissionService, + private BoardService $boardService, + private ChangeHelper $changeHelper, + private LabelServiceValidator $labelServiceValidator, ) { - $this->labelMapper = $labelMapper; - $this->permissionService = $permissionService; - $this->boardService = $boardService; - $this->changeHelper = $changeHelper; - $this->labelServiceValidator = $labelServiceValidator; } /** @@ -59,10 +43,6 @@ public function find($labelId) { } /** - * @param $title - * @param $color - * @param $boardId - * @return \OCP\AppFramework\Db\Entity * @throws StatusException * @throws \OCA\Deck\NoPermissionException * @throws \OCP\AppFramework\Db\DoesNotExistException @@ -99,10 +79,10 @@ public function cloneLabelIfNotExists(int $labelId, int $targetBoardId): Label { $originLabel = $this->find($labelId); $filteredValues = array_values(array_filter($boardLabels, fn ($item) => $item->getTitle() === $originLabel->getTitle())); if (empty($filteredValues)) { - $label = $this->create($originLabel->getTitle(), $originLabel->getColor(), $targetBoardId); - return $label; + return $this->create($originLabel->getTitle(), $originLabel->getColor(), $targetBoardId); } - return $originLabel; + + return $filteredValues[0]; } /** diff --git a/tests/unit/Service/CardServiceTest.php b/tests/unit/Service/CardServiceTest.php index 0f1e1445e..14c90b663 100644 --- a/tests/unit/Service/CardServiceTest.php +++ b/tests/unit/Service/CardServiceTest.php @@ -245,18 +245,22 @@ public function testClone() { $card->setTitle('Card title'); $card->setOwner('admin'); $card->setStackId(12345); + $card->setDescription('A test description'); + $clonedCard = clone $card; $clonedCard->setId(2); $clonedCard->setStackId(1234); + $this->cardMapper->expects($this->exactly(2)) ->method('insert') ->willReturn($card, $clonedCard); $this->cardMapper->expects($this->once()) ->method('update')->willReturn($clonedCard); - $this->cardMapper->expects($this->exactly(2)) + + $this->cardMapper->expects($this->exactly(3)) ->method('find') - ->willReturn($card, $clonedCard); + ->willReturn($card, $clonedCard, $clonedCard); $this->cardMapper->expects($this->any()) ->method('findBoardId') @@ -282,6 +286,10 @@ public function testClone() { ->with(1) ->willReturn([$a1]); + $this->assignedUsersMapper->expects($this->any()) + ->method('findIn') + ->willReturn([]); + // check if labels get cloned $label = new Label(); $label->setId(1); @@ -293,16 +301,31 @@ public function testClone() { ->with($clonedCard->getId(), $label->getId()) ->willReturn($label); + $labelForClone = Label::fromRow([ + 'id' => 1, + 'boardId' => 1234, + 'cardId' => 2, + ]); + $this->labelMapper->expects($this->any()) + ->method('findAssignedLabelsForCards') + ->willReturn([$labelForClone]); + $stackMock = new Stack(); $stackMock->setBoardId(1234); $this->stackMapper->expects($this->any()) ->method('find') ->willReturn($stackMock); + $b = $this->cardService->create('Card title', 123, 'text', 999, 'admin'); $c = $this->cardService->cloneCard($b->getId(), 1234); $this->assertEquals($b->getTitle(), $c->getTitle()); $this->assertEquals($b->getOwner(), $c->getOwner()); $this->assertNotEquals($b->getStackId(), $c->getStackId()); + + $this->assertEquals('A test description', $c->getDescription()); + + $this->assertCount(1, $c->getLabels()); + $this->assertEquals($label->getId(), $c->getLabels()[0]->getId()); } public function testDelete() {