Commit | Line | Data |
---|---|---|
56df2d06 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * @group headless | |
5 | */ | |
6 | class CRM_Mailing_TokensTest extends \CiviUnitTestCase { | |
7 | protected function setUp() { | |
8 | $this->useTransaction(); | |
9 | parent::setUp(); | |
10 | $this->callAPISuccess('mail_settings', 'get', | |
11 | array('api.mail_settings.create' => array('domain' => 'chaos.org'))); | |
12 | } | |
13 | ||
14 | public function getExampleTokens() { | |
15 | $cases = array(); | |
16 | ||
17 | $cases[] = array('text/plain', 'The {mailing.id}!', ';The [0-9]+!;'); | |
18 | $cases[] = array('text/plain', 'The {mailing.name}!', ';The Example Name!;'); | |
19 | $cases[] = array('text/plain', 'The {mailing.editUrl}!', ';The http.*civicrm/mailing/send.*!;'); | |
20 | $cases[] = array('text/plain', 'To subscribe: {action.subscribeUrl}!', ';To subscribe: http.*civicrm/mailing/subscribe.*!;'); | |
21 | $cases[] = array('text/plain', 'To optout: {action.optOutUrl}!', ';To optout: http.*civicrm/mailing/optout.*!;'); | |
22 | $cases[] = array('text/plain', 'To unsubscribe: {action.unsubscribe}!', ';To unsubscribe: u\.123\.456\.abcd1234@chaos.org!;'); | |
23 | ||
24 | // TODO: Think about supporting dynamic tokens like "{action.subscribe.\d+}" | |
25 | ||
26 | return $cases; | |
27 | } | |
28 | ||
29 | /** | |
30 | * Check that mailing-tokens are generated (given a mailing_id as input). | |
31 | * | |
32 | * @param string $inputTemplateFormat | |
33 | * Ex: 'text/plain' or 'text/html' | |
34 | * @param string $inputTemplate | |
35 | * Ex: 'Hello, {contact.first_name}'. | |
36 | * @param string $expectRegex | |
37 | * @dataProvider getExampleTokens | |
38 | */ | |
39 | public function testTokensWithMailingId($inputTemplateFormat, $inputTemplate, $expectRegex) { | |
40 | $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', array( | |
41 | 'name' => 'Example Name', | |
42 | )); | |
43 | $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact'); | |
44 | ||
45 | $p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), array( | |
46 | 'mailingId' => $mailing->id, | |
47 | )); | |
48 | $p->addMessage('example', $inputTemplate, $inputTemplateFormat); | |
49 | $p->addRow()->context(array( | |
50 | 'contactId' => $contact->id, | |
51 | 'mailingJobId' => 123, | |
52 | 'mailingActionTarget' => array( | |
53 | 'id' => 456, | |
54 | 'hash' => 'abcd1234', | |
55 | 'email' => 'someone@example.com', | |
56 | ), | |
57 | )); | |
58 | $p->evaluate(); | |
59 | $count = 0; | |
60 | foreach ($p->getRows() as $row) { | |
61 | $this->assertRegExp($expectRegex, $row->render('example')); | |
62 | $count++; | |
63 | } | |
64 | $this->assertEquals(1, $count); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Check that mailing-tokens are generated (given a mailing DAO as input). | |
69 | */ | |
70 | public function testTokensWithMailingObject() { | |
71 | // We only need one case to see that the mailing-object works as | |
72 | // an alternative to the mailing-id. | |
73 | $inputTemplateFormat = 'text/plain'; | |
74 | $inputTemplate = 'To optout: {action.optOutUrl}!'; | |
75 | $expectRegex = ';To optout: http.*civicrm/mailing/optout.*!;'; | |
76 | ||
77 | $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', array( | |
78 | 'name' => 'Example Name', | |
79 | )); | |
80 | $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact'); | |
81 | ||
82 | $p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), array( | |
83 | 'mailing' => $mailing, | |
84 | )); | |
85 | $p->addMessage('example', $inputTemplate, $inputTemplateFormat); | |
86 | $p->addRow()->context(array( | |
87 | 'contactId' => $contact->id, | |
88 | 'mailingJobId' => 123, | |
89 | 'mailingActionTarget' => array( | |
90 | 'id' => 456, | |
91 | 'hash' => 'abcd1234', | |
92 | 'email' => 'someone@example.com', | |
93 | ), | |
94 | )); | |
95 | $p->evaluate(); | |
96 | $count = 0; | |
97 | foreach ($p->getRows() as $row) { | |
98 | $this->assertRegExp($expectRegex, $row->render('example')); | |
99 | $count++; | |
100 | } | |
101 | $this->assertEquals(1, $count); | |
102 | } | |
103 | ||
b4a0d8e7 TO |
104 | /** |
105 | * Check the behavior in the erroneous situation where someone uses | |
106 | * a mailing-related token without providing a mailing ID. | |
107 | */ | |
108 | public function testTokensWithoutMailing() { | |
109 | // We only need one case to see that the mailing-object works as | |
110 | // an alternative to the mailing-id. | |
111 | $inputTemplateFormat = 'text/plain'; | |
112 | $inputTemplate = 'To optout: {action.optOutUrl}!'; | |
113 | ||
114 | $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', array( | |
115 | 'name' => 'Example Name', | |
116 | )); | |
117 | $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact'); | |
118 | ||
119 | $p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), array( | |
120 | 'mailing' => $mailing, | |
121 | )); | |
122 | $p->addMessage('example', $inputTemplate, $inputTemplateFormat); | |
123 | $p->addRow()->context(array( | |
124 | 'contactId' => $contact->id, | |
125 | )); | |
126 | try { | |
127 | $p->evaluate(); | |
128 | $this->fail('TokenProcessor::evaluate() should have thrown an exception'); | |
129 | } | |
130 | catch (CRM_Core_Exception $e) { | |
131 | $this->assertRegExp(';Cannot use action tokens unless context defines mailingJobId and mailingActionTarget;', $e->getMessage()); | |
132 | } | |
133 | } | |
134 | ||
56df2d06 | 135 | } |