Skip to content

Commit

Permalink
Merge pull request #10052 from nextcloud/backport/10051/stable3.7
Browse files Browse the repository at this point in the history
[stable3.7] fix: don't fail on missing mailbox stats
  • Loading branch information
st3iny authored Aug 26, 2024
2 parents d677efb + 792d5c9 commit cbe761e
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 @@ -163,7 +163,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 @@ -175,6 +175,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 @@ -247,4 +247,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 cbe761e

Please sign in to comment.