Skip to content

Commit

Permalink
Merge pull request #10051 from nextcloud/fix/missing-stats
Browse files Browse the repository at this point in the history
fix: don't fail on missing mailbox stats
  • Loading branch information
st3iny authored Aug 26, 2024
2 parents b5a591c + 1a613e8 commit 7e64fbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/IMAP/MailboxSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function sync(Account $account,
public function syncStats(Account $account, Mailbox $mailbox): void {
$client = $this->imapClientFactory->getClient($account);
try {
$stats = $this->folderMapper->getFoldersStatusAsObject($client, [$mailbox->getName()])[$mailbox->getName()];
$allStats = $this->folderMapper->getFoldersStatusAsObject($client, [$mailbox->getName()]);
} catch (Horde_Imap_Client_Exception $e) {
$id = $mailbox->getId();
throw new ServiceException(
Expand All @@ -159,6 +159,11 @@ public function syncStats(Account $account, Mailbox $mailbox): void {
$client->logout();
}

if (!isset($allStats[$mailbox->getName()])) {
return;
}

$stats = $allStats[$mailbox->getName()];
$mailbox->setMessages($stats->getTotal());
$mailbox->setUnseen($stats->getUnread());
$this->mailboxMapper->update($mailbox);
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/IMAP/MailboxSyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,29 @@ public function testSyncStats(): void {
$this->assertEquals(42, $mailbox->getMessages());
$this->assertEquals(10, $mailbox->getUnseen());
}

public function testSyncStatsWithNoStats(): void {
$account = $this->createMock(Account::class);
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$this->imapClientFactory->expects($this->once())
->method('getClient')
->with($account)
->willReturn($client);
$stats = new MailboxStats(42, 10, null);
$mailbox = new Mailbox();
$mailbox->setMessages(10);
$mailbox->setUnseen(6);
$mailbox->setName('mailbox');
$this->folderMapper->expects(self::once())
->method('getFoldersStatusAsObject')
->with($client, [$mailbox->getName()])
->willReturn(['otherMailbox' => $stats]);
$this->mailboxMapper->expects(self::never())
->method('update');

$this->sync->syncStats($account, $mailbox);

$this->assertEquals(10, $mailbox->getMessages());
$this->assertEquals(6, $mailbox->getUnseen());
}
}

0 comments on commit 7e64fbd

Please sign in to comment.