From d5037e6667da6327ba9f26ef10320045b4b2e1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Rish=C3=B8j?= Date: Wed, 27 Apr 2016 20:04:23 +0200 Subject: [PATCH] add method to copy messages --- src/Fetch/Message.php | 20 ++++++++++++++++++ tests/Fetch/Test/MessageTest.php | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..953667a 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -794,4 +794,24 @@ public function moveToMailBox($mailbox) return $returnValue; } + + /** + * Copy a message to the given mailbox. + * + * @param $mailbox + * + * @return bool + */ + public function copyToMailBox($mailbox) + { + $currentBox = $this->imapConnection->getMailBox(); + $this->imapConnection->setMailBox($this->mailbox); + + $returnValue = imap_mail_copy($this->imapStream, $this->uid, $mailbox, CP_UID); + + $this->imapConnection->setMailBox($currentBox); + + return $returnValue; + } + } diff --git a/tests/Fetch/Test/MessageTest.php b/tests/Fetch/Test/MessageTest.php index 0cb9a76..b1c1df7 100644 --- a/tests/Fetch/Test/MessageTest.php +++ b/tests/Fetch/Test/MessageTest.php @@ -240,6 +240,41 @@ public function testMoveToMailbox() $this->assertEquals($sentFolderNumStart + 1, $server->numMessages(), 'Message moved into Sent Folder.'); } + public function testCopyToMailbox() + { + $server = ServerTest::getServer(); + + // Testing by copying message from "Test Folder" to "Sent" + + // Count Test Folder + $testFolderNumStart = $server->numMessages('Test Folder'); + $server->setMailbox('Test Folder'); + $this->assertEquals($testFolderNumStart, $server->numMessages(), 'Server presents consistent information between numMessages when mailbox set and directly queried for number of messages'); + + // Get message from Test Folder + $message = current($server->getMessages(1)); + $this->assertInstanceOf('\Fetch\Message', $message, 'Server returned Message.'); + + // Switch to Sent folder, count messages + $sentFolderNumStart = $server->numMessages('Sent'); + $server->setMailbox('Sent'); + $this->assertEquals($sentFolderNumStart, $server->numMessages(), 'Server presents consistent information between numMessages when mailbox set and directly queried for number of messages'); + + // Switch to "Flagged" folder in order to test that function properly returns to it + $this->assertTrue($server->setMailBox('Flagged Email')); + // Move the message! + $this->assertTrue($message->copyToMailBox('Sent')); + // Make sure we're still in the same folder + $this->assertEquals('Flagged Email', $server->getMailBox(), 'Returned Server back to right mailbox.'); + $this->assertAttributeEquals('Test Folder', 'mailbox', $message, 'Message mailbox remained unchanged.'); + // Make sure Test Folder didn't lose a message + $this->assertTrue($server->setMailBox('Test Folder')); + $this->assertEquals($testFolderNumStart, $server->numMessages(), 'Message copied from Test Folder.'); + // Make sure Sent folder gains one + $this->assertTrue($server->setMailBox('Sent')); + $this->assertEquals($sentFolderNumStart + 1, $server->numMessages(), 'Message copied into Sent folder.'); + } + public function testDecode() { $quotedPrintableDecoded = "Now's the time for all folk to come to the aid of their country.";