From df990d772d9a9f26cce4e4ced74a7b1d3e439369 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Wed, 11 Sep 2024 18:21:12 +0200 Subject: [PATCH] refactor: move imap flag encoding to helper Signed-off-by: Daniel Kesselberg --- lib/Exception/ImapFlagEncodingException.php | 21 ++++++++++ lib/IMAP/ImapFlag.php | 28 +++++++++++++ lib/Service/MailManager.php | 14 ++++--- tests/Unit/IMAP/ImapFlagTest.php | 46 +++++++++++++++++++++ tests/Unit/Service/MailManagerTest.php | 6 ++- 5 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 lib/Exception/ImapFlagEncodingException.php create mode 100644 lib/IMAP/ImapFlag.php create mode 100644 tests/Unit/IMAP/ImapFlagTest.php diff --git a/lib/Exception/ImapFlagEncodingException.php b/lib/Exception/ImapFlagEncodingException.php new file mode 100644 index 0000000000..5fd2e08bf0 --- /dev/null +++ b/lib/Exception/ImapFlagEncodingException.php @@ -0,0 +1,21 @@ +imapClientFactory = $imapClientFactory; $this->mailboxMapper = $mailboxMapper; $this->mailboxSync = $mailboxSync; @@ -795,12 +798,11 @@ public function isPermflagsEnabled(Horde_Imap_Client_Socket $client, Account $ac } public function createTag(string $displayName, string $color, string $userId): Tag { - $imapLabel = str_replace(' ', '_', $displayName); - $imapLabel = mb_convert_encoding($imapLabel, 'UTF7-IMAP', 'UTF-8'); - if ($imapLabel === false) { - throw new ClientException('Error converting display name to UTF7-IMAP ', 0); + try { + $imapLabel = $this->imapFlag->create($displayName); + } catch (ImapFlagEncodingException $e) { + throw new ClientException('Error converting display name to UTF7-IMAP ', 0, $e); } - $imapLabel = '$' . strtolower(mb_strcut($imapLabel, 0, 63)); try { return $this->getTagByImapLabel($imapLabel, $userId); diff --git a/tests/Unit/IMAP/ImapFlagTest.php b/tests/Unit/IMAP/ImapFlagTest.php new file mode 100644 index 0000000000..3abc3e0740 --- /dev/null +++ b/tests/Unit/IMAP/ImapFlagTest.php @@ -0,0 +1,46 @@ +imapFlag = new ImapFlag(); + } + + /** + * @dataProvider dataCreate + */ + public function testCreate(string $label, string $expected): void { + $actual = $this->imapFlag->create($label); + $this->assertEquals($expected, $actual); + } + + public function dataCreate(): array { + return [ + 'umlauts and lowercase' => [ + 'Test ÄÖÜ', + '$test_&amqa1gdc-' + ], + 'maximum 63 characters' => [ + '1234567890123456789012345678901234567890123456789012345678901234', + '$123456789012345678901234567890123456789012345678901234567890123', + ], + ]; + } +} diff --git a/tests/Unit/Service/MailManagerTest.php b/tests/Unit/Service/MailManagerTest.php index 2c35f3caa7..7c390d253c 100644 --- a/tests/Unit/Service/MailManagerTest.php +++ b/tests/Unit/Service/MailManagerTest.php @@ -27,6 +27,7 @@ use OCA\Mail\Folder; use OCA\Mail\IMAP\FolderMapper; use OCA\Mail\IMAP\IMAPClientFactory; +use OCA\Mail\IMAP\ImapFlag; use OCA\Mail\IMAP\MailboxSync; use OCA\Mail\IMAP\MessageMapper as ImapMessageMapper; use OCA\Mail\Service\MailManager; @@ -72,7 +73,7 @@ class MailManagerTest extends TestCase { /** @var ThreadMapper|MockObject */ private $threadMapper; - + protected function setUp(): void { parent::setUp(); @@ -100,7 +101,8 @@ protected function setUp(): void { $this->logger, $this->tagMapper, $this->messageTagsMapper, - $this->threadMapper + $this->threadMapper, + new ImapFlag(), ); }