Commit | Line | Data |
---|---|---|
a23e13eb TO |
1 | <?php |
2 | ||
3 | namespace Civi\Test; | |
4 | ||
5 | /** | |
6 | * Class ContactTestTrait | |
c46c033c | 7 | * |
a23e13eb TO |
8 | * @package Civi\Test |
9 | * | |
10 | * This trait defines a number of helper functions for managing | |
11 | * test contacts. Generally, it depends on having access to the | |
12 | * API test functions ($this->callAPISuccess()) and to the | |
13 | * standard PHPUnit assertions ($this->assertEquals). It should | |
14 | * not impose any other requirements for the downstream consumer class. | |
15 | */ | |
16 | trait ContactTestTrait { | |
17 | ||
a23e13eb TO |
18 | /** |
19 | * Emulate a logged in user since certain functions use that. | |
20 | * value to store a record in the DB (like activity) | |
21 | * CRM-8180 | |
22 | * | |
23 | * @return int | |
24 | * Contact ID of the created user. | |
25 | */ | |
26 | public function createLoggedInUser() { | |
c46c033c | 27 | $params = [ |
a23e13eb TO |
28 | 'first_name' => 'Logged In', |
29 | 'last_name' => 'User ' . rand(), | |
30 | 'contact_type' => 'Individual', | |
4fcfa5dd | 31 | 'domain_id' => \CRM_Core_Config::domainID(), |
c46c033c | 32 | ]; |
a23e13eb | 33 | $contactID = $this->individualCreate($params); |
3ed232a5 | 34 | $this->callAPISuccess('UFMatch', 'get', ['uf_id' => 6, 'api.UFMatch.delete' => []]); |
c46c033c | 35 | $this->callAPISuccess('UFMatch', 'create', [ |
a23e13eb TO |
36 | 'contact_id' => $contactID, |
37 | 'uf_name' => 'superman', | |
38 | 'uf_id' => 6, | |
c46c033c | 39 | ]); |
a23e13eb TO |
40 | |
41 | $session = \CRM_Core_Session::singleton(); | |
42 | $session->set('userID', $contactID); | |
43 | return $contactID; | |
44 | } | |
45 | ||
46 | /** | |
47 | * Generic function to create Organisation, to be used in test cases | |
48 | * | |
49 | * @param array $params | |
50 | * parameters for civicrm_contact_add api function call | |
51 | * @param int $seq | |
52 | * sequence number if creating multiple organizations | |
53 | * | |
54 | * @return int | |
55 | * id of Organisation created | |
56 | */ | |
c46c033c | 57 | public function organizationCreate($params = [], $seq = 0) { |
a23e13eb | 58 | if (!$params) { |
c46c033c | 59 | $params = []; |
a23e13eb TO |
60 | } |
61 | $params = array_merge($this->sampleContact('Organization', $seq), $params); | |
62 | return $this->_contactCreate($params); | |
63 | } | |
64 | ||
65 | /** | |
66 | * Generic function to create Individual, to be used in test cases | |
67 | * | |
68 | * @param array $params | |
69 | * parameters for civicrm_contact_add api function call | |
70 | * @param int $seq | |
71 | * sequence number if creating multiple individuals | |
72 | * @param bool $random | |
73 | * | |
74 | * @return int | |
75 | * id of Individual created | |
023f9e8a | 76 | * |
77 | * @throws \CRM_Core_Exception | |
a23e13eb | 78 | */ |
c46c033c | 79 | public function individualCreate($params = [], $seq = 0, $random = FALSE) { |
a23e13eb TO |
80 | $params = array_merge($this->sampleContact('Individual', $seq, $random), $params); |
81 | return $this->_contactCreate($params); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Generic function to create Household, to be used in test cases | |
86 | * | |
87 | * @param array $params | |
88 | * parameters for civicrm_contact_add api function call | |
89 | * @param int $seq | |
90 | * sequence number if creating multiple households | |
91 | * | |
92 | * @return int | |
93 | * id of Household created | |
c46c033c | 94 | * |
e15ec147 | 95 | * @throws \CRM_Core_Exception |
a23e13eb | 96 | */ |
c46c033c | 97 | public function householdCreate($params = [], $seq = 0) { |
a23e13eb TO |
98 | $params = array_merge($this->sampleContact('Household', $seq), $params); |
99 | return $this->_contactCreate($params); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Helper function for getting sample contact properties. | |
104 | * | |
105 | * @param string $contact_type | |
106 | * enum contact type: Individual, Organization | |
107 | * @param int $seq | |
108 | * sequence number for the values of this type | |
109 | * @param bool $random | |
110 | * | |
111 | * @return array | |
112 | * properties of sample contact (ie. $params for API call) | |
113 | */ | |
114 | public function sampleContact($contact_type, $seq = 0, $random = FALSE) { | |
c46c033c | 115 | $samples = [ |
116 | 'Individual' => [ | |
a23e13eb | 117 | // The number of values in each list need to be coprime numbers to not have duplicates |
c46c033c | 118 | 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'], |
119 | 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'], | |
120 | 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'], | |
121 | ], | |
122 | 'Organization' => [ | |
123 | 'organization_name' => [ | |
a23e13eb TO |
124 | 'Unit Test Organization', |
125 | 'Acme', | |
126 | 'Roberts and Sons', | |
127 | 'Cryo Space Labs', | |
128 | 'Sharper Pens', | |
c46c033c | 129 | ], |
130 | ], | |
131 | 'Household' => [ | |
132 | 'household_name' => ['Unit Test household'], | |
133 | ], | |
134 | ]; | |
135 | $params = ['contact_type' => $contact_type]; | |
a23e13eb TO |
136 | foreach ($samples[$contact_type] as $key => $values) { |
137 | $params[$key] = $values[$seq % count($values)]; | |
138 | if ($random) { | |
139 | $params[$key] .= substr(sha1(rand()), 0, 5); | |
140 | } | |
141 | } | |
142 | if ($contact_type == 'Individual') { | |
143 | $params['email'] = strtolower( | |
144 | $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org' | |
145 | ); | |
146 | $params['prefix_id'] = 3; | |
147 | $params['suffix_id'] = 3; | |
148 | } | |
149 | return $params; | |
150 | } | |
151 | ||
152 | /** | |
153 | * Private helper function for calling civicrm_contact_add. | |
154 | * | |
155 | * @param array $params | |
156 | * For civicrm_contact_add api function call. | |
157 | * | |
a23e13eb TO |
158 | * @return int |
159 | * id of Household created | |
c46c033c | 160 | * @throws \CRM_Core_Exception |
161 | * | |
a23e13eb TO |
162 | */ |
163 | private function _contactCreate($params) { | |
3493febb | 164 | $result = civicrm_api3('contact', 'create', $params); |
a23e13eb | 165 | if (!empty($result['is_error']) || empty($result['id'])) { |
023f9e8a | 166 | throw new \CRM_Core_Exception('Could not create test contact, with message: ' . \CRM_Utils_Array::value('error_message', $result) . "\nBacktrace:" . \CRM_Utils_Array::value('trace', $result)); |
a23e13eb TO |
167 | } |
168 | return $result['id']; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Delete contact, ensuring it is not the domain contact | |
173 | * | |
174 | * @param int $contactID | |
175 | * Contact ID to delete | |
176 | */ | |
177 | public function contactDelete($contactID) { | |
178 | $domain = new \CRM_Core_BAO_Domain(); | |
179 | $domain->contact_id = $contactID; | |
180 | if (!$domain->find(TRUE)) { | |
c46c033c | 181 | $this->callAPISuccess('contact', 'delete', [ |
a23e13eb TO |
182 | 'id' => $contactID, |
183 | 'skip_undelete' => 1, | |
c46c033c | 184 | ]); |
a23e13eb TO |
185 | } |
186 | } | |
187 | ||
188 | /** | |
189 | * Add a Group. | |
190 | * | |
191 | * @param array $params | |
c46c033c | 192 | * |
a23e13eb TO |
193 | * @return int |
194 | * groupId of created group | |
195 | */ | |
c46c033c | 196 | public function groupCreate($params = []) { |
197 | $params = array_merge([ | |
a23e13eb TO |
198 | 'name' => 'Test Group 1', |
199 | 'domain_id' => 1, | |
200 | 'title' => 'New Test Group Created', | |
201 | 'description' => 'New Test Group Created', | |
202 | 'is_active' => 1, | |
203 | 'visibility' => 'Public Pages', | |
c46c033c | 204 | 'group_type' => [ |
a23e13eb TO |
205 | '1' => 1, |
206 | '2' => 1, | |
c46c033c | 207 | ], |
208 | ], $params); | |
a23e13eb TO |
209 | |
210 | $result = $this->callAPISuccess('Group', 'create', $params); | |
211 | return $result['id']; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Delete a Group. | |
216 | * | |
217 | * @param int $gid | |
218 | */ | |
219 | public function groupDelete($gid) { | |
c46c033c | 220 | $params = [ |
a23e13eb | 221 | 'id' => $gid, |
c46c033c | 222 | ]; |
a23e13eb TO |
223 | |
224 | $this->callAPISuccess('Group', 'delete', $params); | |
225 | } | |
226 | ||
227 | /** | |
228 | * Function to add a Group. | |
229 | * | |
230 | * @params array to add group | |
231 | * | |
232 | * @param int $groupID | |
233 | * @param int $totalCount | |
234 | * @param bool $random | |
c46c033c | 235 | * |
a23e13eb TO |
236 | * @return int |
237 | * groupId of created group | |
238 | */ | |
239 | public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) { | |
c46c033c | 240 | $params = ['group_id' => $groupID]; |
a23e13eb | 241 | for ($i = 1; $i <= $totalCount; $i++) { |
c46c033c | 242 | $contactID = $this->individualCreate([], 0, $random); |
a23e13eb | 243 | if ($i == 1) { |
c46c033c | 244 | $params += ['contact_id' => $contactID]; |
a23e13eb TO |
245 | } |
246 | else { | |
c46c033c | 247 | $params += ["contact_id.$i" => $contactID]; |
a23e13eb TO |
248 | } |
249 | } | |
250 | $result = $this->callAPISuccess('GroupContact', 'create', $params); | |
251 | ||
252 | return $result; | |
253 | } | |
254 | ||
255 | } |