Merge pull request #14040 from eileenmcnaughton/import
[civicrm-core.git] / Civi / Test / ContactTestTrait.php
CommitLineData
a23e13eb
TO
1<?php
2
3namespace Civi\Test;
4
5/**
6 * Class ContactTestTrait
7 * @package Civi\Test
8 *
9 * This trait defines a number of helper functions for managing
10 * test contacts. Generally, it depends on having access to the
11 * API test functions ($this->callAPISuccess()) and to the
12 * standard PHPUnit assertions ($this->assertEquals). It should
13 * not impose any other requirements for the downstream consumer class.
14 */
15trait ContactTestTrait {
16
17 abstract public function callAPISuccess($entity, $action, $params, $checkAgainst = NULL);
18
19 /**
20 * Emulate a logged in user since certain functions use that.
21 * value to store a record in the DB (like activity)
22 * CRM-8180
23 *
24 * @return int
25 * Contact ID of the created user.
26 */
27 public function createLoggedInUser() {
28 $params = array(
29 'first_name' => 'Logged In',
30 'last_name' => 'User ' . rand(),
31 'contact_type' => 'Individual',
4fcfa5dd 32 'domain_id' => \CRM_Core_Config::domainID(),
a23e13eb
TO
33 );
34 $contactID = $this->individualCreate($params);
35 $this->callAPISuccess('UFMatch', 'create', array(
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 = array(), $seq = 0) {
58 if (!$params) {
59 $params = array();
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 public function individualCreate($params = array(), $seq = 0, $random = FALSE) {
78 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
79 return $this->_contactCreate($params);
80 }
81
82 /**
83 * Generic function to create Household, to be used in test cases
84 *
85 * @param array $params
86 * parameters for civicrm_contact_add api function call
87 * @param int $seq
88 * sequence number if creating multiple households
89 *
90 * @return int
91 * id of Household created
92 */
93 public function householdCreate($params = array(), $seq = 0) {
94 $params = array_merge($this->sampleContact('Household', $seq), $params);
95 return $this->_contactCreate($params);
96 }
97
98 /**
99 * Helper function for getting sample contact properties.
100 *
101 * @param string $contact_type
102 * enum contact type: Individual, Organization
103 * @param int $seq
104 * sequence number for the values of this type
105 * @param bool $random
106 *
107 * @return array
108 * properties of sample contact (ie. $params for API call)
109 */
110 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
111 $samples = array(
112 'Individual' => array(
113 // The number of values in each list need to be coprime numbers to not have duplicates
114 'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'),
115 'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'),
116 'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'),
117 ),
118 'Organization' => array(
119 'organization_name' => array(
120 'Unit Test Organization',
121 'Acme',
122 'Roberts and Sons',
123 'Cryo Space Labs',
124 'Sharper Pens',
125 ),
126 ),
127 'Household' => array(
128 'household_name' => array('Unit Test household'),
129 ),
130 );
131 $params = array('contact_type' => $contact_type);
132 foreach ($samples[$contact_type] as $key => $values) {
133 $params[$key] = $values[$seq % count($values)];
134 if ($random) {
135 $params[$key] .= substr(sha1(rand()), 0, 5);
136 }
137 }
138 if ($contact_type == 'Individual') {
139 $params['email'] = strtolower(
140 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
141 );
142 $params['prefix_id'] = 3;
143 $params['suffix_id'] = 3;
144 }
145 return $params;
146 }
147
148 /**
149 * Private helper function for calling civicrm_contact_add.
150 *
151 * @param array $params
152 * For civicrm_contact_add api function call.
153 *
154 * @throws \Exception
155 *
156 * @return int
157 * id of Household created
158 */
159 private function _contactCreate($params) {
160 $result = $this->callAPISuccess('contact', 'create', $params);
161 if (!empty($result['is_error']) || empty($result['id'])) {
162 throw new \Exception('Could not create test contact, with message: ' . \CRM_Utils_Array::value('error_message', $result) . "\nBacktrace:" . \CRM_Utils_Array::value('trace', $result));
163 }
164 return $result['id'];
165 }
166
167 /**
168 * Delete contact, ensuring it is not the domain contact
169 *
170 * @param int $contactID
171 * Contact ID to delete
172 */
173 public function contactDelete($contactID) {
174 $domain = new \CRM_Core_BAO_Domain();
175 $domain->contact_id = $contactID;
176 if (!$domain->find(TRUE)) {
177 $this->callAPISuccess('contact', 'delete', array(
178 'id' => $contactID,
179 'skip_undelete' => 1,
180 ));
181 }
182 }
183
184 /**
185 * Add a Group.
186 *
187 * @param array $params
188 * @return int
189 * groupId of created group
190 */
191 public function groupCreate($params = array()) {
192 $params = array_merge(array(
193 'name' => 'Test Group 1',
194 'domain_id' => 1,
195 'title' => 'New Test Group Created',
196 'description' => 'New Test Group Created',
197 'is_active' => 1,
198 'visibility' => 'Public Pages',
199 'group_type' => array(
200 '1' => 1,
201 '2' => 1,
202 ),
203 ), $params);
204
205 $result = $this->callAPISuccess('Group', 'create', $params);
206 return $result['id'];
207 }
208
209 /**
210 * Delete a Group.
211 *
212 * @param int $gid
213 */
214 public function groupDelete($gid) {
215 $params = array(
216 'id' => $gid,
217 );
218
219 $this->callAPISuccess('Group', 'delete', $params);
220 }
221
222 /**
223 * Function to add a Group.
224 *
225 * @params array to add group
226 *
227 * @param int $groupID
228 * @param int $totalCount
229 * @param bool $random
230 * @return int
231 * groupId of created group
232 */
233 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
234 $params = array('group_id' => $groupID);
235 for ($i = 1; $i <= $totalCount; $i++) {
236 $contactID = $this->individualCreate(array(), 0, $random);
237 if ($i == 1) {
238 $params += array('contact_id' => $contactID);
239 }
240 else {
241 $params += array("contact_id.$i" => $contactID);
242 }
243 }
244 $result = $this->callAPISuccess('GroupContact', 'create', $params);
245
246 return $result;
247 }
248
249}