Merge pull request #21374 from ufundo/event-custom-tokens
[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)
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 79 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
49f03de9
EM
80 $this->ids['Contact']['individual_' . $seq] = $this->_contactCreate($params);
81 return $this->ids['Contact']['individual_' . $seq];
a23e13eb
TO
82 }
83
84 /**
85 * Generic function to create Household, to be used in test cases
86 *
87 * @param array $params
88 * parameters for civicrm_contact_add api function call
89 * @param int $seq
90 * sequence number if creating multiple households
91 *
92 * @return int
93 * id of Household created
c46c033c 94 *
e15ec147 95 * @throws \CRM_Core_Exception
a23e13eb 96 */
c46c033c 97 public function householdCreate($params = [], $seq = 0) {
a23e13eb
TO
98 $params = array_merge($this->sampleContact('Household', $seq), $params);
99 return $this->_contactCreate($params);
100 }
101
102 /**
103 * Helper function for getting sample contact properties.
104 *
105 * @param string $contact_type
106 * enum contact type: Individual, Organization
107 * @param int $seq
108 * sequence number for the values of this type
109 * @param bool $random
110 *
111 * @return array
112 * properties of sample contact (ie. $params for API call)
113 */
114 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
c46c033c 115 $samples = [
116 'Individual' => [
a23e13eb 117 // The number of values in each list need to be coprime numbers to not have duplicates
c46c033c 118 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
119 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
120 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
121 ],
122 'Organization' => [
123 'organization_name' => [
a23e13eb
TO
124 'Unit Test Organization',
125 'Acme',
126 'Roberts and Sons',
127 'Cryo Space Labs',
128 'Sharper Pens',
c46c033c 129 ],
130 ],
131 'Household' => [
132 'household_name' => ['Unit Test household'],
133 ],
134 ];
135 $params = ['contact_type' => $contact_type];
a23e13eb
TO
136 foreach ($samples[$contact_type] as $key => $values) {
137 $params[$key] = $values[$seq % count($values)];
138 if ($random) {
139 $params[$key] .= substr(sha1(rand()), 0, 5);
140 }
141 }
142 if ($contact_type == 'Individual') {
143 $params['email'] = strtolower(
144 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
145 );
146 $params['prefix_id'] = 3;
147 $params['suffix_id'] = 3;
148 }
149 return $params;
150 }
151
152 /**
153 * Private helper function for calling civicrm_contact_add.
154 *
155 * @param array $params
156 * For civicrm_contact_add api function call.
157 *
a23e13eb 158 * @return int
4132c927 159 * id of contact created
a23e13eb 160 */
4d86769f
EM
161 private function _contactCreate(array $params): int {
162 $version = $this->_apiversion;
163 $this->_apiversion = 3;
164 $result = $this->callAPISuccess('contact', 'create', $params);
165 $this->_apiversion = $version;
4132c927 166 return (int) $result['id'];
a23e13eb
TO
167 }
168
169 /**
170 * Delete contact, ensuring it is not the domain contact
171 *
172 * @param int $contactID
173 * Contact ID to delete
174 */
175 public function contactDelete($contactID) {
176 $domain = new \CRM_Core_BAO_Domain();
177 $domain->contact_id = $contactID;
178 if (!$domain->find(TRUE)) {
c46c033c 179 $this->callAPISuccess('contact', 'delete', [
a23e13eb
TO
180 'id' => $contactID,
181 'skip_undelete' => 1,
c46c033c 182 ]);
a23e13eb
TO
183 }
184 }
185
186 /**
187 * Add a Group.
188 *
189 * @param array $params
c46c033c 190 *
a23e13eb
TO
191 * @return int
192 * groupId of created group
193 */
c46c033c 194 public function groupCreate($params = []) {
195 $params = array_merge([
a23e13eb
TO
196 'name' => 'Test Group 1',
197 'domain_id' => 1,
198 'title' => 'New Test Group Created',
199 'description' => 'New Test Group Created',
200 'is_active' => 1,
201 'visibility' => 'Public Pages',
c46c033c 202 'group_type' => [
a23e13eb
TO
203 '1' => 1,
204 '2' => 1,
c46c033c 205 ],
206 ], $params);
a23e13eb
TO
207
208 $result = $this->callAPISuccess('Group', 'create', $params);
209 return $result['id'];
210 }
211
212 /**
213 * Delete a Group.
214 *
215 * @param int $gid
216 */
217 public function groupDelete($gid) {
c46c033c 218 $params = [
a23e13eb 219 'id' => $gid,
c46c033c 220 ];
a23e13eb
TO
221
222 $this->callAPISuccess('Group', 'delete', $params);
223 }
224
225 /**
226 * Function to add a Group.
227 *
228 * @params array to add group
229 *
230 * @param int $groupID
231 * @param int $totalCount
232 * @param bool $random
c46c033c 233 *
a23e13eb
TO
234 * @return int
235 * groupId of created group
236 */
237 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
c46c033c 238 $params = ['group_id' => $groupID];
a23e13eb 239 for ($i = 1; $i <= $totalCount; $i++) {
c46c033c 240 $contactID = $this->individualCreate([], 0, $random);
a23e13eb 241 if ($i == 1) {
c46c033c 242 $params += ['contact_id' => $contactID];
a23e13eb
TO
243 }
244 else {
c46c033c 245 $params += ["contact_id.$i" => $contactID];
a23e13eb
TO
246 }
247 }
248 $result = $this->callAPISuccess('GroupContact', 'create', $params);
249
250 return $result;
251 }
252
253}