Merge pull request #15330 from mattwire/paymentprocessor_testmodelivemode
[civicrm-core.git] / tests / phpunit / CRM / Mailing / TokensTest.php
CommitLineData
56df2d06
TO
1<?php
2
3/**
4 * @group headless
5 */
6class CRM_Mailing_TokensTest extends \CiviUnitTestCase {
39b959db 7
56df2d06
TO
8 protected function setUp() {
9 $this->useTransaction();
10 parent::setUp();
11 $this->callAPISuccess('mail_settings', 'get',
9099cab3 12 ['api.mail_settings.create' => ['domain' => 'chaos.org']]);
56df2d06
TO
13 }
14
15 public function getExampleTokens() {
9099cab3 16 $cases = [];
56df2d06 17
9099cab3
CW
18 $cases[] = ['text/plain', 'The {mailing.id}!', ';The [0-9]+!;'];
19 $cases[] = ['text/plain', 'The {mailing.name}!', ';The Example Name!;'];
20 $cases[] = ['text/plain', 'The {mailing.editUrl}!', ';The http.*civicrm/mailing/send.*!;'];
21 $cases[] = ['text/plain', 'To subscribe: {action.subscribeUrl}!', ';To subscribe: http.*civicrm/mailing/subscribe.*!;'];
22 $cases[] = ['text/plain', 'To optout: {action.optOutUrl}!', ';To optout: http.*civicrm/mailing/optout.*!;'];
23 $cases[] = ['text/plain', 'To unsubscribe: {action.unsubscribe}!', ';To unsubscribe: u\.123\.456\.abcd1234@chaos.org!;'];
56df2d06
TO
24
25 // TODO: Think about supporting dynamic tokens like "{action.subscribe.\d+}"
26
27 return $cases;
28 }
29
30 /**
31 * Check that mailing-tokens are generated (given a mailing_id as input).
32 *
33 * @param string $inputTemplateFormat
34 * Ex: 'text/plain' or 'text/html'
35 * @param string $inputTemplate
36 * Ex: 'Hello, {contact.first_name}'.
37 * @param string $expectRegex
38 * @dataProvider getExampleTokens
39 */
40 public function testTokensWithMailingId($inputTemplateFormat, $inputTemplate, $expectRegex) {
9099cab3 41 $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', [
56df2d06 42 'name' => 'Example Name',
9099cab3 43 ]);
56df2d06
TO
44 $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');
45
9099cab3 46 $p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), [
56df2d06 47 'mailingId' => $mailing->id,
9099cab3 48 ]);
56df2d06 49 $p->addMessage('example', $inputTemplate, $inputTemplateFormat);
9099cab3 50 $p->addRow()->context([
56df2d06
TO
51 'contactId' => $contact->id,
52 'mailingJobId' => 123,
9099cab3 53 'mailingActionTarget' => [
56df2d06
TO
54 'id' => 456,
55 'hash' => 'abcd1234',
56 'email' => 'someone@example.com',
9099cab3
CW
57 ],
58 ]);
56df2d06
TO
59 $p->evaluate();
60 $count = 0;
61 foreach ($p->getRows() as $row) {
62 $this->assertRegExp($expectRegex, $row->render('example'));
63 $count++;
64 }
65 $this->assertEquals(1, $count);
66 }
67
68 /**
69 * Check that mailing-tokens are generated (given a mailing DAO as input).
70 */
71 public function testTokensWithMailingObject() {
72 // We only need one case to see that the mailing-object works as
73 // an alternative to the mailing-id.
74 $inputTemplateFormat = 'text/plain';
75 $inputTemplate = 'To optout: {action.optOutUrl}!';
76 $expectRegex = ';To optout: http.*civicrm/mailing/optout.*!;';
77
9099cab3 78 $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', [
56df2d06 79 'name' => 'Example Name',
9099cab3 80 ]);
56df2d06
TO
81 $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');
82
9099cab3 83 $p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), [
56df2d06 84 'mailing' => $mailing,
9099cab3 85 ]);
56df2d06 86 $p->addMessage('example', $inputTemplate, $inputTemplateFormat);
9099cab3 87 $p->addRow()->context([
56df2d06
TO
88 'contactId' => $contact->id,
89 'mailingJobId' => 123,
9099cab3 90 'mailingActionTarget' => [
56df2d06
TO
91 'id' => 456,
92 'hash' => 'abcd1234',
93 'email' => 'someone@example.com',
9099cab3
CW
94 ],
95 ]);
56df2d06
TO
96 $p->evaluate();
97 $count = 0;
98 foreach ($p->getRows() as $row) {
99 $this->assertRegExp($expectRegex, $row->render('example'));
100 $count++;
101 }
102 $this->assertEquals(1, $count);
103 }
104
d02df737
TO
105 public function getExampleTokensForUseWithoutMailingJob() {
106 $cases = [];
107 $cases[] = ['text/plain', 'To opt out: {action.optOutUrl}!', '@To opt out: .*civicrm/mailing/optout.*&jid=&qid=@'];
108 $cases[] = ['text/html', 'To opt out: <a href="{action.optOutUrl}">click here</a>!', '@To opt out: <a href=".*civicrm/mailing/optout.*&amp;jid=&amp;qid=.*">click@'];
109 return $cases;
110 }
111
b4a0d8e7 112 /**
d02df737
TO
113 * When previewing a mailing, there is no active mailing job, so one cannot
114 * generate fully formed URLs which reference the job. The current behavior
115 * is to link to a placeholder URL which has blank values for key fields
116 * like `jid` and `qid`.
117 *
118 * This current behavior may be wise or unwise - either way, having ensures
119 * that changes are intentional.
120 *
121 * @dataProvider getExampleTokensForUseWithoutMailingJob
b4a0d8e7 122 */
d02df737 123 public function testTokensWithoutMailingJob($inputTemplateFormat, $inputTemplateText, $expectRegex) {
9099cab3 124 $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', [
b4a0d8e7 125 'name' => 'Example Name',
9099cab3 126 ]);
b4a0d8e7
TO
127 $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');
128
9099cab3 129 $p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), [
b4a0d8e7 130 'mailing' => $mailing,
9099cab3 131 ]);
d02df737 132 $p->addMessage('example', $inputTemplateText, $inputTemplateFormat);
9099cab3 133 $p->addRow()->context([
b4a0d8e7 134 'contactId' => $contact->id,
9099cab3 135 ]);
d02df737
TO
136 // try {
137 // $p->evaluate();
138 // $this->fail('TokenProcessor::evaluate() should have thrown an exception');
139 // }
140 // catch (CRM_Core_Exception $e) {
141 // $this->assertRegExp(';Cannot use action tokens unless context defines mailingJobId and mailingActionTarget;', $e->getMessage());
142 // }
143
144 $p->evaluate();
145
146 // FIXME: For compatibility with
147 $actual = $p->getRow(0)->render('example');
148 $this->assertRegExp($expectRegex, $actual);
b4a0d8e7
TO
149 }
150
56df2d06 151}