Skip to content

Commit 527f2fe

Browse files
[stable32] fix(cards): correctly copy label and description when cloning cards
Signed-off-by: Luka Trovic <luka@nextcloud.com>
1 parent 1415d35 commit 527f2fe

3 files changed

Lines changed: 38 additions & 32 deletions

File tree

lib/Service/CardService.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,11 @@ public function cloneCard(int $id, ?int $targetStackId = null):Card {
392392
}
393393
$this->assignmentService->assignUser($newCard->getId(), $assignement->getParticipant());
394394
}
395-
$newCard->setDescription($originCard->getDescription());
396-
$card = $this->enrichCards([$this->cardMapper->update($newCard)]);
395+
396+
$freshCard = $this->cardMapper->find($newCard->getId());
397+
$freshCard->setDescription($originCard->getDescription());
398+
$card = $this->enrichCards([$this->cardMapper->update($freshCard)]);
399+
397400
return $card[0];
398401
}
399402

lib/Service/LabelService.php

Lines changed: 8 additions & 28 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
/**
@@ -59,10 +43,6 @@ public function find($labelId) {
5943
}
6044

6145
/**
62-
* @param $title
63-
* @param $color
64-
* @param $boardId
65-
* @return \OCP\AppFramework\Db\Entity
6646
* @throws StatusException
6747
* @throws \OCA\Deck\NoPermissionException
6848
* @throws \OCP\AppFramework\Db\DoesNotExistException
@@ -99,10 +79,10 @@ public function cloneLabelIfNotExists(int $labelId, int $targetBoardId): Label {
9979
$originLabel = $this->find($labelId);
10080
$filteredValues = array_values(array_filter($boardLabels, fn ($item) => $item->getTitle() === $originLabel->getTitle()));
10181
if (empty($filteredValues)) {
102-
$label = $this->create($originLabel->getTitle(), $originLabel->getColor(), $targetBoardId);
103-
return $label;
82+
return $this->create($originLabel->getTitle(), $originLabel->getColor(), $targetBoardId);
10483
}
105-
return $originLabel;
84+
85+
return $filteredValues[0];
10686
}
10787

10888
/**

tests/unit/Service/CardServiceTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,22 @@ public function testClone() {
245245
$card->setTitle('Card title');
246246
$card->setOwner('admin');
247247
$card->setStackId(12345);
248+
$card->setDescription('A test description');
249+
248250
$clonedCard = clone $card;
249251
$clonedCard->setId(2);
250252
$clonedCard->setStackId(1234);
253+
251254
$this->cardMapper->expects($this->exactly(2))
252255
->method('insert')
253256
->willReturn($card, $clonedCard);
254257

255258
$this->cardMapper->expects($this->once())
256259
->method('update')->willReturn($clonedCard);
257-
$this->cardMapper->expects($this->exactly(2))
260+
261+
$this->cardMapper->expects($this->exactly(3))
258262
->method('find')
259-
->willReturn($card, $clonedCard);
263+
->willReturn($card, $clonedCard, $clonedCard);
260264

261265
$this->cardMapper->expects($this->any())
262266
->method('findBoardId')
@@ -282,6 +286,10 @@ public function testClone() {
282286
->with(1)
283287
->willReturn([$a1]);
284288

289+
$this->assignedUsersMapper->expects($this->any())
290+
->method('findIn')
291+
->willReturn([]);
292+
285293
// check if labels get cloned
286294
$label = new Label();
287295
$label->setId(1);
@@ -293,16 +301,31 @@ public function testClone() {
293301
->with($clonedCard->getId(), $label->getId())
294302
->willReturn($label);
295303

304+
$labelForClone = Label::fromRow([
305+
'id' => 1,
306+
'boardId' => 1234,
307+
'cardId' => 2,
308+
]);
309+
$this->labelMapper->expects($this->any())
310+
->method('findAssignedLabelsForCards')
311+
->willReturn([$labelForClone]);
312+
296313
$stackMock = new Stack();
297314
$stackMock->setBoardId(1234);
298315
$this->stackMapper->expects($this->any())
299316
->method('find')
300317
->willReturn($stackMock);
318+
301319
$b = $this->cardService->create('Card title', 123, 'text', 999, 'admin');
302320
$c = $this->cardService->cloneCard($b->getId(), 1234);
303321
$this->assertEquals($b->getTitle(), $c->getTitle());
304322
$this->assertEquals($b->getOwner(), $c->getOwner());
305323
$this->assertNotEquals($b->getStackId(), $c->getStackId());
324+
325+
$this->assertEquals('A test description', $c->getDescription());
326+
327+
$this->assertCount(1, $c->getLabels());
328+
$this->assertEquals($label->getId(), $c->getLabels()[0]->getId());
306329
}
307330

308331
public function testDelete() {

0 commit comments

Comments
 (0)