diff --git a/src/applications/metamta/adapter/PhabricatorMailTestAdapter.php b/src/applications/metamta/adapter/PhabricatorMailTestAdapter.php --- a/src/applications/metamta/adapter/PhabricatorMailTestAdapter.php +++ b/src/applications/metamta/adapter/PhabricatorMailTestAdapter.php @@ -10,115 +10,125 @@ const ADAPTERTYPE = 'test'; private $guts = array(); - private $config = array(); - protected function validateOptions(array $options) { - PhutilTypeSpec::checkMap( - $options, - array()); - } - - public function newDefaultOptions() { - return array(); - } - - public function prepareForSend(array $config = array()) { - $this->config = $config; - } + private $supportsMessageID; + private $failPermanently; + private $failTemporarily; - public function setFrom($email, $name = '') { - $this->guts['from'] = $email; - $this->guts['from-name'] = $name; + public function setSupportsMessageID($support) { + $this->supportsMessageID = $support; return $this; } - public function addReplyTo($email, $name = '') { - if (empty($this->guts['reply-to'])) { - $this->guts['reply-to'] = array(); - } - $this->guts['reply-to'][] = array( - 'email' => $email, - 'name' => $name, - ); - return $this; - } - - public function addTos(array $emails) { - foreach ($emails as $email) { - $this->guts['tos'][] = $email; - } + public function setFailPermanently($fail) { + $this->failPermanently = true; return $this; } - public function addCCs(array $emails) { - foreach ($emails as $email) { - $this->guts['ccs'][] = $email; - } + public function setFailTemporarily($fail) { + $this->failTemporarily = true; return $this; } - public function addAttachment($data, $filename, $mimetype) { - $this->guts['attachments'][] = array( - 'data' => $data, - 'filename' => $filename, - 'mimetype' => $mimetype, + public function getSupportedMessageTypes() { + return array( + PhabricatorMailEmailMessage::MESSAGETYPE, ); - return $this; } - public function addHeader($header_name, $header_value) { - $this->guts['headers'][] = array($header_name, $header_value); - return $this; - } - - public function setBody($body) { - $this->guts['body'] = $body; - return $this; + protected function validateOptions(array $options) { + PhutilTypeSpec::checkMap($options, array()); } - public function setHTMLBody($html_body) { - $this->guts['html-body'] = $html_body; - return $this; + public function newDefaultOptions() { + return array(); } - public function setSubject($subject) { - $this->guts['subject'] = $subject; - return $this; + public function supportsMessageIDHeader() { + return $this->supportsMessageID; } - public function supportsMessageIDHeader() { - return idx($this->config, 'supportsMessageIDHeader', true); + public function getGuts() { + return $this->guts; } - public function send() { - if (!empty($this->guts['fail-permanently'])) { + public function sendMessage(PhabricatorMailExternalMessage $message) { + if ($this->failPermanently) { throw new PhabricatorMetaMTAPermanentFailureException( pht('Unit Test (Permanent)')); } - if (!empty($this->guts['fail-temporarily'])) { + if ($this->failTemporarily) { throw new Exception( pht('Unit Test (Temporary)')); } - $this->guts['did-send'] = true; - return true; - } + $guts = array(); - public function getGuts() { - return $this->guts; - } + $from = $message->getFromAddress(); + $guts['from'] = (string)$from; - public function setFailPermanently($fail) { - $this->guts['fail-permanently'] = $fail; - return $this; - } + $reply_to = $message->getReplyToAddress(); + if ($reply_to) { + $guts['reply-to'] = (string)$reply_to; + } - public function setFailTemporarily($fail) { - $this->guts['fail-temporarily'] = $fail; - return $this; + $to_addresses = $message->getToAddresses(); + $to = array(); + foreach ($to_addresses as $address) { + $to[] = (string)$address; + } + $guts['tos'] = $to; + + $cc_addresses = $message->getCCAddresses(); + $cc = array(); + foreach ($cc_addresses as $address) { + $cc[] = (string)$address; + } + $guts['ccs'] = $cc; + + $subject = $message->getSubject(); + if (strlen($subject)) { + $guts['subject'] = $subject; + } + + $headers = $message->getHeaders(); + $header_list = array(); + foreach ($headers as $header) { + $header_list[] = array( + $header->getName(), + $header->getValue(), + ); + } + $guts['headers'] = $header_list; + + $text_body = $message->getTextBody(); + if (strlen($text_body)) { + $guts['body'] = $text_body; + } + + $html_body = $message->getHTMLBody(); + if (strlen($html_body)) { + $guts['html-body'] = $html_body; + } + + $attachments = $message->getAttachments(); + $file_list = array(); + foreach ($attachments as $attachment) { + $file_list[] = array( + 'data' => $attachment->getData(), + 'filename' => $attachment->getFilename(), + 'mimetype' => $attachment->getMimeType(), + ); + } + $guts['attachments'] = $file_list; + + $guts['did-send'] = true; + + $this->guts = $guts; } + public function getBody() { return idx($this->guts, 'body'); } @@ -127,4 +137,5 @@ return idx($this->guts, 'html-body'); } + } diff --git a/src/applications/metamta/receiver/__tests__/PhabricatorMailReceiverTestCase.php b/src/applications/metamta/receiver/__tests__/PhabricatorMailReceiverTestCase.php --- a/src/applications/metamta/receiver/__tests__/PhabricatorMailReceiverTestCase.php +++ b/src/applications/metamta/receiver/__tests__/PhabricatorMailReceiverTestCase.php @@ -42,10 +42,10 @@ } public function testReservedAddresses() { - $default_address = id(new PhabricatorMetaMTAMail()) + $default_address = id(new PhabricatorMailEmailEngine()) ->newDefaultEmailAddress(); - $void_address = id(new PhabricatorMetaMTAMail()) + $void_address = id(new PhabricatorMailEmailEngine()) ->newVoidEmailAddress(); $map = array( diff --git a/src/applications/metamta/storage/__tests__/PhabricatorMetaMTAMailTestCase.php b/src/applications/metamta/storage/__tests__/PhabricatorMetaMTAMailTestCase.php --- a/src/applications/metamta/storage/__tests__/PhabricatorMetaMTAMailTestCase.php +++ b/src/applications/metamta/storage/__tests__/PhabricatorMetaMTAMailTestCase.php @@ -182,21 +182,29 @@ $supports_message_id, $is_first_mail) { + $user = $this->generateNewTestUser(); + $phid = $user->getPHID(); + $mailer = new PhabricatorMailTestAdapter(); - $mailer->prepareForSend( - array( - 'supportsMessageIDHeader' => $supports_message_id, - )); + $mailer->setSupportsMessageID($supports_message_id); - $thread_id = ''; + $thread_id = 'somethread-12345'; - $mail = new PhabricatorMetaMTAMail(); - $mail->setThreadID($thread_id, $is_first_mail); - $mail->sendWithMailers(array($mailer)); + $mail = id(new PhabricatorMetaMTAMail()) + ->setThreadID($thread_id, $is_first_mail) + ->addTos(array($phid)) + ->sendWithMailers(array($mailer)); $guts = $mailer->getGuts(); - $dict = ipull($guts['headers'], 1, 0); + + $headers = idx($guts, 'headers', array()); + + $dict = array(); + foreach ($headers as $header) { + list($name, $value) = $header; + $dict[$name] = $value; + } if ($is_first_mail && $supports_message_id) { $expect_message_id = true; diff --git a/src/applications/metamta/util/PhabricatorMailUtil.php b/src/applications/metamta/util/PhabricatorMailUtil.php --- a/src/applications/metamta/util/PhabricatorMailUtil.php +++ b/src/applications/metamta/util/PhabricatorMailUtil.php @@ -93,13 +93,13 @@ return true; } - $default_address = id(new PhabricatorMetaMTAMail()) + $default_address = id(new PhabricatorMailEmailEngine()) ->newDefaultEmailAddress(); if (self::matchAddresses($address, $default_address)) { return true; } - $void_address = id(new PhabricatorMetaMTAMail()) + $void_address = id(new PhabricatorMailEmailEngine()) ->newVoidEmailAddress(); if (self::matchAddresses($address, $void_address)) { return true;