6 * Class ContactTestTrait
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.
15 trait ContactTestTrait
{
17 abstract public function callAPISuccess($entity, $action, $params, $checkAgainst = NULL);
20 * Emulate a logged in user since certain functions use that.
21 * value to store a record in the DB (like activity)
25 * Contact ID of the created user.
27 public function createLoggedInUser() {
29 'first_name' => 'Logged In',
30 'last_name' => 'User ' . rand(),
31 'contact_type' => 'Individual',
33 $contactID = $this->individualCreate($params);
34 $this->callAPISuccess('UFMatch', 'create', array(
35 'contact_id' => $contactID,
36 'uf_name' => 'superman',
40 $session = \CRM_Core_Session
::singleton();
41 $session->set('userID', $contactID);
46 * Generic function to create Organisation, to be used in test cases
48 * @param array $params
49 * parameters for civicrm_contact_add api function call
51 * sequence number if creating multiple organizations
54 * id of Organisation created
56 public function organizationCreate($params = array(), $seq = 0) {
60 $params = array_merge($this->sampleContact('Organization', $seq), $params);
61 return $this->_contactCreate($params);
65 * Generic function to create Individual, to be used in test cases
67 * @param array $params
68 * parameters for civicrm_contact_add api function call
70 * sequence number if creating multiple individuals
74 * id of Individual created
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);
82 * Generic function to create Household, to be used in test cases
84 * @param array $params
85 * parameters for civicrm_contact_add api function call
87 * sequence number if creating multiple households
90 * id of Household created
92 public function householdCreate($params = array(), $seq = 0) {
93 $params = array_merge($this->sampleContact('Household', $seq), $params);
94 return $this->_contactCreate($params);
98 * Helper function for getting sample contact properties.
100 * @param string $contact_type
101 * enum contact type: Individual, Organization
103 * sequence number for the values of this type
104 * @param bool $random
107 * properties of sample contact (ie. $params for API call)
109 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
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'),
117 'Organization' => array(
118 'organization_name' => array(
119 'Unit Test Organization',
126 'Household' => array(
127 'household_name' => array('Unit Test household'),
130 $params = array('contact_type' => $contact_type);
131 foreach ($samples[$contact_type] as $key => $values) {
132 $params[$key] = $values[$seq %
count($values)];
134 $params[$key] .= substr(sha1(rand()), 0, 5);
137 if ($contact_type == 'Individual') {
138 $params['email'] = strtolower(
139 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
141 $params['prefix_id'] = 3;
142 $params['suffix_id'] = 3;
148 * Private helper function for calling civicrm_contact_add.
150 * @param array $params
151 * For civicrm_contact_add api function call.
156 * id of Household created
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));
163 return $result['id'];
167 * Delete contact, ensuring it is not the domain contact
169 * @param int $contactID
170 * Contact ID to delete
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(
178 'skip_undelete' => 1,
186 * @param array $params
188 * groupId of created group
190 public function groupCreate($params = array()) {
191 $params = array_merge(array(
192 'name' => 'Test Group 1',
194 'title' => 'New Test Group Created',
195 'description' => 'New Test Group Created',
197 'visibility' => 'Public Pages',
198 'group_type' => array(
204 $result = $this->callAPISuccess('Group', 'create', $params);
205 return $result['id'];
213 public function groupDelete($gid) {
218 $this->callAPISuccess('Group', 'delete', $params);
222 * Function to add a Group.
224 * @params array to add group
226 * @param int $groupID
227 * @param int $totalCount
228 * @param bool $random
230 * groupId of created group
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);
237 $params +
= array('contact_id' => $contactID);
240 $params +
= array("contact_id.$i" => $contactID);
243 $result = $this->callAPISuccess('GroupContact', 'create', $params);