| 1 | <?php |
| 2 | |
| 3 | namespace Civi\Test; |
| 4 | |
| 5 | /** |
| 6 | * Class ContactTestTrait |
| 7 | * @package Civi\Test |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | trait ContactTestTrait { |
| 16 | |
| 17 | /** |
| 18 | * Emulate a logged in user since certain functions use that. |
| 19 | * value to store a record in the DB (like activity) |
| 20 | * CRM-8180 |
| 21 | * |
| 22 | * @return int |
| 23 | * Contact ID of the created user. |
| 24 | */ |
| 25 | public function createLoggedInUser() { |
| 26 | $params = array( |
| 27 | 'first_name' => 'Logged In', |
| 28 | 'last_name' => 'User ' . rand(), |
| 29 | 'contact_type' => 'Individual', |
| 30 | 'domain_id' => \CRM_Core_Config::domainID(), |
| 31 | ); |
| 32 | $contactID = $this->individualCreate($params); |
| 33 | $this->callAPISuccess('UFMatch', 'create', array( |
| 34 | 'contact_id' => $contactID, |
| 35 | 'uf_name' => 'superman', |
| 36 | 'uf_id' => 6, |
| 37 | )); |
| 38 | |
| 39 | $session = \CRM_Core_Session::singleton(); |
| 40 | $session->set('userID', $contactID); |
| 41 | return $contactID; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Generic function to create Organisation, to be used in test cases |
| 46 | * |
| 47 | * @param array $params |
| 48 | * parameters for civicrm_contact_add api function call |
| 49 | * @param int $seq |
| 50 | * sequence number if creating multiple organizations |
| 51 | * |
| 52 | * @return int |
| 53 | * id of Organisation created |
| 54 | */ |
| 55 | public function organizationCreate($params = array(), $seq = 0) { |
| 56 | if (!$params) { |
| 57 | $params = array(); |
| 58 | } |
| 59 | $params = array_merge($this->sampleContact('Organization', $seq), $params); |
| 60 | return $this->_contactCreate($params); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Generic function to create Individual, to be used in test cases |
| 65 | * |
| 66 | * @param array $params |
| 67 | * parameters for civicrm_contact_add api function call |
| 68 | * @param int $seq |
| 69 | * sequence number if creating multiple individuals |
| 70 | * @param bool $random |
| 71 | * |
| 72 | * @return int |
| 73 | * id of Individual created |
| 74 | * |
| 75 | * @throws \CRM_Core_Exception |
| 76 | */ |
| 77 | public function individualCreate($params = array(), $seq = 0, $random = FALSE) { |
| 78 | $params = array_merge($this->sampleContact('Individual', $seq, $random), $params); |
| 79 | return $this->_contactCreate($params); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Generic function to create Household, to be used in test cases |
| 84 | * |
| 85 | * @param array $params |
| 86 | * parameters for civicrm_contact_add api function call |
| 87 | * @param int $seq |
| 88 | * sequence number if creating multiple households |
| 89 | * |
| 90 | * @return int |
| 91 | * id of Household created |
| 92 | * @throws \Exception |
| 93 | */ |
| 94 | public function householdCreate($params = array(), $seq = 0) { |
| 95 | $params = array_merge($this->sampleContact('Household', $seq), $params); |
| 96 | return $this->_contactCreate($params); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Helper function for getting sample contact properties. |
| 101 | * |
| 102 | * @param string $contact_type |
| 103 | * enum contact type: Individual, Organization |
| 104 | * @param int $seq |
| 105 | * sequence number for the values of this type |
| 106 | * @param bool $random |
| 107 | * |
| 108 | * @return array |
| 109 | * properties of sample contact (ie. $params for API call) |
| 110 | */ |
| 111 | public function sampleContact($contact_type, $seq = 0, $random = FALSE) { |
| 112 | $samples = array( |
| 113 | 'Individual' => array( |
| 114 | // The number of values in each list need to be coprime numbers to not have duplicates |
| 115 | 'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'), |
| 116 | 'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'), |
| 117 | 'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'), |
| 118 | ), |
| 119 | 'Organization' => array( |
| 120 | 'organization_name' => array( |
| 121 | 'Unit Test Organization', |
| 122 | 'Acme', |
| 123 | 'Roberts and Sons', |
| 124 | 'Cryo Space Labs', |
| 125 | 'Sharper Pens', |
| 126 | ), |
| 127 | ), |
| 128 | 'Household' => array( |
| 129 | 'household_name' => array('Unit Test household'), |
| 130 | ), |
| 131 | ); |
| 132 | $params = array('contact_type' => $contact_type); |
| 133 | foreach ($samples[$contact_type] as $key => $values) { |
| 134 | $params[$key] = $values[$seq % count($values)]; |
| 135 | if ($random) { |
| 136 | $params[$key] .= substr(sha1(rand()), 0, 5); |
| 137 | } |
| 138 | } |
| 139 | if ($contact_type == 'Individual') { |
| 140 | $params['email'] = strtolower( |
| 141 | $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org' |
| 142 | ); |
| 143 | $params['prefix_id'] = 3; |
| 144 | $params['suffix_id'] = 3; |
| 145 | } |
| 146 | return $params; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Private helper function for calling civicrm_contact_add. |
| 151 | * |
| 152 | * @param array $params |
| 153 | * For civicrm_contact_add api function call. |
| 154 | * |
| 155 | * @throws CRM_Core_Exception |
| 156 | * |
| 157 | * @return int |
| 158 | * id of Household created |
| 159 | */ |
| 160 | private function _contactCreate($params) { |
| 161 | $result = $this->callAPISuccess('contact', 'create', $params); |
| 162 | if (!empty($result['is_error']) || empty($result['id'])) { |
| 163 | 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)); |
| 164 | } |
| 165 | return $result['id']; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Delete contact, ensuring it is not the domain contact |
| 170 | * |
| 171 | * @param int $contactID |
| 172 | * Contact ID to delete |
| 173 | */ |
| 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', array( |
| 179 | 'id' => $contactID, |
| 180 | 'skip_undelete' => 1, |
| 181 | )); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Add a Group. |
| 187 | * |
| 188 | * @param array $params |
| 189 | * @return int |
| 190 | * groupId of created group |
| 191 | */ |
| 192 | public function groupCreate($params = array()) { |
| 193 | $params = array_merge(array( |
| 194 | 'name' => 'Test Group 1', |
| 195 | 'domain_id' => 1, |
| 196 | 'title' => 'New Test Group Created', |
| 197 | 'description' => 'New Test Group Created', |
| 198 | 'is_active' => 1, |
| 199 | 'visibility' => 'Public Pages', |
| 200 | 'group_type' => array( |
| 201 | '1' => 1, |
| 202 | '2' => 1, |
| 203 | ), |
| 204 | ), $params); |
| 205 | |
| 206 | $result = $this->callAPISuccess('Group', 'create', $params); |
| 207 | return $result['id']; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Delete a Group. |
| 212 | * |
| 213 | * @param int $gid |
| 214 | */ |
| 215 | public function groupDelete($gid) { |
| 216 | $params = array( |
| 217 | 'id' => $gid, |
| 218 | ); |
| 219 | |
| 220 | $this->callAPISuccess('Group', 'delete', $params); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Function to add a Group. |
| 225 | * |
| 226 | * @params array to add group |
| 227 | * |
| 228 | * @param int $groupID |
| 229 | * @param int $totalCount |
| 230 | * @param bool $random |
| 231 | * @return int |
| 232 | * groupId of created group |
| 233 | */ |
| 234 | public function groupContactCreate($groupID, $totalCount = 10, $random = FALSE) { |
| 235 | $params = array('group_id' => $groupID); |
| 236 | for ($i = 1; $i <= $totalCount; $i++) { |
| 237 | $contactID = $this->individualCreate(array(), 0, $random); |
| 238 | if ($i == 1) { |
| 239 | $params += array('contact_id' => $contactID); |
| 240 | } |
| 241 | else { |
| 242 | $params += array("contact_id.$i" => $contactID); |
| 243 | } |
| 244 | } |
| 245 | $result = $this->callAPISuccess('GroupContact', 'create', $params); |
| 246 | |
| 247 | return $result; |
| 248 | } |
| 249 | |
| 250 | } |