4 * Class CRM_Utils_TokenTest
7 class CRM_Utils_TokenTest
extends CiviUnitTestCase
{
10 * Basic test on getTokenDetails function.
12 public function testGetTokenDetails() {
13 $contactID = $this->individualCreate(['preferred_communication_method' => ['Phone', 'Fax']]);
14 [$resolvedTokens] = CRM_Utils_Token
::getTokenDetails([$contactID]);
15 $this->assertEquals('Phone, Fax', $resolvedTokens[$contactID]['preferred_communication_method']);
19 * Test getting contacts w/o primary location type
21 * Check for situation described in CRM-19876.
23 public function testSearchByPrimaryLocation() {
24 // Disable searchPrimaryDetailsOnly civi settings so we could test the functionality without it.
25 Civi
::settings()->set('searchPrimaryDetailsOnly', '0');
27 // create a contact with multiple email address and among which one is primary
28 $contactID = $this->individualCreate();
29 $primaryEmail = uniqid() . '@primary.com';
30 $this->callAPISuccess('Email', 'create', [
31 'contact_id' => $contactID,
32 'email' => $primaryEmail,
33 'location_type_id' => 'Other',
36 $this->callAPISuccess('Email', 'create', [
37 'contact_id' => $contactID,
38 'email' => uniqid() . '@galaxy.com',
39 'location_type_id' => 'Work',
42 $this->callAPISuccess('Email', 'create', [
43 'contact_id' => $contactID,
44 'email' => uniqid() . '@galaxy.com',
45 'location_type_id' => 'Work',
49 $contactIDs = [$contactID];
51 // when we are fetching contact details ON basis of primary address fields
52 [$contactDetails] = CRM_Utils_Token
::getTokenDetails($contactIDs);
53 $this->assertEquals($primaryEmail, $contactDetails[$contactID]['email']);
56 Civi
::settings()->set('searchPrimaryDetailsOnly', '1');
60 * Test for replaceGreetingTokens.
63 public function testReplaceGreetingTokens(): void
{
64 $tokenString = 'First Name: {contact.first_name} Last Name: {contact.last_name} Birth Date: {contact.birth_date} Prefix: {contact.prefix_id:label} Suffix: {contact.individual_suffix}';
69 'contact_type' => 'Individual',
70 'first_name' => 'Morticia',
71 'last_name' => 'Addams',
77 $className = 'CRM_Contact_BAO_Contact';
79 CRM_Utils_Token
::replaceGreetingTokens($tokenString, $contactDetails, $contactId, $className, $escapeSmarty);
80 $this->assertEquals($tokenString, 'First Name: Morticia Last Name: Addams Birth Date: Prefix: Ms. Suffix: ');
82 // Test compatibility with custom tokens (#14943)
83 $tokenString = 'Custom {custom.custom}';
84 CRM_Utils_Token
::replaceGreetingTokens($tokenString, $contactDetails, $contactId, $className, $escapeSmarty);
85 $this->assertEquals($tokenString, 'Custom ');
89 * Test getting multiple contacts.
91 * Check for situation described in CRM-19876.
93 public function testGetTokenDetailsMultipleEmails() {
103 'email' => 'guardians@galaxy.com',
104 'legal_identifier' => 'convict 56',
105 'nick_name' => 'bob',
106 'contact_source' => 'bargain basement',
107 'formal_title' => 'Your silliness',
108 'job_title' => 'World Saviour',
110 'birth_date' => '2017-01-01',
111 // 'city' => 'Metropolis',
115 $contactIDs[] = $contactID = $this->individualCreate($params);
116 $this->callAPISuccess('Email', 'create', [
117 'contact_id' => $contactID,
118 'email' => 'goodguy@galaxy.com',
119 'location_type_id' => 'Other',
122 $this->callAPISuccess('Email', 'create', [
123 'contact_id' => $contactID,
124 'email' => 'villain@galaxy.com',
125 'location_type_id' => 'Work',
130 unset($params['email']);
132 [$resolvedTokens] = CRM_Utils_Token
::getTokenDetails($contactIDs);
133 foreach ($contactIDs as $contactID) {
134 $resolvedContactTokens = $resolvedTokens[$contactID];
135 $this->assertEquals('Individual', $resolvedContactTokens['contact_type']);
136 $this->assertEquals('Anderson, Anthony', $resolvedContactTokens['sort_name']);
137 $this->assertEquals('en_US', $resolvedContactTokens['preferred_language']);
138 $this->assertEquals('Both', $resolvedContactTokens['preferred_mail_format']);
139 $this->assertEquals(3, $resolvedContactTokens['prefix_id']);
140 $this->assertEquals(3, $resolvedContactTokens['suffix_id']);
141 $this->assertEquals('Mr. Anthony J. Anderson II', $resolvedContactTokens['addressee_display']);
142 $this->assertEquals('villain@galaxy.com', $resolvedContactTokens['email']);
144 foreach ($params as $key => $value) {
145 $this->assertEquals($value, $resolvedContactTokens[$key]);
151 * This is a basic test of the token processor (currently testing TokenCompatSubscriber)
152 * and makes sure that greeting + contact tokens are replaced.
153 * This is a good example to copy/expand when creating additional tests for token processor
154 * in "real" situations.
156 public function testTokenProcessor(): void
{
157 $params['contact_id'] = $this->individualCreate();
159 // Prepare the processor and general context.
160 $tokenProc = new \Civi\Token\
TokenProcessor(\Civi
::dispatcher(), [
161 // Unique(ish) identifier for our controller/use-case.
162 'controller' => 'civicrm_tokentest',
164 // Provide hints about what data will be available for each row.
165 // Ex: 'schema' => ['contactId', 'activityId', 'caseId'],
166 'schema' => ['contactId'],
168 // Whether to enable Smarty evaluation.
169 'smarty' => (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
),
172 // Define message templates.
173 $tokenProc->addMessage('body_html', 'Good morning, <p>{contact.email_greeting} {contact.display_name}</p>. {custom.foobar} Bye!', 'text/html');
174 $tokenProc->addMessage('body_text', 'Good morning, {contact.email_greeting} {contact.display_name} Bye!', 'text/plain');
176 $expect[$params['contact_id']]['html'] = 'Good morning, <p>Dear Anthony Mr. Anthony Anderson II</p>. Bye!';
177 $expect[$params['contact_id']]['text'] = 'Good morning, Dear Anthony Mr. Anthony Anderson II Bye!';
180 foreach (explode(',', $params['contact_id']) as $contactId) {
181 $context = ['contactId' => $contactId];
182 $tokenProc->addRow()->context($context);
185 $tokenProc->evaluate();
187 $this->assertNotEmpty($tokenProc->getRows());
188 foreach ($tokenProc->getRows() as $tokenRow) {
189 /** @var \Civi\Token\TokenRow $tokenRow */
190 $html = $tokenRow->render('body_html');
191 $text = $tokenRow->render('body_text');
192 $this->assertEquals($expect[$params['contact_id']]['html'], $html);
193 $this->assertEquals($expect[$params['contact_id']]['text'], $text);