[REF] Fix some more calls to getTokens to make it clear only the first return value...
[civicrm-core.git] / tests / phpunit / CRM / Utils / TokenTest.php
CommitLineData
5973d2e1 1<?php
2
3/**
4 * Class CRM_Utils_TokenTest
5 * @group headless
6 */
7class CRM_Utils_TokenTest extends CiviUnitTestCase {
8
9 /**
10 * Basic test on getTokenDetails function.
11 */
12 public function testGetTokenDetails() {
9099cab3 13 $contactID = $this->individualCreate(['preferred_communication_method' => ['Phone', 'Fax']]);
cb47d053 14 [$resolvedTokens] = CRM_Utils_Token::getTokenDetails([$contactID]);
15 $this->assertEquals('Phone, Fax', $resolvedTokens[$contactID]['preferred_communication_method']);
5973d2e1 16 }
17
663cc0b4 18 /**
0606198b 19 * Test getting contacts w/o primary location type
663cc0b4 20 *
0606198b 21 * Check for situation described in CRM-19876.
663cc0b4 22 */
0606198b 23 public function testSearchByPrimaryLocation() {
24 // Disable searchPrimaryDetailsOnly civi settings so we could test the functionality without it.
25 Civi::settings()->set('searchPrimaryDetailsOnly', '0');
26
27 // create a contact with multiple email address and among which one is primary
28 $contactID = $this->individualCreate();
29 $primaryEmail = uniqid() . '@primary.com';
9099cab3 30 $this->callAPISuccess('Email', 'create', [
0606198b 31 'contact_id' => $contactID,
32 'email' => $primaryEmail,
33 'location_type_id' => 'Other',
34 'is_primary' => 1,
9099cab3
CW
35 ]);
36 $this->callAPISuccess('Email', 'create', [
0606198b 37 'contact_id' => $contactID,
38 'email' => uniqid() . '@galaxy.com',
39 'location_type_id' => 'Work',
40 'is_primary' => 0,
9099cab3
CW
41 ]);
42 $this->callAPISuccess('Email', 'create', [
0606198b 43 'contact_id' => $contactID,
44 'email' => uniqid() . '@galaxy.com',
45 'location_type_id' => 'Work',
46 'is_primary' => 0,
9099cab3 47 ]);
0606198b 48
9099cab3 49 $contactIDs = [$contactID];
0606198b 50
51 // when we are fetching contact details ON basis of primary address fields
cb47d053 52 [$contactDetails] = CRM_Utils_Token::getTokenDetails($contactIDs);
53 $this->assertEquals($primaryEmail, $contactDetails[$contactID]['email']);
0606198b 54
55 // restore setting
56 Civi::settings()->set('searchPrimaryDetailsOnly', '1');
663cc0b4
J
57 }
58
02a459c5 59 /**
60 * Test for replaceGreetingTokens.
61 *
62 */
63 public function testReplaceGreetingTokens() {
64 $tokenString = 'First Name: {contact.first_name} Last Name: {contact.last_name} Birth Date: {contact.birth_date} Prefix: {contact.prefix_id} Suffix: {contact.individual_suffix}';
9099cab3
CW
65 $contactDetails = [
66 [
67 2811 => [
02a459c5 68 'id' => '2811',
69 'contact_type' => 'Individual',
70 'first_name' => 'Morticia',
71 'last_name' => 'Addams',
72 'prefix_id' => 2,
9099cab3
CW
73 ],
74 ],
75 ];
02a459c5 76 $contactId = 2811;
77 $className = 'CRM_Contact_BAO_Contact';
78 $escapeSmarty = TRUE;
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: ');
641bb717
J
81
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 ');
02a459c5 86 }
87
0b3cb19d 88 /**
89 * Test getting multiple contacts.
90 *
91 * Check for situation described in CRM-19876.
92 */
93 public function testGetTokenDetailsMultipleEmails() {
94 $i = 0;
95
9099cab3 96 $params = [
0b3cb19d 97 'do_not_phone' => 1,
98 'do_not_email' => 0,
99 'do_not_mail' => 1,
100 'do_not_sms' => 1,
101 'do_not_trade' => 1,
102 'is_opt_out' => 0,
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',
109 'gender_id' => '1',
110 'birth_date' => '2017-01-01',
111 // 'city' => 'Metropolis',
9099cab3
CW
112 ];
113 $contactIDs = [];
0b3cb19d 114 while ($i < 27) {
115 $contactIDs[] = $contactID = $this->individualCreate($params);
9099cab3 116 $this->callAPISuccess('Email', 'create', [
0b3cb19d 117 'contact_id' => $contactID,
118 'email' => 'goodguy@galaxy.com',
119 'location_type_id' => 'Other',
120 'is_primary' => 0,
9099cab3
CW
121 ]);
122 $this->callAPISuccess('Email', 'create', [
0b3cb19d 123 'contact_id' => $contactID,
124 'email' => 'villain@galaxy.com',
125 'location_type_id' => 'Work',
126 'is_primary' => 1,
9099cab3 127 ]);
0b3cb19d 128 $i++;
129 }
130 unset($params['email']);
131
132 $resolvedTokens = CRM_Utils_Token::getTokenDetails($contactIDs);
133 foreach ($contactIDs as $contactID) {
134 $resolvedContactTokens = $resolvedTokens[0][$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']);
143
144 foreach ($params as $key => $value) {
145 $this->assertEquals($value, $resolvedContactTokens[$key]);
146 }
147 }
148 }
149
30a231df
MW
150 /**
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.
155 *
156 * @throws \CRM_Core_Exception
157 */
158 public function testTokenProcessor() {
159 $params['contact_id'] = $this->individualCreate();
160
161 // Prepare the processor and general context.
162 $tokenProc = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), [
163 // Unique(ish) identifier for our controller/use-case.
164 'controller' => 'civicrm_tokentest',
165
166 // Provide hints about what data will be available for each row.
167 // Ex: 'schema' => ['contactId', 'activityId', 'caseId'],
168 'schema' => ['contactId'],
169
170 // Whether to enable Smarty evaluation.
171 'smarty' => (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY),
172 ]);
173
174 // Define message templates.
175 $tokenProc->addMessage('body_html', 'Good morning, <p>{contact.email_greeting} {contact.display_name}</p>. {custom.foobar} Bye!', 'text/html');
176 $tokenProc->addMessage('body_text', 'Good morning, {contact.email_greeting} {contact.display_name} Bye!', 'text/plain');
177
178 $expect[$params['contact_id']]['html'] = 'Good morning, <p>Dear Anthony Mr. Anthony Anderson II</p>. Bye!';
179 $expect[$params['contact_id']]['text'] = 'Good morning, Dear Anthony Mr. Anthony Anderson II Bye!';
180
181 // Define row data.
182 foreach (explode(',', $params['contact_id']) as $contactId) {
183 $context = ['contactId' => $contactId];
184 $tokenProc->addRow()->context($context);
185 }
186
187 $tokenProc->evaluate();
188
96e792ad 189 $this->assertNotEmpty($tokenProc->getRows());
30a231df
MW
190 foreach ($tokenProc->getRows() as $tokenRow) {
191 /** @var \Civi\Token\TokenRow $tokenRow */
192 $html = $tokenRow->render('body_html');
193 $text = $tokenRow->render('body_text');
194 $this->assertEquals($expect[$params['contact_id']]['html'], $html);
195 $this->assertEquals($expect[$params['contact_id']]['text'], $text);
196 }
197 }
198
5973d2e1 199}