[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/metamta/storage/__tests__/ -> PhabricatorMetaMTAMailTestCase.php (source)

   1  <?php
   2  
   3  final class PhabricatorMetaMTAMailTestCase extends PhabricatorTestCase {
   4  
   5    protected function getPhabricatorTestCaseConfiguration() {
   6      return array(
   7        self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,
   8      );
   9    }
  10  
  11    public function testMailSendFailures() {
  12      $user = $this->generateNewTestUser();
  13      $phid = $user->getPHID();
  14  
  15  
  16      // Normally, the send should succeed.
  17      $mail = new PhabricatorMetaMTAMail();
  18      $mail->addTos(array($phid));
  19  
  20      $mailer = new PhabricatorMailImplementationTestAdapter();
  21      $mail->sendNow($force = true, $mailer);
  22      $this->assertEqual(
  23        PhabricatorMetaMTAMail::STATUS_SENT,
  24        $mail->getStatus());
  25  
  26  
  27      // When the mailer fails temporarily, the mail should remain queued.
  28      $mail = new PhabricatorMetaMTAMail();
  29      $mail->addTos(array($phid));
  30  
  31      $mailer = new PhabricatorMailImplementationTestAdapter();
  32      $mailer->setFailTemporarily(true);
  33      try {
  34        $mail->sendNow($force = true, $mailer);
  35      } catch (Exception $ex) {
  36        // Ignore.
  37      }
  38      $this->assertEqual(
  39        PhabricatorMetaMTAMail::STATUS_QUEUE,
  40        $mail->getStatus());
  41  
  42  
  43      // When the mailer fails permanently, the mail should be failed.
  44      $mail = new PhabricatorMetaMTAMail();
  45      $mail->addTos(array($phid));
  46  
  47      $mailer = new PhabricatorMailImplementationTestAdapter();
  48      $mailer->setFailPermanently(true);
  49      try {
  50        $mail->sendNow($force = true, $mailer);
  51      } catch (Exception $ex) {
  52        // Ignore.
  53      }
  54      $this->assertEqual(
  55        PhabricatorMetaMTAMail::STATUS_FAIL,
  56        $mail->getStatus());
  57    }
  58  
  59    public function testRecipients() {
  60      $user = $this->generateNewTestUser();
  61      $phid = $user->getPHID();
  62  
  63      $prefs = $user->loadPreferences();
  64  
  65      $mailer = new PhabricatorMailImplementationTestAdapter();
  66  
  67      $mail = new PhabricatorMetaMTAMail();
  68      $mail->addTos(array($phid));
  69  
  70      $this->assertTrue(
  71        in_array($phid, $mail->buildRecipientList()),
  72        '"To" is a recipient.');
  73  
  74  
  75      // Test that the "No Self Mail" and "No Mail" preferences work correctly.
  76      $mail->setFrom($phid);
  77  
  78      $this->assertTrue(
  79        in_array($phid, $mail->buildRecipientList()),
  80        '"From" does not exclude recipients by default.');
  81  
  82      $prefs->setPreference(
  83        PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL,
  84        true);
  85      $prefs->save();
  86  
  87      $this->assertFalse(
  88        in_array($phid, $mail->buildRecipientList()),
  89        '"From" excludes recipients with no-self-mail set.');
  90  
  91      $prefs->unsetPreference(
  92        PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL);
  93      $prefs->save();
  94  
  95      $this->assertTrue(
  96        in_array($phid, $mail->buildRecipientList()),
  97        '"From" does not exclude recipients by default.');
  98  
  99      $prefs->setPreference(
 100        PhabricatorUserPreferences::PREFERENCE_NO_MAIL,
 101        true);
 102      $prefs->save();
 103  
 104      $this->assertFalse(
 105        in_array($phid, $mail->buildRecipientList()),
 106        '"From" excludes recipients with no-mail set.');
 107  
 108      $mail->setForceDelivery(true);
 109  
 110      $this->assertTrue(
 111        in_array($phid, $mail->buildRecipientList()),
 112        '"From" includes no-mail recipients when forced.');
 113  
 114      $mail->setForceDelivery(false);
 115  
 116      $prefs->unsetPreference(
 117        PhabricatorUserPreferences::PREFERENCE_NO_MAIL);
 118      $prefs->save();
 119  
 120      $this->assertTrue(
 121        in_array($phid, $mail->buildRecipientList()),
 122        '"From" does not exclude recipients by default.');
 123  
 124  
 125      // Test that explicit exclusion works correctly.
 126      $mail->setExcludeMailRecipientPHIDs(array($phid));
 127  
 128      $this->assertFalse(
 129        in_array($phid, $mail->buildRecipientList()),
 130        'Explicit exclude excludes recipients.');
 131  
 132      $mail->setExcludeMailRecipientPHIDs(array());
 133  
 134  
 135      // Test that mail tag preferences exclude recipients.
 136      $prefs->setPreference(
 137        PhabricatorUserPreferences::PREFERENCE_MAILTAGS,
 138        array(
 139          'test-tag' => false,
 140        ));
 141      $prefs->save();
 142  
 143      $mail->setMailTags(array('test-tag'));
 144  
 145      $this->assertFalse(
 146        in_array($phid, $mail->buildRecipientList()),
 147        'Tag preference excludes recipients.');
 148  
 149      $prefs->unsetPreference(PhabricatorUserPreferences::PREFERENCE_MAILTAGS);
 150      $prefs->save();
 151  
 152      $this->assertTrue(
 153        in_array($phid, $mail->buildRecipientList()),
 154        'Recipients restored after tag preference removed.');
 155    }
 156  
 157    public function testThreadIDHeaders() {
 158      $this->runThreadIDHeadersWithConfiguration(true, true);
 159      $this->runThreadIDHeadersWithConfiguration(true, false);
 160      $this->runThreadIDHeadersWithConfiguration(false, true);
 161      $this->runThreadIDHeadersWithConfiguration(false, false);
 162    }
 163  
 164    private function runThreadIDHeadersWithConfiguration(
 165      $supports_message_id,
 166      $is_first_mail) {
 167  
 168      $mailer = new PhabricatorMailImplementationTestAdapter(
 169        array(
 170          'supportsMessageIDHeader' => $supports_message_id,
 171        ));
 172  
 173      $thread_id = '<[email protected]>';
 174  
 175      $mail = new PhabricatorMetaMTAMail();
 176      $mail->setThreadID($thread_id, $is_first_mail);
 177      $mail->sendNow($force = true, $mailer);
 178  
 179      $guts = $mailer->getGuts();
 180      $dict = ipull($guts['headers'], 1, 0);
 181  
 182      if ($is_first_mail && $supports_message_id) {
 183        $expect_message_id = true;
 184        $expect_in_reply_to = false;
 185        $expect_references = false;
 186      } else {
 187        $expect_message_id = false;
 188        $expect_in_reply_to = true;
 189        $expect_references = true;
 190      }
 191  
 192      $case = '<message-id = '.($supports_message_id ? 'Y' : 'N').', '.
 193              'first = '.($is_first_mail ? 'Y' : 'N').'>';
 194  
 195      $this->assertTrue(
 196        isset($dict['Thread-Index']),
 197        "Expect Thread-Index header for case {$case}.");
 198      $this->assertEqual(
 199        $expect_message_id,
 200        isset($dict['Message-ID']),
 201        "Expectation about existence of Message-ID header for case {$case}.");
 202      $this->assertEqual(
 203        $expect_in_reply_to,
 204        isset($dict['In-Reply-To']),
 205        "Expectation about existence of In-Reply-To header for case {$case}.");
 206      $this->assertEqual(
 207        $expect_references,
 208        isset($dict['References']),
 209        "Expectation about existence of References header for case {$case}.");
 210    }
 211  
 212  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1