diff options
-rwxr-xr-x | lib/Controller/MessagesController.php | 5 | ||||
-rw-r--r-- | lib/Db/Message.php | 4 | ||||
-rw-r--r-- | tests/Integration/Db/MessageTest.php | 21 | ||||
-rw-r--r-- | tests/Unit/Controller/MessagesControllerTest.php | 91 |
4 files changed, 116 insertions, 5 deletions
diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php index 0ec4bdea7..36945b46c 100755 --- a/lib/Controller/MessagesController.php +++ b/lib/Controller/MessagesController.php @@ -134,7 +134,6 @@ class MessagesController extends Controller { ITrustedSenderService $trustedSenderService, IMailTransmission $mailTransmission) { parent::__construct($appName, $request); - $this->accountService = $accountService; $this->mailManager = $mailManager; $this->mailSearch = $mailSearch; @@ -328,6 +327,10 @@ class MessagesController extends Controller { return new JSONResponse([], Http::STATUS_FORBIDDEN); } + if (empty($message->getThreadRootId())) { + return new JSONResponse([], Http::STATUS_NOT_FOUND); + } + return new JSONResponse($this->mailManager->getThread($account, $message->getThreadRootId())); } diff --git a/lib/Db/Message.php b/lib/Db/Message.php index 5eb1c5141..e5c60395e 100644 --- a/lib/Db/Message.php +++ b/lib/Db/Message.php @@ -174,7 +174,9 @@ class Message extends Entity implements JsonSerializable { } public function setThreadRootId(?string $messageId): void { - $this->setMessageIdFieldIfNotEmpty('threadRootId', $messageId); + $threadRootId = (!empty($messageId)) ? '<' . rtrim(ltrim($messageId, '<'), '>') . '>' : $this->getMessageId(); + $parsed = new Horde_Mail_Rfc822_Identification($threadRootId); + $this->setter('threadRootId', [$parsed->ids[0] ?? $this->getMessageId()]); } private function setMessageIdFieldIfNotEmpty(string $field, ?string $id): void { diff --git a/tests/Integration/Db/MessageTest.php b/tests/Integration/Db/MessageTest.php index be891eed1..c0e5ac09f 100644 --- a/tests/Integration/Db/MessageTest.php +++ b/tests/Integration/Db/MessageTest.php @@ -84,7 +84,20 @@ class MessageTest extends TestCase { $message->setThreadRootId(null); $this->assertEquals($expected, $message->getMessageId()); - $this->assertNull($message->getThreadRootId()); + $this->assertNotNull($message->getThreadRootId()); + $this->assertEquals($expected, $message->getThreadRootId()); + } + + public function testThreadrootIdSet(): void { + $expected = '<abc@123.com>'; + $message = new Message(); + + $message->setMessageId($expected); + $message->setThreadRootId('<cde789@test.com>'); + + $this->assertEquals($expected, $message->getMessageId()); + $this->assertNotNull($message->getThreadRootId()); + $this->assertEquals('<cde789@test.com>', $message->getThreadRootId()); } public function testThreadrootIdEmptyString(): void { @@ -95,7 +108,8 @@ class MessageTest extends TestCase { $message->setThreadRootId(''); $this->assertEquals($expected, $message->getMessageId()); - $this->assertNull($message->getThreadRootId()); + $this->assertNotNull($message->getThreadRootId()); + $this->assertEquals($expected, $message->getThreadRootId()); } public function testSetInReplyToEmpty(): void { @@ -107,7 +121,8 @@ class MessageTest extends TestCase { $message->setInReplyTo(''); $this->assertEquals($expected, $message->getMessageId()); - $this->assertNull($message->getThreadRootId()); + $this->assertNotNull($message->getThreadRootId()); + $this->assertEquals($expected, $message->getThreadRootId()); $this->assertNull($message->getInReplyTo()); } } diff --git a/tests/Unit/Controller/MessagesControllerTest.php b/tests/Unit/Controller/MessagesControllerTest.php index 1784003a9..ff6ed12b2 100644 --- a/tests/Unit/Controller/MessagesControllerTest.php +++ b/tests/Unit/Controller/MessagesControllerTest.php @@ -978,4 +978,95 @@ class MessagesControllerTest extends TestCase { $this->controller->destroy($id); } + + public function testGetThread(): void { + $accountId = 17; + $mailboxId = 987; + $id = 123; + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setThreadRootId('<marlon@slimehunter.com>'); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->willReturn($this->account); + $this->mailManager->expects($this->once()) + ->method('getThread') + ->with($this->account, $message->getThreadRootId()); + + $this->controller->getThread($id); + } + + public function testGetThreadNoThreadRootId(): void { + $accountId = 17; + $mailboxId = 987; + $id = 123; + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->willReturn($this->account); + $this->mailManager->expects($this->never()) + ->method('getThread'); + + $this->controller->getThread($id); + } + + public function testGetThreadThreadRootIdEmptyString(): void { + $accountId = 17; + $mailboxId = 987; + $id = 123; + $message = new \OCA\Mail\Db\Message(); + $message->setUid(444); + $message->setMailboxId($mailboxId); + $message->setMessageId('<123@cde.com>'); + $message->setThreadRootId(''); + $mailbox = new \OCA\Mail\Db\Mailbox(); + $mailbox->setName('INBOX'); + $mailbox->setAccountId($accountId); + + $this->mailManager->expects($this->once()) + ->method('getMessage') + ->with($this->userId, $id) + ->willReturn($message); + $this->mailManager->expects($this->once()) + ->method('getMailbox') + ->with($this->userId, $mailboxId) + ->willReturn($mailbox); + $this->accountService->expects($this->once()) + ->method('find') + ->with($this->equalTo($this->userId), $this->equalTo($accountId)) + ->willReturn($this->account); + $this->mailManager->expects($this->once()) + ->method('getThread'); + + $this->controller->getThread($id); + } } |