Skip to content

Commit 2bdb039

Browse files
[stable33] fix(cards): correctly copy label and description when cloning cards
Signed-off-by: Luka Trovic <luka@nextcloud.com>
1 parent f9b2a18 commit 2bdb039

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

lib/Service/CardService.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,11 @@ public function cloneCard(int $id, ?int $targetStackId = null):Card {
374374
}
375375
$this->assignmentService->assignUser($newCard->getId(), $assignement->getParticipant());
376376
}
377-
$newCard->setDescription($originCard->getDescription());
378-
$card = $this->enrichCards([$this->cardMapper->update($newCard)]);
377+
378+
$freshCard = $this->cardMapper->find($newCard->getId());
379+
$freshCard->setDescription($originCard->getDescription());
380+
$card = $this->enrichCards([$this->cardMapper->update($freshCard)]);
381+
379382
return $card[0];
380383
}
381384

lib/Service/LabelService.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,13 @@
1717

1818
class LabelService {
1919

20-
/** @var LabelMapper */
21-
private $labelMapper;
22-
/** @var PermissionService */
23-
private $permissionService;
24-
/** @var BoardService */
25-
private $boardService;
26-
/** @var ChangeHelper */
27-
private $changeHelper;
28-
/** @var LabelServiceValidator */
29-
private LabelServiceValidator $labelServiceValidator;
30-
3120
public function __construct(
32-
LabelMapper $labelMapper,
33-
PermissionService $permissionService,
34-
BoardService $boardService,
35-
ChangeHelper $changeHelper,
36-
LabelServiceValidator $labelServiceValidator,
21+
private LabelMapper $labelMapper,
22+
private PermissionService $permissionService,
23+
private BoardService $boardService,
24+
private ChangeHelper $changeHelper,
25+
private LabelServiceValidator $labelServiceValidator,
3726
) {
38-
$this->labelMapper = $labelMapper;
39-
$this->permissionService = $permissionService;
40-
$this->boardService = $boardService;
41-
$this->changeHelper = $changeHelper;
42-
$this->labelServiceValidator = $labelServiceValidator;
4327
}
4428

4529
/**
@@ -90,10 +74,10 @@ public function cloneLabelIfNotExists(int $labelId, int $targetBoardId): Label {
9074
$originLabel = $this->find($labelId);
9175
$filteredValues = array_values(array_filter($boardLabels, fn ($item) => $item->getTitle() === $originLabel->getTitle()));
9276
if (empty($filteredValues)) {
93-
$label = $this->create($originLabel->getTitle(), $originLabel->getColor(), $targetBoardId);
94-
return $label;
77+
return $this->create($originLabel->getTitle(), $originLabel->getColor(), $targetBoardId);
9578
}
96-
return $originLabel;
79+
80+
return $filteredValues[0];
9781
}
9882

9983
/**

tests/unit/Service/CardServiceTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,22 @@ public function testClone() {
250250
$card->setOrder(0);
251251
$card->setOwner('admin');
252252
$card->setStackId(12345);
253+
$card->setDescription('A test description');
254+
253255
$clonedCard = clone $card;
254256
$clonedCard->setId(2);
255257
$clonedCard->setStackId(1234);
258+
256259
$this->cardMapper->expects($this->exactly(2))
257260
->method('insert')
258261
->willReturn($card, $clonedCard);
259262

260263
$this->cardMapper->expects($this->once())
261264
->method('update')->willReturn($clonedCard);
262-
$this->cardMapper->expects($this->exactly(2))
265+
266+
$this->cardMapper->expects($this->exactly(3))
263267
->method('find')
264-
->willReturn($card, $clonedCard);
268+
->willReturn($card, $clonedCard, $clonedCard);
265269

266270
$this->cardMapper->expects($this->any())
267271
->method('findBoardId')
@@ -287,6 +291,10 @@ public function testClone() {
287291
->with(1)
288292
->willReturn([$a1]);
289293

294+
$this->assignedUsersMapper->expects($this->any())
295+
->method('findIn')
296+
->willReturn([]);
297+
290298
// check if labels get cloned
291299
$label = new Label();
292300
$label->setId(1);
@@ -297,16 +305,31 @@ public function testClone() {
297305
->method('assignLabel')
298306
->with($clonedCard->getId(), $label->getId());
299307

308+
$labelForClone = Label::fromRow([
309+
'id' => 1,
310+
'boardId' => 1234,
311+
'cardId' => 2,
312+
]);
313+
$this->labelMapper->expects($this->any())
314+
->method('findAssignedLabelsForCards')
315+
->willReturn([$labelForClone]);
316+
300317
$stackMock = new Stack();
301318
$stackMock->setBoardId(1234);
302319
$this->stackMapper->expects($this->any())
303320
->method('find')
304321
->willReturn($stackMock);
322+
305323
$b = $this->cardService->create('Card title', 123, 'text', 999, 'admin');
306324
$c = $this->cardService->cloneCard($b->getId(), 1234);
307325
$this->assertEquals($b->getTitle(), $c->getTitle());
308326
$this->assertEquals($b->getOwner(), $c->getOwner());
309327
$this->assertNotEquals($b->getStackId(), $c->getStackId());
328+
329+
$this->assertEquals('A test description', $c->getDescription());
330+
331+
$this->assertCount(1, $c->getLabels());
332+
$this->assertEquals($label->getId(), $c->getLabels()[0]->getId());
310333
}
311334

312335
public function testDelete() {

0 commit comments

Comments
 (0)