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.
27 public function createLoggedInUser(): int {
29 'first_name' => 'Logged In',
30 'last_name' => 'User ' . rand(),
31 'contact_type' => 'Individual',
32 'domain_id' => \CRM_Core_Config
::domainID(),
34 $contactID = $this->individualCreate($params);
35 $this->callAPISuccess('UFMatch', 'get', ['uf_id' => 6, 'api.UFMatch.delete' => []]);
36 $this->callAPISuccess('UFMatch', 'create', [
37 'contact_id' => $contactID,
38 'uf_name' => 'superman',
42 $session = \CRM_Core_Session
::singleton();
43 $session->set('userID', $contactID);
48 * Generic function to create Organisation, to be used in test cases
50 * @param array $params
51 * parameters for civicrm_contact_add api function call
53 * sequence number if creating multiple organizations
56 * id of Organisation created
58 public function organizationCreate($params = [], $seq = 0): int {
62 $params = array_merge($this->sampleContact('Organization', $seq), $params);
63 return $this->_contactCreate($params);
67 * Generic function to create Individual, to be used in test cases
69 * @param array $params
70 * parameters for civicrm_contact_add api function call
72 * sequence number if creating multiple individuals
76 * id of Individual created
78 public function individualCreate(array $params = [], $seq = 0, $random = FALSE): int {
79 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
80 return $this->_contactCreate($params);
84 * Generic function to create Household, to be used in test cases
86 * @param array $params
87 * parameters for civicrm_contact_add api function call
89 * sequence number if creating multiple households
92 * id of Household created
94 * @throws \CRM_Core_Exception
96 public function householdCreate($params = [], $seq = 0) {
97 $params = array_merge($this->sampleContact('Household', $seq), $params);
98 return $this->_contactCreate($params);
102 * Helper function for getting sample contact properties.
104 * @param string $contact_type
105 * enum contact type: Individual, Organization
107 * sequence number for the values of this type
108 * @param bool $random
111 * properties of sample contact (ie. $params for API call)
113 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
116 // The number of values in each list need to be coprime numbers to not have duplicates
117 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
118 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
119 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
122 'organization_name' => [
123 'Unit Test Organization',
131 'household_name' => ['Unit Test household'],
134 $params = ['contact_type' => $contact_type];
135 foreach ($samples[$contact_type] as $key => $values) {
136 $params[$key] = $values[$seq %
count($values)];
138 $params[$key] .= substr(sha1(rand()), 0, 5);
141 if ($contact_type == 'Individual') {
142 $params['email'] = strtolower(
143 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
145 $params['prefix_id'] = 3;
146 $params['suffix_id'] = 3;
152 * Private helper function for calling civicrm_contact_add.
154 * @param array $params
155 * For civicrm_contact_add api function call.
158 * id of contact created
160 private function _contactCreate(array $params): int {
161 $version = $this->_apiversion
;
162 $this->_apiversion
= 3;
163 $result = $this->callAPISuccess('contact', 'create', $params);
164 $this->_apiversion
= $version;
165 return (int) $result['id'];
169 * Delete contact, ensuring it is not the domain contact
171 * @param int $contactID
172 * Contact ID to delete
174 public function contactDelete($contactID) {
175 $domain = new \
CRM_Core_BAO_Domain();
176 $domain->contact_id
= $contactID;
177 if (!$domain->find(TRUE)) {
178 $this->callAPISuccess('contact', 'delete', [
180 'skip_undelete' => 1,
188 * @param array $params
191 * groupId of created group
193 public function groupCreate($params = []) {
194 $params = array_merge([
195 'name' => 'Test Group 1',
197 'title' => 'New Test Group Created',
198 'description' => 'New Test Group Created',
200 'visibility' => 'Public Pages',
207 $result = $this->callAPISuccess('Group', 'create', $params);
208 return $result['id'];
216 public function groupDelete($gid) {
221 $this->callAPISuccess('Group', 'delete', $params);
225 * Function to add a Group.
227 * @params array to add group
229 * @param int $groupID
230 * @param int $totalCount
231 * @param bool $random
234 * groupId of created group
236 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
237 $params = ['group_id' => $groupID];
238 for ($i = 1; $i <= $totalCount; $i++
) {
239 $contactID = $this->individualCreate([], 0, $random);
241 $params +
= ['contact_id' => $contactID];
244 $params +
= ["contact_id.$i" => $contactID];
247 $result = $this->callAPISuccess('GroupContact', 'create', $params);