Skip to content

Commit 8db60e7

Browse files
authored
Merge pull request #77 from SparkPost/issue71
add version in user-agent
2 parents 07ea35c + 6978d2c commit 8db60e7

File tree

9 files changed

+249
-45
lines changed

9 files changed

+249
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ git clone git@github.com:SparkPost/wordpress-sparkpost.git ~/src/wordpress/wp-co
4949
## Releasing
5050

5151
* Create a branch off master: `git checkout -b bump`
52-
* Update the version in [wordpress-sparkpost.php](wordpress-sparkpost.php)
52+
* Update the version in plugin meta and `WPSP_PLUGIN_VERSION` constant in [wordpress-sparkpost.php](wordpress-sparkpost.php)
5353
* Update the version and change log in [readme.txt](readme.txt)
5454
* Commit the changes and push the branch
5555
* Create a pull request

mailer.http.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ protected function get_request_headers($hide_api_key = false)
262262
}
263263

264264
return apply_filters('wpsp_request_headers', array(
265-
'User-Agent' => 'wordpress-sparkpost',
265+
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
266266
'Content-Type' => 'application/json',
267267
'Authorization' => $api_key
268268
));

tests/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
},
44
"require-dev": {
55
"satooshi/php-coveralls": "^1.0",
6-
"phpunit/phpunit": "^5.6"
6+
"phpunit/phpunit": "^5.6",
7+
"nyholm/nsa": "^1.0",
8+
"mockery/mockery": "^0.9.5"
79
},
810
"scripts": {
911
"test": "./vendor/bin/phpunit"

tests/composer.lock

Lines changed: 159 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/specs/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ function _manually_load_plugin() {
2525

2626
// Start up the WP testing environment.
2727
require $_tests_dir . '/includes/bootstrap.php';
28-
require __DIR__. '/wp-sparkpost.php';
28+
require dirname(__DIR__) . '/vendor/autoload.php';

tests/specs/test-mailer.http.class.php

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,25 @@
33
* @package wp-sparkpost
44
*/
55
namespace WPSparkPost;
6+
use \Nyholm\NSA;
7+
use \Mockery;
68

7-
class TestHttpMailer extends TestSparkPost {
9+
class TestHttpMailer extends \WP_UnitTestCase {
810
var $mailer;
911

1012
function setUp() {
11-
global $phpmailer;
12-
$this->phpmailer = new SparkPostHTTPMailer();
13+
$this->mailer = new SparkPostHTTPMailer();
1314
}
1415

15-
function call($method) {
16-
return $this->invokeMethod($this->phpmailer, $method);
17-
}
18-
19-
function test_mailer_is_a_phpmailer_instance() {
20-
$this->assertTrue( $this->phpmailer instanceof \PHPMailer );
16+
function test_mailer_is_a_mailer_instance() {
17+
$this->assertTrue( $this->mailer instanceof \PHPMailer );
2118
}
2219

2320
function test_recipients_list() {
2421

25-
$this->phpmailer->addAddress('abc@xyz.com', 'abc');
26-
$this->phpmailer->addAddress('def@xyz.com', 'def');
27-
$this->phpmailer->addAddress('noname@xyz.com');
22+
$this->mailer->addAddress('abc@xyz.com', 'abc');
23+
$this->mailer->addAddress('def@xyz.com', 'def');
24+
$this->mailer->addAddress('noname@xyz.com');
2825
$prepared_list = array(
2926
array(
3027
'address' => array(
@@ -45,26 +42,93 @@ function test_recipients_list() {
4542
)
4643
)
4744
);
48-
$this->assertTrue($this->call('get_recipients') == $prepared_list);
45+
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_recipients') == $prepared_list);
4946
}
5047

5148
function test_sender_with_name() {
52-
$this->phpmailer->setFrom( 'me@hello.com', 'me' );
49+
$this->mailer->setFrom( 'me@hello.com', 'me' );
5350
$sender = array(
5451
'name' => 'me',
5552
'email' => 'me@hello.com'
5653
);
5754

58-
$this->assertTrue($this->call('get_sender') == $sender);
55+
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_sender') == $sender);
5956
}
6057

6158
function test_sender_without_name() {
62-
$this->phpmailer->setFrom( 'me@hello.com', '' );
59+
$this->mailer->setFrom( 'me@hello.com', '' );
6360
$sender = array(
6461
'email' => 'me@hello.com'
6562
);
6663

67-
$this->assertTrue($this->call('get_sender') == $sender);
64+
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_sender') == $sender);
6865
}
6966

67+
function test_get_request_headers() {
68+
$expected = array(
69+
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
70+
'Content-Type' => 'application/json',
71+
'Authorization' => ''
72+
);
73+
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_request_headers') == $expected);
74+
75+
NSA::setProperty($this->mailer, 'settings', array('password' => 'abcd1234'));
76+
$expected = array(
77+
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
78+
'Content-Type' => 'application/json',
79+
'Authorization' => 'abcd1234'
80+
);
81+
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_request_headers') == $expected);
82+
}
83+
84+
function test_get_request_headers_obfuscate_key() {
85+
NSA::setProperty($this->mailer, 'settings', array('password' => 'abcd1234'));
86+
$expected = array(
87+
'User-Agent' => 'wordpress-sparkpost/' . WPSP_PLUGIN_VERSION,
88+
'Content-Type' => 'application/json',
89+
'Authorization' => 'abcd'.str_repeat('*', 36)
90+
);
91+
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_request_headers', true) == $expected);
92+
}
93+
94+
function test_get_headers() {
95+
$raw_headers = "Date: Wed, 26 Oct 2016 23:45:32 +0000
96+
To: undisclosed-recipients:;
97+
From: Root User <root@localhost>
98+
Subject: Hello
99+
Reply-To: replyto@mydomain.com
100+
Message-ID: <abcd@example.org>
101+
MIME-Version: 1.0
102+
Content-Type: text/plain; charset=iso-8859-1
103+
Content-Transfer-Encoding: 8bit";
104+
105+
$expected = array(
106+
'Message-ID' => '<abcd@example.org>',
107+
'Date' => 'Wed, 26 Oct 2016 23:45:32 +0000'
108+
);
109+
$stub = Mockery::mock($this->mailer);
110+
$stub->shouldReceive('createHeader')->andReturn($raw_headers);
111+
$formatted_headers = NSA::invokeMethod($stub, 'get_headers');
112+
113+
$this->assertTrue($formatted_headers == $expected);
114+
}
115+
116+
117+
function test_get_headers_should_include_cc_if_exists() {
118+
$raw_headers = "Date: Wed, 26 Oct 2016 23:45:32 +0000
119+
Reply-To: replyto@mydomain.com";
120+
121+
$expected = array(
122+
'Date' => 'Wed, 26 Oct 2016 23:45:32 +0000',
123+
'CC' => 'hello@abc.com,Name <name@domain.com>'
124+
);
125+
$stub = Mockery::mock($this->mailer);
126+
$stub->shouldReceive('createHeader')->andReturn($raw_headers);
127+
$stub->addCc('hello@abc.com');
128+
$stub->addCc('name@domain.com', 'Name');
129+
130+
$formatted_headers = NSA::invokeMethod($stub, 'get_headers');
131+
132+
$this->assertTrue($formatted_headers == $expected);
133+
}
70134
}

tests/specs/test-wordpress-sparkpost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace WPSparkPost;
77

8-
class TestWordPressSparkPost extends TestSparkPost {
8+
class TestWordPressSparkPost extends \WP_UnitTestCase {
99

1010
function test_plugin_dir_constants() {
1111
$this->assertTrue( defined('WPSP_PLUGIN_DIR') );

tests/specs/wp-sparkpost.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)