From a6c145476e51af7ee45fd3784d19b21cd76752c3 Mon Sep 17 00:00:00 2001 From: MrCasual Date: Tue, 13 Oct 2015 15:31:51 -0400 Subject: [PATCH] - Adds wp_mail() mailer + tests - Updates Mailer router + tests - Updates mailer classes + tests Closes #174 --- lib/Mailer/{API => }/MailPoet.php | 2 +- lib/Mailer/{SMTP => }/SMTP.php | 2 +- lib/Mailer/WPMail.php | 58 ++++++++++++++++ lib/Router/Mailer.php | 57 +++++++++++++--- tests/unit/Mailer/API/AmazonSESCest.php | 9 ++- tests/unit/Mailer/{API => }/MailPoetCest.php | 3 +- tests/unit/Mailer/{SMTP => }/SMTPCest.php | 3 +- tests/unit/Mailer/WPMailCest.php | 70 ++++++++++++++++++++ tests/unit/Router/MailerCest.php | 8 ++- 9 files changed, 190 insertions(+), 22 deletions(-) rename lib/Mailer/{API => }/MailPoet.php (98%) rename lib/Mailer/{SMTP => }/SMTP.php (98%) create mode 100644 lib/Mailer/WPMail.php rename tests/unit/Mailer/{API => }/MailPoetCest.php (98%) rename tests/unit/Mailer/{SMTP => }/SMTPCest.php (98%) create mode 100644 tests/unit/Mailer/WPMailCest.php diff --git a/lib/Mailer/API/MailPoet.php b/lib/Mailer/MailPoet.php similarity index 98% rename from lib/Mailer/API/MailPoet.php rename to lib/Mailer/MailPoet.php index 2e6f29a6ca..e7651a397c 100644 --- a/lib/Mailer/API/MailPoet.php +++ b/lib/Mailer/MailPoet.php @@ -1,5 +1,5 @@ fromEmail = $fromEmail; + $this->fromName = $fromName; + add_filter('wp_mail_from', array( + $this, + 'setFromEmail' + )); + $this->filters = array( + 'wp_mail_from' => 'setFromEmail', + 'wp_mail_from_name' => 'setFromName', + 'wp_mail_content_type' => 'setContentType' + ); + } + + function addFilters() { + foreach ($this->filters as $filter => $method) { + add_filter($filter, array( + $this, + $method + )); + } + } + + function removeFilters() { + foreach ($this->filters as $filter => $method) { + remove_filter($filter, array( + $this, + $method + )); + } + } + + function setFromEmail() { + return $this->fromEmail; + } + + function setFromName() { + return $this->fromName; + } + + function setContentType() { + return 'text/html'; + } + + function send($newsletter, $subscriber) { + $this->addFilters(); + $result = wp_mail($subscriber, $newsletter['subject'], $newsletter['body']['html']); + $this->removeFilters(); + return ($result === true); + } +} \ No newline at end of file diff --git a/lib/Router/Mailer.php b/lib/Router/Mailer.php index dcbe426ac0..19932d2401 100644 --- a/lib/Router/Mailer.php +++ b/lib/Router/Mailer.php @@ -20,25 +20,54 @@ class Mailer { function buildMailer() { switch ($this->mailer['name']) { case 'AmazonSES': - $mailer = new $this->mailer['class']($this->mailer['region'], $this->mailer['access_key'], $this->mailer['secret_key'], $this->from); + $mailer = new $this->mailer['class']( + $this->mailer['region'], + $this->mailer['access_key'], + $this->mailer['secret_key'], + $this->from + ); break; case 'ElasticEmail': - $mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName); + $mailer = new $this->mailer['class']( + $this->mailer['api_key'], + $this->fromEmail, $this->fromName + ); break; case 'MailGun': - $mailer = new $this->mailer['class']($this->mailer['domain'], $this->mailer['api_key'], $this->from); + $mailer = new $this->mailer['class']( + $this->mailer['domain'], + $this->mailer['api_key'], + $this->from + ); break; case 'MailPoet': - $mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName); + $mailer = new $this->mailer['class']( + $this->mailer['api_key'], + $this->fromEmail, + $this->fromName + ); break; case 'Mandrill': - $mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName); + $mailer = new $this->mailer['class']( + $this->mailer['api_key'], + $this->fromEmail, $this->fromName + ); break; case 'SendGrid': - $mailer = new $this->mailer['class']($this->mailer['api_key'], $this->fromEmail, $this->fromName); + $mailer = new $this->mailer['class']( + $this->mailer['api_key'], + $this->fromEmail, + $this->fromName + ); break; case 'SMTP': - $mailer = new $this->mailer['class']($this->mailer['host'], $this->mailer['port'], $this->mailer['authentication'], $this->mailer['encryption'], $this->fromEmail, $this->fromName); + $mailer = new $this->mailer['class']( + $this->mailer['host'], + $this->mailer['port'], + $this->mailer['authentication'], + $this->mailer['encryption'], + $this->fromEmail, + $this->fromName); break; } return $mailer; @@ -62,7 +91,7 @@ class Mailer { 'type' => 'API', 'access_key' => 'AKIAJM6Y5HMGXBLDNSRA', 'secret_key' => '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', - 'region' => 'us-east-1', + 'region' => 'us-east-1' ), array( 'name' => 'ElasticEmail', @@ -77,7 +106,6 @@ class Mailer { ), array( 'name' => 'MailPoet', - 'type' => 'API', 'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU' ), array( @@ -92,7 +120,6 @@ class Mailer { ), array( 'name' => 'SMTP', - 'type' => 'SMTP', 'host' => 'email-smtp.us-west-2.amazonaws.com', 'port' => 587, 'authentication' => array( @@ -100,10 +127,18 @@ class Mailer { 'password' => 'AudVHXHaYkvr54veCzqiqOxDiMMyfQW3/V6F1tYzGXY3' ), 'encryption' => 'tls' + ), + array( + 'name' => 'WPMail' ) ); $mailer = $mailers[array_rand($mailers)]; - return array_merge($mailer, array('class' => sprintf('MailPoet\\Mailer\\%s\\%s', $mailer['type'], $mailer['name']))); + $mailer['class'] = 'MailPoet\\Mailer\\' . + ((isset($mailer['type'])) ? + $mailer['type'] . '\\' . $mailer['name'] : + $mailer['name'] + ); + return $mailer; } if($setting === 'from_name') return 'Sender'; if($setting === 'from_address') return 'staff@mailpoet.com'; diff --git a/tests/unit/Mailer/API/AmazonSESCest.php b/tests/unit/Mailer/API/AmazonSESCest.php index 920d8b14b5..bce41944a0 100644 --- a/tests/unit/Mailer/API/AmazonSESCest.php +++ b/tests/unit/Mailer/API/AmazonSESCest.php @@ -28,8 +28,13 @@ class AmazonSESCest { } function itsConstructorWorks() { - expect($this->mailer->awsEndpoint)->equals('email.us-east-1.amazonaws.com'); - expect($this->mailer->url)->equals('https://email.us-east-1.amazonaws.com'); + expect($this->mailer->awsEndpoint) + ->equals( + sprintf('email.%s.amazonaws.com', $this->settings['region']) + ); + expect($this->mailer->url) ->equals( + sprintf('https://email.%s.amazonaws.com', $this->settings['region']) + ); expect(preg_match('!^\d{8}T\d{6}Z$!', $this->mailer->date))->equals(1); expect(preg_match('!^\d{8}$!', $this->mailer->dateWithoutTime))->equals(1); } diff --git a/tests/unit/Mailer/API/MailPoetCest.php b/tests/unit/Mailer/MailPoetCest.php similarity index 98% rename from tests/unit/Mailer/API/MailPoetCest.php rename to tests/unit/Mailer/MailPoetCest.php index a5c5909355..999e387050 100644 --- a/tests/unit/Mailer/API/MailPoetCest.php +++ b/tests/unit/Mailer/MailPoetCest.php @@ -1,12 +1,11 @@ settings = array( 'name' => 'MailPoet', - 'type' => 'API', 'api_key' => 'dhNSqj1XHkVltIliyQDvMiKzQShOA5rs0m_DdRUVZHU' ); $this->fromEmail = 'staff@mailpoet.com'; diff --git a/tests/unit/Mailer/SMTP/SMTPCest.php b/tests/unit/Mailer/SMTPCest.php similarity index 98% rename from tests/unit/Mailer/SMTP/SMTPCest.php rename to tests/unit/Mailer/SMTPCest.php index 930eb02e9a..7fd61532ad 100644 --- a/tests/unit/Mailer/SMTP/SMTPCest.php +++ b/tests/unit/Mailer/SMTPCest.php @@ -1,12 +1,11 @@ settings = array( 'name' => 'SMTP', - 'type' => 'SMTP', 'host' => 'email-smtp.us-west-2.amazonaws.com', 'port' => 587, 'authentication' => array( diff --git a/tests/unit/Mailer/WPMailCest.php b/tests/unit/Mailer/WPMailCest.php new file mode 100644 index 0000000000..1110948e5f --- /dev/null +++ b/tests/unit/Mailer/WPMailCest.php @@ -0,0 +1,70 @@ +settings = array( + 'name' => 'WPMail' + ); + $this->fromEmail = 'staff@mailpoet.com'; + $this->fromName = 'Sender'; + $this->mailer = new WPMail( + $this->fromEmail, + $this->fromName + ); + $this->subscriber = 'Recipient '; + $this->newsletter = array( + 'subject' => 'testing SMTP', + 'body' => array( + 'html' => 'HTML body', + 'text' => 'TEXT body' + ) + ); + } + + function itCanAddFilters() { + $this->mailer->addFilters(); + expect(has_filter('wp_mail_from_name', array( + $this->mailer, + 'setFromName' + )))->notEmpty(); + expect(has_filter('wp_mail_from', array( + $this->mailer, + 'setFromEmail' + )))->notEmpty(); + expect(has_filter('wp_mail_content_type', array( + $this->mailer, + 'setContentType' + )))->notEmpty(); + } + + function itCanRemoveFilters() { + $this->mailer->addFilters(); + $this->mailer->removeFilters(); + expect(has_filter('wp_mail_from_name'))->false(); + expect(has_filter('wp_mail_from'))->false(); + expect(has_filter('wp_mail_content_type'))->false(); + } + + function itCanSetFromName() { + expect($this->mailer->setFromName())->equals($this->fromName); + } + + function itCanSetFromEmail() { + expect($this->mailer->setFromName())->equals($this->fromName); + } + + function itCanSetContentType() { + expect($this->mailer->setContentType())->equals('text/html'); + } + + function itCanSend() { + $_SERVER['SERVER_NAME'] = 'localhost'; + $result = $this->mailer->send( + $this->newsletter, + $this->subscriber + ); + expect($result)->true(); + } +} \ No newline at end of file diff --git a/tests/unit/Router/MailerCest.php b/tests/unit/Router/MailerCest.php index 15a18d91aa..ec3a581240 100644 --- a/tests/unit/Router/MailerCest.php +++ b/tests/unit/Router/MailerCest.php @@ -43,15 +43,17 @@ class MailerCest { function itCanConfigureMailer() { $mailer = $this->router->buildMailer(); $class = 'Mailpoet\\Mailer\\' . - $this->router->mailer['type'] . '\\' . - $this->router->mailer['name']; + ((isset($this->router->mailer['type'])) ? + $this->router->mailer['type'] . '\\' . $this->router->mailer['name'] : + $this->router->mailer['name'] + ); expect($mailer instanceof $class)->true(); expect(method_exists($mailer, 'send'))->true(); } function itCanSend() { $newsletter = array( - 'subject' => 'testing Mailer router with '.$this->router->mailer['name'], + 'subject' => 'testing Mailer router with ' . $this->router->mailer['name'], 'body' => array( 'html' => 'HTML body', 'text' => 'TEXT body'