|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\SparkPost\Test; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Environment; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
use SilverStripe\Control\Email\Email; |
|
8
|
|
|
use LeKoala\SparkPost\SparkPostHelper; |
|
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
10
|
|
|
use Symfony\Component\Mailer\Envelope; |
|
11
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
12
|
|
|
use Symfony\Component\Mime\Address; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Test for SparkPost |
|
16
|
|
|
* |
|
17
|
|
|
* @group SparkPost |
|
18
|
|
|
*/ |
|
19
|
|
|
class SparkPostTest extends SapphireTest |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var MailerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $testMailer; |
|
25
|
|
|
protected bool $isDummy = false; |
|
26
|
|
|
|
|
27
|
|
|
protected function setUp(): void |
|
28
|
|
|
{ |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
|
|
31
|
|
|
// add dummy api key |
|
32
|
|
|
if (!SparkPostHelper::getAPIKey()) { |
|
33
|
|
|
$this->isDummy = true; |
|
34
|
|
|
SparkPostHelper::config()->api_key = "dummy"; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->testMailer = Injector::inst()->get(MailerInterface::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function tearDown(): void |
|
41
|
|
|
{ |
|
42
|
|
|
parent::tearDown(); |
|
43
|
|
|
|
|
44
|
|
|
Injector::inst()->registerService($this->testMailer, MailerInterface::class); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testSetup(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$inst = SparkPostHelper::registerTransport(); |
|
50
|
|
|
$mailer = SparkPostHelper::getMailer(); |
|
51
|
|
|
$instClass = get_class($inst); |
|
52
|
|
|
$instMailer = get_class($mailer); |
|
53
|
|
|
$this->assertEquals($instClass, $instMailer); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testClient(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$client = SparkPostHelper::getClient(); |
|
59
|
|
|
|
|
60
|
|
|
if ($this->isDummy) { |
|
61
|
|
|
$this->assertTrue(true); |
|
62
|
|
|
} else { |
|
63
|
|
|
$result = $client->listAllSendingDomains(); |
|
64
|
|
|
$this->assertTrue(is_array($result)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testTLSVersion(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$ch = curl_init(); |
|
71
|
|
|
// This fixes ca cert issues if server is not configured properly |
|
72
|
|
|
$cainfo = ini_get('curl.cainfo'); |
|
73
|
|
|
if (is_string($cainfo) && strlen($cainfo) === 0) { |
|
74
|
|
|
curl_setopt($ch, CURLOPT_CAINFO, \Composer\CaBundle\CaBundle::getBundledCaBundlePath()); |
|
75
|
|
|
} |
|
76
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://www.howsmyssl.com/a/check'); |
|
77
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
78
|
|
|
$data = curl_exec($ch); |
|
79
|
|
|
if (!$data) { |
|
80
|
|
|
$this->markTestIncomplete('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch)); |
|
81
|
|
|
} |
|
82
|
|
|
curl_close($ch); |
|
83
|
|
|
if (is_string($data)) { |
|
84
|
|
|
$json = json_decode($data); |
|
85
|
|
|
$this->assertNotEquals("TLS 1.0", $json->tls_version); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testSending(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$test_to = Environment::getEnv('SPARKPOST_TEST_TO'); |
|
92
|
|
|
$test_from = Environment::getEnv('SPARKPOST_TEST_FROM'); |
|
93
|
|
|
|
|
94
|
|
|
$mailer = SparkPostHelper::registerTransport(); |
|
95
|
|
|
|
|
96
|
|
|
$email = new Email(); |
|
97
|
|
|
$email->setSubject('Test email'); |
|
98
|
|
|
$email->setBody("Body of my email"); |
|
99
|
|
|
|
|
100
|
|
|
if (!$test_from || !$test_to || $this->isDummy) { |
|
101
|
|
|
$test_to = "example@localhost"; |
|
102
|
|
|
$test_from = "sender@localhost"; |
|
103
|
|
|
// don't try to send it for real |
|
104
|
|
|
$email->getHeaders()->addTextHeader('X-SendingDisabled', "true"); |
|
105
|
|
|
} |
|
106
|
|
|
$email->setTo($test_to); |
|
107
|
|
|
$email->setFrom($test_from); |
|
108
|
|
|
|
|
109
|
|
|
// This is async, therefore it does not return anything anymore |
|
110
|
|
|
$email->send(); |
|
111
|
|
|
|
|
112
|
|
|
/** @var \LeKoala\SparkPost\SparkPostApiTransport $transport */ |
|
113
|
|
|
$transport = SparkPostHelper::getTransportFromMailer($mailer); |
|
114
|
|
|
$result = $transport->getApiResult(); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertEquals(1, $result['total_accepted_recipients']); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testPayload(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$mailer = SparkPostHelper::registerTransport(); |
|
122
|
|
|
/** @var \LeKoala\SparkPost\SparkPostApiTransport $transport */ |
|
123
|
|
|
$transport = SparkPostHelper::getTransportFromMailer($mailer); |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
$html = <<<HTML |
|
127
|
|
|
<!DOCTYPE html> |
|
128
|
|
|
<html> |
|
129
|
|
|
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">.red {color:red;}</style></head> |
|
130
|
|
|
<body><span class="red">red</span></body> |
|
131
|
|
|
</html> |
|
132
|
|
|
HTML; |
|
133
|
|
|
$result = <<<HTML |
|
134
|
|
|
<!DOCTYPE html> |
|
135
|
|
|
<html> |
|
136
|
|
|
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head> |
|
137
|
|
|
<body><span class="red" style="color: red;">red</span></body> |
|
138
|
|
|
</html> |
|
139
|
|
|
|
|
140
|
|
|
HTML; |
|
141
|
|
|
|
|
142
|
|
|
$sender = new Address('[email protected]', "testman"); |
|
143
|
|
|
$recipients = [ |
|
144
|
|
|
new Address('[email protected]', "testrec"), |
|
145
|
|
|
]; |
|
146
|
|
|
$email = new Email(); |
|
147
|
|
|
$email->setBody($html); |
|
148
|
|
|
$envelope = new Envelope($sender, $recipients); |
|
149
|
|
|
$payload = $transport->getPayload($email, $envelope); |
|
150
|
|
|
$content = $payload['content']; |
|
151
|
|
|
|
|
152
|
|
|
$payloadRecipients = $payload['recipients'][0]; |
|
153
|
|
|
$this->assertEquals("testrec", $recipients[0]->getName()); |
|
154
|
|
|
$this->assertEquals( |
|
155
|
|
|
[ |
|
156
|
|
|
'address' => [ |
|
157
|
|
|
'email' => '[email protected]', |
|
158
|
|
|
'name' => 'rec' // extracted from email due to how recipients work |
|
159
|
|
|
] |
|
160
|
|
|
], |
|
161
|
|
|
$payloadRecipients |
|
162
|
|
|
); |
|
163
|
|
|
$payloadSender = $content['from']; |
|
164
|
|
|
$this->assertEquals([ |
|
165
|
|
|
'email' => '[email protected]', |
|
166
|
|
|
'name' => 'testman' |
|
167
|
|
|
], $payloadSender); |
|
168
|
|
|
|
|
169
|
|
|
// Make sure our styles are properly inlined |
|
170
|
|
|
$this->assertEquals('red', $content['text']); |
|
171
|
|
|
$this->assertEquals($result, $content['html']); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|