diff --git a/lib/Service/CardService.php b/lib/Service/CardService.php index 5b8130878..99b1ad2c7 100644 --- a/lib/Service/CardService.php +++ b/lib/Service/CardService.php @@ -89,8 +89,11 @@ public function enrichCards(array $cards): array { // TODO We should find a better way just to get the comment count so we can save 1-3 queries per card here $countComments = $this->commentsManager->getNumberOfCommentsForObject('deckCard', (string)$card->getId()); - $lastRead = $countComments > 0 ? $this->commentsManager->getReadMark('deckCard', (string)$card->getId(), $user) : null; - $countUnreadComments = $lastRead ? $this->commentsManager->getNumberOfCommentsForObject('deckCard', (string)$card->getId(), $lastRead) : 0; + $lastRead = null; + if ($countComments > 0 && $user !== null) { + $lastRead = $this->commentsManager->getReadMark('deckCard', (string)$card->getId(), $user); + } + $countUnreadComments = $lastRead !== null ? $this->commentsManager->getNumberOfCommentsForObject('deckCard', (string)$card->getId(), $lastRead) : 0; $card->setCommentsUnread($countUnreadComments); $card->setCommentsCount($countComments); diff --git a/tests/unit/Service/BatchQueryPerformanceTest.php b/tests/unit/Service/BatchQueryPerformanceTest.php index 6ed201af2..0e545fb2f 100644 --- a/tests/unit/Service/BatchQueryPerformanceTest.php +++ b/tests/unit/Service/BatchQueryPerformanceTest.php @@ -167,6 +167,32 @@ public function testCountForCardsWithEmptyInputReturnsEmptyWithoutAnyQuery(): vo $this->assertSame([], $this->attachmentService->countForCards([])); } + public function testEnrichCardsSkipsReadMarkLookupWhenUserIsMissing(): void { + $cards = $this->buildCards(1, stackId: 1); + $stack = new Stack(); + $stack->setId(1); + $stack->setBoardId(1); + + $this->userManager->method('get')->willReturn(null); + $this->commentsManager->expects($this->once()) + ->method('getNumberOfCommentsForObject') + ->with('deckCard', (string)$cards[0]->getId()) + ->willReturn(2); + $this->commentsManager->expects($this->never())->method('getReadMark'); + $this->stackMapper->method('findByIds')->willReturn([1 => $stack]); + $this->attachmentServiceMock->method('countForCards')->willReturn([1 => 0]); + $this->labelMapper->method('findAssignedLabelsForCards')->willReturn([]); + $this->assignedUsersMapper->method('findIn')->willReturn([]); + $this->boardService->method('find')->willReturn($this->createMock(Board::class)); + $this->referenceManager->method('extractReferences')->willReturn([]); + + $result = $this->cardService->enrichCards($cards); + + $this->assertCount(1, $result); + $this->assertSame(0, $result[0]->getCommentsUnread()); + $this->assertSame(2, $result[0]->getCommentsCount()); + } + /** * enrichCards() with 100 cards (one board's worth) must call * AttachmentService::countForCards() exactly once, passing all 100 IDs