Merge pull request #20918 from eileenmcnaughton/cust2
[civicrm-core.git] / Civi / Test / ContactTestTrait.php
1 <?php
2
3 namespace Civi\Test;
4
5 /**
6 * Class ContactTestTrait
7 *
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
18 /**
19 * Emulate a logged in user since certain functions use that.
20 * value to store a record in the DB (like activity)
21 *
22 * @see https://issues.civicrm.org/jira/browse/CRM-8180
23 *
24 * @return int
25 * Contact ID of the created user.
26 */
27 public function createLoggedInUser(): int {
28 $params = [
29 'first_name' => 'Logged In',
30 'last_name' => 'User ' . rand(),
31 'contact_type' => 'Individual',
32 'domain_id' => \CRM_Core_Config::domainID(),
33 ];
34 $contactID = $this->individualCreate($params);
35 $this->callAPISuccess('UFMatch', 'get', ['uf_id' => 6, 'api.UFMatch.delete' => []]);
36 $this->callAPISuccess('UFMatch', 'create', [
37 'contact_id' => $contactID,
38 'uf_name' => 'superman',
39 'uf_id' => 6,
40 ]);
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 *
58 * @throws \CiviCRM_API3_Exception
59 */
60 public function organizationCreate($params = [], $seq = 0): int {
61 if (!$params) {
62 $params = [];
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 */
80 public function individualCreate(array $params = [], $seq = 0, $random = FALSE): int {
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
95 *
96 * @throws \CRM_Core_Exception
97 */
98 public function householdCreate($params = [], $seq = 0) {
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) {
116 $samples = [
117 'Individual' => [
118 // The number of values in each list need to be coprime numbers to not have duplicates
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' => [
125 'Unit Test Organization',
126 'Acme',
127 'Roberts and Sons',
128 'Cryo Space Labs',
129 'Sharper Pens',
130 ],
131 ],
132 'Household' => [
133 'household_name' => ['Unit Test household'],
134 ],
135 ];
136 $params = ['contact_type' => $contact_type];
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 *
159 * @return int
160 * id of contact created
161 */
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;
167 return (int) $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)) {
180 $this->callAPISuccess('contact', 'delete', [
181 'id' => $contactID,
182 'skip_undelete' => 1,
183 ]);
184 }
185 }
186
187 /**
188 * Add a Group.
189 *
190 * @param array $params
191 *
192 * @return int
193 * groupId of created group
194 */
195 public function groupCreate($params = []) {
196 $params = array_merge([
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',
203 'group_type' => [
204 '1' => 1,
205 '2' => 1,
206 ],
207 ], $params);
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) {
219 $params = [
220 'id' => $gid,
221 ];
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
234 *
235 * @return int
236 * groupId of created group
237 */
238 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
239 $params = ['group_id' => $groupID];
240 for ($i = 1; $i <= $totalCount; $i++) {
241 $contactID = $this->individualCreate([], 0, $random);
242 if ($i == 1) {
243 $params += ['contact_id' => $contactID];
244 }
245 else {
246 $params += ["contact_id.$i" => $contactID];
247 }
248 }
249 $result = $this->callAPISuccess('GroupContact', 'create', $params);
250
251 return $result;
252 }
253
254 }