bd8d5bb16fcf21a708805d6274ca09a2b089f072
6 * Class ContactTestTrait
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.
16 trait ContactTestTrait
{
19 * Emulate a logged in user since certain functions use that.
20 * value to store a record in the DB (like activity)
22 * @see https://issues.civicrm.org/jira/browse/CRM-8180
25 * Contact ID of the created user.
26 * @throws \CiviCRM_API3_Exception
28 public function createLoggedInUser(): int {
30 'first_name' => 'Logged In',
31 'last_name' => 'User ' . rand(),
32 'contact_type' => 'Individual',
33 'domain_id' => \CRM_Core_Config
::domainID(),
35 $contactID = $this->individualCreate($params);
36 $this->callAPISuccess('UFMatch', 'get', ['uf_id' => 6, 'api.UFMatch.delete' => []]);
37 $this->callAPISuccess('UFMatch', 'create', [
38 'contact_id' => $contactID,
39 'uf_name' => 'superman',
43 $session = \CRM_Core_Session
::singleton();
44 $session->set('userID', $contactID);
49 * Generic function to create Organisation, to be used in test cases
51 * @param array $params
52 * parameters for civicrm_contact_add api function call
54 * sequence number if creating multiple organizations
57 * id of Organisation created
59 * @throws \CiviCRM_API3_Exception
61 public function organizationCreate($params = [], $seq = 0): int {
65 $params = array_merge($this->sampleContact('Organization', $seq), $params);
66 return $this->_contactCreate($params);
70 * Generic function to create Individual, to be used in test cases
72 * @param array $params
73 * parameters for civicrm_contact_add api function call
75 * sequence number if creating multiple individuals
79 * id of Individual created
81 * @throws \CiviCRM_API3_Exception
83 public function individualCreate($params = [], $seq = 0, $random = FALSE): int {
84 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
85 return $this->_contactCreate($params);
89 * Generic function to create Household, to be used in test cases
91 * @param array $params
92 * parameters for civicrm_contact_add api function call
94 * sequence number if creating multiple households
97 * id of Household created
99 * @throws \CRM_Core_Exception
101 public function householdCreate($params = [], $seq = 0) {
102 $params = array_merge($this->sampleContact('Household', $seq), $params);
103 return $this->_contactCreate($params);
107 * Helper function for getting sample contact properties.
109 * @param string $contact_type
110 * enum contact type: Individual, Organization
112 * sequence number for the values of this type
113 * @param bool $random
116 * properties of sample contact (ie. $params for API call)
118 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
121 // The number of values in each list need to be coprime numbers to not have duplicates
122 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
123 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
124 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
127 'organization_name' => [
128 'Unit Test Organization',
136 'household_name' => ['Unit Test household'],
139 $params = ['contact_type' => $contact_type];
140 foreach ($samples[$contact_type] as $key => $values) {
141 $params[$key] = $values[$seq %
count($values)];
143 $params[$key] .= substr(sha1(rand()), 0, 5);
146 if ($contact_type == 'Individual') {
147 $params['email'] = strtolower(
148 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
150 $params['prefix_id'] = 3;
151 $params['suffix_id'] = 3;
157 * Private helper function for calling civicrm_contact_add.
159 * @param array $params
160 * For civicrm_contact_add api function call.
163 * id of contact created
165 private function _contactCreate(array $params): int {
166 $version = $this->_apiversion
;
167 $this->_apiversion
= 3;
168 $result = $this->callAPISuccess('contact', 'create', $params);
169 $this->_apiversion
= $version;
170 return (int) $result['id'];
174 * Delete contact, ensuring it is not the domain contact
176 * @param int $contactID
177 * Contact ID to delete
179 public function contactDelete($contactID) {
180 $domain = new \
CRM_Core_BAO_Domain();
181 $domain->contact_id
= $contactID;
182 if (!$domain->find(TRUE)) {
183 $this->callAPISuccess('contact', 'delete', [
185 'skip_undelete' => 1,
193 * @param array $params
196 * groupId of created group
198 public function groupCreate($params = []) {
199 $params = array_merge([
200 'name' => 'Test Group 1',
202 'title' => 'New Test Group Created',
203 'description' => 'New Test Group Created',
205 'visibility' => 'Public Pages',
212 $result = $this->callAPISuccess('Group', 'create', $params);
213 return $result['id'];
221 public function groupDelete($gid) {
226 $this->callAPISuccess('Group', 'delete', $params);
230 * Function to add a Group.
232 * @params array to add group
234 * @param int $groupID
235 * @param int $totalCount
236 * @param bool $random
239 * groupId of created group
241 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
242 $params = ['group_id' => $groupID];
243 for ($i = 1; $i <= $totalCount; $i++
) {
244 $contactID = $this->individualCreate([], 0, $random);
246 $params +
= ['contact_id' => $contactID];
249 $params +
= ["contact_id.$i" => $contactID];
252 $result = $this->callAPISuccess('GroupContact', 'create', $params);