33* @package wp-sparkpost
44*/
55namespace 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}
0 commit comments