This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCallbackSubscriberTest.php
More file actions
233 lines (205 loc) · 8.66 KB
/
CallbackSubscriberTest.php
File metadata and controls
233 lines (205 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
declare(strict_types=1);
namespace MauticPlugin\SparkpostBundle\Tests\Functional\EventSubscriber;
use Mautic\CoreBundle\Helper\DateTimeHelper;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\EmailBundle\Entity\Stat;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\Request;
class CallbackSubscriberTest extends MauticMysqlTestCase
{
protected function setUp(): void
{
if ('testSparkpostTransportNotConfigured' !== $this->getName()) {
$this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us';
}
parent::setUp();
}
public function testSparkpostTransportNotConfigured(): void
{
$this->client->request(Request::METHOD_POST, '/mailer/callback');
$response = $this->client->getResponse();
Assert::assertSame('No email transport that could process this callback was found', $response->getContent());
Assert::assertSame(404, $response->getStatusCode());
}
/**
* @dataProvider provideMessageEventType
*/
public function testSparkpostCallbackProcessByHashId(string $type, string $bounceClass): void
{
$parameters = $this->getParameters($type, $bounceClass);
$parameters[0]['msys']['message_event']['rcpt_meta'] = ['hashId' => '65763254757234'];
$contact = $this->createContact('contact@an.email');
$now = new \DateTime();
$stat = $this->createStat($contact, '65763254757234', 'contact@an.email', $now);
$this->em->flush();
$this->client->request(Request::METHOD_POST, '/mailer/callback', $parameters);
$response = $this->client->getResponse();
Assert::assertSame('Callback processed', $response->getContent());
Assert::assertSame(200, $response->getStatusCode());
// Only parse hard bounces
if ('bounce' !== $type && '25' !== $bounceClass) {
$result = $this->getCommentAndReason($type);
$openDetails = $stat->getOpenDetails();
$bounces = $openDetails['bounces'][0];
Assert::assertSame($now->format(DateTimeHelper::FORMAT_DB), $bounces['datetime']);
Assert::assertSame($result['comments'], $bounces['reason']);
$dnc = $contact->getDoNotContact()->current();
Assert::assertSame('email', $dnc->getChannel());
Assert::assertSame($result['comments'], $dnc->getComments());
Assert::assertSame($now->format(DateTimeHelper::FORMAT_DB), $dnc->getDateAdded()->format(DateTimeHelper::FORMAT_DB));
Assert::assertSame($contact, $dnc->getLead());
Assert::assertSame($result['reason'], $dnc->getReason());
}
}
/**
* @dataProvider provideMessageEventType
*/
public function testSparkpostCallbackProcessByEmailAddress(string $type, string $bounceClass): void
{
$parameters = $this->getParameters($type, $bounceClass);
$contact = $this->createContact('recipient@example.com');
$this->em->flush();
$now = new \DateTime();
$nowFormatted = $now->format(DateTimeHelper::FORMAT_DB);
$this->client->request(Request::METHOD_POST, '/mailer/callback', $parameters);
$response = $this->client->getResponse();
Assert::assertSame('Callback processed', $response->getContent());
Assert::assertSame(200, $response->getStatusCode());
// Only parse hard bounces
if ('bounce' !== $type && '25' !== $bounceClass) {
$result = $this->getCommentAndReason($type);
$dnc = $contact->getDoNotContact()->current();
Assert::assertSame('email', $dnc->getChannel());
Assert::assertSame($result['comments'], $dnc->getComments());
Assert::assertSame($nowFormatted, $dnc->getDateAdded()->format(DateTimeHelper::FORMAT_DB));
Assert::assertSame($contact, $dnc->getLead());
Assert::assertSame($result['reason'], $dnc->getReason());
}
}
/**
* @return array<mixed>
*/
public function provideMessageEventType(): iterable
{
yield ['policy_rejection', '25'];
yield ['out_of_band', '25'];
yield ['bounce', '25'];
yield ['bounce', '10'];
yield ['spam_complaint', '25'];
yield ['list_unsubscribe', '25'];
yield ['link_unsubscribe', '25'];
}
/**
* @return array<mixed>
*/
private function getParameters(string $type, string $bounceClass): array
{
return [
[
'msys' => [
'message_event' => [
'type' => $type,
'campaign_id' => 'Example Campaign Name',
'customer_id' => '1',
'error_code' => '554',
'event_id' => '92356927693813856',
'friendly_from' => 'sender@example.com',
'message_id' => '000443ee14578172be22',
'msg_from' => 'sender@example.com',
'rcpt_tags' => [
'male',
'US',
],
'rcpt_to' => 'recipient@example.com',
'raw_rcpt_to' => 'recipient@example.com',
'rcpt_type' => 'to',
'raw_reason' => 'MAIL REFUSED - IP (19.99.99.99) is in black list',
'reason' => 'MAIL REFUSED - IP (a.b.c.d) is in black list',
'remote_addr' => '127.0.0.1',
'subaccount_id' => '101',
'template_id' => 'templ-1234',
'template_version' => '1',
'timestamp' => '1454442600',
'transmission_id' => '65832150921904138',
'bounce_class' => $bounceClass,
],
],
],
];
}
private function createContact(string $email): Lead
{
$lead = new Lead();
$lead->setEmail($email);
$this->em->persist($lead);
return $lead;
}
private function createStat(Lead $contact, string $trackingHash, string $emailAddress, \DateTime $dateSent): Stat
{
$stat = new Stat();
$stat->setLead($contact);
$stat->setTrackingHash($trackingHash);
$stat->setEmailAddress($emailAddress);
$stat->setDateSent($dateSent);
$this->em->persist($stat);
return $stat;
}
/**
* @return array<mixed>
*/
private function getCommentAndReason(string $type): array
{
return match ($type) {
'policy_rejection', 'out_of_band', 'bounce' => [
'comments' => 'MAIL REFUSED - IP (19.99.99.99) is in black list',
'reason' => DoNotContact::BOUNCED,
],
'spam_complaint' => [
'comments' => '',
'reason' => DoNotContact::UNSUBSCRIBED,
],
'list_unsubscribe', 'link_unsubscribe' => [
'comments' => 'unsubscribed',
'reason' => DoNotContact::UNSUBSCRIBED,
],
default => [
'comments' => '',
'reason' => '',
],
};
}
/**
* For the message with 'type': 'out of band' and 'bounce class': 60 should never be called transportCallback.
*/
public function testProcessCallbackRequestWhenSoftBounce(): void
{
$payload = <<<JSON
[
{
"msys": {
"message_event": {
"reason":"550 [internal] [oob] The message is an auto-reply/vacation mail.",
"msg_from":"msprvs1=18290qww0ygol=bounces-44585-172@bounces.mauticsparkt3.com",
"event_id":"13251575597141532",
"raw_reason":"550 [internal] [oob] The message is an auto-reply/vacation mail.",
"error_code":"550",
"subaccount_id":172,
"delv_method":"esmtp",
"customer_id":44585,
"type":"out_of_band",
"bounce_class":"60",
"timestamp":"2020-01-22T21:59:32.000Z"
}
}
}
]
JSON;
$request = new Request([], json_decode($payload, true));
$this->sparkpostTransport->processCallbackRequest($request);
$this->transportCallback->expects($this->never())
->method($this->anything());
}
}