Skip to content

Commit

Permalink
tests: for event dispatching of card events
Browse files Browse the repository at this point in the history
Signed-off-by: nicolashimmelmann <[email protected]>
  • Loading branch information
nicolashimmelmann committed Jan 10, 2025
1 parent 58d2497 commit 2062692
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 8 deletions.
37 changes: 29 additions & 8 deletions lib/Service/CardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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));
}

/**
Expand All @@ -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) {
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/Service/CardServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
}

Expand All @@ -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());
Expand All @@ -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());
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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());
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 2062692

Please sign in to comment.