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 | |
e0fa7133 | 57 | * |
58 | * @throws \CiviCRM_API3_Exception | |
a23e13eb | 59 | */ |
e0fa7133 | 60 | public function organizationCreate($params = [], $seq = 0): int { |
a23e13eb | 61 | if (!$params) { |
c46c033c | 62 | $params = []; |
a23e13eb TO |
63 | } |
64 | $params = array_merge($this->sampleContact('Organization', $seq), $params); | |
65 | return $this->_contactCreate($params); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Generic function to create Individual, to be used in test cases | |
70 | * | |
71 | * @param array $params | |
72 | * parameters for civicrm_contact_add api function call | |
73 | * @param int $seq | |
74 | * sequence number if creating multiple individuals | |
75 | * @param bool $random | |
76 | * | |
77 | * @return int | |
78 | * id of Individual created | |
79 | */ | |
c7ca9aeb | 80 | public function individualCreate(array $params = [], $seq = 0, $random = FALSE): int { |
a23e13eb TO |
81 | $params = array_merge($this->sampleContact('Individual', $seq, $random), $params); |
82 | return $this->_contactCreate($params); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Generic function to create Household, to be used in test cases | |
87 | * | |
88 | * @param array $params | |
89 | * parameters for civicrm_contact_add api function call | |
90 | * @param int $seq | |
91 | * sequence number if creating multiple households | |
92 | * | |
93 | * @return int | |
94 | * id of Household created | |
c46c033c | 95 | * |
e15ec147 | 96 | * @throws \CRM_Core_Exception |
a23e13eb | 97 | */ |
c46c033c | 98 | public function householdCreate($params = [], $seq = 0) { |
a23e13eb TO |
99 | $params = array_merge($this->sampleContact('Household', $seq), $params); |
100 | return $this->_contactCreate($params); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Helper function for getting sample contact properties. | |
105 | * | |
106 | * @param string $contact_type | |
107 | * enum contact type: Individual, Organization | |
108 | * @param int $seq | |
109 | * sequence number for the values of this type | |
110 | * @param bool $random | |
111 | * | |
112 | * @return array | |
113 | * properties of sample contact (ie. $params for API call) | |
114 | */ | |
115 | public function sampleContact($contact_type, $seq = 0, $random = FALSE) { | |
c46c033c | 116 | $samples = [ |
117 | 'Individual' => [ | |
a23e13eb | 118 | // The number of values in each list need to be coprime numbers to not have duplicates |
c46c033c | 119 | 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'], |
120 | 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'], | |
121 | 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'], | |
122 | ], | |
123 | 'Organization' => [ | |
124 | 'organization_name' => [ | |
a23e13eb TO |
125 | 'Unit Test Organization', |
126 | 'Acme', | |
127 | 'Roberts and Sons', | |
128 | 'Cryo Space Labs', | |
129 | 'Sharper Pens', | |
c46c033c | 130 | ], |
131 | ], | |
132 | 'Household' => [ | |
133 | 'household_name' => ['Unit Test household'], | |
134 | ], | |
135 | ]; | |
136 | $params = ['contact_type' => $contact_type]; | |
a23e13eb TO |
137 | foreach ($samples[$contact_type] as $key => $values) { |
138 | $params[$key] = $values[$seq % count($values)]; | |
139 | if ($random) { | |
140 | $params[$key] .= substr(sha1(rand()), 0, 5); | |
141 | } | |
142 | } | |
143 | if ($contact_type == 'Individual') { | |
144 | $params['email'] = strtolower( | |
145 | $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org' | |
146 | ); | |
147 | $params['prefix_id'] = 3; | |
148 | $params['suffix_id'] = 3; | |
149 | } | |
150 | return $params; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Private helper function for calling civicrm_contact_add. | |
155 | * | |
156 | * @param array $params | |
157 | * For civicrm_contact_add api function call. | |
158 | * | |
a23e13eb | 159 | * @return int |
4132c927 | 160 | * id of contact created |
a23e13eb | 161 | */ |
4d86769f EM |
162 | private function _contactCreate(array $params): int { |
163 | $version = $this->_apiversion; | |
164 | $this->_apiversion = 3; | |
165 | $result = $this->callAPISuccess('contact', 'create', $params); | |
166 | $this->_apiversion = $version; | |
4132c927 | 167 | return (int) $result['id']; |
a23e13eb TO |
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 | } |