Move logic for return path fallback address from method classes to factory

[MAILPOET-4115]
This commit is contained in:
Rostislav Wolny
2022-03-31 15:12:05 +02:00
committed by Veljko V
parent b8bfa60634
commit 36edab34e6
8 changed files with 14 additions and 51 deletions

View File

@@ -47,7 +47,7 @@ class MailerFactory {
$sender = $this->getSenderNameAndAddress($sender); $sender = $this->getSenderNameAndAddress($sender);
$replyTo = $this->getReplyToNameAndAddress($sender, $replyTo); $replyTo = $this->getReplyToNameAndAddress($sender, $replyTo);
$mailerConfig = $mailerConfig ?? $this->getMailerConfig(); $mailerConfig = $mailerConfig ?? $this->getMailerConfig();
$returnPath = $returnPath ?? $this->getReturnPathAddress(); $returnPath = $returnPath ?? $this->getReturnPathAddress($sender);
switch ($mailerConfig['method']) { switch ($mailerConfig['method']) {
case Mailer::METHOD_AMAZONSES: case Mailer::METHOD_AMAZONSES:
$mailerInstance = new AmazonSES( $mailerInstance = new AmazonSES(
@@ -147,9 +147,9 @@ class MailerFactory {
]; ];
} }
private function getReturnPathAddress(): ?string { private function getReturnPathAddress(array $sender): ?string {
$bounceAddress = $this->settings->get('bounce.address'); $bounceAddress = $this->settings->get('bounce.address');
return $this->wp->isEmail($bounceAddress) ? $bounceAddress : null; return $this->wp->isEmail($bounceAddress) ? $bounceAddress : $sender['from_email'];
} }
private function encodeAddressNamePart($name): string { private function encodeAddressNamePart($name): string {

View File

@@ -83,9 +83,7 @@ class AmazonSES implements MailerMethod {
$this->url = 'https://' . $this->awsEndpoint; $this->url = 'https://' . $this->awsEndpoint;
$this->sender = $sender; $this->sender = $sender;
$this->replyTo = $replyTo; $this->replyTo = $replyTo;
$this->returnPath = ($returnPath) ? $this->returnPath = $returnPath;
$returnPath :
$this->sender['from_email'];
$this->date = gmdate('Ymd\THis\Z'); $this->date = gmdate('Ymd\THis\Z');
$this->dateWithoutTime = gmdate('Ymd'); $this->dateWithoutTime = gmdate('Ymd');
$this->errorMapper = $errorMapper; $this->errorMapper = $errorMapper;

View File

@@ -29,9 +29,7 @@ class PHPMail implements MailerMethod {
) { ) {
$this->sender = $sender; $this->sender = $sender;
$this->replyTo = $replyTo; $this->replyTo = $replyTo;
$this->returnPath = ($returnPath) ? $this->returnPath = $returnPath;
$returnPath :
$this->sender['from_email'];
$this->mailer = $this->buildMailer(); $this->mailer = $this->buildMailer();
$this->errorMapper = $errorMapper; $this->errorMapper = $errorMapper;
$this->blacklist = new BlacklistCheck(); $this->blacklist = new BlacklistCheck();

View File

@@ -55,9 +55,7 @@ class SMTP implements MailerMethod {
$this->encryption = $encryption; $this->encryption = $encryption;
$this->sender = $sender; $this->sender = $sender;
$this->replyTo = $replyTo; $this->replyTo = $replyTo;
$this->returnPath = ($returnPath) ? $this->returnPath = $returnPath;
$returnPath :
$this->sender['from_email'];
$this->mailer = $this->buildMailer(); $this->mailer = $this->buildMailer();
$this->mailerLogger = new Swift_Plugins_Loggers_ArrayLogger(); $this->mailerLogger = new Swift_Plugins_Loggers_ArrayLogger();
$this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->mailerLogger)); $this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->mailerLogger));

View File

@@ -153,6 +153,14 @@ class MailerFactoryTest extends \MailPoetTest {
]); ]);
} }
public function testItIgnoresInvalidBounceAddressAndUsesSenderAddressInstead() {
$this->settings->set('bounce.address', 'invalid');
$mailer = $this->factory->getDefaultMailer();
$mailerMethod = $mailer->mailerInstance;
$this->assertInstanceOf(PHPMail::class, $mailerMethod);
expect($mailerMethod->returnPath)->equals('sender@email.com');
}
public function testItUsesSenderAddressInReplyToInCaseReplyToHasOnlyName() { public function testItUsesSenderAddressInReplyToInCaseReplyToHasOnlyName() {
$this->settings->set('reply_to', ['name' => 'Reply To']); $this->settings->set('reply_to', ['name' => 'Reply To']);
$mailer = $this->factory->getDefaultMailer(); $mailer = $this->factory->getDefaultMailer();

View File

@@ -79,19 +79,6 @@ class AmazonSESTest extends \MailPoetTest {
expect(preg_match('!^\d{8}$!', $this->mailer->dateWithoutTime))->equals(1); expect(preg_match('!^\d{8}$!', $this->mailer->dateWithoutTime))->equals(1);
} }
public function testWhenReturnPathIsNullItIsSetToSenderEmail() {
$mailer = new AmazonSES(
$this->settings['region'],
$this->settings['access_key'],
$this->settings['secret_key'],
$this->sender,
$this->replyTo,
$returnPath = false,
new AmazonSESMapper()
);
expect($mailer->returnPath)->equals($this->sender['from_email']);
}
public function testItChecksForValidRegion() { public function testItChecksForValidRegion() {
try { try {
$mailer = new AmazonSES( $mailer = new AmazonSES(

View File

@@ -58,16 +58,6 @@ class PHPMailTest extends \MailPoetTest {
expect($mailer->Mailer)->equals('mail'); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps expect($mailer->Mailer)->equals('mail'); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
} }
public function testWhenReturnPathIsNullItIsSetToSenderEmail() {
$mailer = new PHPMail(
$this->sender,
$this->replyTo,
$returnPath = false,
new PHPMailMapper()
);
expect($mailer->returnPath)->equals($this->sender['from_email']);
}
public function testItCanConfigureMailerWithMessage() { public function testItCanConfigureMailerWithMessage() {
$mailer = $this->mailer $mailer = $this->mailer
->configureMailerWithMessage($this->newsletter, $this->subscriber, $this->extraParams); ->configureMailerWithMessage($this->newsletter, $this->subscriber, $this->extraParams);

View File

@@ -87,22 +87,6 @@ class SMTPTest extends \MailPoetTest {
->equals($this->settings['encryption']); ->equals($this->settings['encryption']);
} }
public function testWhenReturnPathIsNullItIsSetToSenderEmail() {
$mailer = new SMTP(
$this->settings['host'],
$this->settings['port'],
$this->settings['authentication'],
$this->settings['encryption'],
$this->sender,
$this->replyTo,
$returnPath = false,
new SMTPMapper(),
$this->settings['login'],
$this->settings['password']
);
expect($mailer->returnPath)->equals($this->sender['from_email']);
}
public function testItCanCreateMessage() { public function testItCanCreateMessage() {
$message = $this->mailer $message = $this->mailer
->createMessage($this->newsletter, $this->subscriber, $this->extraParams); ->createMessage($this->newsletter, $this->subscriber, $this->extraParams);