merge in 5.25
[civicrm-core.git] / tests / phpunit / CRM / Mailing / TokensTest.php
1 <?php
2
3 /**
4 * @group headless
5 */
6 class CRM_Mailing_TokensTest extends \CiviUnitTestCase {
7
8 protected function setUp() {
9 $this->useTransaction();
10 parent::setUp();
11 $this->callAPISuccess('mail_settings', 'get',
12 ['api.mail_settings.create' => ['domain' => 'chaos.org']]);
13 }
14
15 public function getExampleTokens() {
16 $cases = [];
17
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!;'];
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) {
41 $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', [
42 'name' => 'Example Name',
43 ]);
44 $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');
45
46 $p = new \Civi\Token\TokenProcessor(Civi::dispatcher(), [
47 'mailingId' => $mailing->id,
48 ]);
49 $p->addMessage('example', $inputTemplate, $inputTemplateFormat);
50 $p->addRow()->context([
51 'contactId' => $contact->id,
52 'mailingJobId' => 123,
53 'mailingActionTarget' => [
54 'id' => 456,
55 'hash' => 'abcd1234',
56 'email' => 'someone@example.com',
57 ],
58 ]);
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
78 $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', [
79 'name' => 'Example Name',
80 ]);
81 $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');
82
83 $p = new \Civi\Token\TokenProcessor(Civi::dispatcher(), [
84 'mailing' => $mailing,
85 ]);
86 $p->addMessage('example', $inputTemplate, $inputTemplateFormat);
87 $p->addRow()->context([
88 'contactId' => $contact->id,
89 'mailingJobId' => 123,
90 'mailingActionTarget' => [
91 'id' => 456,
92 'hash' => 'abcd1234',
93 'email' => 'someone@example.com',
94 ],
95 ]);
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
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
112 /**
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
122 */
123 public function testTokensWithoutMailingJob($inputTemplateFormat, $inputTemplateText, $expectRegex) {
124 $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', [
125 'name' => 'Example Name',
126 ]);
127 $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact');
128
129 $p = new \Civi\Token\TokenProcessor(Civi::dispatcher(), [
130 'mailing' => $mailing,
131 ]);
132 $p->addMessage('example', $inputTemplateText, $inputTemplateFormat);
133 $p->addRow()->context([
134 'contactId' => $contact->id,
135 ]);
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);
149 }
150
151 }