Move helpers from CiviUnitTestCase to ContactTestTrait
[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',
32 );
33 $contactID = $this->individualCreate($params);
34 $this->callAPISuccess('UFMatch', 'create', array(
35 'contact_id' => $contactID,
36 'uf_name' => 'superman',
37 'uf_id' => 6,
38 ));
39
40 $session = \CRM_Core_Session::singleton();
41 $session->set('userID', $contactID);
42 return $contactID;
43 }
44
45 /**
46 * Generic function to create Organisation, to be used in test cases
47 *
48 * @param array $params
49 * parameters for civicrm_contact_add api function call
50 * @param int $seq
51 * sequence number if creating multiple organizations
52 *
53 * @return int
54 * id of Organisation created
55 */
56 public function organizationCreate($params = array(), $seq = 0) {
57 if (!$params) {
58 $params = array();
59 }
60 $params = array_merge($this->sampleContact('Organization', $seq), $params);
61 return $this->_contactCreate($params);
62 }
63
64 /**
65 * Generic function to create Individual, to be used in test cases
66 *
67 * @param array $params
68 * parameters for civicrm_contact_add api function call
69 * @param int $seq
70 * sequence number if creating multiple individuals
71 * @param bool $random
72 *
73 * @return int
74 * id of Individual created
75 */
76 public function individualCreate($params = array(), $seq = 0, $random = FALSE) {
77 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
78 return $this->_contactCreate($params);
79 }
80
81 /**
82 * Generic function to create Household, to be used in test cases
83 *
84 * @param array $params
85 * parameters for civicrm_contact_add api function call
86 * @param int $seq
87 * sequence number if creating multiple households
88 *
89 * @return int
90 * id of Household created
91 */
92 public function householdCreate($params = array(), $seq = 0) {
93 $params = array_merge($this->sampleContact('Household', $seq), $params);
94 return $this->_contactCreate($params);
95 }
96
97 /**
98 * Helper function for getting sample contact properties.
99 *
100 * @param string $contact_type
101 * enum contact type: Individual, Organization
102 * @param int $seq
103 * sequence number for the values of this type
104 * @param bool $random
105 *
106 * @return array
107 * properties of sample contact (ie. $params for API call)
108 */
109 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
110 $samples = array(
111 'Individual' => array(
112 // The number of values in each list need to be coprime numbers to not have duplicates
113 'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'),
114 'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'),
115 'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'),
116 ),
117 'Organization' => array(
118 'organization_name' => array(
119 'Unit Test Organization',
120 'Acme',
121 'Roberts and Sons',
122 'Cryo Space Labs',
123 'Sharper Pens',
124 ),
125 ),
126 'Household' => array(
127 'household_name' => array('Unit Test household'),
128 ),
129 );
130 $params = array('contact_type' => $contact_type);
131 foreach ($samples[$contact_type] as $key => $values) {
132 $params[$key] = $values[$seq % count($values)];
133 if ($random) {
134 $params[$key] .= substr(sha1(rand()), 0, 5);
135 }
136 }
137 if ($contact_type == 'Individual') {
138 $params['email'] = strtolower(
139 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
140 );
141 $params['prefix_id'] = 3;
142 $params['suffix_id'] = 3;
143 }
144 return $params;
145 }
146
147 /**
148 * Private helper function for calling civicrm_contact_add.
149 *
150 * @param array $params
151 * For civicrm_contact_add api function call.
152 *
153 * @throws \Exception
154 *
155 * @return int
156 * id of Household created
157 */
158 private function _contactCreate($params) {
159 $result = $this->callAPISuccess('contact', 'create', $params);
160 if (!empty($result['is_error']) || empty($result['id'])) {
161 throw new \Exception('Could not create test contact, with message: ' . \CRM_Utils_Array::value('error_message', $result) . "\nBacktrace:" . \CRM_Utils_Array::value('trace', $result));
162 }
163 return $result['id'];
164 }
165
166 /**
167 * Delete contact, ensuring it is not the domain contact
168 *
169 * @param int $contactID
170 * Contact ID to delete
171 */
172 public function contactDelete($contactID) {
173 $domain = new \CRM_Core_BAO_Domain();
174 $domain->contact_id = $contactID;
175 if (!$domain->find(TRUE)) {
176 $this->callAPISuccess('contact', 'delete', array(
177 'id' => $contactID,
178 'skip_undelete' => 1,
179 ));
180 }
181 }
182
183 /**
184 * Add a Group.
185 *
186 * @param array $params
187 * @return int
188 * groupId of created group
189 */
190 public function groupCreate($params = array()) {
191 $params = array_merge(array(
192 'name' => 'Test Group 1',
193 'domain_id' => 1,
194 'title' => 'New Test Group Created',
195 'description' => 'New Test Group Created',
196 'is_active' => 1,
197 'visibility' => 'Public Pages',
198 'group_type' => array(
199 '1' => 1,
200 '2' => 1,
201 ),
202 ), $params);
203
204 $result = $this->callAPISuccess('Group', 'create', $params);
205 return $result['id'];
206 }
207
208 /**
209 * Delete a Group.
210 *
211 * @param int $gid
212 */
213 public function groupDelete($gid) {
214 $params = array(
215 'id' => $gid,
216 );
217
218 $this->callAPISuccess('Group', 'delete', $params);
219 }
220
221 /**
222 * Function to add a Group.
223 *
224 * @params array to add group
225 *
226 * @param int $groupID
227 * @param int $totalCount
228 * @param bool $random
229 * @return int
230 * groupId of created group
231 */
232 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
233 $params = array('group_id' => $groupID);
234 for ($i = 1; $i <= $totalCount; $i++) {
235 $contactID = $this->individualCreate(array(), 0, $random);
236 if ($i == 1) {
237 $params += array('contact_id' => $contactID);
238 }
239 else {
240 $params += array("contact_id.$i" => $contactID);
241 }
242 }
243 $result = $this->callAPISuccess('GroupContact', 'create', $params);
244
245 return $result;
246 }
247
248}