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. | |
26 | */ | |
9db37f9f | 27 | public function createLoggedInUser(): int { |
c46c033c | 28 | $params = [ |
a23e13eb TO |
29 | 'first_name' => 'Logged In', |
30 | 'last_name' => 'User ' . rand(), | |
31 | 'contact_type' => 'Individual', | |
4fcfa5dd | 32 | 'domain_id' => \CRM_Core_Config::domainID(), |
c46c033c | 33 | ]; |
a23e13eb | 34 | $contactID = $this->individualCreate($params); |
3ed232a5 | 35 | $this->callAPISuccess('UFMatch', 'get', ['uf_id' => 6, 'api.UFMatch.delete' => []]); |
c46c033c | 36 | $this->callAPISuccess('UFMatch', 'create', [ |
a23e13eb TO |
37 | 'contact_id' => $contactID, |
38 | 'uf_name' => 'superman', | |
39 | 'uf_id' => 6, | |
c46c033c | 40 | ]); |
a23e13eb TO |
41 | |
42 | $session = \CRM_Core_Session::singleton(); | |
43 | $session->set('userID', $contactID); | |
44 | return $contactID; | |
45 | } | |
46 | ||
47 | /** | |
48 | * Generic function to create Organisation, to be used in test cases | |
49 | * | |
50 | * @param array $params | |
51 | * parameters for civicrm_contact_add api function call | |
52 | * @param int $seq | |
53 | * sequence number if creating multiple organizations | |
54 | * | |
55 | * @return int | |
56 | * id of Organisation created | |
57 | */ | |
e0fa7133 | 58 | public function organizationCreate($params = [], $seq = 0): int { |
a23e13eb | 59 | if (!$params) { |
c46c033c | 60 | $params = []; |
a23e13eb TO |
61 | } |
62 | $params = array_merge($this->sampleContact('Organization', $seq), $params); | |
63 | return $this->_contactCreate($params); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Generic function to create Individual, to be used in test cases | |
68 | * | |
69 | * @param array $params | |
70 | * parameters for civicrm_contact_add api function call | |
71 | * @param int $seq | |
72 | * sequence number if creating multiple individuals | |
73 | * @param bool $random | |
74 | * | |
75 | * @return int | |
76 | * id of Individual created | |
77 | */ | |
c7ca9aeb | 78 | public function individualCreate(array $params = [], $seq = 0, $random = FALSE): int { |
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 | 157 | * @return int |
4132c927 | 158 | * id of contact created |
a23e13eb | 159 | */ |
4d86769f EM |
160 | private function _contactCreate(array $params): int { |
161 | $version = $this->_apiversion; | |
162 | $this->_apiversion = 3; | |
163 | $result = $this->callAPISuccess('contact', 'create', $params); | |
164 | $this->_apiversion = $version; | |
4132c927 | 165 | return (int) $result['id']; |
a23e13eb TO |
166 | } |
167 | ||
168 | /** | |
169 | * Delete contact, ensuring it is not the domain contact | |
170 | * | |
171 | * @param int $contactID | |
172 | * Contact ID to delete | |
173 | */ | |
174 | public function contactDelete($contactID) { | |
175 | $domain = new \CRM_Core_BAO_Domain(); | |
176 | $domain->contact_id = $contactID; | |
177 | if (!$domain->find(TRUE)) { | |
c46c033c | 178 | $this->callAPISuccess('contact', 'delete', [ |
a23e13eb TO |
179 | 'id' => $contactID, |
180 | 'skip_undelete' => 1, | |
c46c033c | 181 | ]); |
a23e13eb TO |
182 | } |
183 | } | |
184 | ||
185 | /** | |
186 | * Add a Group. | |
187 | * | |
188 | * @param array $params | |
c46c033c | 189 | * |
a23e13eb TO |
190 | * @return int |
191 | * groupId of created group | |
192 | */ | |
c46c033c | 193 | public function groupCreate($params = []) { |
194 | $params = array_merge([ | |
a23e13eb TO |
195 | 'name' => 'Test Group 1', |
196 | 'domain_id' => 1, | |
197 | 'title' => 'New Test Group Created', | |
198 | 'description' => 'New Test Group Created', | |
199 | 'is_active' => 1, | |
200 | 'visibility' => 'Public Pages', | |
c46c033c | 201 | 'group_type' => [ |
a23e13eb TO |
202 | '1' => 1, |
203 | '2' => 1, | |
c46c033c | 204 | ], |
205 | ], $params); | |
a23e13eb TO |
206 | |
207 | $result = $this->callAPISuccess('Group', 'create', $params); | |
208 | return $result['id']; | |
209 | } | |
210 | ||
211 | /** | |
212 | * Delete a Group. | |
213 | * | |
214 | * @param int $gid | |
215 | */ | |
216 | public function groupDelete($gid) { | |
c46c033c | 217 | $params = [ |
a23e13eb | 218 | 'id' => $gid, |
c46c033c | 219 | ]; |
a23e13eb TO |
220 | |
221 | $this->callAPISuccess('Group', 'delete', $params); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Function to add a Group. | |
226 | * | |
227 | * @params array to add group | |
228 | * | |
229 | * @param int $groupID | |
230 | * @param int $totalCount | |
231 | * @param bool $random | |
c46c033c | 232 | * |
a23e13eb TO |
233 | * @return int |
234 | * groupId of created group | |
235 | */ | |
236 | public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) { | |
c46c033c | 237 | $params = ['group_id' => $groupID]; |
a23e13eb | 238 | for ($i = 1; $i <= $totalCount; $i++) { |
c46c033c | 239 | $contactID = $this->individualCreate([], 0, $random); |
a23e13eb | 240 | if ($i == 1) { |
c46c033c | 241 | $params += ['contact_id' => $contactID]; |
a23e13eb TO |
242 | } |
243 | else { | |
c46c033c | 244 | $params += ["contact_id.$i" => $contactID]; |
a23e13eb TO |
245 | } |
246 | } | |
247 | $result = $this->callAPISuccess('GroupContact', 'create', $params); | |
248 | ||
249 | return $result; | |
250 | } | |
251 | ||
252 | } |