6 class CRM_Mailing_TokensTest
extends \CiviUnitTestCase
{
8 protected function setUp() {
9 $this->useTransaction();
11 $this->callAPISuccess('mail_settings', 'get',
12 ['api.mail_settings.create' => ['domain' => 'chaos.org']]);
15 public function getExampleTokens() {
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!;'];
25 // TODO: Think about supporting dynamic tokens like "{action.subscribe.\d+}"
31 * Check that mailing-tokens are generated (given a mailing_id as input).
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
40 public function testTokensWithMailingId($inputTemplateFormat, $inputTemplate, $expectRegex) {
41 $mailing = CRM_Core_DAO
::createTestObject('CRM_Mailing_DAO_Mailing', [
42 'name' => 'Example Name',
44 $contact = CRM_Core_DAO
::createTestObject('CRM_Contact_DAO_Contact');
46 $p = new \Civi\Token\
TokenProcessor(Civi
::dispatcher(), [
47 'mailingId' => $mailing->id
,
49 $p->addMessage('example', $inputTemplate, $inputTemplateFormat);
50 $p->addRow()->context([
51 'contactId' => $contact->id
,
52 'mailingJobId' => 123,
53 'mailingActionTarget' => [
56 'email' => 'someone@example.com',
61 foreach ($p->getRows() as $row) {
62 $this->assertRegExp($expectRegex, $row->render('example'));
65 $this->assertEquals(1, $count);
69 * Check that mailing-tokens are generated (given a mailing DAO as input).
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.*!;';
78 $mailing = CRM_Core_DAO
::createTestObject('CRM_Mailing_DAO_Mailing', [
79 'name' => 'Example Name',
81 $contact = CRM_Core_DAO
::createTestObject('CRM_Contact_DAO_Contact');
83 $p = new \Civi\Token\
TokenProcessor(Civi
::dispatcher(), [
84 'mailing' => $mailing,
86 $p->addMessage('example', $inputTemplate, $inputTemplateFormat);
87 $p->addRow()->context([
88 'contactId' => $contact->id
,
89 'mailingJobId' => 123,
90 'mailingActionTarget' => [
93 'email' => 'someone@example.com',
98 foreach ($p->getRows() as $row) {
99 $this->assertRegExp($expectRegex, $row->render('example'));
102 $this->assertEquals(1, $count);
105 public function getExampleTokensForUseWithoutMailingJob() {
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.*&jid=&qid=.*">click@'];
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`.
118 * This current behavior may be wise or unwise - either way, having ensures
119 * that changes are intentional.
121 * @dataProvider getExampleTokensForUseWithoutMailingJob
123 public function testTokensWithoutMailingJob($inputTemplateFormat, $inputTemplateText, $expectRegex) {
124 $mailing = CRM_Core_DAO
::createTestObject('CRM_Mailing_DAO_Mailing', [
125 'name' => 'Example Name',
127 $contact = CRM_Core_DAO
::createTestObject('CRM_Contact_DAO_Contact');
129 $p = new \Civi\Token\
TokenProcessor(Civi
::dispatcher(), [
130 'mailing' => $mailing,
132 $p->addMessage('example', $inputTemplateText, $inputTemplateFormat);
133 $p->addRow()->context([
134 'contactId' => $contact->id
,
138 // $this->fail('TokenProcessor::evaluate() should have thrown an exception');
140 // catch (CRM_Core_Exception $e) {
141 // $this->assertRegExp(';Cannot use action tokens unless context defines mailingJobId and mailingActionTarget;', $e->getMessage());
146 // FIXME: For compatibility with
147 $actual = $p->getRow(0)->render('example');
148 $this->assertRegExp($expectRegex, $actual);