Skip to content

Commit

Permalink
Merge pull request #77 from SparkPost/issue71
Browse files Browse the repository at this point in the history
add version in user-agent
  • Loading branch information
rajumsys committed Nov 1, 2016
2 parents 07ea35c + 6978d2c commit 8db60e7
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ git clone git@github.com:SparkPost/wordpress-sparkpost.git ~/src/wordpress/wp-co
## Releasing

* Create a branch off master: `git checkout -b bump`
* Update the version in [wordpress-sparkpost.php](wordpress-sparkpost.php)
* Update the version in plugin meta and `WPSP_PLUGIN_VERSION` constant in [wordpress-sparkpost.php](wordpress-sparkpost.php)
* Update the version and change log in [readme.txt](readme.txt)
* Commit the changes and push the branch
* Create a pull request
Expand Down
2 changes: 1 addition & 1 deletion mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ protected function get_request_headers($hide_api_key = false)
}

return apply_filters('wpsp_request_headers', array(
'User-Agent' => 'wordpress-sparkpost',
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
'Content-Type' => 'application/json',
'Authorization' => $api_key
));
Expand Down
4 changes: 3 additions & 1 deletion tests/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
},
"require-dev": {
"satooshi/php-coveralls": "^1.0",
"phpunit/phpunit": "^5.6"
"phpunit/phpunit": "^5.6",
"nyholm/nsa": "^1.0",
"mockery/mockery": "^0.9.5"
},
"scripts": {
"test": "./vendor/bin/phpunit"
Expand Down
161 changes: 159 additions & 2 deletions tests/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/specs/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ function _manually_load_plugin() {

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
require __DIR__. '/wp-sparkpost.php';
require dirname(__DIR__) . '/vendor/autoload.php';
98 changes: 81 additions & 17 deletions tests/specs/test-mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
* @package wp-sparkpost
*/
namespace WPSparkPost;
use \Nyholm\NSA;
use \Mockery;

class TestHttpMailer extends TestSparkPost {
class TestHttpMailer extends \WP_UnitTestCase {
var $mailer;

function setUp() {
global $phpmailer;
$this->phpmailer = new SparkPostHTTPMailer();
$this->mailer = new SparkPostHTTPMailer();
}

function call($method) {
return $this->invokeMethod($this->phpmailer, $method);
}

function test_mailer_is_a_phpmailer_instance() {
$this->assertTrue( $this->phpmailer instanceof \PHPMailer );
function test_mailer_is_a_mailer_instance() {
$this->assertTrue( $this->mailer instanceof \PHPMailer );
}

function test_recipients_list() {

$this->phpmailer->addAddress('abc@xyz.com', 'abc');
$this->phpmailer->addAddress('def@xyz.com', 'def');
$this->phpmailer->addAddress('noname@xyz.com');
$this->mailer->addAddress('abc@xyz.com', 'abc');
$this->mailer->addAddress('def@xyz.com', 'def');
$this->mailer->addAddress('noname@xyz.com');
$prepared_list = array(
array(
'address' => array(
Expand All @@ -45,26 +42,93 @@ function test_recipients_list() {
)
)
);
$this->assertTrue($this->call('get_recipients') == $prepared_list);
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_recipients') == $prepared_list);
}

function test_sender_with_name() {
$this->phpmailer->setFrom( 'me@hello.com', 'me' );
$this->mailer->setFrom( 'me@hello.com', 'me' );
$sender = array(
'name' => 'me',
'email' => 'me@hello.com'
);

$this->assertTrue($this->call('get_sender') == $sender);
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_sender') == $sender);
}

function test_sender_without_name() {
$this->phpmailer->setFrom( 'me@hello.com', '' );
$this->mailer->setFrom( 'me@hello.com', '' );
$sender = array(
'email' => 'me@hello.com'
);

$this->assertTrue($this->call('get_sender') == $sender);
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_sender') == $sender);
}

function test_get_request_headers() {
$expected = array(
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
'Content-Type' => 'application/json',
'Authorization' => ''
);
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_request_headers') == $expected);

NSA::setProperty($this->mailer, 'settings', array('password' => 'abcd1234'));
$expected = array(
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
'Content-Type' => 'application/json',
'Authorization' => 'abcd1234'
);
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_request_headers') == $expected);
}

function test_get_request_headers_obfuscate_key() {
NSA::setProperty($this->mailer, 'settings', array('password' => 'abcd1234'));
$expected = array(
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
'Content-Type' => 'application/json',
'Authorization' => 'abcd'.str_repeat('*', 36)
);
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_request_headers', true) == $expected);
}

function test_get_headers() {
$raw_headers = "Date: Wed, 26 Oct 2016 23:45:32 +0000
To: undisclosed-recipients:;
From: Root User <root@localhost>
Subject: Hello
Reply-To: replyto@mydomain.com
Message-ID: <abcd@example.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit";

$expected = array(
'Message-ID' => '<abcd@example.org>',
'Date' => 'Wed, 26 Oct 2016 23:45:32 +0000'
);
$stub = Mockery::mock($this->mailer);
$stub->shouldReceive('createHeader')->andReturn($raw_headers);
$formatted_headers = NSA::invokeMethod($stub, 'get_headers');

$this->assertTrue($formatted_headers == $expected);
}


function test_get_headers_should_include_cc_if_exists() {
$raw_headers = "Date: Wed, 26 Oct 2016 23:45:32 +0000
Reply-To: replyto@mydomain.com";

$expected = array(
'Date' => 'Wed, 26 Oct 2016 23:45:32 +0000',
'CC' => 'hello@abc.com,Name <name@domain.com>'
);
$stub = Mockery::mock($this->mailer);
$stub->shouldReceive('createHeader')->andReturn($raw_headers);
$stub->addCc('hello@abc.com');
$stub->addCc('name@domain.com', 'Name');

$formatted_headers = NSA::invokeMethod($stub, 'get_headers');

$this->assertTrue($formatted_headers == $expected);
}
}
2 changes: 1 addition & 1 deletion tests/specs/test-wordpress-sparkpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace WPSparkPost;

class TestWordPressSparkPost extends TestSparkPost {
class TestWordPressSparkPost extends \WP_UnitTestCase {

function test_plugin_dir_constants() {
$this->assertTrue( defined('WPSP_PLUGIN_DIR') );
Expand Down
20 changes: 0 additions & 20 deletions tests/specs/wp-sparkpost.php

This file was deleted.

Loading

0 comments on commit 8db60e7

Please sign in to comment.