c98e457b05d76247544ae7461cbd6475572e0e6f
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
{
18 * Emulate a logged in user since certain functions use that.
19 * value to store a record in the DB (like activity)
23 * Contact ID of the created user.
25 public function createLoggedInUser() {
27 'first_name' => 'Logged In',
28 'last_name' => 'User ' . rand(),
29 'contact_type' => 'Individual',
30 'domain_id' => \CRM_Core_Config
::domainID(),
32 $contactID = $this->individualCreate($params);
33 $this->callAPISuccess('UFMatch', 'create', array(
34 'contact_id' => $contactID,
35 'uf_name' => 'superman',
39 $session = \CRM_Core_Session
::singleton();
40 $session->set('userID', $contactID);
45 * Generic function to create Organisation, to be used in test cases
47 * @param array $params
48 * parameters for civicrm_contact_add api function call
50 * sequence number if creating multiple organizations
53 * id of Organisation created
55 public function organizationCreate($params = array(), $seq = 0) {
59 $params = array_merge($this->sampleContact('Organization', $seq), $params);
60 return $this->_contactCreate($params);
64 * Generic function to create Individual, to be used in test cases
66 * @param array $params
67 * parameters for civicrm_contact_add api function call
69 * sequence number if creating multiple individuals
73 * 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
93 public function householdCreate($params = array(), $seq = 0) {
94 $params = array_merge($this->sampleContact('Household', $seq), $params);
95 return $this->_contactCreate($params);
99 * Helper function for getting sample contact properties.
101 * @param string $contact_type
102 * enum contact type: Individual, Organization
104 * sequence number for the values of this type
105 * @param bool $random
108 * properties of sample contact (ie. $params for API call)
110 public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
112 'Individual' => array(
113 // The number of values in each list need to be coprime numbers to not have duplicates
114 'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'),
115 'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'),
116 'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'),
118 'Organization' => array(
119 'organization_name' => array(
120 'Unit Test Organization',
127 'Household' => array(
128 'household_name' => array('Unit Test household'),
131 $params = array('contact_type' => $contact_type);
132 foreach ($samples[$contact_type] as $key => $values) {
133 $params[$key] = $values[$seq %
count($values)];
135 $params[$key] .= substr(sha1(rand()), 0, 5);
138 if ($contact_type == 'Individual') {
139 $params['email'] = strtolower(
140 $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
142 $params['prefix_id'] = 3;
143 $params['suffix_id'] = 3;
149 * Private helper function for calling civicrm_contact_add.
151 * @param array $params
152 * For civicrm_contact_add api function call.
157 * id of Household created
159 private function _contactCreate($params) {
160 $result = $this->callAPISuccess('contact', 'create', $params);
161 if (!empty($result['is_error']) ||
empty($result['id'])) {
162 throw new \
Exception('Could not create test contact, with message: ' . \CRM_Utils_Array
::value('error_message', $result) . "\nBacktrace:" . \CRM_Utils_Array
::value('trace', $result));
164 return $result['id'];
168 * Delete contact, ensuring it is not the domain contact
170 * @param int $contactID
171 * Contact ID to delete
173 public function contactDelete($contactID) {
174 $domain = new \
CRM_Core_BAO_Domain();
175 $domain->contact_id
= $contactID;
176 if (!$domain->find(TRUE)) {
177 $this->callAPISuccess('contact', 'delete', array(
179 'skip_undelete' => 1,
187 * @param array $params
189 * groupId of created group
191 public function groupCreate($params = array()) {
192 $params = array_merge(array(
193 'name' => 'Test Group 1',
195 'title' => 'New Test Group Created',
196 'description' => 'New Test Group Created',
198 'visibility' => 'Public Pages',
199 'group_type' => array(
205 $result = $this->callAPISuccess('Group', 'create', $params);
206 return $result['id'];
214 public function groupDelete($gid) {
219 $this->callAPISuccess('Group', 'delete', $params);
223 * Function to add a Group.
225 * @params array to add group
227 * @param int $groupID
228 * @param int $totalCount
229 * @param bool $random
231 * groupId of created group
233 public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) {
234 $params = array('group_id' => $groupID);
235 for ($i = 1; $i <= $totalCount; $i++
) {
236 $contactID = $this->individualCreate(array(), 0, $random);
238 $params +
= array('contact_id' => $contactID);
241 $params +
= array("contact_id.$i" => $contactID);
244 $result = $this->callAPISuccess('GroupContact', 'create', $params);