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)
24 * Contact ID of the created user.
26 public function createLoggedInUser() {
28 'first_name' => 'Logged In',
29 'last_name' => 'User ' . rand(),
30 'contact_type' => 'Individual',
31 'domain_id' => \CRM_Core_Config
::domainID(),
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',
41 $session = \CRM_Core_Session
::singleton();
42 $session->set('userID', $contactID);
47 * Generic function to create Organisation, to be used in test cases
49 * @param array $params
50 * parameters for civicrm_contact_add api function call
52 * sequence number if creating multiple organizations
55 * id of Organisation created
57 public function organizationCreate($params = [], $seq = 0) {
61 $params = array_merge($this->sampleContact('Organization', $seq), $params);
62 return $this->_contactCreate($params);
66 * Generic function to create Individual, to be used in test cases
68 * @param array $params
69 * parameters for civicrm_contact_add api function call
71 * sequence number if creating multiple individuals
75 * id of Individual created
77 * @throws \CRM_Core_Exception
79 public function individualCreate($params = [], $seq = 0, $random = FALSE) {
80 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
81 return $this->_contactCreate($params);
85 * Generic function to create Household, to be used in test cases
87 * @param array $params
88 * parameters for civicrm_contact_add api function call
90 * sequence number if creating multiple households
93 * id of Household created
95 * @throws \CRM_Core_Exception
97 public function householdCreate($params = [], $seq = 0) {
98 $params = array_merge($this->sampleContact('Household', $seq), $params);
99 return $this->_contactCreate($params);
103 * Helper function for getting sample contact properties.
105 * @param string $contact_type
106 * enum contact type: Individual, Organization
108 * sequence number for the values of this type
109 * @param bool $random
112 * properties of sample contact (ie. $params for API call)
114 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
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'],
123 'organization_name' => [
124 'Unit Test Organization',
132 'household_name' => ['Unit Test household'],
135 $params = ['contact_type' => $contact_type];
136 foreach ($samples[$contact_type] as $key => $values) {
137 $params[$key] = $values[$seq %
count($values)];
139 $params[$key] .= substr(sha1(rand()), 0, 5);
142 if ($contact_type == 'Individual') {
143 $params['email'] = strtolower(
144 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
146 $params['prefix_id'] = 3;
147 $params['suffix_id'] = 3;
153 * Private helper function for calling civicrm_contact_add.
155 * @param array $params
156 * For civicrm_contact_add api function call.
159 * id of Household created
160 * @throws \CRM_Core_Exception
163 private function _contactCreate($params) {
164 $result = civicrm_api3('contact', 'create', $params);
165 if (!empty($result['is_error']) ||
empty($result['id'])) {
166 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 return $result['id'];
172 * Delete contact, ensuring it is not the domain contact
174 * @param int $contactID
175 * Contact ID to delete
177 public function contactDelete($contactID) {
178 $domain = new \
CRM_Core_BAO_Domain();
179 $domain->contact_id
= $contactID;
180 if (!$domain->find(TRUE)) {
181 $this->callAPISuccess('contact', 'delete', [
183 'skip_undelete' => 1,
191 * @param array $params
194 * groupId of created group
196 public function groupCreate($params = []) {
197 $params = array_merge([
198 'name' => 'Test Group 1',
200 'title' => 'New Test Group Created',
201 'description' => 'New Test Group Created',
203 'visibility' => 'Public Pages',
210 $result = $this->callAPISuccess('Group', 'create', $params);
211 return $result['id'];
219 public function groupDelete($gid) {
224 $this->callAPISuccess('Group', 'delete', $params);
228 * Function to add a Group.
230 * @params array to add group
232 * @param int $groupID
233 * @param int $totalCount
234 * @param bool $random
237 * groupId of created group
239 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
240 $params = ['group_id' => $groupID];
241 for ($i = 1; $i <= $totalCount; $i++
) {
242 $contactID = $this->individualCreate([], 0, $random);
244 $params +
= ['contact_id' => $contactID];
247 $params +
= ["contact_id.$i" => $contactID];
250 $result = $this->callAPISuccess('GroupContact', 'create', $params);