Merge pull request #19422 from mattwire/deprecatedcaseactivity
[civicrm-core.git] / Civi / Test / ContactTestTrait.php
CommitLineData
a23e13eb
TO
1<?php
2
3namespace 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 */
16trait 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)
0e480632 21 * @see https://issues.civicrm.org/jira/browse/CRM-8180
a23e13eb
TO
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);
3ed232a5 34 $this->callAPISuccess('UFMatch', 'get', ['uf_id' => 6, 'api.UFMatch.delete' => []]);
c46c033c 35 $this->callAPISuccess('UFMatch', 'create', [
a23e13eb
TO
36 'contact_id' => $contactID,
37 'uf_name' => 'superman',
38 'uf_id' => 6,
c46c033c 39 ]);
a23e13eb
TO
40
41 $session = \CRM_Core_Session::singleton();
42 $session->set('userID', $contactID);
43 return $contactID;
44 }
45
46 /**
47 * Generic function to create Organisation, to be used in test cases
48 *
49 * @param array $params
50 * parameters for civicrm_contact_add api function call
51 * @param int $seq
52 * sequence number if creating multiple organizations
53 *
54 * @return int
55 * id of Organisation created
e0fa7133 56 *
57 * @throws \CiviCRM_API3_Exception
a23e13eb 58 */
e0fa7133 59 public function organizationCreate($params = [], $seq = 0): int {
a23e13eb 60 if (!$params) {
c46c033c 61 $params = [];
a23e13eb
TO
62 }
63 $params = array_merge($this->sampleContact('Organization', $seq), $params);
64 return $this->_contactCreate($params);
65 }
66
67 /**
68 * Generic function to create Individual, to be used in test cases
69 *
70 * @param array $params
71 * parameters for civicrm_contact_add api function call
72 * @param int $seq
73 * sequence number if creating multiple individuals
74 * @param bool $random
75 *
76 * @return int
77 * id of Individual created
023f9e8a 78 *
2bd8b028 79 * @throws \CiviCRM_API3_Exception
a23e13eb 80 */
e0fa7133 81 public function individualCreate($params = [], $seq = 0, $random = FALSE): int {
a23e13eb
TO
82 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
83 return $this->_contactCreate($params);
84 }
85
86 /**
87 * Generic function to create Household, to be used in test cases
88 *
89 * @param array $params
90 * parameters for civicrm_contact_add api function call
91 * @param int $seq
92 * sequence number if creating multiple households
93 *
94 * @return int
95 * id of Household created
c46c033c 96 *
e15ec147 97 * @throws \CRM_Core_Exception
a23e13eb 98 */
c46c033c 99 public function householdCreate($params = [], $seq = 0) {
a23e13eb
TO
100 $params = array_merge($this->sampleContact('Household', $seq), $params);
101 return $this->_contactCreate($params);
102 }
103
104 /**
105 * Helper function for getting sample contact properties.
106 *
107 * @param string $contact_type
108 * enum contact type: Individual, Organization
109 * @param int $seq
110 * sequence number for the values of this type
111 * @param bool $random
112 *
113 * @return array
114 * properties of sample contact (ie. $params for API call)
115 */
116 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
c46c033c 117 $samples = [
118 'Individual' => [
a23e13eb 119 // The number of values in each list need to be coprime numbers to not have duplicates
c46c033c 120 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
121 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
122 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
123 ],
124 'Organization' => [
125 'organization_name' => [
a23e13eb
TO
126 'Unit Test Organization',
127 'Acme',
128 'Roberts and Sons',
129 'Cryo Space Labs',
130 'Sharper Pens',
c46c033c 131 ],
132 ],
133 'Household' => [
134 'household_name' => ['Unit Test household'],
135 ],
136 ];
137 $params = ['contact_type' => $contact_type];
a23e13eb
TO
138 foreach ($samples[$contact_type] as $key => $values) {
139 $params[$key] = $values[$seq % count($values)];
140 if ($random) {
141 $params[$key] .= substr(sha1(rand()), 0, 5);
142 }
143 }
144 if ($contact_type == 'Individual') {
145 $params['email'] = strtolower(
146 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
147 );
148 $params['prefix_id'] = 3;
149 $params['suffix_id'] = 3;
150 }
151 return $params;
152 }
153
154 /**
155 * Private helper function for calling civicrm_contact_add.
156 *
157 * @param array $params
158 * For civicrm_contact_add api function call.
159 *
a23e13eb 160 * @return int
4132c927 161 * id of contact created
c46c033c 162 *
4132c927 163 * @throws \CiviCRM_API3_Exception
a23e13eb 164 */
e0fa7133 165 private function _contactCreate($params): int {
3493febb 166 $result = civicrm_api3('contact', 'create', $params);
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}