Commit | Line | Data |
---|---|---|
5973d2e1 | 1 | <?php |
2 | ||
3 | /** | |
4 | * Class CRM_Utils_TokenTest | |
5 | * @group headless | |
6 | */ | |
7 | class CRM_Utils_TokenTest extends CiviUnitTestCase { | |
8 | ||
9 | /** | |
10 | * Basic test on getTokenDetails function. | |
11 | */ | |
12 | public function testGetTokenDetails() { | |
13 | $contactID = $this->individualCreate(array('preferred_communication_method' => array('Phone', 'Fax'))); | |
14 | $resolvedTokens = CRM_Utils_Token::getTokenDetails(array($contactID)); | |
15 | $this->assertEquals('Phone, Fax', $resolvedTokens[0][$contactID]['preferred_communication_method']); | |
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'; | |
30 | $this->callAPISuccess('Email', 'create', array( | |
31 | 'contact_id' => $contactID, | |
32 | 'email' => $primaryEmail, | |
33 | 'location_type_id' => 'Other', | |
34 | 'is_primary' => 1, | |
35 | )); | |
36 | $this->callAPISuccess('Email', 'create', array( | |
37 | 'contact_id' => $contactID, | |
38 | 'email' => uniqid() . '@galaxy.com', | |
39 | 'location_type_id' => 'Work', | |
40 | 'is_primary' => 0, | |
41 | )); | |
42 | $this->callAPISuccess('Email', 'create', array( | |
43 | 'contact_id' => $contactID, | |
44 | 'email' => uniqid() . '@galaxy.com', | |
45 | 'location_type_id' => 'Work', | |
46 | 'is_primary' => 0, | |
47 | )); | |
48 | ||
49 | $contactIDs = array($contactID); | |
50 | ||
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[0][$contactID]['email']); | |
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}'; | |
65 | $contactDetails = array( | |
66 | array( | |
67 | 2811 => array( | |
68 | 'id' => '2811', | |
69 | 'contact_type' => 'Individual', | |
70 | 'first_name' => 'Morticia', | |
71 | 'last_name' => 'Addams', | |
72 | 'prefix_id' => 2, | |
73 | ), | |
74 | ), | |
75 | ); | |
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: '); | |
81 | } | |
82 | ||
0b3cb19d | 83 | /** |
84 | * Test getting multiple contacts. | |
85 | * | |
86 | * Check for situation described in CRM-19876. | |
87 | */ | |
88 | public function testGetTokenDetailsMultipleEmails() { | |
89 | $i = 0; | |
90 | ||
91 | $params = array( | |
92 | 'do_not_phone' => 1, | |
93 | 'do_not_email' => 0, | |
94 | 'do_not_mail' => 1, | |
95 | 'do_not_sms' => 1, | |
96 | 'do_not_trade' => 1, | |
97 | 'is_opt_out' => 0, | |
98 | 'email' => 'guardians@galaxy.com', | |
99 | 'legal_identifier' => 'convict 56', | |
100 | 'nick_name' => 'bob', | |
101 | 'contact_source' => 'bargain basement', | |
102 | 'formal_title' => 'Your silliness', | |
103 | 'job_title' => 'World Saviour', | |
104 | 'gender_id' => '1', | |
105 | 'birth_date' => '2017-01-01', | |
106 | // 'city' => 'Metropolis', | |
107 | ); | |
108 | $contactIDs = array(); | |
109 | while ($i < 27) { | |
110 | $contactIDs[] = $contactID = $this->individualCreate($params); | |
111 | $this->callAPISuccess('Email', 'create', array( | |
112 | 'contact_id' => $contactID, | |
113 | 'email' => 'goodguy@galaxy.com', | |
114 | 'location_type_id' => 'Other', | |
115 | 'is_primary' => 0, | |
116 | )); | |
117 | $this->callAPISuccess('Email', 'create', array( | |
118 | 'contact_id' => $contactID, | |
119 | 'email' => 'villain@galaxy.com', | |
120 | 'location_type_id' => 'Work', | |
121 | 'is_primary' => 1, | |
122 | )); | |
123 | $i++; | |
124 | } | |
125 | unset($params['email']); | |
126 | ||
127 | $resolvedTokens = CRM_Utils_Token::getTokenDetails($contactIDs); | |
128 | foreach ($contactIDs as $contactID) { | |
129 | $resolvedContactTokens = $resolvedTokens[0][$contactID]; | |
130 | $this->assertEquals('Individual', $resolvedContactTokens['contact_type']); | |
131 | $this->assertEquals('Anderson, Anthony', $resolvedContactTokens['sort_name']); | |
132 | $this->assertEquals('en_US', $resolvedContactTokens['preferred_language']); | |
133 | $this->assertEquals('Both', $resolvedContactTokens['preferred_mail_format']); | |
134 | $this->assertEquals(3, $resolvedContactTokens['prefix_id']); | |
135 | $this->assertEquals(3, $resolvedContactTokens['suffix_id']); | |
136 | $this->assertEquals('Mr. Anthony J. Anderson II', $resolvedContactTokens['addressee_display']); | |
137 | $this->assertEquals('villain@galaxy.com', $resolvedContactTokens['email']); | |
138 | ||
139 | foreach ($params as $key => $value) { | |
140 | $this->assertEquals($value, $resolvedContactTokens[$key]); | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
5973d2e1 | 145 | } |