public function testRecipients()
 {
     $user = $this->generateNewTestUser();
     $phid = $user->getPHID();
     $mailer = new PhabricatorMailImplementationTestAdapter();
     $mail = new PhabricatorMetaMTAMail();
     $mail->addTos(array($phid));
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"To" is a recipient.'));
     // Test that the "No Self Mail" and "No Mail" preferences work correctly.
     $mail->setFrom($phid);
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" does not exclude recipients by default.'));
     $user = $this->writeSetting($user, PhabricatorEmailSelfActionsSetting::SETTINGKEY, true);
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('"From" excludes recipients with no-self-mail set.'));
     $user = $this->writeSetting($user, PhabricatorEmailSelfActionsSetting::SETTINGKEY, null);
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" does not exclude recipients by default.'));
     $user = $this->writeSetting($user, PhabricatorEmailNotificationsSetting::SETTINGKEY, true);
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('"From" excludes recipients with no-mail set.'));
     $mail->setForceDelivery(true);
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" includes no-mail recipients when forced.'));
     $mail->setForceDelivery(false);
     $user = $this->writeSetting($user, PhabricatorEmailNotificationsSetting::SETTINGKEY, null);
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" does not exclude recipients by default.'));
     // Test that explicit exclusion works correctly.
     $mail->setExcludeMailRecipientPHIDs(array($phid));
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('Explicit exclude excludes recipients.'));
     $mail->setExcludeMailRecipientPHIDs(array());
     // Test that mail tag preferences exclude recipients.
     $user = $this->writeSetting($user, PhabricatorEmailTagsSetting::SETTINGKEY, array('test-tag' => false));
     $mail->setMailTags(array('test-tag'));
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('Tag preference excludes recipients.'));
     $user = $this->writeSetting($user, PhabricatorEmailTagsSetting::SETTINGKEY, null);
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), 'Recipients restored after tag preference removed.');
 }
 public function testRecipients()
 {
     $user = $this->generateNewTestUser();
     $phid = $user->getPHID();
     $prefs = $user->loadPreferences();
     $mailer = new PhabricatorMailImplementationTestAdapter();
     $mail = new PhabricatorMetaMTAMail();
     $mail->addTos(array($phid));
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"To" is a recipient.'));
     // Test that the "No Self Mail" and "No Mail" preferences work correctly.
     $mail->setFrom($phid);
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" does not exclude recipients by default.'));
     $prefs->setPreference(PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL, true);
     $prefs->save();
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('"From" excludes recipients with no-self-mail set.'));
     $prefs->unsetPreference(PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL);
     $prefs->save();
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" does not exclude recipients by default.'));
     $prefs->setPreference(PhabricatorUserPreferences::PREFERENCE_NO_MAIL, true);
     $prefs->save();
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('"From" excludes recipients with no-mail set.'));
     $mail->setForceDelivery(true);
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" includes no-mail recipients when forced.'));
     $mail->setForceDelivery(false);
     $prefs->unsetPreference(PhabricatorUserPreferences::PREFERENCE_NO_MAIL);
     $prefs->save();
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), pht('"From" does not exclude recipients by default.'));
     // Test that explicit exclusion works correctly.
     $mail->setExcludeMailRecipientPHIDs(array($phid));
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('Explicit exclude excludes recipients.'));
     $mail->setExcludeMailRecipientPHIDs(array());
     // Test that mail tag preferences exclude recipients.
     $prefs->setPreference(PhabricatorUserPreferences::PREFERENCE_MAILTAGS, array('test-tag' => false));
     $prefs->save();
     $mail->setMailTags(array('test-tag'));
     $this->assertFalse(in_array($phid, $mail->buildRecipientList()), pht('Tag preference excludes recipients.'));
     $prefs->unsetPreference(PhabricatorUserPreferences::PREFERENCE_MAILTAGS);
     $prefs->save();
     $this->assertTrue(in_array($phid, $mail->buildRecipientList()), 'Recipients restored after tag preference removed.');
 }