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)
21 * @see https://issues.civicrm.org/jira/browse/CRM-8180
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 * @throws \CiviCRM_API3_Exception
59 public function organizationCreate($params = [], $seq = 0): int {
63 $params = array_merge($this->sampleContact('Organization', $seq), $params);
64 return $this->_contactCreate($params);
68 * Generic function to create Individual, to be used in test cases
70 * @param array $params
71 * parameters for civicrm_contact_add api function call
73 * sequence number if creating multiple individuals
77 * id of Individual created
79 * @throws \CiviCRM_API3_Exception
81 public function individualCreate($params = [], $seq = 0, $random = FALSE): int {
82 $params = array_merge($this->sampleContact('Individual', $seq, $random), $params);
83 return $this->_contactCreate($params);
87 * Generic function to create Household, to be used in test cases
89 * @param array $params
90 * parameters for civicrm_contact_add api function call
92 * sequence number if creating multiple households
95 * id of Household created
97 * @throws \CRM_Core_Exception
99 public function householdCreate($params = [], $seq = 0) {
100 $params = array_merge($this->sampleContact('Household', $seq), $params);
101 return $this->_contactCreate($params);
105 * Helper function for getting sample contact properties.
107 * @param string $contact_type
108 * enum contact type: Individual, Organization
110 * sequence number for the values of this type
111 * @param bool $random
114 * properties of sample contact (ie. $params for API call)
116 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
119 // The number of values in each list need to be coprime numbers to not have duplicates
120 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'],
121 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'],
122 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'],
125 'organization_name' => [
126 'Unit Test Organization',
134 'household_name' => ['Unit Test household'],
137 $params = ['contact_type' => $contact_type];
138 foreach ($samples[$contact_type] as $key => $values) {
139 $params[$key] = $values[$seq %
count($values)];
141 $params[$key] .= substr(sha1(rand()), 0, 5);
144 if ($contact_type == 'Individual') {
145 $params['email'] = strtolower(
146 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
148 $params['prefix_id'] = 3;
149 $params['suffix_id'] = 3;
155 * Private helper function for calling civicrm_contact_add.
157 * @param array $params
158 * For civicrm_contact_add api function call.
161 * id of contact created
163 * @throws \CiviCRM_API3_Exception
165 private function _contactCreate($params): int {
166 $result = civicrm_api3('contact', 'create', $params);
167 return (int) $result['id'];
171 * Delete contact, ensuring it is not the domain contact
173 * @param int $contactID
174 * Contact ID to delete
176 public function contactDelete($contactID) {
177 $domain = new \
CRM_Core_BAO_Domain();
178 $domain->contact_id
= $contactID;
179 if (!$domain->find(TRUE)) {
180 $this->callAPISuccess('contact', 'delete', [
182 'skip_undelete' => 1,
190 * @param array $params
193 * groupId of created group
195 public function groupCreate($params = []) {
196 $params = array_merge([
197 'name' => 'Test Group 1',
199 'title' => 'New Test Group Created',
200 'description' => 'New Test Group Created',
202 'visibility' => 'Public Pages',
209 $result = $this->callAPISuccess('Group', 'create', $params);
210 return $result['id'];
218 public function groupDelete($gid) {
223 $this->callAPISuccess('Group', 'delete', $params);
227 * Function to add a Group.
229 * @params array to add group
231 * @param int $groupID
232 * @param int $totalCount
233 * @param bool $random
236 * groupId of created group
238 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
239 $params = ['group_id' => $groupID];
240 for ($i = 1; $i <= $totalCount; $i++
) {
241 $contactID = $this->individualCreate([], 0, $random);
243 $params +
= ['contact_id' => $contactID];
246 $params +
= ["contact_id.$i" => $contactID];
249 $result = $this->callAPISuccess('GroupContact', 'create', $params);