Merge pull request #14667 from hoegrammer/master
[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 * CRM-8180
22 *
23 * @return int
24 * Contact ID of the created user.
25 */
26 public function createLoggedInUser() {
27 $params = [
28 'first_name' => 'Logged In',
29 'last_name' => 'User ' . rand(),
30 'contact_type' => 'Individual',
31 'domain_id' => \CRM_Core_Config::domainID(),
32 ];
33 $contactID = $this->individualCreate($params);
34 $this->callAPISuccess('UFMatch', 'get', ['uf_id' => 6, 'api.UFMatch.delete' => []]);
35 $this->callAPISuccess('UFMatch', 'create', [
36 'contact_id' => $contactID,
37 'uf_name' => 'superman',
38 'uf_id' => 6,
39 ]);
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
56 */
57 public function organizationCreate($params = [], $seq = 0) {
58 if (!$params) {
59 $params = [];
60 }
61 $params = array_merge($this->sampleContact('Organization', $seq), $params);
62 return $this->_contactCreate($params);
63 }
64
65 /**
66 * Generic function to create Individual, to be used in test cases
67 *
68 * @param array $params
69 * parameters for civicrm_contact_add api function call
70 * @param int $seq
71 * sequence number if creating multiple individuals
72 * @param bool $random
73 *
74 * @return int
75 * id of Individual created
76 *
77 * @throws \CRM_Core_Exception
78 */
79 public function individualCreate($params = [], $seq = 0, $random = FALSE) {
80 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
81 return $this->_contactCreate($params);
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
94 *
95 * @throws \CRM_Core_Exception
96 */
97 public function householdCreate($params = [], $seq = 0) {
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) {
115 $samples = [
116 'Individual' => [
117 // The number of values in each list need to be coprime numbers to not have duplicates
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' => [
124 'Unit Test Organization',
125 'Acme',
126 'Roberts and Sons',
127 'Cryo Space Labs',
128 'Sharper Pens',
129 ],
130 ],
131 'Household' => [
132 'household_name' => ['Unit Test household'],
133 ],
134 ];
135 $params = ['contact_type' => $contact_type];
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 *
158 * @return int
159 * id of contact created
160 *
161 * @throws \CRM_Core_Exception
162 * @throws \CiviCRM_API3_Exception
163 */
164 private function _contactCreate($params) {
165 $result = civicrm_api3('contact', 'create', $params);
166 if (!empty($result['is_error']) || empty($result['id'])) {
167 throw new \CRM_Core_Exception('Could not create test contact, with message: ' . \CRM_Utils_Array::value('error_message', $result) . "\nBacktrace:" . \CRM_Utils_Array::value('trace', $result));
168 }
169 return (int) $result['id'];
170 }
171
172 /**
173 * Delete contact, ensuring it is not the domain contact
174 *
175 * @param int $contactID
176 * Contact ID to delete
177 */
178 public function contactDelete($contactID) {
179 $domain = new \CRM_Core_BAO_Domain();
180 $domain->contact_id = $contactID;
181 if (!$domain->find(TRUE)) {
182 $this->callAPISuccess('contact', 'delete', [
183 'id' => $contactID,
184 'skip_undelete' => 1,
185 ]);
186 }
187 }
188
189 /**
190 * Add a Group.
191 *
192 * @param array $params
193 *
194 * @return int
195 * groupId of created group
196 */
197 public function groupCreate($params = []) {
198 $params = array_merge([
199 'name' => 'Test Group 1',
200 'domain_id' => 1,
201 'title' => 'New Test Group Created',
202 'description' => 'New Test Group Created',
203 'is_active' => 1,
204 'visibility' => 'Public Pages',
205 'group_type' => [
206 '1' => 1,
207 '2' => 1,
208 ],
209 ], $params);
210
211 $result = $this->callAPISuccess('Group', 'create', $params);
212 return $result['id'];
213 }
214
215 /**
216 * Delete a Group.
217 *
218 * @param int $gid
219 */
220 public function groupDelete($gid) {
221 $params = [
222 'id' => $gid,
223 ];
224
225 $this->callAPISuccess('Group', 'delete', $params);
226 }
227
228 /**
229 * Function to add a Group.
230 *
231 * @params array to add group
232 *
233 * @param int $groupID
234 * @param int $totalCount
235 * @param bool $random
236 *
237 * @return int
238 * groupId of created group
239 */
240 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
241 $params = ['group_id' => $groupID];
242 for ($i = 1; $i <= $totalCount; $i++) {
243 $contactID = $this->individualCreate([], 0, $random);
244 if ($i == 1) {
245 $params += ['contact_id' => $contactID];
246 }
247 else {
248 $params += ["contact_id.$i" => $contactID];
249 }
250 }
251 $result = $this->callAPISuccess('GroupContact', 'create', $params);
252
253 return $result;
254 }
255
256 }