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