diff --git a/lib/Service/CardService.php b/lib/Service/CardService.php index 9f1576a2e..283dfd1a3 100644 --- a/lib/Service/CardService.php +++ b/lib/Service/CardService.php @@ -408,11 +408,12 @@ public function rename($id, $title) { if ($card->getArchived()) { throw new StatusException('Operation not allowed. This card is archived.'); } + $cardBefore = clone $card; $card->setTitle($title); $this->changeHelper->cardChanged($card->getId(), false); $update = $this->cardMapper->update($card); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); return $update; } @@ -449,6 +450,8 @@ public function reorder($id, $stackId, $order) { $changes->setAfter($card); $this->activityManager->triggerUpdateEvents(ActivityManager::DECK_OBJECT_CARD, $changes, ActivityManager::SUBJECT_CARD_UPDATE); + $cardBefore = clone $card; + $cards = $this->cardMapper->findAll($stackId); $result = []; $i = 0; @@ -472,7 +475,7 @@ public function reorder($id, $stackId, $order) { $result[$card->getOrder()] = $card; } $this->changeHelper->cardChanged($id, false); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); return array_values($result); } @@ -495,13 +498,16 @@ public function archive($id) { throw new StatusException('Operation not allowed. This board is archived.'); } $card = $this->cardMapper->find($id); + + $cardBefore = clone $card; + $card->setArchived(true); $newCard = $this->cardMapper->update($card); $this->notificationHelper->markDuedateAsRead($card); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $newCard, ActivityManager::SUBJECT_CARD_UPDATE_ARCHIVE); $this->changeHelper->cardChanged($id, false); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); return $newCard; } @@ -524,12 +530,15 @@ public function unarchive($id) { throw new StatusException('Operation not allowed. This board is archived.'); } $card = $this->cardMapper->find($id); + + $cardBefore = clone $card; + $card->setArchived(false); $newCard = $this->cardMapper->update($card); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $newCard, ActivityManager::SUBJECT_CARD_UPDATE_UNARCHIVE); $this->changeHelper->cardChanged($id, false); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); return $newCard; } @@ -549,13 +558,16 @@ public function done(int $id): Card { throw new StatusException('Operation not allowed. This board is archived.'); } $card = $this->cardMapper->find($id); + + $cardBefore = clone $card; + $card->setDone(new \DateTime()); $newCard = $this->cardMapper->update($card); $this->notificationHelper->markDuedateAsRead($card); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $newCard, ActivityManager::SUBJECT_CARD_UPDATE_DONE); $this->changeHelper->cardChanged($id, false); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); return $newCard; } @@ -575,12 +587,15 @@ public function undone(int $id): Card { throw new StatusException('Operation not allowed. This board is archived.'); } $card = $this->cardMapper->find($id); + + $cardBefore = clone $card; + $card->setDone(null); $newCard = $this->cardMapper->update($card); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $newCard, ActivityManager::SUBJECT_CARD_UPDATE_UNDONE); $this->changeHelper->cardChanged($id, false); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); return $newCard; } @@ -606,12 +621,15 @@ public function assignLabel($cardId, $labelId) { if ($card->getArchived()) { throw new StatusException('Operation not allowed. This card is archived.'); } + + $cardBefore = clone $card; + $label = $this->labelMapper->find($labelId); $this->cardMapper->assignLabel($cardId, $labelId); $this->changeHelper->cardChanged($cardId); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_LABEL_ASSIGN, ['label' => $label]); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); } /** @@ -635,12 +653,15 @@ public function removeLabel($cardId, $labelId) { if ($card->getArchived()) { throw new StatusException('Operation not allowed. This card is archived.'); } + + $cardBefore = clone $card; + $label = $this->labelMapper->find($labelId); $this->cardMapper->removeLabel($cardId, $labelId); $this->changeHelper->cardChanged($cardId); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_LABEL_UNASSING, ['label' => $label]); - $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); + $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $cardBefore)); } public function getCardUrl($cardId) { diff --git a/tests/unit/Service/CardServiceTest.php b/tests/unit/Service/CardServiceTest.php index e5600b3ae..34246d7f1 100644 --- a/tests/unit/Service/CardServiceTest.php +++ b/tests/unit/Service/CardServiceTest.php @@ -36,6 +36,9 @@ use OCA\Deck\Db\LabelMapper; use OCA\Deck\Db\Stack; use OCA\Deck\Db\StackMapper; +use OCA\Deck\Event\CardCreatedEvent; +use OCA\Deck\Event\CardDeletedEvent; +use OCA\Deck\Event\CardUpdatedEvent; use OCA\Deck\Model\CardDetails; use OCA\Deck\Notification\NotificationHelper; use OCA\Deck\StatusException; @@ -216,6 +219,15 @@ public function testCreate() { $this->cardMapper->expects($this->once()) ->method('insert') ->willReturn($card); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) use ($card) { + $this->assertInstanceOf(CardCreatedEvent::class, $event); + $this->assertEquals($card->getTitle(), $event->getCard()->getTitle()); + return true; + })); + $b = $this->cardService->create('Card title', 123, 'text', 999, 'admin'); $this->assertEquals($b->getTitle(), 'Card title'); @@ -288,7 +300,15 @@ public function testDelete() { $this->cardMapper->expects($this->once()) ->method('update') ->willReturn($cardToBeDeleted); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return $event instanceof CardDeletedEvent; + })); + $this->cardService->delete(123); + $this->assertTrue($cardToBeDeleted->getDeletedAt() <= time(), 'deletedAt is in the past'); } @@ -300,7 +320,15 @@ public function testUpdate() { $this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function ($c) { return $c; }); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return $event instanceof CardUpdatedEvent; + })); + $actual = $this->cardService->update(123, 'newtitle', 234, 'text', 'admin', 'foo', 999, '2017-01-01 00:00:00', null); + $this->assertEquals('newtitle', $actual->getTitle()); $this->assertEquals(234, $actual->getStackId()); $this->assertEquals('text', $actual->getType()); @@ -327,7 +355,17 @@ public function testRename() { $this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function ($c) { return $c; }); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return ($event instanceof CardUpdatedEvent) + && ($event->getCard()->getTitle() == '') + && ($event->getCardBefore()->getTitle() == 'title'); + })); + $actual = $this->cardService->rename(123, 'newtitle'); + $this->assertEquals('newtitle', $actual->getTitle()); } @@ -356,7 +394,15 @@ public function testReorder($cardId, $newPosition, $order) { $card = new Card(); $card->setStackId(123); $this->cardMapper->expects($this->once())->method('find')->willReturn($card); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return $event instanceof CardUpdatedEvent; + })); + $result = $this->cardService->reorder($cardId, 123, $newPosition); + foreach ($result as $card) { $actual[$card->getOrder()] = $card->getId(); } @@ -393,6 +439,12 @@ public function testArchive() { $this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function ($c) { return $c; }); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return $event instanceof CardUpdatedEvent; + })); $this->assertTrue($this->cardService->archive(123)->getArchived()); } public function testUnarchive() { @@ -403,6 +455,12 @@ public function testUnarchive() { $this->cardMapper->expects($this->once())->method('update')->willReturnCallback(function ($c) { return $c; }); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return $event instanceof CardUpdatedEvent; + })); $this->assertFalse($this->cardService->unarchive(123)->getArchived()); } @@ -411,6 +469,12 @@ public function testAssignLabel() { $card->setArchived(false); $this->cardMapper->expects($this->once())->method('find')->willReturn($card); $this->cardMapper->expects($this->once())->method('assignLabel'); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return $event instanceof CardUpdatedEvent; + })); $this->cardService->assignLabel(123, 999); } @@ -428,6 +492,12 @@ public function testRemoveLabel() { $card->setArchived(false); $this->cardMapper->expects($this->once())->method('find')->willReturn($card); $this->cardMapper->expects($this->once())->method('removeLabel'); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(function ($event) { + return $event instanceof CardUpdatedEvent; + })); $this->cardService->removeLabel(123, 999); }