From 8ab9e3180b445ca97c5ed0af5fac3a263f1a04a3 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 9 Mar 2017 18:33:15 -0500 Subject: [PATCH 01/21] resolve conflicts --- admin.widget.class.php | 4 +-- mailer.http.class.php | 66 ++++++++++++++++++++++++++++++++--------- templates.class.php | 60 +++++++++++++++++++++++++++++++++++++ wordpress-sparkpost.php | 3 ++ 4 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 templates.class.php diff --git a/admin.widget.class.php b/admin.widget.class.php index 9b737c47..3b206ae3 100644 --- a/admin.widget.class.php +++ b/admin.widget.class.php @@ -56,8 +56,8 @@ private function send_email($recipient) $result = wp_mail($recipient, 'SparkPost email test', '

Hurray!!

You\'ve got mail!

Regards,
SparkPost WordPress plugin

', - $headers, - $attachments + $headers + , $attachments ); remove_filter('wp_mail_content_type', array($this, 'set_html_content_type')); return $result; diff --git a/mailer.http.class.php b/mailer.http.class.php index 986e5607..7f232979 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -5,6 +5,7 @@ if (!defined('ABSPATH')) exit(); require_once ABSPATH . WPINC . '/class-phpmailer.php'; +require_once WPSP_PLUGIN_DIR . '/templates.class.php'; class SparkPostHTTPMailer extends \PHPMailer { @@ -66,6 +67,31 @@ function sparkpost_send() } } + /** + * Prepare substitution data to be used in template + */ + protected function get_template_substitutes($sender, $replyTo){ + $substitution_data = array(); + $substitution_data['content'] = $this->Body; + $substitution_data['subject'] = $this->Subject; + $substitution_data['from_name'] = $sender['name']; + $substitution_data['from'] = $sender['email']; + if ($replyTo) { + $substitution_data['reply_to'] = $replyTo; + } + $localpart = explode('@', $sender['email']); + if (!empty($localpart)) { + $substitution_data['from_localpart'] = $localpart[0]; + } + + return $substitution_data; + } + + function get_template_preview($template_id, $substitution_data) { + $template = new SparkPostTemplates(); + return $template->preview($template_id, $substitution_data); + } + /** * Build the request body to be sent to the SparkPost API. */ @@ -88,23 +114,36 @@ protected function get_request_body() $template_id = apply_filters('wpsp_template_id', $this->settings['template']); + $attachments = $this->get_attachments(); + // pass through either stored template or inline content if (!empty($template_id)) { - // stored template - $body['content']['template_id'] = $template_id; + // stored template + $substitution_data = $this->get_template_substitutes($sender, $replyTo); + if(sizeof($attachments) > 0){ //get template preview data and then send it as inline + $preview_contents = $this->get_template_preview($template_id, $substitution_data); + $body['content'] = array( + 'from' => $preview_contents->from, + 'subject' => $preview_contents->subject, + 'headers' => $this->get_headers() + ); + + if(property_exists($preview_contents, 'text')) { + $body['content']['text'] = $preview_contents->text; + } - // supply substitution data so users can add variables to templates - $body['substitution_data']['content'] = $this->Body; - $body['substitution_data']['subject'] = $this->Subject; - $body['substitution_data']['from_name'] = $sender['name']; - $body['substitution_data']['from'] = $sender['email']; - if ($replyTo) { - $body['substitution_data']['reply_to'] = $replyTo; + if(property_exists($preview_contents, 'html')){ + $body['content']['html'] = $preview_contents->html; } - $localpart = explode('@', $sender['email']); - if (!empty($localpart)) { - $body['substitution_data']['from_localpart'] = $localpart[0]; + + if(property_exists($preview_contents, 'reply_to')) { + $body['content']['reply_to'] = $preview_contents->reply_to; } + + } else { // simply subsititute template tags + $body['content']['template_id'] = $template_id; + $body['substitution_data'] = $substitution_data; + } } else { // inline content $body['content'] = array( @@ -131,8 +170,7 @@ protected function get_request_body() } } - $attachments = $this->get_attachments(); - if (count($attachments)) { + if (sizeof($attachments)) { $body['content']['attachments'] = $attachments; } diff --git a/templates.class.php b/templates.class.php new file mode 100644 index 00000000..e5ada755 --- /dev/null +++ b/templates.class.php @@ -0,0 +1,60 @@ +settings = SparkPost::get_settings(); + } + + protected function get_request_headers($hide_api_key = false) + { + $api_key = apply_filters('wpsp_api_key', $this->settings['password']); + + return apply_filters('wpsp_request_headers', array( + 'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION, + 'Content-Type' => 'application/json', + 'Authorization' => $api_key + )); + } + + public function preview($id, $substitution_data){ + $url = "{$this->endpoint}/{$id}/preview?draft=false"; + $http = apply_filters('wpsp_get_http_lib', _wp_http_get_object()); + + $body = array( + 'substitution_data' => $substitution_data + ); + + $data = array( + 'method' => 'POST', + 'timeout' => 15, + 'headers' => $this->get_request_headers(), + 'body' => json_encode($body) + ); + + $response = $http->request($url, $data); + $body = json_decode($response['body']); + + if (property_exists($body, 'errors')) { + $this->edebug('Error in getting template data'); + $this->setError($body->errors); + return false; + } + + if (property_exists($body, 'results')) { + return $body->results; + return $body->results; + } else { + $this->edebug('API response is unknown'); + $this->setError('Unknown response'); + return false; + } + } + +} diff --git a/wordpress-sparkpost.php b/wordpress-sparkpost.php index 27496f61..ce07d5bc 100644 --- a/wordpress-sparkpost.php +++ b/wordpress-sparkpost.php @@ -35,3 +35,6 @@ add_filter('wp_mail', array($sp, 'init_sp_http_mailer')); } } +define('WP_DEBUG', true); +ini_set('display_errors', 1); +error_reporting(E_ALL); From 66adb9fe5d08cced1b981c35005b0f04506cc8ec Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 9 Mar 2017 18:37:07 -0500 Subject: [PATCH 02/21] update formatting, remove duplicate line --- templates.class.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/templates.class.php b/templates.class.php index e5ada755..b3b54417 100644 --- a/templates.class.php +++ b/templates.class.php @@ -12,8 +12,7 @@ public function __construct(){ $this->settings = SparkPost::get_settings(); } - protected function get_request_headers($hide_api_key = false) - { + protected function get_request_headers($hide_api_key = false){ $api_key = apply_filters('wpsp_api_key', $this->settings['password']); return apply_filters('wpsp_request_headers', array( @@ -32,28 +31,27 @@ public function preview($id, $substitution_data){ ); $data = array( - 'method' => 'POST', - 'timeout' => 15, - 'headers' => $this->get_request_headers(), - 'body' => json_encode($body) + 'method' => 'POST', + 'timeout' => 15, + 'headers' => $this->get_request_headers(), + 'body' => json_encode($body) ); $response = $http->request($url, $data); $body = json_decode($response['body']); if (property_exists($body, 'errors')) { - $this->edebug('Error in getting template data'); - $this->setError($body->errors); - return false; + $this->edebug('Error in getting template data'); + $this->setError($body->errors); + return false; } if (property_exists($body, 'results')) { return $body->results; - return $body->results; } else { - $this->edebug('API response is unknown'); - $this->setError('Unknown response'); - return false; + $this->edebug('API response is unknown'); + $this->setError('Unknown response'); + return false; } } From f6c20794380ca343134fd831dc997aa4819a5516 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 11:34:16 -0400 Subject: [PATCH 03/21] Updates notes --- admin.widget.class.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/admin.widget.class.php b/admin.widget.class.php index 3b206ae3..63f87705 100644 --- a/admin.widget.class.php +++ b/admin.widget.class.php @@ -53,10 +53,10 @@ private function send_email($recipient) add_filter('wp_mail_content_type', array($this, 'set_html_content_type')); $headers = array(); $attachments= array(__DIR__ . '/sample.txt'); - $result = wp_mail($recipient, - 'SparkPost email test', - '

Hurray!!

You\'ve got mail!

Regards,
SparkPost WordPress plugin

', - $headers + $result = wp_mail($recipient + , 'SparkPost email test' + , '

Hurray!!

You\'ve got mail!

Regards,
SparkPost WordPress plugin

' + , $headers , $attachments ); remove_filter('wp_mail_content_type', array($this, 'set_html_content_type')); @@ -233,7 +233,8 @@ public function render_password_field() printf( '
-
  • For SMTP, set up an API key with the Send via SMTP permission
  • For HTTP API, set up an API Key with the Transmissions: Read/Write permission
  • Need help creating a SparkPost API key?
    ', +
    • For SMTP, set up an API key with the Send via SMTP permission
    • +
    • For HTTP API, set up an API Key with the Transmissions: Read/Write, Templates: Preview permissions
    • Need help creating a SparkPost API key?
      ', isset($api_key) ? $api_key : '' ); } @@ -247,7 +248,6 @@ public function render_template_field()
      • - Please see this article for detailed information about using templates with this plugin.
      • - Templates can only be used with the HTTP API.
      • -
      • - Does not work with attachment.
      • - Leave this field blank to disable use of a template. You can still specify it by using hooks.
      @@ -256,7 +256,7 @@ public function render_template_field() public function render_from_email_field() { - $hint = 'Important: Domain must match with one of your verified sending domains.'; + $hint = 'Important: Domain must match with one of your verified sending domains.'; if(empty($this->settings['from_email'])){ $hostname = parse_url(get_bloginfo('url'), PHP_URL_HOST); $hint .= sprintf(' When left blank, %s will be used as email domain', $hostname); From e38f6ad8624235d40141e977ba913b7009c9fd12 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 11:34:49 -0400 Subject: [PATCH 04/21] use phpmailer class instance to re-use some methods --- templates.class.php | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/templates.class.php b/templates.class.php index b3b54417..08963749 100644 --- a/templates.class.php +++ b/templates.class.php @@ -8,23 +8,14 @@ class SparkPostTemplates { public $endpoint = 'https://api.sparkpost.com/api/v1/templates'; - public function __construct(){ + public function __construct($mailer){ + $this->mailer = $mailer; $this->settings = SparkPost::get_settings(); } - protected function get_request_headers($hide_api_key = false){ - $api_key = apply_filters('wpsp_api_key', $this->settings['password']); - - return apply_filters('wpsp_request_headers', array( - 'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION, - 'Content-Type' => 'application/json', - 'Authorization' => $api_key - )); - } - public function preview($id, $substitution_data){ $url = "{$this->endpoint}/{$id}/preview?draft=false"; - $http = apply_filters('wpsp_get_http_lib', _wp_http_get_object()); + $http = $this->mailer->get_http_lib(); $body = array( 'substitution_data' => $substitution_data @@ -33,7 +24,7 @@ public function preview($id, $substitution_data){ $data = array( 'method' => 'POST', 'timeout' => 15, - 'headers' => $this->get_request_headers(), + 'headers' => $this->mailer->get_request_headers(), 'body' => json_encode($body) ); @@ -41,16 +32,16 @@ public function preview($id, $substitution_data){ $body = json_decode($response['body']); if (property_exists($body, 'errors')) { - $this->edebug('Error in getting template data'); - $this->setError($body->errors); + $this->mailer->edebug('Error in getting template data'); + $this->mailer->setError($body->errors); return false; } if (property_exists($body, 'results')) { return $body->results; } else { - $this->edebug('API response is unknown'); - $this->setError('Unknown response'); + $this->mailer->edebug('API response is unknown'); + $this->mailer->setError('Unknown response'); return false; } } From 3ffc8f318404a66289906f7671b16511a15aecd1 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 11:35:12 -0400 Subject: [PATCH 05/21] remove debugging codes --- wordpress-sparkpost.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/wordpress-sparkpost.php b/wordpress-sparkpost.php index ce07d5bc..27496f61 100644 --- a/wordpress-sparkpost.php +++ b/wordpress-sparkpost.php @@ -35,6 +35,3 @@ add_filter('wp_mail', array($sp, 'init_sp_http_mailer')); } } -define('WP_DEBUG', true); -ini_set('display_errors', 1); -error_reporting(E_ALL); From ef645130397d0b5987de740434188ce8b38fa2e7 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 11:36:03 -0400 Subject: [PATCH 06/21] cast data, refactor codes --- mailer.http.class.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mailer.http.class.php b/mailer.http.class.php index 4fc11ed6..aac8f86b 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -37,6 +37,10 @@ protected function mailSend($header, $body) return $this->sparkpost_send(); } + function get_http_lib() { + return apply_filters('wpsp_get_http_lib', _wp_http_get_object()); + } + function sparkpost_send() { $this->edebug('Preparing request data'); @@ -48,7 +52,7 @@ function sparkpost_send() 'body' => json_encode($this->get_request_body()) ); - $http = apply_filters('wpsp_get_http_lib', _wp_http_get_object()); + $http = $this->get_http_lib(); $this->edebug(sprintf('Request headers: %s', print_r($this->get_request_headers(true), true))); $this->edebug(sprintf('Request body: %s', $data['body'])); @@ -88,7 +92,7 @@ protected function get_template_substitutes($sender, $replyTo){ } function get_template_preview($template_id, $substitution_data) { - $template = new SparkPostTemplates(); + $template = new SparkPostTemplates($this); return $template->preview($template_id, $substitution_data); } @@ -123,11 +127,11 @@ protected function get_request_body() if(sizeof($attachments) > 0){ //get template preview data and then send it as inline $preview_contents = $this->get_template_preview($template_id, $substitution_data); $body['content'] = array( - 'from' => $preview_contents->from, - 'subject' => $preview_contents->subject, - 'headers' => $this->get_headers() + 'from' => (array) $preview_contents->from, + 'subject' => (string) $preview_contents->subject, + 'headers' => (array) $this->get_headers() ); - + if(property_exists($preview_contents, 'text')) { $body['content']['text'] = $preview_contents->text; } @@ -174,6 +178,7 @@ protected function get_request_body() $body['content']['attachments'] = $attachments; } + var_dump($body['content']['from']); if (isset($body['content']['from']['email']) && SparkPost::is_sandbox($body['content']['from']['email'])) { $body['options']['sandbox'] = true; } @@ -297,7 +302,7 @@ protected function get_recipients() return apply_filters('wpsp_recipients', $recipients); } - protected function get_request_headers($hide_api_key = false) + function get_request_headers($hide_api_key = false) { $api_key = apply_filters('wpsp_api_key', $this->settings['password']); if ($hide_api_key) { From 8c0667122e593a31daaf49bb60fcd4d30c368b0c Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 15:35:22 -0400 Subject: [PATCH 07/21] Update correct permission name --- admin.widget.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin.widget.class.php b/admin.widget.class.php index 63f87705..9f767c99 100644 --- a/admin.widget.class.php +++ b/admin.widget.class.php @@ -234,7 +234,7 @@ public function render_password_field() printf( '
      • For SMTP, set up an API key with the Send via SMTP permission
      • -
      • For HTTP API, set up an API Key with the Transmissions: Read/Write, Templates: Preview permissions
      • Need help creating a SparkPost API key?
        ', +
      • For HTTP API, set up an API Key with the Transmissions: Read/Write, Templates: Read/Write permissions
      • Need help creating a SparkPost API key?', isset($api_key) ? $api_key : '' ); } From 8722210fb8c3fe0e5667694706617c74d599744f Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 15:36:38 -0400 Subject: [PATCH 08/21] Fix permission name, update methods, add debugging messages --- templates.class.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/templates.class.php b/templates.class.php index 08963749..c5d988a3 100644 --- a/templates.class.php +++ b/templates.class.php @@ -3,7 +3,6 @@ // If ABSPATH is defined, we assume WP is calling us. // Otherwise, this could be an illicit direct request. if (!defined('ABSPATH')) exit(); -// require_once WPSP_PLUGIN_DIR '/sparkpost.class.php'; class SparkPostTemplates { public $endpoint = 'https://api.sparkpost.com/api/v1/templates'; @@ -13,6 +12,7 @@ public function __construct($mailer){ $this->settings = SparkPost::get_settings(); } + public function preview($id, $substitution_data){ $url = "{$this->endpoint}/{$id}/preview?draft=false"; $http = $this->mailer->get_http_lib(); @@ -28,20 +28,26 @@ public function preview($id, $substitution_data){ 'body' => json_encode($body) ); + $this->mailer->debug('Making template API request'); + $this->mailer->debug(print_r($data, true)); + $response = $http->request($url, $data); + $this->mailer->debug('Template API request completed'); + $this->mailer->check_permission_error($response, 'Templates: Read/Write'); + $body = json_decode($response['body']); if (property_exists($body, 'errors')) { - $this->mailer->edebug('Error in getting template data'); - $this->mailer->setError($body->errors); + $this->mailer->debug('Error in getting template data'); + $this->mailer->error($body->errors); return false; } if (property_exists($body, 'results')) { return $body->results; } else { - $this->mailer->edebug('API response is unknown'); - $this->mailer->setError('Unknown response'); + $this->mailer->debug('API response is unknown'); + $this->mailer->error('Unknown response'); return false; } } From 7797142c6ac90db4d849b0a27af01e52f5d77deb Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 15:56:54 -0400 Subject: [PATCH 09/21] Add option to include attachments --- admin.widget.class.php | 47 +++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/admin.widget.class.php b/admin.widget.class.php index 9f767c99..6cc81378 100644 --- a/admin.widget.class.php +++ b/admin.widget.class.php @@ -48,22 +48,21 @@ public function set_html_content_type() return 'text/html'; } - private function send_email($recipient) + private function send_email($recipient, $attachments = array()) { add_filter('wp_mail_content_type', array($this, 'set_html_content_type')); $headers = array(); - $attachments= array(__DIR__ . '/sample.txt'); - $result = wp_mail($recipient - , 'SparkPost email test' - , '

        Hurray!!

        You\'ve got mail!

        Regards,
        SparkPost WordPress plugin

        ' - , $headers - , $attachments + $result = wp_mail($recipient, + 'SparkPost email test', + '

        Hurray!!

        You\'ve got mail!

        Regards,
        SparkPost WordPress plugin

        ', + $headers, + $attachments ); remove_filter('wp_mail_content_type', array($this, 'set_html_content_type')); return $result; } - public function test_email_sending($recipient, $debug = false) + public function test_email_sending($recipient, $debug = false, $include_attachment = false) { if (empty($recipient)) { return $this->render_message('Please enter a valid email address in the recipient field below.'); @@ -73,14 +72,21 @@ public function test_email_sending($recipient, $debug = false) return $this->render_message('Recipient is not a valid email address.'); } + + if($include_attachment) { + $attachments = array(__DIR__ . '/sample.txt'); + } else { + $attachments = array(); + } + if ($debug) { add_action('phpmailer_init', array($this, 'phpmailer_enable_debugging')); echo '
        '; echo '

        Debug Messages

        '; - $result = $this->send_email($recipient); + $result = $this->send_email($recipient, $attachments); echo '
        '; } else { - $result = $this->send_email($recipient); + $result = $this->send_email($recipient, $attachments); } if ($result) { @@ -111,7 +117,7 @@ public function wpsp_admin_page()

        Test Email

        test_email_sending($_POST['to_email'], !empty($_POST['enable_debugging'])); + $this->test_email_sending($_POST['to_email'], !empty($_POST['enable_debugging']), !empty($_POST['include_attachment'])); } ?> @@ -144,6 +150,7 @@ public function admin_page_init() add_settings_section('test_email', '', null, 'sp-test-email'); add_settings_field('to_email', 'Recipient*', array($this, 'render_to_email_field'), 'sp-test-email', 'test_email'); + add_settings_field('include_attachment', '', array($this, 'render_include_attachment_field'), 'sp-test-email', 'test_email'); add_settings_field('debug_messages', 'Debug', array($this, 'render_enable_debugging_field'), 'sp-test-email', 'test_email'); } @@ -309,11 +316,17 @@ public function render_enable_debugging_field() echo ''; } - public function render_transactional_field() - { - printf(' -
        Upon checked, by default, it\'ll set mark all emails as transactional. It should be set false (using hooks) for non-transactional emails.', - $this->settings['transactional'] ? 'checked' : ''); + public function render_transactional_field() + { + printf(' +
        Upon checked, by default, it\'ll set mark all emails as transactional. It should be set false (using hooks) for non-transactional emails.', + $this->settings['transactional'] ? 'checked' : ''); + + } + - } + public function render_include_attachment_field() + { + echo ''; + } } From 93da7adb2f9e8c20d63ff9c535473d2909f8d33b Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 15:57:04 -0400 Subject: [PATCH 10/21] add doc for new hook --- docs/hooks.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/hooks.md b/docs/hooks.md index 0bb3599f..78553815 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -27,3 +27,4 @@ Hook names are prefixed with `wpsp_`. | wpsp_body_headers | Filter | | wpsp_smtp_msys_api | Filter | | wpsp_transactional | Filter | Set whether an email is transactional or not. +| wpsp_substitution_data | Filter | Modify substitution_data object From e954b3628e434832a57f6c6eb61936e0d93eb2f3 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 15:57:43 -0400 Subject: [PATCH 11/21] unify making http request --- mailer.http.class.php | 88 ++++++++++++++++++++++++++++--------------- templates.class.php | 3 +- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/mailer.http.class.php b/mailer.http.class.php index aac8f86b..12ff189e 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -37,34 +37,29 @@ protected function mailSend($header, $body) return $this->sparkpost_send(); } - function get_http_lib() { - return apply_filters('wpsp_get_http_lib', _wp_http_get_object()); - } - function sparkpost_send() { - $this->edebug('Preparing request data'); + $this->debug('Preparing request data'); + + $request_body = $this->get_request_body(); + + if(!$request_body) { + $this->error('Failed to prepare transmission request body'); + return false; + } $data = array( 'method' => 'POST', 'timeout' => 15, 'headers' => $this->get_request_headers(), - 'body' => json_encode($this->get_request_body()) + 'body' => json_encode($request_body) ); - $http = $this->get_http_lib(); - - $this->edebug(sprintf('Request headers: %s', print_r($this->get_request_headers(true), true))); - $this->edebug(sprintf('Request body: %s', $data['body'])); - $this->edebug(sprintf('Making HTTP POST request to %s', $this->endpoint)); - do_action('wpsp_before_send', $this->endpoint, $data); - $result = $http->request($this->endpoint, $data); - do_action('wpsp_after_send', $result); - $this->edebug('Response received'); + $result = $this->request($this->endpoint, $data); $result = apply_filters('wpsp_handle_response', $result); if(is_bool($result)) { // it means, response been already processed by the hooked filter. so just return the value. - $this->edebug('Skipping response processing'); + $this->debug('Skipping response processing'); return $result; } else { return $this->handle_response($result); @@ -84,11 +79,12 @@ protected function get_template_substitutes($sender, $replyTo){ $substitution_data['reply_to'] = $replyTo; } $localpart = explode('@', $sender['email']); + if (!empty($localpart)) { $substitution_data['from_localpart'] = $localpart[0]; } - return $substitution_data; + return apply_filters('wpsp_substitution_data', $substitution_data); } function get_template_preview($template_id, $substitution_data) { @@ -126,6 +122,9 @@ protected function get_request_body() $substitution_data = $this->get_template_substitutes($sender, $replyTo); if(sizeof($attachments) > 0){ //get template preview data and then send it as inline $preview_contents = $this->get_template_preview($template_id, $substitution_data); + if($preview_contents === false) { + return false; + } $body['content'] = array( 'from' => (array) $preview_contents->from, 'subject' => (string) $preview_contents->subject, @@ -178,7 +177,6 @@ protected function get_request_body() $body['content']['attachments'] = $attachments; } - var_dump($body['content']['from']); if (isset($body['content']['from']['email']) && SparkPost::is_sandbox($body['content']['from']['email'])) { $body['options']['sandbox'] = true; } @@ -234,41 +232,41 @@ public function isMail() protected function handle_response($response) { if (is_wp_error($response)) { - $this->edebug('Request completed with error'); - $this->setError($response->get_error_messages()); //WP_Error implements this method - $this->edebug($response->get_error_messages()); + $this->debug('Request completed with error'); + $this->error($response->get_error_messages()); //WP_Error implements this method + $this->debug($response->get_error_messages()); return false; } - $this->edebug('Response headers: ' . print_r($response['headers'], true)); - $this->edebug('Response body: ' . print_r($response['body'], true)); + $this->debug('Response headers: ' . print_r($response['headers'], true)); + $this->debug('Response body: ' . print_r($response['body'], true)); $body = json_decode($response['body']); do_action('wpsp_response_body', $body); if (property_exists($body, 'errors')) { - $this->edebug('Error in transmission'); - $this->setError($body->errors); + $this->debug('Error in transmission'); + $this->error($body->errors); return false; } if (property_exists($body, 'results')) { $data = $body->results; } else { - $this->edebug('API response is unknown'); - $this->setError('Unknown response'); + $this->debug('API response is unknown'); + $this->error('Unknown response'); return false; } if ($data->total_rejected_recipients > 0) { - $this->edebug(sprintf('Sending to %d recipient(s) failed', $data->total_rejected_recipients)); - $this->setError($data); + $this->debug(sprintf('Sending to %d recipient(s) failed', $data->total_rejected_recipients)); + $this->error($data); return false; } if ($data->total_accepted_recipients > 0) { - $this->edebug(sprintf('Successfully sent to %d recipient(s)', $data->total_accepted_recipients)); - $this->edebug(sprintf('Transmission ID is %s', $data->id)); + $this->debug(sprintf('Successfully sent to %d recipient(s)', $data->total_accepted_recipients)); + $this->debug(sprintf('Transmission ID is %s', $data->id)); return true; } return false; @@ -460,4 +458,32 @@ protected function get_headers() return apply_filters('wpsp_body_headers', $formatted_headers); } + + function check_permission_error($response, $permission) { + if($response['response']['code'] === 403) { + $this->debug("API Key might not have {$permission} permission. Actual Error: " . print_r($response['response'], true)); + $this->error("API Key might not have {$permission} permission"); + } + } + + public function debug($msg) { + $this->edebug($msg); + } + + public function error($msg) { + $this->setError($msg); + } + + public function request($endpoint, $data) { + $http = apply_filters('wpsp_get_http_lib', _wp_http_get_object()); + + $this->debug(sprintf('Request headers: %s', print_r($this->get_request_headers(true), true))); + $this->debug(sprintf('Request body: %s', $data['body'])); + $this->debug(sprintf('Making HTTP POST request to %s', $endpoint)); + do_action('wpsp_before_send', $this->endpoint, $data); + $result = $http->request($endpoint, $data); + do_action('wpsp_after_send', $result); + $this->debug('Response received'); + return $result; + } } diff --git a/templates.class.php b/templates.class.php index c5d988a3..9d7116f8 100644 --- a/templates.class.php +++ b/templates.class.php @@ -15,7 +15,6 @@ public function __construct($mailer){ public function preview($id, $substitution_data){ $url = "{$this->endpoint}/{$id}/preview?draft=false"; - $http = $this->mailer->get_http_lib(); $body = array( 'substitution_data' => $substitution_data @@ -31,7 +30,7 @@ public function preview($id, $substitution_data){ $this->mailer->debug('Making template API request'); $this->mailer->debug(print_r($data, true)); - $response = $http->request($url, $data); + $response = $this->mailer->request($url, $data); $this->mailer->debug('Template API request completed'); $this->mailer->check_permission_error($response, 'Templates: Read/Write'); From 2cdb17379ba82a2acf585812d43264c97c419831 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 16:02:43 -0400 Subject: [PATCH 12/21] wip tests --- tests/specs/test-mailer.http.class.php | 52 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index bd856e8f..f77b4f7b 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -265,7 +265,7 @@ function test_get_request_body_template_in_hook_but_not_in_settings() { $this->mailer->setFrom( 'me@hello.com', 'me'); $callback = function(){ - return 'test-template'; + return 'test-template'; }; add_filter('wpsp_template_id', $callback); @@ -342,6 +342,56 @@ function test_get_request_body_with_template() { $this->assertTrue($expected_request_body == $actual); } + function test_get_request_body_with_template_and_attachments() { + $this->mailer->addAddress('abc@xyz.com', 'abc'); + /* TODO avoid creating actual file */ + $temp = tempnam('/tmp', 'php-wordpress-sparkpost'); + $this->mailer->addAttachment($temp); + NSA::setProperty($this->mailer, 'settings', [ + 'template' => 'hello', + 'enable_tracking' => false, + 'transactional' => false + ]); + $header_to = 'abc '; + + $expected_request_body = [ + 'recipients' => [ + [ + 'address' => [ + 'email' => 'abc@xyz.com', + 'header_to' => $header_to + ] + ] + ], + 'options' => [ + 'open_tracking' => (bool) false, + 'click_tracking' => (bool) false, + 'transactional' => (bool) false + ], + 'content' => [ + 'template_id' => 'hello', + ], + 'substitution_data' => [ + 'content' => 'abc content', + 'subject' => 'abc subject', + 'from_name' => 'me', + 'from' => 'me@hello.com', + 'from_localpart' => 'me' + ] + ]; + + $actual = NSA::invokeMethod($this->mailer, 'get_request_body'); + unlink($temp); + $this->assertTrue($expected_request_body == $actual); + + //INCLUDE REPLYTO + $this->mailer->addReplyTo('reply@abc.com', 'reply-to'); + $this->mailer->addCustomHeader('Reply-To', 'reply-to '); //for below version v4.6 + $actual = NSA::invokeMethod($this->mailer, 'get_request_body'); + $expected_request_body['substitution_data']['reply_to'] = 'reply-to '; + $this->assertTrue($expected_request_body == $actual); + } + function test_get_request_body_content_type_text_plain() { $this->mailer->ContentType = 'text/plain'; $this->mailer->Body = '

        hello world

        '; From eb6207ceccb5d148d5f028d25688e1fa7b93d207 Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 23 Mar 2017 23:08:32 -0400 Subject: [PATCH 13/21] Fix method sig --- mailer.http.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mailer.http.class.php b/mailer.http.class.php index 12ff189e..77f6e652 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -300,7 +300,7 @@ protected function get_recipients() return apply_filters('wpsp_recipients', $recipients); } - function get_request_headers($hide_api_key = false) + public function get_request_headers($hide_api_key = false) { $api_key = apply_filters('wpsp_api_key', $this->settings['password']); if ($hide_api_key) { From d0eb0d233900da7c052325e0d2435463251b8059 Mon Sep 17 00:00:00 2001 From: mhossain Date: Sun, 26 Mar 2017 20:16:50 -0400 Subject: [PATCH 14/21] add test for template with attachments --- templates.class.php | 1 - tests/specs/bootstrap.php | 1 + tests/specs/test-mailer.http.class.php | 74 ++++++++++++++++---------- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/templates.class.php b/templates.class.php index 9d7116f8..ecff217e 100644 --- a/templates.class.php +++ b/templates.class.php @@ -9,7 +9,6 @@ class SparkPostTemplates { public function __construct($mailer){ $this->mailer = $mailer; - $this->settings = SparkPost::get_settings(); } diff --git a/tests/specs/bootstrap.php b/tests/specs/bootstrap.php index a7f0e217..ca15b78e 100644 --- a/tests/specs/bootstrap.php +++ b/tests/specs/bootstrap.php @@ -18,6 +18,7 @@ */ function _manually_load_plugin() { require BASE_DIR . '/wordpress-sparkpost.php'; + require BASE_DIR . '/templates.class.php'; require BASE_DIR . '/mailer.http.class.php'; } diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index f77b4f7b..822102f6 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -7,6 +7,7 @@ use \Mockery; use phpmock\phpunit\PHPMock; + class TestHttpMailer extends \WP_UnitTestCase { use PHPMock; @@ -343,16 +344,42 @@ function test_get_request_body_with_template() { } function test_get_request_body_with_template_and_attachments() { - $this->mailer->addAddress('abc@xyz.com', 'abc'); - /* TODO avoid creating actual file */ - $temp = tempnam('/tmp', 'php-wordpress-sparkpost'); - $this->mailer->addAttachment($temp); - NSA::setProperty($this->mailer, 'settings', [ - 'template' => 'hello', - 'enable_tracking' => false, - 'transactional' => false - ]); + $mock = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('get_template_preview', 'get_attachments')) + ->getMock(); + + $template_preview = (object) array( + 'from' => array( + 'from' => 'me@hello.com', + 'from_name' => 'me' + ), + 'subject' => 'test subject', + 'headers' => array(), + 'html' => '

        Hello there

        ' + ); + $mock->addAddress('abc@xyz.com', 'abc'); + $mock->setFrom( 'me@hello.com', 'me'); + + $mock->expects($this->once()) + ->method('get_template_preview') + ->will($this->returnValue($template_preview)); + + $attachments = [ + 'name' => 'php-wordpress-sparkpost.txt', + 'type' => 'plain/text', + 'data' => base64_encode('TEST') + ]; + + $mock->expects($this->once()) + ->method('get_attachments') + ->will($this->returnValue($attachments)); + $header_to = 'abc '; + NSA::setProperty($mock, 'settings', [ + 'enable_tracking' => true, + 'transactional' => false, + 'template' => 'hello' + ]); $expected_request_body = [ 'recipients' => [ @@ -364,32 +391,25 @@ function test_get_request_body_with_template_and_attachments() { ] ], 'options' => [ - 'open_tracking' => (bool) false, - 'click_tracking' => (bool) false, + 'open_tracking' => (bool) true, + 'click_tracking' => (bool) true, 'transactional' => (bool) false ], 'content' => [ - 'template_id' => 'hello', - ], - 'substitution_data' => [ - 'content' => 'abc content', - 'subject' => 'abc subject', - 'from_name' => 'me', - 'from' => 'me@hello.com', - 'from_localpart' => 'me' + 'from' => [ + 'from_name' => 'me', + 'from' => 'me@hello.com' + ], + 'subject' => 'test subject', + 'html' => '

        Hello there

        ', + 'attachments' => $attachments ] ]; - $actual = NSA::invokeMethod($this->mailer, 'get_request_body'); - unlink($temp); + $actual = NSA::invokeMethod($mock, 'get_request_body'); + unset($actual['content']['headers']); //to simplify assertion $this->assertTrue($expected_request_body == $actual); - //INCLUDE REPLYTO - $this->mailer->addReplyTo('reply@abc.com', 'reply-to'); - $this->mailer->addCustomHeader('Reply-To', 'reply-to '); //for below version v4.6 - $actual = NSA::invokeMethod($this->mailer, 'get_request_body'); - $expected_request_body['substitution_data']['reply_to'] = 'reply-to '; - $this->assertTrue($expected_request_body == $actual); } function test_get_request_body_content_type_text_plain() { From c9020619019babc96be9a9042ab668500f6fc0ee Mon Sep 17 00:00:00 2001 From: mhossain Date: Sun, 26 Mar 2017 22:07:53 -0400 Subject: [PATCH 15/21] refactor test --- mailer.http.class.php | 3 +- tests/specs/test-mailer.http.class.php | 45 +++++++++++++++----------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/mailer.http.class.php b/mailer.http.class.php index 77f6e652..1d13e5ce 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -19,6 +19,7 @@ class SparkPostHTTPMailer extends \PHPMailer function __construct($exceptions = false) { $this->settings = SparkPost::get_settings(); + $this->template = new SparkPostTemplates($this); parent::__construct($exceptions); do_action('wpsp_init_mailer', $this); @@ -121,7 +122,7 @@ protected function get_request_body() // stored template $substitution_data = $this->get_template_substitutes($sender, $replyTo); if(sizeof($attachments) > 0){ //get template preview data and then send it as inline - $preview_contents = $this->get_template_preview($template_id, $substitution_data); + $preview_contents = $this->template->preview($template_id, $substitution_data); if($preview_contents === false) { return false; } diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index 822102f6..71c792b0 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -344,11 +344,7 @@ function test_get_request_body_with_template() { } function test_get_request_body_with_template_and_attachments() { - $mock = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') - ->setMethods(array('get_template_preview', 'get_attachments')) - ->getMock(); - - $template_preview = (object) array( + $template_data = (object) array( 'from' => array( 'from' => 'me@hello.com', 'from_name' => 'me' @@ -357,25 +353,38 @@ function test_get_request_body_with_template_and_attachments() { 'headers' => array(), 'html' => '

        Hello there

        ' ); - $mock->addAddress('abc@xyz.com', 'abc'); - $mock->setFrom( 'me@hello.com', 'me'); - - $mock->expects($this->once()) - ->method('get_template_preview') - ->will($this->returnValue($template_preview)); - - $attachments = [ + $attachments_data = [ 'name' => 'php-wordpress-sparkpost.txt', 'type' => 'plain/text', 'data' => base64_encode('TEST') ]; - $mock->expects($this->once()) + + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('get_attachments')) + ->getMock(); + + $template = $this->getMockBuilder('WPSparkPost\SparkPostTemplates') + ->setConstructorArgs(array($mailer)) + ->setMethods(array('preview')) + ->getMock(); + + $template->expects($this->once()) + ->method('preview') + ->will($this->returnValue($template_data)); + + $mailer->template = $template; + + $mailer->addAddress('abc@xyz.com', 'abc'); + $mailer->setFrom( 'me@hello.com', 'me'); + + + $mailer->expects($this->once()) ->method('get_attachments') - ->will($this->returnValue($attachments)); + ->will($this->returnValue($attachments_data)); $header_to = 'abc '; - NSA::setProperty($mock, 'settings', [ + NSA::setProperty($mailer, 'settings', [ 'enable_tracking' => true, 'transactional' => false, 'template' => 'hello' @@ -402,11 +411,11 @@ function test_get_request_body_with_template_and_attachments() { ], 'subject' => 'test subject', 'html' => '

        Hello there

        ', - 'attachments' => $attachments + 'attachments' => $attachments_data ] ]; - $actual = NSA::invokeMethod($mock, 'get_request_body'); + $actual = NSA::invokeMethod($mailer, 'get_request_body'); unset($actual['content']['headers']); //to simplify assertion $this->assertTrue($expected_request_body == $actual); From 65dfd39cffcf7438f0ee4a8b72f56acc31861448 Mon Sep 17 00:00:00 2001 From: mhossain Date: Sun, 26 Mar 2017 22:22:45 -0400 Subject: [PATCH 16/21] remove unused method, add test data to improve coverage --- mailer.http.class.php | 5 ----- tests/specs/test-mailer.http.class.php | 6 +++++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mailer.http.class.php b/mailer.http.class.php index 1d13e5ce..6d43290b 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -88,11 +88,6 @@ protected function get_template_substitutes($sender, $replyTo){ return apply_filters('wpsp_substitution_data', $substitution_data); } - function get_template_preview($template_id, $substitution_data) { - $template = new SparkPostTemplates($this); - return $template->preview($template_id, $substitution_data); - } - /** * Build the request body to be sent to the SparkPost API. */ diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index 71c792b0..e0dcac1d 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -351,7 +351,9 @@ function test_get_request_body_with_template_and_attachments() { ), 'subject' => 'test subject', 'headers' => array(), - 'html' => '

        Hello there

        ' + 'html' => '

        Hello there

        ', + 'text' => 'hello there', + 'reply_to' => 'me@hello.com' ); $attachments_data = [ 'name' => 'php-wordpress-sparkpost.txt', @@ -411,6 +413,8 @@ function test_get_request_body_with_template_and_attachments() { ], 'subject' => 'test subject', 'html' => '

        Hello there

        ', + 'text' => 'hello there', + 'reply_to' => 'me@hello.com', 'attachments' => $attachments_data ] ]; From 2be2f4ee22748e7fb34dcad01911f1c91c2a1fba Mon Sep 17 00:00:00 2001 From: mhossain Date: Mon, 27 Mar 2017 12:30:17 -0400 Subject: [PATCH 17/21] add tests for request_body error --- mailer.http.class.php | 5 +++- tests/specs/test-mailer.http.class.php | 32 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/mailer.http.class.php b/mailer.http.class.php index 6d43290b..442135ac 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -59,6 +59,7 @@ function sparkpost_send() $result = $this->request($this->endpoint, $data); $result = apply_filters('wpsp_handle_response', $result); + $this->check_permission_error($result, 'Transmissions: Read/Write'); if(is_bool($result)) { // it means, response been already processed by the hooked filter. so just return the value. $this->debug('Skipping response processing'); return $result; @@ -456,9 +457,11 @@ protected function get_headers() } function check_permission_error($response, $permission) { - if($response['response']['code'] === 403) { + $response = (array) $response; + if(!empty($response['response']) && $response['response']['code'] === 403) { $this->debug("API Key might not have {$permission} permission. Actual Error: " . print_r($response['response'], true)); $this->error("API Key might not have {$permission} permission"); + return false; } } diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index e0dcac1d..5b2372f4 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -422,7 +422,36 @@ function test_get_request_body_with_template_and_attachments() { $actual = NSA::invokeMethod($mailer, 'get_request_body'); unset($actual['content']['headers']); //to simplify assertion $this->assertTrue($expected_request_body == $actual); + } + + function test_get_request_body_false_on_error() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('get_attachments', 'get_sender','get_reply_to', 'get_template_substitutes')) + ->getMock(); + + $mailer->expects($this->once()) + ->method('get_attachments') + ->will($this->returnValue(array('name' => 'test-attachment.txt'))); + + $template = $this->getMockBuilder('WPSparkPost\SparkPostTemplates') + ->setConstructorArgs(array($mailer)) + ->setMethods(array('preview')) + ->getMock(); + + $template->expects($this->once()) + ->method('preview') + ->will($this->returnValue(false)); + + NSA::setProperty($mailer, 'settings', [ + 'enable_tracking' => true, + 'transactional' => false, + 'template' => 'hello' + ]); + + $mailer->template = $template; + $result = NSA::invokeMethod($mailer, 'get_request_body'); + $this->assertEquals($result, false); } function test_get_request_body_content_type_text_plain() { @@ -467,6 +496,9 @@ function test_get_request_body_with_attachments() { function sparkpost_send_prepare_mocks($num_rejected) { $this->mailer->addAddress('abc@xyz.com', 'abc'); $response = array( + 'response' => array( + 'code' => 200 + ), 'headers' => array(), 'body' => json_encode(array( 'results' => array( From 7eddf6cb4383ca823640e62940e27e52fd664e40 Mon Sep 17 00:00:00 2001 From: mhossain Date: Mon, 27 Mar 2017 12:52:48 -0400 Subject: [PATCH 18/21] fix missing coverages --- mailer.http.class.php | 3 +- tests/specs/test-mailer.http.class.php | 50 ++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/mailer.http.class.php b/mailer.http.class.php index 442135ac..7527b859 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -461,8 +461,9 @@ function check_permission_error($response, $permission) { if(!empty($response['response']) && $response['response']['code'] === 403) { $this->debug("API Key might not have {$permission} permission. Actual Error: " . print_r($response['response'], true)); $this->error("API Key might not have {$permission} permission"); - return false; + return true; } + return false; } public function debug($msg) { diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index 5b2372f4..09b9e185 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -361,7 +361,6 @@ function test_get_request_body_with_template_and_attachments() { 'data' => base64_encode('TEST') ]; - $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') ->setMethods(array('get_attachments')) ->getMock(); @@ -424,7 +423,20 @@ function test_get_request_body_with_template_and_attachments() { $this->assertTrue($expected_request_body == $actual); } - function test_get_request_body_false_on_error() { + function test_sparkpost_send_false_on_error() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('get_request_body')) + ->getMock(); + + $mailer->expects($this->once()) + ->method('get_request_body') + ->will($this->returnValue(false)); + + $result = NSA::invokeMethod($mailer, 'sparkpost_send'); + $this->assertEquals($result, false); + } + + function test_get_request_body_with_attachments_returns_false_on_error() { $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') ->setMethods(array('get_attachments', 'get_sender','get_reply_to', 'get_template_substitutes')) ->getMock(); @@ -493,6 +505,23 @@ function test_get_request_body_with_attachments() { unlink($temp); } + function test_get_request_body_with_sandbox() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('get_attachments', 'get_reply_to', 'get_recipients')) + ->getMock(); + + $mailer->addAddress('abc@xyz.com', 'abc'); + $mailer->setFrom( 'me@sparkpostbox.com', 'me'); + NSA::setProperty($mailer, 'settings', [ + 'enable_tracking' => true, + 'transactional' => false, + 'template' => null + ]); + + $body = NSA::invokeMethod($mailer, 'get_request_body'); + $this->assertTrue($body['options']['sandbox'] == true); + } + function sparkpost_send_prepare_mocks($num_rejected) { $this->mailer->addAddress('abc@xyz.com', 'abc'); $response = array( @@ -618,5 +647,22 @@ function test_get_reply_to_below_wp46(){ $this->assertEquals(NSA::invokeMethod($this->mailer, 'get_reply_to'), 'abc@xyz.com'); } + function test_check_permission_error(){ + $response = [ + 'response' => [ + 'code' => 403 + ] + ]; + + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('debug', 'error')) + ->getMock(); + + $this->assertTrue($mailer->check_permission_error($response, 'test_perm') === true); + + $response['response']['code'] = 200; + $this->assertTrue($mailer->check_permission_error($response, 'test_perm') === false); + } + } From 7c3a8733679eefb4f538f47e006a56cad574700b Mon Sep 17 00:00:00 2001 From: mhossain Date: Mon, 27 Mar 2017 13:17:55 -0400 Subject: [PATCH 19/21] add tests for template class --- templates.class.php | 1 - tests/phpunit.xml.dist | 1 + tests/specs/test-templates.class.php | 98 ++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/specs/test-templates.class.php diff --git a/templates.class.php b/templates.class.php index ecff217e..e1f1db14 100644 --- a/templates.class.php +++ b/templates.class.php @@ -11,7 +11,6 @@ public function __construct($mailer){ $this->mailer = $mailer; } - public function preview($id, $substitution_data){ $url = "{$this->endpoint}/{$id}/preview?draft=false"; diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist index becafe37..29f2213d 100644 --- a/tests/phpunit.xml.dist +++ b/tests/phpunit.xml.dist @@ -18,6 +18,7 @@ ../mailer.smtp.class.php ../mailer.http.class.php ../admin.widget.class.php + ../templates.class.php diff --git a/tests/specs/test-templates.class.php b/tests/specs/test-templates.class.php new file mode 100644 index 00000000..a371137c --- /dev/null +++ b/tests/specs/test-templates.class.php @@ -0,0 +1,98 @@ +assertTrue($templateObj->mailer instanceof SparkPostHTTPMailer); + } + + function test_preview_returns_template_data() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('request', 'get_request_headers', 'error', 'debug')) + ->getMock(); + + $response = array( + 'response' => array( + 'code' => 200 + ), + 'headers' => array(), + 'body' => json_encode(array( + 'results' => array( + 'from' => array( + 'from' => 'me@hello.com', + 'from_name' => 'me' + ), + 'subject' => 'test subject', + 'headers' => array(), + 'html' => '

        Hello there

        ', + 'text' => 'hello there', + 'reply_to' => 'me@hello.com' + ) + )) + ); + + $mailer->expects($this->once()) + ->method('request') + ->will($this->returnValue($response)); + + $templateObj = new SparkPostTemplates($mailer); + $result = $templateObj->preview('abcd', array()); + $this->assertEquals($result->subject, 'test subject'); + $this->assertEquals($result->html, '

        Hello there

        '); + $this->assertEquals($result->text, 'hello there'); + } + + function test_preview_returns_false_on_error() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('request', 'get_request_headers', 'error', 'debug')) + ->getMock(); + + $response = array( + 'response' => array( + 'code' => 200 + ), + 'headers' => array(), + 'body' => json_encode(array( + 'errors' => array( + 'some interesting error' + ) + )) + ); + + $mailer->expects($this->once()) + ->method('request') + ->will($this->returnValue($response)); + + $templateObj = new SparkPostTemplates($mailer); + $result = $templateObj->preview('abcd', array()); + $this->assertEquals($result, false); + } + + function test_preview_returns_false_on_unknown_error() { + $mailer = $this->getMockBuilder('WPSparkPost\SparkPostHTTPMailer') + ->setMethods(array('request', 'get_request_headers', 'error', 'debug')) + ->getMock(); + + $response = array( + 'response' => array( + 'code' => 200 + ), + 'headers' => array(), + 'body' => json_encode(array('unknown_key' => 'unknown result')) + ); + + $mailer->expects($this->once()) + ->method('request') + ->will($this->returnValue($response)); + + $templateObj = new SparkPostTemplates($mailer); + $result = $templateObj->preview('abcd', array()); + $this->assertEquals($result, false); + } +} From 7a27886f70e3b04ed0ca26e538cbaf456ae01353 Mon Sep 17 00:00:00 2001 From: mhossain Date: Mon, 27 Mar 2017 14:13:38 -0400 Subject: [PATCH 20/21] add method call assertions --- tests/specs/test-templates.class.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/specs/test-templates.class.php b/tests/specs/test-templates.class.php index a371137c..4b72a59b 100644 --- a/tests/specs/test-templates.class.php +++ b/tests/specs/test-templates.class.php @@ -41,6 +41,12 @@ function test_preview_returns_template_data() { ->method('request') ->will($this->returnValue($response)); + $mailer->expects($this->never()) + ->method('error'); + + $mailer->expects($this->exactly(3)) + ->method('debug'); + $templateObj = new SparkPostTemplates($mailer); $result = $templateObj->preview('abcd', array()); $this->assertEquals($result->subject, 'test subject'); @@ -69,6 +75,13 @@ function test_preview_returns_false_on_error() { ->method('request') ->will($this->returnValue($response)); + $mailer->expects($this->once()) + ->method('error'); + + $mailer->expects($this->exactly(4)) + ->method('debug'); + + $templateObj = new SparkPostTemplates($mailer); $result = $templateObj->preview('abcd', array()); $this->assertEquals($result, false); @@ -91,6 +104,12 @@ function test_preview_returns_false_on_unknown_error() { ->method('request') ->will($this->returnValue($response)); + $mailer->expects($this->once()) + ->method('error'); + + $mailer->expects($this->exactly(4)) + ->method('debug'); + $templateObj = new SparkPostTemplates($mailer); $result = $templateObj->preview('abcd', array()); $this->assertEquals($result, false); From 15d558bb3a969d468a5f0ee573d26516665cfd7d Mon Sep 17 00:00:00 2001 From: mhossain Date: Thu, 30 Mar 2017 15:12:59 -0400 Subject: [PATCH 21/21] remove unnecessary formatting --- admin.widget.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin.widget.class.php b/admin.widget.class.php index 6cc81378..1a990dd6 100644 --- a/admin.widget.class.php +++ b/admin.widget.class.php @@ -327,6 +327,6 @@ public function render_transactional_field() public function render_include_attachment_field() { - echo ''; + echo ''; } }