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