Skip to content

Commit 84f0a76

Browse files
authored
Merge pull request #80 from SparkPost/issue79
Handle multiple recipients in `To` correctly
2 parents 8db60e7 + 77caeb7 commit 84f0a76

File tree

6 files changed

+224
-42
lines changed

6 files changed

+224
-42
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ env:
2121
- WP_VERSION=4.6 WP_MULTISITE=0
2222

2323
before_script:
24-
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
2524
- cd tests
25+
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
2626
- composer install --no-interaction
2727

2828
script: composer test

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ git clone git@github.com:SparkPost/wordpress-sparkpost.git ~/src/wordpress/wp-co
3939

4040
## Running Tests
4141
* Make sure you're using PHP 5.6 or above.
42-
* Install test files by running `bash bin/install-wp-tests.sh wordpress_test root '' localhost latest`. Details on [wp-cli.org](http://wp-cli.org/docs/plugin-unit-tests/).
43-
* [Install composer](https://getcomposer.org/doc/00-intro.md)
4442
* Go to `./tests` directory.
43+
* Install test files by running `bash bin/install-wp-tests.sh wordpress_test root '' localhost latest` (Try `127.0.0.1` instead of `localhost` if you're getting error). Details on [wp-cli.org](http://wp-cli.org/docs/plugin-unit-tests/).
44+
* [Install composer](https://getcomposer.org/doc/00-intro.md)
4545
* Run `composer install` to install required packages.
4646
* To run tests, run `composer test`.
4747
* Add your tests in `tests/specs` directory. Upon pushing the branch, Travis will automatically run it and generate reports (tests and coverage).

mailer.http.class.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,21 @@ protected function get_recipients()
232232
$recipients = array();
233233
$recipients_header_to = array();
234234

235+
//prepare header_to
235236
foreach ($this->to as $to) {
236-
$recipients[] = $this->build_recipient($to[0], $to[1]);
237-
238-
// prepare for header_to
239-
if(!empty($to[1])) {
240-
$recipients_header_to[] = sprintf('%s <%s>', $to[1], $to[0]);
241-
} else {
237+
if(empty($to[1])) { // if name is empty use only address
242238
$recipients_header_to[] = $to[0];
239+
} else { // otherwise, use name and email
240+
$recipients_header_to[] = sprintf('%s <%s>', $to[1], $to[0]);
243241
}
244242
}
245-
$recipients_header_to = implode(',', $recipients_header_to);
243+
$recipients_header_to = implode(', ', $recipients_header_to);
244+
245+
foreach ($this->to as $to) {
246+
$recipients[] = $this->build_recipient($to[0], $to[1], $recipients_header_to);
247+
}
246248

247249
// include bcc to recipients
248-
// sparkposts recipients list acts as bcc by default
249250
$recipients = array_merge($recipients, $this->get_bcc($recipients_header_to));
250251

251252
// include cc to recipients, they need to included in recipients and in headers (refer to get_headers method)
@@ -277,7 +278,7 @@ protected function get_request_headers($hide_api_key = false)
277278
protected function parse_reply_to_from_custom_header()
278279
{
279280
$replyTos = array();
280-
foreach ($this->CustomHeader as $header) { // wp_mail sets Reply-To as custom header (does not use phpmailer->addReplyTo)
281+
foreach ($this->getCustomHeaders() as $header) { // wp_mail sets Reply-To as custom header (does not use phpmailer->addReplyTo)
281282
list($name, $value) = $header;
282283
if ($name === 'Reply-To' && !empty($value)) {
283284
$replyTos[] = trim($value);

tests/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
</filter>
2323
<logging>
2424
<log type="coverage-clover" target="coverage/clover.xml"/>
25+
<log type="coverage-html" target="coverage/report"/>
2526
</logging>
2627
</phpunit>

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

Lines changed: 210 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,18 @@ function setUp() {
1313
$this->mailer = new SparkPostHTTPMailer();
1414
}
1515

16-
function test_mailer_is_a_mailer_instance() {
17-
$this->assertTrue( $this->mailer instanceof \PHPMailer );
18-
}
16+
function test_mailSend_calls_sparkpost_send() {
17+
$stub = Mockery::mock($this->mailer);
18+
$stub->shouldReceive('sparkpost_send')->andReturn('woowoo');
1919

20-
function test_recipients_list() {
20+
$this->assertTrue(NSA::invokeMethod($stub, 'mailSend', null, null) == 'woowoo');
21+
}
2122

22-
$this->mailer->addAddress('abc@xyz.com', 'abc');
23-
$this->mailer->addAddress('def@xyz.com', 'def');
24-
$this->mailer->addAddress('noname@xyz.com');
25-
$prepared_list = array(
26-
array(
27-
'address' => array(
28-
'email' => 'abc@xyz.com',
29-
'name' => 'abc',
30-
)
31-
),
32-
array(
33-
'address' => array(
34-
'name' => 'def',
35-
'email' => 'def@xyz.com'
36-
)
37-
),
38-
array(
39-
'address' => array(
40-
'email' => 'noname@xyz.com',
41-
'name' => ''
42-
)
43-
)
44-
);
45-
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_recipients') == $prepared_list);
23+
function test_mailer_is_a_mailer_instance() {
24+
$this->assertTrue( $this->mailer instanceof \PHPMailer );
4625
}
4726

48-
function test_sender_with_name() {
27+
function test_get_sender_with_name() {
4928
$this->mailer->setFrom( 'me@hello.com', 'me' );
5029
$sender = array(
5130
'name' => 'me',
@@ -55,7 +34,7 @@ function test_sender_with_name() {
5534
$this->assertTrue(NSA::invokeMethod($this->mailer, 'get_sender') == $sender);
5635
}
5736

58-
function test_sender_without_name() {
37+
function test_get_sender_without_name() {
5938
$this->mailer->setFrom( 'me@hello.com', '' );
6039
$sender = array(
6140
'email' => 'me@hello.com'
@@ -131,4 +110,205 @@ function test_get_headers_should_include_cc_if_exists() {
131110

132111
$this->assertTrue($formatted_headers == $expected);
133112
}
113+
114+
function test_get_recipients() {
115+
$this->mailer->addAddress('to@abc.com');
116+
$this->mailer->addAddress('to1@abc.com', 'to1');
117+
$this->mailer->addCc('cc@abc.com');
118+
$this->mailer->addCc('cc1@abc.com', 'cc1');
119+
$this->mailer->addBcc('bcc@abc.com');
120+
$this->mailer->addBcc('bcc1@abc.com', 'bcc1');
121+
122+
$header_to = implode(', ', [
123+
'to@abc.com',
124+
'to1 <to1@abc.com>',
125+
]);
126+
127+
$expected = [
128+
[
129+
'address' => [
130+
'email' => 'to@abc.com',
131+
'header_to' => $header_to
132+
]
133+
],
134+
[
135+
'address' => [
136+
'email' => 'to1@abc.com',
137+
'header_to' => $header_to
138+
]
139+
],
140+
[
141+
'address' => [
142+
'email' => 'bcc@abc.com',
143+
'header_to' => $header_to
144+
]
145+
],
146+
[
147+
'address' => [
148+
'email' => 'bcc1@abc.com',
149+
'header_to' => $header_to
150+
]
151+
],
152+
[
153+
'address' => [
154+
'email' => 'cc@abc.com',
155+
'header_to' => $header_to
156+
]
157+
],
158+
[
159+
'address' => [
160+
'email' => 'cc1@abc.com',
161+
'header_to' => $header_to
162+
]
163+
]
164+
];
165+
166+
$recipients = NSA::invokeMethod($this->mailer, 'get_recipients');
167+
$this->assertTrue($recipients == $expected);
168+
}
169+
170+
function test_get_attachments() {
171+
$temp = tempnam('/tmp', 'php-wordpress-sparkpost');
172+
file_put_contents($temp, 'TEST');
173+
$this->mailer->addAttachment($temp);
174+
$attachments = NSA::invokeMethod($this->mailer, 'get_attachments');
175+
$this->assertTrue($attachments[0]['type'] === 'application/octet-stream');
176+
$this->assertTrue($attachments[0]['name'] === basename($temp));
177+
$this->assertTrue($attachments[0]['data'] === base64_encode('TEST'));
178+
unlink($temp);
179+
}
180+
181+
function test_isMail() {
182+
// test if isMail sets correct mailer
183+
$this->mailer->Mailer = 'abc';
184+
$this->assertTrue($this->mailer->Mailer === 'abc');
185+
$this->mailer->isMail();
186+
$this->assertTrue($this->mailer->Mailer === 'sparkpost');
187+
}
188+
189+
function test_get_request_body_without_template() {
190+
// WITHOUT TEMPLATE
191+
$this->mailer->addAddress('abc@xyz.com', 'abc');
192+
$this->mailer->addBcc('bcc@xyz.com', 'bcc');
193+
$this->mailer->addCc('cc@xyz.com', 'cc');
194+
$this->mailer->setFrom( 'me@hello.com', 'me');
195+
196+
NSA::setProperty($this->mailer, 'settings', [
197+
'enable_tracking' => true,
198+
'transactional' => false
199+
]);
200+
201+
$header_to = 'abc <abc@xyz.com>';
202+
$expected_request_body = [
203+
'recipients' => [
204+
[
205+
'address' => [
206+
'email' => 'abc@xyz.com',
207+
'header_to' => $header_to
208+
]
209+
],
210+
[
211+
'address' => [
212+
'email' => 'bcc@xyz.com',
213+
'header_to' => $header_to
214+
]
215+
],
216+
[
217+
'address' => [
218+
'email' => 'cc@xyz.com',
219+
'header_to' => $header_to
220+
]
221+
]
222+
],
223+
'options' => [
224+
'open_tracking' => (bool) true,
225+
'click_tracking' => (bool) true,
226+
'transactional' => (bool) false
227+
],
228+
'content' => [
229+
'from' => [
230+
'name' => 'me',
231+
'email' =>'me@hello.com'
232+
],
233+
'subject' => '',
234+
'headers' => [],
235+
'text' => ''
236+
]
237+
];
238+
239+
$actual = NSA::invokeMethod($this->mailer, 'get_request_body');
240+
// for simpler expectation reset content.headers to empty array.
241+
// alternative is to stub get_headers which isn't working expectedly
242+
$actual['content']['headers'] = [];
243+
$this->assertTrue($expected_request_body == $actual);
244+
245+
//INCLUDE REPLYTO
246+
$this->mailer->addReplyTo('reply@abc.com', 'reply-to');
247+
$this->mailer->addCustomHeader('Reply-To', 'reply-to <reply@abc.com>'); //for below version v4.6
248+
$actual = NSA::invokeMethod($this->mailer, 'get_request_body');
249+
$actual['content']['headers'] = []; //see note above
250+
$expected_request_body['content']['reply_to'] = 'reply-to <reply@abc.com>';
251+
$this->assertTrue($expected_request_body == $actual);
252+
}
253+
254+
function test_get_request_body_with_template() {
255+
$this->mailer->addAddress('abc@xyz.com', 'abc');
256+
$this->mailer->addBcc('bcc@xyz.com', 'bcc');
257+
$this->mailer->addCc('cc@xyz.com', 'cc');
258+
$this->mailer->setFrom( 'me@hello.com', 'me');
259+
$header_to = 'abc <abc@xyz.com>';
260+
NSA::setProperty($this->mailer, 'settings', [
261+
'enable_tracking' => true,
262+
'transactional' => false,
263+
'template' => 'hello'
264+
]);
265+
266+
$expected_request_body = [
267+
'recipients' => [
268+
[
269+
'address' => [
270+
'email' => 'abc@xyz.com',
271+
'header_to' => $header_to
272+
]
273+
],
274+
[
275+
'address' => [
276+
'email' => 'bcc@xyz.com',
277+
'header_to' => $header_to
278+
]
279+
],
280+
[
281+
'address' => [
282+
'email' => 'cc@xyz.com',
283+
'header_to' => $header_to
284+
]
285+
]
286+
],
287+
'options' => [
288+
'open_tracking' => (bool) true,
289+
'click_tracking' => (bool) true,
290+
'transactional' => (bool) false
291+
],
292+
'content' => [
293+
'template_id' => 'hello',
294+
],
295+
'substitution_data' => [
296+
'content' => '',
297+
'subject' => '',
298+
'from_name' => 'me',
299+
'from' => 'me <me@hello.com>',
300+
'from_localpart' => 'me'
301+
]
302+
];
303+
304+
$actual = NSA::invokeMethod($this->mailer, 'get_request_body');
305+
$this->assertTrue($expected_request_body == $actual);
306+
307+
//INCLUDE REPLYTO
308+
$this->mailer->addReplyTo('reply@abc.com', 'reply-to');
309+
$this->mailer->addCustomHeader('Reply-To', 'reply-to <reply@abc.com>'); //for below version v4.6
310+
$actual = NSA::invokeMethod($this->mailer, 'get_request_body');
311+
$expected_request_body['substitution_data']['reply_to'] = 'reply-to <reply@abc.com>';
312+
$this->assertTrue($expected_request_body == $actual);
313+
}
134314
}

0 commit comments

Comments
 (0)