3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test that content produced by CiviMail looks the way it's expected.
31 * @package CiviCRM_APIv3
34 * @copyright CiviCRM LLC (c) 2004-2019
38 * Class CRM_Mailing_MailingSystemTest
40 * @see \Civi\FlexMailer\FlexMailerSystemTest
41 * @see CRM_Mailing_MailingSystemTest
43 abstract class CRM_Mailing_BaseMailingSystemTest
extends CiviUnitTestCase
{
44 protected $_apiversion = 3;
46 public $DBResetRequired = FALSE;
47 public $defaultParams = [];
55 public function setUp() {
56 $this->useTransaction();
58 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0;
60 $this->_groupID
= $this->groupCreate();
61 $this->createContactsInGroup(2, $this->_groupID
);
63 $this->defaultParams
= [
64 'name' => 'mailing name',
66 'groups' => ['include' => [$this->_groupID
]],
67 'scheduled_date' => 'now',
69 $this->_mut
= new CiviMailUtils($this, TRUE);
70 $this->callAPISuccess('mail_settings', 'get',
71 ['api.mail_settings.create' => ['domain' => 'chaos.org']]);
76 public function tearDown() {
78 CRM_Utils_Hook
::singleton()->reset();
80 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0;
85 * Generate a fully-formatted mailing with standard email headers.
87 public function testBasicHeaders() {
88 $allMessages = $this->runMailingSuccess([
89 'subject' => 'Accidents in cars cause children for {contact.display_name}!',
90 'body_text' => 'BEWARE children need regular infusions of toys. Santa knows your {domain.address}. There is no {action.optOutUrl}.',
92 foreach ($allMessages as $k => $message) {
93 /** @var ezcMail $message */
97 $this->assertEquals("FIXME", $message->from
->name
);
98 $this->assertEquals("info@EXAMPLE.ORG", $message->from
->email
);
99 $this->assertEquals("Mr. Foo{$offset} Anderson II", $message->to
[0]->name
);
100 $this->assertEquals("mail{$offset}@nul.example.com", $message->to
[0]->email
);
102 $this->assertRegExp('#^text/plain; charset=utf-8#', $message->headers
['Content-Type']);
103 $this->assertRegExp(';^b\.[\d\.a-f]+@chaos.org$;', $message->headers
['Return-Path']);
104 $this->assertRegExp(';^b\.[\d\.a-f]+@chaos.org$;', $message->headers
['X-CiviMail-Bounce'][0]);
105 $this->assertRegExp(';^\<mailto:u\.[\d\.a-f]+@chaos.org\>$;', $message->headers
['List-Unsubscribe'][0]);
106 $this->assertEquals('bulk', $message->headers
['Precedence'][0]);
111 * Generate a fully-formatted mailing (with body_text content).
113 public function testText() {
114 $allMessages = $this->runMailingSuccess([
115 'subject' => 'Accidents in cars cause children for {contact.display_name}!',
116 'body_text' => 'BEWARE children need regular infusions of toys. Santa knows your {domain.address}. There is no {action.optOutUrl}.',
117 'open_tracking' => 1,
118 // Note: open_tracking does nothing with text, but we'll just verify that it does nothing
120 foreach ($allMessages as $message) {
121 /** @var ezcMail $message */
122 /** @var ezcMailText $textPart */
124 $this->assertTrue($message->body
instanceof ezcMailText
);
126 $this->assertEquals('plain', $message->body
->subType
);
130 "Sample Header for TEXT formatted content.\n" .
131 "BEWARE children need regular infusions of toys. Santa knows your .*\\. There is no http.*civicrm/mailing/optout.*\\.\n" .
133 "to unsubscribe: http.*civicrm/mailing/optout" .
141 * Generate a fully-formatted mailing (with body_html content).
143 public function testHtmlWithOpenTracking() {
144 $allMessages = $this->runMailingSuccess([
145 'subject' => 'Example Subject',
146 'body_html' => '<p>You can go to <a href="http://example.net/first?{contact.checksum}">Google</a> or <a href="{action.optOutUrl}">opt out</a>.</p>',
147 'open_tracking' => 1,
150 foreach ($allMessages as $message) {
151 /** @var ezcMail $message */
152 /** @var ezcMailText $htmlPart */
153 /** @var ezcMailText $textPart */
155 $this->assertTrue($message->body
instanceof ezcMailMultipartAlternative
);
157 list($textPart, $htmlPart) = $message->body
->getParts();
159 $this->assertEquals('html', $htmlPart->subType
);
163 "Sample Header for HTML formatted content.\n" .
164 // FIXME: CiviMail puts double " after hyperlink!
166 "<p>You can go to <a href=\"http://example.net/first\\?cs=[0-9a-f_]+\"\"?>Google</a> or <a href=\"http.*civicrm/mailing/optout.*\">opt out</a>.</p>\n" .
168 "Sample Footer for HTML formatted content" .
170 "<img src=\".*extern/open.php.*\"" .
175 $this->assertEquals('plain', $textPart->subType
);
179 "Sample Header for TEXT formatted content.\n" .
180 // body_html, filtered
181 "You can go to Google \\[1\\] or opt out \\[2\\]\\.\n" .
185 "\\[1\\] http://example.net/first\\?cs=[0-9a-f_]+\n" .
186 "\\[2\\] http.*civicrm/mailing/optout.*\n" .
189 "to unsubscribe: http.*civicrm/mailing/optout" .
197 * Generate a fully-formatted mailing (with body_html content).
199 public function testHtmlWithOpenAndUrlTracking() {
200 $allMessages = $this->runMailingSuccess([
201 'subject' => 'Example Subject',
202 'body_html' => '<p>You can go to <a href="http://example.net">Google</a> or <a href="{action.optOutUrl}">opt out</a>.</p>',
203 'open_tracking' => 1,
206 foreach ($allMessages as $message) {
207 /** @var ezcMail $message */
208 /** @var ezcMailText $htmlPart */
209 /** @var ezcMailText $textPart */
211 $this->assertTrue($message->body
instanceof ezcMailMultipartAlternative
);
213 list($textPart, $htmlPart) = $message->body
->getParts();
215 $this->assertEquals('html', $htmlPart->subType
);
219 "<p>You can go to <a href=['\"].*extern/url\.php\?u=\d+&\\;qid=\d+['\"] rel='nofollow'>Google</a>" .
220 " or <a href=\"http.*civicrm/mailing/optout.*\">opt out</a>.</p>\n" .
222 "Sample Footer for HTML formatted content" .
224 // Open-tracking code
225 "<img src=\".*extern/open.php.*\"" .
230 $this->assertEquals('plain', $textPart->subType
);
233 // body_html, filtered
234 "You can go to Google \\[1\\] or opt out \\[2\\]\\.\n" .
238 "\\[1\\] .*extern/url\.php\?u=\d+&qid=\d+\n" .
239 "\\[2\\] http.*civicrm/mailing/optout.*\n" .
242 "to unsubscribe: http.*civicrm/mailing/optout" .
250 * Each case comes in four parts:
251 * 1. Mailing HTML (body_html)
252 * 2. Regex to run against final HTML
253 * 3. Regex to run against final text
254 * 4. Additional mailing options
258 public function urlTrackingExamples() {
263 '<p><a href="http://example.net/">Foo</a></p>',
264 ';<p><a href="http://example\.net/">Foo</a></p>;',
265 ';\\[1\\] http://example\.net/;',
266 ['url_tracking' => 0],
269 '<p><a href="http://example.net/?id={contact.contact_id}">Foo</a></p>',
270 // FIXME: Legacy tracker adds extra quote after URL
271 ';<p><a href="http://example\.net/\?id=\d+""?>Foo</a></p>;',
272 ';\\[1\\] http://example\.net/\?id=\d+;',
273 ['url_tracking' => 0],
276 '<p><a href="{action.optOutUrl}">Foo</a></p>',
277 ';<p><a href="http.*civicrm/mailing/optout.*">Foo</a></p>;',
278 ';\\[1\\] http.*civicrm/mailing/optout.*;',
279 ['url_tracking' => 0],
282 '<p>Look at <img src="http://example.net/foo.png">.</p>',
283 ';<p>Look at <img src="http://example\.net/foo\.png">\.</p>;',
285 ['url_tracking' => 0],
288 // Plain-text URL's are tracked in plain-text emails...
289 // but not in HTML emails.
290 "<p>Please go to: http://example.net/</p>",
291 ";<p>Please go to: http://example\.net/</p>;",
292 ';Please go to: http://example\.net/;',
293 ['url_tracking' => 0],
298 '<p><a href="http://example.net/">Foo</a></p>',
299 ';<p><a href=[\'"].*extern/url\.php\?u=\d+.*[\'"]>Foo</a></p>;',
300 ';\\[1\\] .*extern/url\.php\?u=\d+.*;',
301 ['url_tracking' => 1],
304 // FIXME: CiviMail URL tracking doesn't track tokenized links.
305 '<p><a href="http://example.net/?id={contact.contact_id}">Foo</a></p>',
306 // FIXME: Legacy tracker adds extra quote after URL
307 ';<p><a href="http://example\.net/\?id=\d+""?>Foo</a></p>;',
308 ';\\[1\\] http://example\.net/\?id=\d+;',
309 ['url_tracking' => 1],
312 // It would be redundant/slow to track the action URLs?
313 '<p><a href="{action.optOutUrl}">Foo</a></p>',
314 ';<p><a href="http.*civicrm/mailing/optout.*">Foo</a></p>;',
315 ';\\[1\\] http.*civicrm/mailing/optout.*;',
316 ['url_tracking' => 1],
319 // It would be excessive/slow to track every embedded image.
320 '<p>Look at <img src="http://example.net/foo.png">.</p>',
321 ';<p>Look at <img src="http://example\.net/foo\.png">\.</p>;',
323 ['url_tracking' => 1],
326 // Plain-text URL's are tracked in plain-text emails...
327 // but not in HTML emails.
328 "<p>Please go to: http://example.net/</p>",
329 ";<p>Please go to: http://example\.net/</p>;",
330 ';Please go to: .*extern/url.php\?u=\d+&qid=\d+;',
331 ['url_tracking' => 1],
338 * Generate a fully-formatted mailing (with body_html content).
340 * @dataProvider urlTrackingExamples
342 public function testUrlTracking($inputHtml, $htmlUrlRegex, $textUrlRegex, $params) {
343 $caseName = print_r(['inputHtml' => $inputHtml, 'params' => $params], 1);
345 $allMessages = $this->runMailingSuccess($params +
[
346 'subject' => 'Example Subject',
347 'body_html' => $inputHtml,
349 foreach ($allMessages as $message) {
350 /** @var ezcMail $message */
351 /** @var ezcMailText $htmlPart */
352 /** @var ezcMailText $textPart */
354 $this->assertTrue($message->body
instanceof ezcMailMultipartAlternative
);
356 list($textPart, $htmlPart) = $message->body
->getParts();
359 $this->assertEquals('html', $htmlPart->subType
, "Should have HTML part in case: $caseName");
360 $this->assertRegExp($htmlUrlRegex, $htmlPart->text
, "Should have correct HTML in case: $caseName");
364 $this->assertEquals('plain', $textPart->subType
, "Should have text part in case: $caseName");
365 $this->assertRegExp($textUrlRegex, $textPart->text
, "Should have correct text in case: $caseName");
371 * Create contacts in group.
374 * @param int $groupID
375 * @param string $domain
377 protected function createContactsInGroup(
380 $domain = 'nul.example.com'
382 for ($i = 1; $i <= $count; $i++
) {
383 $contactID = $this->individualCreate([
384 'first_name' => "Foo{$i}",
385 'email' => 'mail' . $i . '@' . $domain,
387 $this->callAPISuccess('group_contact', 'create', [
388 'contact_id' => $contactID,
389 'group_id' => $groupID,
396 * Create and execute a mailing. Return the matching messages.
398 * @param array $params
399 * List of parameters to send to Mailing.create API.
400 * @return array<ezcMail>
402 protected function runMailingSuccess($params) {
403 $mailingParams = array_merge($this->defaultParams
, $params);
404 $this->callAPISuccess('mailing', 'create', $mailingParams);
405 $this->_mut
->assertRecipients([]);
406 $this->callAPISuccess('job', 'process_mailing', ['runInNonProductionEnvironment' => TRUE]);
408 $allMessages = $this->_mut
->getAllMessages('ezc');
409 // There are exactly two contacts produced by setUp().
410 $this->assertEquals(2, count($allMessages));