mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-10 00:42:41 +01:00
Update Mail test adapter for the newer adapter API and make all tests pass
Summary: Depends on D19956. Ref T920. Move the TestAdapter to the new API and adjust a couple of tests for the changes. Test Plan: All tests now pass. Reviewers: amckinley Reviewed By: amckinley Maniphest Tasks: T920 Differential Revision: https://secure.phabricator.com/D19957
This commit is contained in:
parent
a8657e6ab6
commit
bc97a7d755
4 changed files with 123 additions and 104 deletions
|
@ -10,114 +10,124 @@ final class PhabricatorMailTestAdapter
|
|||
const ADAPTERTYPE = 'test';
|
||||
|
||||
private $guts = array();
|
||||
private $config = array();
|
||||
|
||||
private $supportsMessageID;
|
||||
private $failPermanently;
|
||||
private $failTemporarily;
|
||||
|
||||
public function setSupportsMessageID($support) {
|
||||
$this->supportsMessageID = $support;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setFailPermanently($fail) {
|
||||
$this->failPermanently = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setFailTemporarily($fail) {
|
||||
$this->failTemporarily = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSupportedMessageTypes() {
|
||||
return array(
|
||||
PhabricatorMailEmailMessage::MESSAGETYPE,
|
||||
);
|
||||
}
|
||||
|
||||
protected function validateOptions(array $options) {
|
||||
PhutilTypeSpec::checkMap(
|
||||
$options,
|
||||
array());
|
||||
PhutilTypeSpec::checkMap($options, array());
|
||||
}
|
||||
|
||||
public function newDefaultOptions() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function prepareForSend(array $config = array()) {
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function setFrom($email, $name = '') {
|
||||
$this->guts['from'] = $email;
|
||||
$this->guts['from-name'] = $name;
|
||||
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;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCCs(array $emails) {
|
||||
foreach ($emails as $email) {
|
||||
$this->guts['ccs'][] = $email;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addAttachment($data, $filename, $mimetype) {
|
||||
$this->guts['attachments'][] = array(
|
||||
'data' => $data,
|
||||
'filename' => $filename,
|
||||
'mimetype' => $mimetype,
|
||||
);
|
||||
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;
|
||||
}
|
||||
|
||||
public function setHTMLBody($html_body) {
|
||||
$this->guts['html-body'] = $html_body;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setSubject($subject) {
|
||||
$this->guts['subject'] = $subject;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function supportsMessageIDHeader() {
|
||||
return idx($this->config, 'supportsMessageIDHeader', true);
|
||||
}
|
||||
|
||||
public function send() {
|
||||
if (!empty($this->guts['fail-permanently'])) {
|
||||
throw new PhabricatorMetaMTAPermanentFailureException(
|
||||
pht('Unit Test (Permanent)'));
|
||||
}
|
||||
|
||||
if (!empty($this->guts['fail-temporarily'])) {
|
||||
throw new Exception(
|
||||
pht('Unit Test (Temporary)'));
|
||||
}
|
||||
|
||||
$this->guts['did-send'] = true;
|
||||
return true;
|
||||
return $this->supportsMessageID;
|
||||
}
|
||||
|
||||
public function getGuts() {
|
||||
return $this->guts;
|
||||
}
|
||||
|
||||
public function setFailPermanently($fail) {
|
||||
$this->guts['fail-permanently'] = $fail;
|
||||
return $this;
|
||||
public function sendMessage(PhabricatorMailExternalMessage $message) {
|
||||
if ($this->failPermanently) {
|
||||
throw new PhabricatorMetaMTAPermanentFailureException(
|
||||
pht('Unit Test (Permanent)'));
|
||||
}
|
||||
|
||||
if ($this->failTemporarily) {
|
||||
throw new Exception(
|
||||
pht('Unit Test (Temporary)'));
|
||||
}
|
||||
|
||||
$guts = array();
|
||||
|
||||
$from = $message->getFromAddress();
|
||||
$guts['from'] = (string)$from;
|
||||
|
||||
$reply_to = $message->getReplyToAddress();
|
||||
if ($reply_to) {
|
||||
$guts['reply-to'] = (string)$reply_to;
|
||||
}
|
||||
|
||||
$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 setFailTemporarily($fail) {
|
||||
$this->guts['fail-temporarily'] = $fail;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBody() {
|
||||
return idx($this->guts, 'body');
|
||||
|
@ -127,4 +137,5 @@ final class PhabricatorMailTestAdapter
|
|||
return idx($this->guts, 'html-body');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -42,10 +42,10 @@ final class PhabricatorMailReceiverTestCase extends PhabricatorTestCase {
|
|||
}
|
||||
|
||||
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(
|
||||
|
|
|
@ -182,21 +182,29 @@ final class PhabricatorMetaMTAMailTestCase extends PhabricatorTestCase {
|
|||
$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 = '<somethread-12345@somedomain.tld>';
|
||||
$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;
|
||||
|
|
|
@ -93,13 +93,13 @@ final class PhabricatorMailUtil
|
|||
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;
|
||||
|
|
Loading…
Reference in a new issue