Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(imap): Ignore non existent mailboxes / no select for MYRIGHTS #8563

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/IMAP/FolderMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function getFolders(Account $account, Horde_Imap_Client_Socket $client,
]);

return array_filter(array_map(static function (array $mailbox) use ($account) {
$attributes = array_flip(array_map('strtolower', $mailbox['attributes']));
if (isset($attributes['\\nonexistent'])) {
// Ignore mailbox that does not exist, similar to \Horde_Imap_Client::MBOX_SUBSCRIBED_EXISTS mode
return null;
}
if (in_array($mailbox['mailbox']->utf8, self::DOVECOT_SIEVE_FOLDERS, true)) {
// This is a special folder that must not be shown
return null;
Expand Down Expand Up @@ -118,7 +123,7 @@ public function fetchFolderAcls(array $folders,

foreach ($folders as $folder) {
$acls = null;
if ($hasAcls) {
if ($hasAcls && !in_array('\\noselect', array_map('strtolower', $folder->getAttributes()), true)) {
$acls = (string)$client->getMyACLRights($folder->getMailbox());
}

Expand Down
68 changes: 68 additions & 0 deletions tests/Unit/IMAP/FolderMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,48 @@ public function testGetFoldersEmtpyAccount(): void {
$this->assertEquals([], $folders);
}

public function testGetFoldersNonExistent(): void {
$account = $this->createMock(Account::class);
$account->method('getId')->willReturn(27);
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$client->expects($this->once())
->method('listMailboxes')
->with($this->equalTo('*'), $this->equalTo(Horde_Imap_Client::MBOX_ALL_SUBSCRIBED),
$this->equalTo([
'delimiter' => true,
'attributes' => true,
'special_use' => true,
'status' => Horde_Imap_Client::STATUS_ALL,
]))
->willReturn([
[
'mailbox' => new Horde_Imap_Client_Mailbox('INBOX'),
'attributes' => [],
'delimiter' => '.',
'status' => [
'unseen' => 0,
],
],
[
'mailbox' => new Horde_Imap_Client_Mailbox('shared'),
'attributes' => [
'\\nonexistent',
],
'delimiter' => '.',
'status' => [
'unseen' => null,
],
],
]);
$expected = [
new Folder(27, new Horde_Imap_Client_Mailbox('INBOX'), [], '.', ['unseen' => 0]),
];

$folders = $this->mapper->getFolders($account, $client);

$this->assertEquals($expected, $folders);
}

public function testGetFolders(): void {
$account = $this->createMock(Account::class);
$account->method('getId')->willReturn(27);
Expand Down Expand Up @@ -139,6 +181,30 @@ public function testCreateFolder(): void {
$this->assertEquals($expected, $created);
}

public function testFetchFoldersAclsNoSelect(): void {
$folders = [
$this->createMock(Folder::class),
];
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$folders[0]->expects($this->any())
->method('getAttributes')
->willReturn(['\\NoSelect']);
$capability = $this->createMock(Horde_Imap_Client_Data_Capability::class);
$client
->method('__get')
->with('capability')
->willReturn($capability);
$capability
->expects(self::once())
->method('query')
->with('ACL')
->willReturn(true);
$client->expects(self::never())
->method('getMyACLRights');

$this->mapper->fetchFolderAcls($folders, $client);
}

public function testFetchFoldersAcls(): void {
$folders = [
$this->createMock(Folder::class),
Expand All @@ -157,6 +223,8 @@ public function testFetchFoldersAcls(): void {
->method('query')
->with('ACL')
->willReturn(false);
$client->expects(self::never())
->method('getMyACLRights');

$this->mapper->fetchFolderAcls($folders, $client);
}
Expand Down
Loading